Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
added smpi to cvs repository. still need to do a lot of integration work.
[simgrid.git] / src / smpi / sample / allreduce.c
diff --git a/src/smpi/sample/allreduce.c b/src/smpi/sample/allreduce.c
new file mode 100644 (file)
index 0000000..1f8cd88
--- /dev/null
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <mpi.h>
+
+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_Allreduce(sendbuf, recvbuf, size, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
+  printf("node %d has: ", rank);
+  for (i = 0; i < size; i++) {
+    printf("%d ", recvbuf[i]);
+  }
+  printf("\n");
+  free(sendbuf);
+  free(recvbuf);
+  MPI_Finalize();
+  return 0;
+}