X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1c080fdf88d3dc8cbf7413b1f8b3ce66d475070a..d47debaeffa6807956ee9cc7da90759efe49beda:/src/smpi/sample/reduce.c diff --git a/src/smpi/sample/reduce.c b/src/smpi/sample/reduce.c new file mode 100644 index 0000000000..9fc0be2483 --- /dev/null +++ b/src/smpi/sample/reduce.c @@ -0,0 +1,31 @@ +#include +#include +#include + +int main(int argc, char *argv[]) { + int rank, size; + int i; + int *sendbuf, *recvbuf; + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + sendbuf = malloc(sizeof(int) * size); + recvbuf = malloc(sizeof(int) * size); + for (i = 0; i < size; i++) { + sendbuf[i] = 0; + recvbuf[i] = 0; + } + sendbuf[rank] = rank + 1; + MPI_Reduce(sendbuf, recvbuf, size, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); + if (0 == rank) { + printf("nodes: ", rank); + for (i = 0; i < size; i++) { + printf("%d ", recvbuf[i]); + } + printf("\n"); + } + free(sendbuf); + free(recvbuf); + MPI_Finalize(); + return 0; +}