source: CIVL/examples/mpi/collective/vectorSum_bad.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/**
2 * This program should deadlock but it doesn't.
3 * Not all processes are executing the collective
4 * operations in the same order.
5 */
6#include<mpi.h>
7#include<stdlib.h>
8#include<assert.h>
9#include<stdio.h>
10
11int main(int argc, char * argv[])
12{
13 int rank;
14 int procs;
15 int* sendBuf;
16 int* rcvBuf;
17 int* sum;
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 sum = (int*)malloc(sizeof(int)*procs);
26 for(int i=0; i < procs; i++){
27 sendBuf[i] = i;
28 sum[i] = 0;
29 }
30 }else{
31 sum = (int*)malloc(sizeof(int)*procs);
32 sendBuf = (int*)malloc(sizeof(int));
33 }
34 rcvBuf = (int*)malloc(sizeof(int)*procs);
35
36 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
37
38 *sendBuf = *rcvBuf;
39
40 if(rank % 2)
41 MPI_Allgather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, MPI_COMM_WORLD);
42 else
43 MPI_Bcast(sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
44
45 printf("Vector process %d is: (", rank);
46 for(int i=0; i<procs; i++){
47 printf("%d", rcvBuf[i]);
48 if(i != procs-1)
49 printf(", ");
50 }
51 printf(")\n");
52
53 if(rank%2)
54 MPI_Bcast(sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
55 else
56 MPI_Allgather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, MPI_COMM_WORLD);
57
58 MPI_Reduce(rcvBuf, sum, procs, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
59
60 if(rank == 0){
61 printf("Vector sum is: (");
62 for(int i=0; i<procs; i++){
63 printf("%d", sum[i]);
64 if(i != procs-1)
65 printf(", ");
66 }
67 printf(")\n");
68 }
69
70 free(sendBuf);
71 free(rcvBuf);
72 free(sum);
73 return 0;
74}
Note: See TracBrowser for help on using the repository browser.