Skip to content

Commit

Permalink
mpi examples added
Browse files Browse the repository at this point in the history
  • Loading branch information
albeertito7 committed Sep 24, 2021
1 parent bfe1166 commit df5db40
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 0 deletions.
22 changes: 22 additions & 0 deletions MPI/mpi-broadcast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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(&param,1,MPI_INT,5,MPI_COMM_WORLD);
printf("\nProcess %d received by broadcast value %d",rank,param);

MPI_Finalize();
}
43 changes: 43 additions & 0 deletions MPI/mpi-grouping-count.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#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<nprocs;i++) {
MPI_Send((void*)vector, 10, MPI_FLOAT, i, 0, MPI_COMM_WORLD);
}

} else {
MPI_Status status;
/*recieve the data to work*/
MPI_Recv((void*)vector, 10, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
}

char result[500];
int i=0;

sprintf(result,"Process %d vector :",rank);

for(i=0;i<10;i++) {
sprintf(result,"%s %f",result,vector[i]);
}
printf("%s\n",result);

MPI_Finalize();
}
50 changes: 50 additions & 0 deletions MPI/mpi-grouping-pack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#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<nprocs; i++) {
MPI_Send((void*)buffer, 100, MPI_PACKED, i,0, MPI_COMM_WORLD);
}
} else {
//Recieve the message
MPI_Status status;
MPI_Recv((void*)buffer,100,MPI_PACKED,0,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
//Unpack the contents of buffer
position=0;
MPI_Unpack(buffer, 100, &position, &data1, 1, MPI_FLOAT, MPI_COMM_WORLD);
MPI_Unpack(buffer, 100, &position, &data2, 1, MPI_INT, MPI_COMM_WORLD);

}

char result[500];
int i=0;
sprintf(result,"Process %d data %f %d",rank,data1,data2);
printf("%s\n",result);

MPI_Finalize();
}
59 changes: 59 additions & 0 deletions MPI/mpi-grouping-struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
#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<nprocs;i++) {
MPI_Send(&data, 1, mydataStruct,i, 0, MPI_COMM_WORLD );
}
} else {
MPI_Status status;
MPI_Recv(&data,1,mydataStruct,0,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
}

printf( "Process %d got %f, %d and %s\n", rank, data.data1, data.data2,data.data3 );

/* Clean up the type */
MPI_Type_free(&mydataStruct);
MPI_Finalize();

return 0;
}
34 changes: 34 additions & 0 deletions MPI/mpi-reduce-maxminloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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();
}
28 changes: 28 additions & 0 deletions MPI/mpi-reduce-onevalue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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();
}
42 changes: 42 additions & 0 deletions MPI/mpi-reduce-vectors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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();
}
27 changes: 27 additions & 0 deletions MPI/mpi-status-struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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();
}

0 comments on commit df5db40

Please sign in to comment.