| 1 | /**
|
|---|
| 2 | * This is a simple program to demonstrate the correct usage of two MPI colletive operations,
|
|---|
| 3 | * i.e., they have to be executed in the same order for each process.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include<mpi.h>
|
|---|
| 7 | #include<stdlib.h>
|
|---|
| 8 | #include<assert.h>
|
|---|
| 9 | #include<stdio.h>
|
|---|
| 10 |
|
|---|
| 11 | int main(int argc, char * argv[])
|
|---|
| 12 | {
|
|---|
| 13 | int rank;
|
|---|
| 14 | int procs;
|
|---|
| 15 | int* sendBuf;
|
|---|
| 16 | int* rcvBuf;
|
|---|
| 17 |
|
|---|
| 18 | MPI_Init(&argc,&argv);
|
|---|
| 19 | MPI_Comm_size(MPI_COMM_WORLD, &procs);
|
|---|
| 20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 21 |
|
|---|
| 22 | if (rank == 0) {
|
|---|
| 23 | sendBuf = (int*)malloc(sizeof(int)*procs);
|
|---|
| 24 | //rcvBuf = (int*)malloc(sizeof(int)*procs);
|
|---|
| 25 | for(int i=0; i < procs; i++)
|
|---|
| 26 | sendBuf[i] = i;
|
|---|
| 27 | }else{
|
|---|
| 28 | sendBuf = (int*)malloc(sizeof(int));
|
|---|
| 29 | }
|
|---|
| 30 | rcvBuf = (int*)malloc(sizeof(int)*procs);
|
|---|
| 31 |
|
|---|
| 32 | MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 33 |
|
|---|
| 34 | *sendBuf = *rcvBuf + rank;
|
|---|
| 35 |
|
|---|
| 36 | MPI_Allgather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, MPI_COMM_WORLD);
|
|---|
| 37 |
|
|---|
| 38 | printf("receive buffer of process %d:\n", rank);
|
|---|
| 39 | for(int i=0; i<procs; i++){
|
|---|
| 40 | printf("rcvBuf[%d]=%d\n", i, rcvBuf[i]);
|
|---|
| 41 | assert(i*2 == rcvBuf[i]);
|
|---|
| 42 | }
|
|---|
| 43 | free(sendBuf);
|
|---|
| 44 | free(rcvBuf);
|
|---|
| 45 | MPI_Finalize();
|
|---|
| 46 | return 0;
|
|---|
| 47 | }
|
|---|