diff --git a/hpc/hello-world/mpi_bcred.c b/hpc/hello-world/mpi_bcred.c new file mode 100644 index 0000000000000000000000000000000000000000..f73a779766f8d9ca5e5686e0c4f7084467b21283 --- /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 0000000000000000000000000000000000000000..6def20931d05fd6e95e9115eb9dbff5bc389dc14 --- /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 d579224309fb441920ce127a4df1720d637d5f25..8543f6e1071e98250758a47fdc2c5a008127d96f 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) {