Commit af58973f authored by LAMBERT Jean-charles's avatar LAMBERT Jean-charles
Browse files

add OpenMP and MPI exercices

parent d87aaf4e
#define NMAX 1000000
int main(int argc, char **argv)
{
int a[NMAX],i;
#pragma omp parallel for
for (i = 0; i < NMAX; i++) {
a[i] = 2 * i ;
}
return 0;
}
#include <mpi.h> // PROVIDES THE BASIC MPI DEFINITION AND TYPES
#include <stdio.h>
#include <string.h>
#define NMAX 24
#define MIN(a,b) ((a) < (b) ? (a) : (b))
int main(int argc, char **argv) {
int i, my_rank, partner, size,a[NMAX],chunk,istart,istop;
MPI_Status stat;
MPI_Init(&argc, &argv); // START MPI
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); // DETERMINE RANK OF THIS PROCESSOR
MPI_Comm_size(MPI_COMM_WORLD, &size); // DETERMINE TOTAL NUMBER OF PROCESSORS
chunk=NMAX/size; // #chunks per processors
istart=(chunk*my_rank); // first index of my rank
if (my_rank<size-1) {
istop=MIN(istart+chunk,NMAX); // last index of my rank
} else {
istop=NMAX;
}
for (i=istart; i<istop; i++) // EVERY PROCESSOR COMPUTE ONE CHUNK OF THE ARRAY
a[i] = 2 * i;
if (my_rank == 0) { // master GATHER ALL RESULTS
for (partner = 1; partner < size; partner++){
istart=(chunk*partner);
if (partner<size-1) {
istop=MIN(istart+chunk,NMAX);
} else {
istop=NMAX;
}
MPI_Recv(a + istart ,istop-istart, MPI_INT, partner, 1, MPI_COMM_WORLD, &stat);
}
for (i=0; i<NMAX; i++)
fprintf(stderr,"a[%5d] = %8d\n",i,a[i]);
}
else { // ALL processors except the master
MPI_Send(a+istart,istop-istart , MPI_INT, 0,1,MPI_COMM_WORLD);
}
MPI_Finalize(); // EXIT MPI
}
#include <mpi.h> // PROVIDES THE BASIC MPI DEFINITION AND TYPES
#include <stdio.h>
#include <string.h>
#define NMAX 24
#define MIN(a,b) ((a) < (b) ? (a) : (b))
int main(int argc, char **argv) {
int i, my_rank, partner, size,a[NMAX],chunk,istart,istop;
MPI_Status stat;
MPI_Init(&argc, &argv); // START MPI
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); // DETERMINE RANK OF THIS PROCESSOR
MPI_Comm_size(MPI_COMM_WORLD, &size); // DETERMINE TOTAL NUMBER OF PROCESSORS
chunk=NMAX/size; // #chunks per processors
istart=(chunk*my_rank); // first index of my rank
istop=MIN(istart+chunk,NMAX); // last index of my rank
for (i=istart; i<istop; i++) // EVERY PROCESSOR COMPUTE ONE CHUNK OF THE ARRAY
a[i] = 2 * i;
if (my_rank == 0) { // master GATHER ALL RESULTS
for (partner = 1; partner < size; partner++){
istart=(chunk*partner);
istop=MIN(istart+chunk,NMAX);
MPI_Recv(a + istart ,istop-istart, MPI_INT, partner, 1, MPI_COMM_WORLD, &stat);
}
for (i=0; i<NMAX; i++)
fprintf(stderr,"a[%5d] = %8d\n",i,a[i]);
}
else { // ALL processors except the master
MPI_Send(a+istart,istop-istart , MPI_INT, 0,1,MPI_COMM_WORLD);
}
MPI_Finalize(); // EXIT MPI
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment