-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPI_Alltoall.c
43 lines (38 loc) · 1.02 KB
/
MPI_Alltoall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Simple MPI program to understand Alltoall Operation. */
/* Author : Dhaval Lad */
#include "mpi.h"
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
int size, rank,i ,n;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
// Array size must be equal to #processer.
int sendBuf[size];
int receiveBuf[size];
int whoPrints = 1;
for(i=0;i<size;i++){
sendBuf[i] = 1+ 4*rank + i;
}
// Comment only if condition to print all processor buffer.
if(rank == whoPrints){
printf("\nAt Process#%d ",rank);
for(i=0;i<size;i++){
printf("%d ",sendBuf[i]);
}
// printf("\n");
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Alltoall(sendBuf,1,MPI_INT,receiveBuf,1,MPI_INT,MPI_COMM_WORLD);
if(rank == 1) printf("\nAfter Calling MPI_Alltoall function...");
// Comment only if condition to print all processor buffer.
if(rank == whoPrints){
printf("\nAt Process#%d ",rank);
for(i=0;i<size;i++){
printf("%d ",receiveBuf[i]);
}
printf("\n");
}
MPI_Finalize();
}