スライド 1

Sample answer of the first exercise. (1)
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<sys/time.h>
"mpi.h"
int main(int argc, char *argv[])
{
int r, myid, procs;
struct timeval tv;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &procs);
if(myid==0){
result = (int *)malloc(sizeof(int)*procs);
if( result == NULL){
printf("Not enough memory\n");
exit(EXIT_FAILURE);
}
}
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
r = rand();
MPI_Gather(&r,1,MPI_INT,result,1,MPI_INT,0,MPI_COMM_WORLD);
if(myid == 0){
for(i=0;i < procs ;i++){
printf("%d: %d\n",i, result[i]);
}
free(result);
MPI_Finalize();
}
1
Sample answer of the first exercise. (2)
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<sys/time.h>
"mpi.h"
int main(int argc, char *argv[])
{
int r, myid, procs;
struct timeval tv;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &procs);
gettimeofday(&tv,NULL);
srand(tv.tv_usec);
r = rand();
if (myid != 0)
MPI_Recv(&val,1,MPI_INT,myid-1,0,MPI_COMM_WORLD,&status);
printf("%d: %d\n",myid,r);
if (myid !=procs-1)
MPI_Send(&val,1,MPI_INT,myid+1,0,MPI_COMM_WORLD);
MPI_Finalize();
}
2