diff --git a/MPI/mpi-broadcast.c b/MPI/mpi-broadcast.c new file mode 100644 index 0000000..c488573 --- /dev/null +++ b/MPI/mpi-broadcast.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include "mpi.h" + +int main(int argc, char *argv[]) +{ + int rank, nprocs; + int param; + + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + srand(time(NULL)*rank); + + if(rank==5) param = 23; + + MPI_Bcast(¶m,1,MPI_INT,5,MPI_COMM_WORLD); + printf("\nProcess %d received by broadcast value %d",rank,param); + + MPI_Finalize(); +} \ No newline at end of file diff --git a/MPI/mpi-grouping-count.c b/MPI/mpi-grouping-count.c new file mode 100644 index 0000000..af962b0 --- /dev/null +++ b/MPI/mpi-grouping-count.c @@ -0,0 +1,43 @@ +#include +#include "mpi.h" + +int main(int argc, char *argv[]) +{ + int rank, nprocs; + float vector[10]; + + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + + if (rank==0) { + /* Get the input values and write them in “vector”. */ + /* As the values are the same type. */ + int i=0; + for(i=0;i<10;i++) { + vector[i] = (float) i; + } + + /*send the vector to the processes*/ + for(i=1;i +#include "mpi.h" + +int main(int argc, char *argv[]) +{ + int rank, nprocs; + + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + + char buffer[100]; // buffer to store the data(MPI_Pack/MPI_Unpack) + int position; // the position of the buffer + + float data1; + int data2; + + if (rank== 0) { + //define the Data + data1=25.30; + data2=10; + + //pack the data into buffer + position=0; // Start at beginning of buffer + MPI_Pack(&data1, 1, MPI_FLOAT, buffer, 100, &position, MPI_COMM_WORLD); + // Position has been incremented by sizeof(float) bytes + MPI_Pack(&data2, 1, MPI_INT, buffer, 100, &position, MPI_COMM_WORLD); + // Send the contents of buffer to processors + int i=0; + for(i=1; i +#include "mpi.h" + +int main(int argc, char **argv ) +{ + int rank,nprocs; + struct {float data1; int data2;char* data3;} data; + MPI_Datatype mydataStruct; + int blocks[3]; + MPI_Aint indices[3]; + MPI_Datatype MPItypes[3]; + + MPI_Init( &argc, &argv ); + MPI_Comm_rank( MPI_COMM_WORLD, &rank ); + MPI_Comm_size( MPI_COMM_WORLD, &nprocs); + + /* One value of each type */ + blocks[0] = 1; + blocks[1] = 1; + blocks[2] =10; + /* The base types */ + MPItypes[0] = MPI_FLOAT; + MPItypes[1] = MPI_INT; + MPItypes[2] = MPI_CHAR; + /* The locations of each element */ + MPI_Get_address( &data.data1, &indices[0] ); + MPI_Get_address( &data.data2, &indices[1] ); + MPI_Get_address( &data.data3, &indices[2] ); + /* Make relative */ + MPI_Aint baseaddres=indices[0]; + indices[0] = indices[0] - baseaddres; + indices[1] = indices[1] - baseaddres; + indices[3] = indices[3] - baseaddres; + MPI_Type_create_struct( 3, blocks, indices, MPItypes, &mydataStruct ); + MPI_Type_commit( &mydataStruct ); + + + if (rank == 0) { + data.data1=25.30; + data.data2=7; + data.data3="String 10!"; + + int i; + for(i=1;i +#include +#include +#include "mpi.h" + +int main(int argc, char *argv[]) +{ + int rank, nprocs, root; + struct { + double value; + int rank; + } in, out; + + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + + root=0; + + in.value=rank*10; + in.rank = rank; + + MPI_Reduce(&in,&out,1,MPI_DOUBLE_INT,MPI_MAXLOC,root,MPI_COMM_WORLD); + if(rank==0) { + printf("Rank %d: max=%lf at rank = %d \n", rank,out.value,out.rank); + } + + MPI_Reduce(&in,&out,1,MPI_DOUBLE_INT,MPI_MINLOC,root,MPI_COMM_WORLD); + if(rank==0) { + printf("Rank %d: min=%lf at rank = %d \n", rank,out.value,out.rank); + } + + MPI_Finalize(); +} \ No newline at end of file diff --git a/MPI/mpi-reduce-onevalue.c b/MPI/mpi-reduce-onevalue.c new file mode 100644 index 0000000..6acce8a --- /dev/null +++ b/MPI/mpi-reduce-onevalue.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include "mpi.h" + +int main(int argc, char *argv[]) +{ + int rank, nprocs; + int value; + int sumtotal; + int prodtotal; + + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + + value = (rank+1)*10; + + MPI_Reduce(&value,&sumtotal,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD); + MPI_Reduce(&value,&prodtotal,1,MPI_INT,MPI_PROD,0,MPI_COMM_WORLD); + + if(rank==0) { + printf("Collective sum result %d\n", sumtotal); + printf("Collective product results %d\n", prodtotal); + } + + MPI_Finalize(); +} \ No newline at end of file diff --git a/MPI/mpi-reduce-vectors.c b/MPI/mpi-reduce-vectors.c new file mode 100644 index 0000000..ed3b682 --- /dev/null +++ b/MPI/mpi-reduce-vectors.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include "mpi.h" + +int main(int argc, char *argv[]) +{ + int rank, nprocs; + int vector[20]; + int sum[20]; + int i; + int result; + + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + srand(time(NULL)*rank); + //each processor initializes the vector + for(i=0;i<20;i++){ + vector[i]=rand()%10; + } + +// printf("\nProcess %d vector: ",rank); +// for(i=0;i<20;i++){ +// printf(" %d-%2d ",rank,vector[i]); +// } +// printf("\n\n"); + + //Apply MPI_SUM for each process and store result to sum + MPI_Reduce((void*)vector,&sum,20,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD); + + if(rank==0){ + //process 0 obtains the results, calculates the final sum and prints + result=0; + for(i=0;i<20;i++) { + result+=sum[i]; +// printf(" %d ",sum[i]); + } + printf("sum = %d\n",result); + } + MPI_Finalize(); +} \ No newline at end of file diff --git a/MPI/mpi-status-struct.c b/MPI/mpi-status-struct.c new file mode 100644 index 0000000..7c5f4bb --- /dev/null +++ b/MPI/mpi-status-struct.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include "mpi.h" + +int main(int argc, char *argv[]) +{ + int rank, nprocs, count, i; + float in[200],out[200]; + MPI_Status status; + + MPI_Init(&argc,&argv); + MPI_Comm_size(MPI_COMM_WORLD,&nprocs); + MPI_Comm_rank(MPI_COMM_WORLD,&rank); + + if (rank==0) { + for (i=0; i<100; i++) out[i]=i; + MPI_Send(out,100,MPI_FLOAT,1,55,MPI_COMM_WORLD); + } + else { + MPI_Recv(in,200,MPI_FLOAT,MPI_ANY_SOURCE,55,MPI_COMM_WORLD,&status); + MPI_Get_count(&status,MPI_FLOAT,&count); + printf("Rank %d : Got %d data elements from processor %d.\n", rank, count, status.MPI_SOURCE); + } + + MPI_Finalize(); +} \ No newline at end of file