| [e689ae5] | 1 | /* Create NPROCS processes. Have them exchange data in a cycle.
|
|---|
| 2 | * Command line verification example:
|
|---|
| 3 | * civl verify -inputNPROCS_BOUND=10 -inputN_BOUND=5 ring.cvl
|
|---|
| 4 | */
|
|---|
| 5 | #include<civlc.h>
|
|---|
| 6 | #define TAG 0
|
|---|
| 7 |
|
|---|
| 8 | $input int NPROCS_BOUND; // upper bound of the number of MPI processes
|
|---|
| 9 | $input int NPROCS; // number of MPI processes
|
|---|
| 10 | $assume 0 < NPROCS && NPROCS <= NPROCS_BOUND;
|
|---|
| 11 | $input int N_BOUND; // upper bound on number times to loop
|
|---|
| 12 | $input int N; // number of times to loop
|
|---|
| 13 | $assume 0 < N && N <= N_BOUND;
|
|---|
| 14 | $gcomm GCOMM_WORLD = $gcomm_create($here, NPROCS); // global comm object
|
|---|
| 15 |
|
|---|
| 16 | void MPI_Process (int rank) {
|
|---|
| 17 | $comm comm = $comm_create($here, GCOMM_WORLD, rank);
|
|---|
| 18 | int left = (rank+NPROCS-1)%NPROCS;
|
|---|
| 19 | int right = (rank+NPROCS+1)%NPROCS;
|
|---|
| 20 | int x=rank, y; // send a message from x, recv into y
|
|---|
| 21 | $message in, out;
|
|---|
| 22 |
|
|---|
| 23 | for (int i=0; i<N; i++) {
|
|---|
| 24 | if (rank%2==0) { // send first, then recv
|
|---|
| 25 | out = $message_pack(rank, right, TAG, &x, sizeof(int));
|
|---|
| 26 | $comm_enqueue(comm, out);
|
|---|
| 27 | in = $comm_dequeue(comm, left, TAG);
|
|---|
| 28 | $message_unpack(in, &y, sizeof(int));
|
|---|
| 29 | $assert(y==left);
|
|---|
| 30 | } else { // recv first, then send
|
|---|
| 31 | in = $comm_dequeue(comm, left, TAG);
|
|---|
| 32 | $message_unpack(in, &y, sizeof(int));
|
|---|
| 33 | $assert(y==left);
|
|---|
| 34 | out = $message_pack(rank, right, TAG, &x, sizeof(int));
|
|---|
| 35 | $comm_enqueue(comm, out);
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| [9b0fc93] | 38 | $comm_destroy(comm);
|
|---|
| [e689ae5] | 39 | }
|
|---|
| 40 |
|
|---|
| [6346753] | 41 | void main() {
|
|---|
| 42 | $proc procs[NPROCS];
|
|---|
| 43 |
|
|---|
| 44 | for (int i=0; i<NPROCS; i++)
|
|---|
| 45 | procs[i] = $spawn MPI_Process(i);
|
|---|
| 46 | for (int i=0; i<NPROCS; i++)
|
|---|
| 47 | $wait(procs[i]);
|
|---|
| [9b0fc93] | 48 | $gcomm_destroy(GCOMM_WORLD);
|
|---|
| [e689ae5] | 49 | }
|
|---|