source: CIVL/examples/mpi/collective/scatterGather_bad.c@ 2261cec

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 2261cec was 2d81ab2, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

added tests for mpi colletive operations.

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

  • Property mode set to 100644
File size: 1.3 KB
Line 
1/**
2 * This program has a deadlock because processes dont execute MPI_Scatter
3 * and MPI_Gather in the same order.
4 */
5#include<mpi.h>
6#include<stdlib.h>
7#include<assert.h>
8#include<stdio.h>
9
10int main()
11{
12 int argc;
13 char** argv;
14 int rank;
15 int procs;
16 int* sendBuf;
17 int* rcvBuf;
18
19 MPI_Init(&argc,&argv);
20 MPI_Comm_size(MPI_COMM_WORLD, &procs);
21 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22
23 if (rank == 0) {
24 sendBuf = (int*)malloc(sizeof(int)*procs);
25 rcvBuf = (int*)malloc(sizeof(int)*procs);
26 for(int i=0; i < procs; i++)
27 sendBuf[i] = i;
28 }else{
29 sendBuf = (int*)malloc(sizeof(int));
30 rcvBuf = (int*)malloc(sizeof(int));
31 }
32
33 if(rank%2)
34 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
35 else
36 MPI_Gather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
37
38 if(rank%2)
39 MPI_Gather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
40 else
41 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
42
43 if(rank == 0){
44 for(int i=0; i<procs; i++){
45 printf("sendBuf[%d]=%d, rcvBuf[%d]=%d\n", i, sendBuf[i], i, rcvBuf[i]);
46 assert(sendBuf[i] == rcvBuf[i]);
47 }
48 }
49
50 free(sendBuf);
51 free(rcvBuf);
52 return 0;
53}
Note: See TracBrowser for help on using the repository browser.