| 1 | /* Create nprocs processes. Have them exchange data in a cycle.
|
|---|
| 2 | * Command line execution:
|
|---|
| 3 | * civl verify -inputNPROCS_BOUND=10 -inputN_BOUND=5 ring.cvl
|
|---|
| 4 | */
|
|---|
| 5 | #include<civlc.cvh>
|
|---|
| 6 | #include<comm.cvh>
|
|---|
| 7 | #include<stdio.h>
|
|---|
| 8 | #define TAG 0
|
|---|
| 9 |
|
|---|
| 10 | $input int NPROCS_BOUND = 2; // upper bound of the number of mpi processes
|
|---|
| 11 | $input int NPROCS; // number of mpi processes
|
|---|
| 12 | $assume(0 < NPROCS && NPROCS <= NPROCS_BOUND);
|
|---|
| 13 | $input int N_BOUND = 2; // upper bound on number times to loop
|
|---|
| 14 | $input int N; // number of times to loop
|
|---|
| 15 | $assume(0 < N && N <= N_BOUND);
|
|---|
| 16 | _Bool initialized = $false;
|
|---|
| 17 | $gcomm MPI_COMM_WORLD; // the default MPI communicator
|
|---|
| 18 |
|
|---|
| 19 | void MPI_Process (int rank) {
|
|---|
| 20 | int left = (rank+NPROCS-1)%NPROCS;
|
|---|
| 21 | int right = (rank+NPROCS+1)%NPROCS;
|
|---|
| 22 | int i=0;
|
|---|
| 23 | int x=rank, y; // send a message from x, recv into y, for checking
|
|---|
| 24 | $message in, out;
|
|---|
| 25 | $comm myComm;
|
|---|
| 26 | //test variables
|
|---|
| 27 | int comm_size;
|
|---|
| 28 | int comm_place;
|
|---|
| 29 | _Bool hasMessage;
|
|---|
| 30 | $message matchedMessage;
|
|---|
| 31 |
|
|---|
| 32 | $when (initialized);
|
|---|
| 33 | myComm = $comm_create($here, MPI_COMM_WORLD, rank);
|
|---|
| 34 | while (i<N) {
|
|---|
| 35 | if (rank%2==0) { // send first, then recv
|
|---|
| 36 | out = $message_pack(rank, right, TAG, &x, sizeof(int));
|
|---|
| 37 | $comm_enqueue(myComm, out);
|
|---|
| 38 | hasMessage = $comm_probe(myComm, left, TAG);
|
|---|
| 39 | comm_size = $comm_size(myComm);
|
|---|
| 40 | comm_place = $comm_place(myComm);
|
|---|
| 41 | printf("hasMessage is %s\n", hasMessage);
|
|---|
| 42 | printf("comm_size is %d\n", comm_size);
|
|---|
| 43 | printf("comm_place is %d\n", comm_place);
|
|---|
| 44 | matchedMessage = $comm_seek(myComm, left, TAG);
|
|---|
| 45 | printf("matchedMessage is %s\n", matchedMessage);
|
|---|
| 46 | in = $comm_dequeue(myComm, left, TAG);
|
|---|
| 47 | $message_unpack(in, &y, sizeof(int));
|
|---|
| 48 | $assert(y==left);
|
|---|
| 49 | } else { // recv first, then send
|
|---|
| 50 | hasMessage = $comm_probe(myComm, left, TAG);
|
|---|
| 51 | comm_size = $comm_size(myComm);
|
|---|
| 52 | comm_place = $comm_place(myComm);
|
|---|
| 53 | printf("hasMessage is %s\n", hasMessage);
|
|---|
| 54 | printf("comm_size is %d\n", comm_size);
|
|---|
| 55 | printf("comm_place is %d\n", comm_place);
|
|---|
| 56 | matchedMessage = $comm_seek(myComm, left, TAG);
|
|---|
| 57 | printf("matchedMessage is %s\n", matchedMessage);
|
|---|
| 58 | in = $comm_dequeue(myComm, left, TAG);
|
|---|
| 59 | $message_unpack(in, &y, sizeof(int));
|
|---|
| 60 | $assert(y==left);
|
|---|
| 61 | out = $message_pack(rank, right, TAG, &x, sizeof(int));
|
|---|
| 62 | $comm_enqueue(myComm, out);
|
|---|
| 63 | }
|
|---|
| 64 | i++;
|
|---|
| 65 | }
|
|---|
| 66 | // destroy the local communicator.
|
|---|
| 67 | $comm_destroy(myComm);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | void main() {
|
|---|
| 71 | $proc procs[NPROCS];
|
|---|
| 72 |
|
|---|
| 73 | for (int i=0; i<NPROCS; i++)
|
|---|
| 74 | procs[i] = $spawn MPI_Process(i);
|
|---|
| 75 | MPI_COMM_WORLD = $gcomm_create($here, NPROCS);
|
|---|
| 76 | initialized = $true;
|
|---|
| 77 | for (int i=0; i<NPROCS; i++)
|
|---|
| 78 | $wait(procs[i]);
|
|---|
| 79 | // destroy the global communicator.
|
|---|
| 80 | $gcomm_destroy(MPI_COMM_WORLD, NULL);
|
|---|
| 81 | }
|
|---|