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