1 | |
---|
2 | #include <stdio.h> |
---|
3 | #include "mpi.h" |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | main(int argc, char *argv[]) |
---|
10 | { |
---|
11 | MPI_Request sreq[10], sreq2[10], rreq[10], rreq2[10]; |
---|
12 | int sbuf[10],sbuf2[10],rbuf[10],rbuf2[10]; |
---|
13 | int tag; |
---|
14 | MPI_Status status[10], sr_status; |
---|
15 | int i,j; |
---|
16 | MPI_Comm comm2; |
---|
17 | int flag; |
---|
18 | MPI_Group mygroup; |
---|
19 | char pname[MPI_MAX_PROCESSOR_NAME]; |
---|
20 | int pnamelen; |
---|
21 | |
---|
22 | int position, temp; |
---|
23 | |
---|
24 | |
---|
25 | printf("Time: %f\n",MPI_Wtime()); |
---|
26 | |
---|
27 | MPI_Initialized(&flag); |
---|
28 | printf("MPI is initialized = %d\n",flag); |
---|
29 | |
---|
30 | MPI_Init(NULL,NULL); |
---|
31 | |
---|
32 | MPI_Get_processor_name(pname,&pnamelen); |
---|
33 | printf("Processor name: %s (len=%d)\n",pname,pnamelen); |
---|
34 | |
---|
35 | #if 0 |
---|
36 | MPI_Comm_dup(MPI_COMM_WORLD,&comm2); |
---|
37 | #endif |
---|
38 | |
---|
39 | #if 0 |
---|
40 | MPI_Comm_split(MPI_COMM_WORLD,42,99,&comm2); |
---|
41 | #endif |
---|
42 | |
---|
43 | #if 1 |
---|
44 | MPI_Comm_group(MPI_COMM_WORLD,&mygroup); |
---|
45 | MPI_Comm_create(MPI_COMM_WORLD,mygroup,&comm2); |
---|
46 | #endif |
---|
47 | |
---|
48 | MPI_Initialized(&flag); |
---|
49 | printf("MPI is initialized = %d\n",flag); |
---|
50 | |
---|
51 | for (i=0; i<5; i++) |
---|
52 | { |
---|
53 | tag=100+i; |
---|
54 | printf("COMWORLD Post ireceive tag %d\n",tag); |
---|
55 | |
---|
56 | MPI_Irecv(&rbuf[2*i],1,MPI_2INT, |
---|
57 | 0,tag,MPI_COMM_WORLD,&rreq[i]); |
---|
58 | } |
---|
59 | |
---|
60 | for (i=0; i<5; i++) |
---|
61 | { |
---|
62 | sbuf2[i]=1000+10*i; |
---|
63 | tag=100+i; |
---|
64 | printf("COM2 Post isend %d tag %d\n",sbuf2[i],tag); |
---|
65 | MPI_Isend(&sbuf2[i],1,MPI_INT,0,tag,comm2,&sreq2[i]); |
---|
66 | } |
---|
67 | |
---|
68 | for (i=0; i<5; i++) |
---|
69 | { |
---|
70 | sbuf[2*i]=10*i; |
---|
71 | sbuf[2*i+1]=10*i+1; |
---|
72 | tag=100+(4-i); |
---|
73 | printf("COMWORLD Post irsend %d tag %d\n",sbuf[i],tag); |
---|
74 | MPI_Irsend(&sbuf[2*i],1,MPI_2INT,0,tag,MPI_COMM_WORLD,&sreq[i]); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | printf("Time: %f\n",MPI_Wtime()); |
---|
79 | MPI_Waitall(5,sreq,status); |
---|
80 | MPI_Waitall(5,rreq,status); |
---|
81 | |
---|
82 | printf("Waiting for COMWORLD send/receives\n"); |
---|
83 | |
---|
84 | for (i=0; i<5; i++) |
---|
85 | printf("tag %d rbuf= %d %d\n",status[i].MPI_TAG,rbuf[2*i],rbuf[2*i+1]); |
---|
86 | |
---|
87 | |
---|
88 | for (i=0; i<5; i++) |
---|
89 | { |
---|
90 | tag=100+i; |
---|
91 | printf("COM2 Post receive tag %d\n",tag); |
---|
92 | |
---|
93 | MPI_Irecv(&rbuf2[i],1,MPI_INT, |
---|
94 | 0,tag,comm2,&rreq2[i]); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | MPI_Waitall(5,sreq2,status); |
---|
99 | MPI_Waitall(5,rreq2,status); |
---|
100 | |
---|
101 | printf("Waiting for COM2 send/receive\n"); |
---|
102 | |
---|
103 | for (i=0; i<5; i++) |
---|
104 | printf("tag %d rbuf= %d\n",status[i].MPI_TAG,rbuf2[i]); |
---|
105 | |
---|
106 | |
---|
107 | /* |
---|
108 | * pack/unpack |
---|
109 | */ |
---|
110 | |
---|
111 | position=0; |
---|
112 | for (i=0; i<5; i++) |
---|
113 | { |
---|
114 | temp=100+i; |
---|
115 | MPI_Pack(&temp, 1, MPI_INT, sbuf, 20, &position, MPI_COMM_WORLD); |
---|
116 | } |
---|
117 | |
---|
118 | MPI_Isend( sbuf, position, MPI_PACKED, 0, 0, MPI_COMM_WORLD,&sreq[0]); |
---|
119 | |
---|
120 | MPI_Irecv( rbuf, position, MPI_PACKED, 0, 0, MPI_COMM_WORLD, &rreq[0] ); |
---|
121 | MPI_Waitall(1,rreq,status); |
---|
122 | |
---|
123 | printf("Pack/send/unpack: \n"); |
---|
124 | |
---|
125 | position=0; |
---|
126 | for (i=0; i<5; i++) |
---|
127 | { |
---|
128 | MPI_Unpack(rbuf,20,&position,&temp,1,MPI_INT,MPI_COMM_WORLD); |
---|
129 | printf("%d\n",temp); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /* |
---|
134 | * sendrecv |
---|
135 | */ |
---|
136 | |
---|
137 | |
---|
138 | sbuf[0]=42; |
---|
139 | rbuf[0]=0; |
---|
140 | sr_status.MPI_SOURCE= -1; |
---|
141 | sr_status.MPI_TAG= -1; |
---|
142 | |
---|
143 | MPI_Sendrecv(sbuf,1,MPI_INT,0,127, |
---|
144 | rbuf,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG, |
---|
145 | MPI_COMM_WORLD,&sr_status); |
---|
146 | |
---|
147 | printf("Done with MPI_Sendrecv, rbuf=%d source=%d tag=%d\n", |
---|
148 | rbuf[0],sr_status.MPI_SOURCE,sr_status.MPI_TAG); |
---|
149 | |
---|
150 | |
---|
151 | /* |
---|
152 | * Send to nowhere |
---|
153 | */ |
---|
154 | |
---|
155 | |
---|
156 | printf("Send to MPI_PROC_NULL\n"); |
---|
157 | MPI_Send(sbuf, 1, MPI_INT, MPI_PROC_NULL, 77, MPI_COMM_WORLD); |
---|
158 | |
---|
159 | |
---|
160 | printf("Receive from MPI_PROC_NULL\n"); |
---|
161 | MPI_Recv(rbuf, 1, MPI_INT, MPI_PROC_NULL, 78, MPI_COMM_WORLD, status); |
---|
162 | printf(" status: source=%d tag=%d\n", |
---|
163 | status[0].MPI_SOURCE,status[0].MPI_TAG); |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | /* |
---|
168 | * Finish up |
---|
169 | */ |
---|
170 | |
---|
171 | |
---|
172 | MPI_Finalize(); |
---|
173 | |
---|
174 | |
---|
175 | for (i=0; i<5; i++) |
---|
176 | { |
---|
177 | printf("Time: %f\n",MPI_Wtime()); |
---|
178 | sleep(1); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|