source: CIVL/examples/languageFeatures/communicatorFeatures.cvl@ 325d439

1.23 2.0 main test-branch
Last change on this file since 325d439 was 9b0fc93, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

renamed $gcomm_free (comm_free) to $gcomm_destroy (comm_destroy).

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@755 fb995dde-84ed-4084-dfe6-e5aef3e2452c

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