From b80ebac2216714bed4b9e551fb54d75714b71141 Mon Sep 17 00:00:00 2001
From: Ardhi Putra Pratama H <ardhi.putra@cs.ui.ac.id>
Date: Fri, 26 Jan 2018 14:57:21 +0700
Subject: [PATCH] Add collective comp code

---
 hpc/hello-world/mpi_bcred.c   | 34 ++++++++++++++++++++++++++++++++++
 hpc/hello-world/mpi_scatter.c | 33 +++++++++++++++++++++++++++++++++
 hpc/hello-world/mpi_send.c    |  2 +-
 3 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 hpc/hello-world/mpi_bcred.c
 create mode 100644 hpc/hello-world/mpi_scatter.c

diff --git a/hpc/hello-world/mpi_bcred.c b/hpc/hello-world/mpi_bcred.c
new file mode 100644
index 0000000..f73a779
--- /dev/null
+++ b/hpc/hello-world/mpi_bcred.c
@@ -0,0 +1,34 @@
+#include "mpi.h"
+#include <stdio.h>
+#include <math.h>
+#define DATASIZE 10
+
+int main(int argc, char **argv) {
+    int myid, numprocs;
+    int i, x, low, high, myresult=0, result;
+
+    int data[DATASIZE] = {1,2,3,4,5,6,7,8,9,10}
+
+    MPI_Init(&argc, &argv);
+    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
+    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
+
+    MPI_Bcast(data, DATASIZE, MPI_INT, 0, MPI_COMM_WORLD);
+
+    /* add portion of data */
+    x = DATASIZE/numprocs;	/* must be an integer */
+    low = myid * x;
+    high = low + x;
+    for(i=low; i<high; i++) {
+        myresult += data[i];
+    }
+    printf("%d has %d\n", myresult, myid);
+
+    MPI_Reduce(&myresult, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
+
+    if(0 == myid) {
+        printf("The sum is %d.\n", result);
+    }
+
+    MPI_Finalize();
+}
diff --git a/hpc/hello-world/mpi_scatter.c b/hpc/hello-world/mpi_scatter.c
new file mode 100644
index 0000000..6def209
--- /dev/null
+++ b/hpc/hello-world/mpi_scatter.c
@@ -0,0 +1,33 @@
+#include "mpi.h"
+#include <stdio.h>
+#define SIZE 4
+
+int main(int argc, char** argv)  {
+    int numtasks, rank, sendcount, recvcount, source;
+    float sendbuf[SIZE][SIZE] = {
+        {1.0, 2.0, 3.0, 4.0},
+        {5.0, 6.0, 7.0, 8.0},
+        {9.0, 10.0, 11.0, 12.0},
+        {13.0, 14.0, 15.0, 16.0}
+    };
+    float recvbuf[SIZE];
+
+    MPI_Init(&argc,&argv);
+    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+    MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
+
+    if (numtasks == SIZE) {
+        source = 1;
+        sendcount = SIZE;
+        recvcount = SIZE;
+        MPI_Scatter(sendbuf,sendcount,MPI_FLOAT,recvbuf,recvcount,
+                    MPI_FLOAT,source,MPI_COMM_WORLD);
+
+        printf("rank= %d  Results: %f %f %f %f\n",rank,recvbuf[0],
+               recvbuf[1],recvbuf[2],recvbuf[3]);
+    }
+    else
+        printf("Must specify %d processors. Terminating.\n",SIZE);
+
+    MPI_Finalize();
+}
diff --git a/hpc/hello-world/mpi_send.c b/hpc/hello-world/mpi_send.c
index d579224..8543f6e 100644
--- a/hpc/hello-world/mpi_send.c
+++ b/hpc/hello-world/mpi_send.c
@@ -1,4 +1,4 @@
-#include <mpi.h>
+#include "mpi.h"
 #include <stdio.h>
 
 int main(int argc, char** argv) {
-- 
GitLab