manual_broadcast.c
/******************************************************************/
/* */
/* Question 2: Broadcast simple en utilisant MPI_Send et MPI_Recv */
/* */
/******************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <mpi.h>
#include <sys/time.h>
#define BUFLEN 65536
long ms_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return(tv.tv_sec*1000 + tv.tv_usec/1000);
}
void long_computation(void)
{
long now=ms_time();
while(ms_time()<now+4000)
{
}
}
void my_broadcast(void *buffer, int count, MPI_Datatype datatype, int root) {
int my_id, nb_proc;
MPI_Status status;
int i;
MPI_Comm_rank(MPI_COMM_WORLD,&my_id);
MPI_Comm_size(MPI_COMM_WORLD,&nb_proc);
if (my_id == root) {
for (i=0; i<nb_proc; i++) {
if (i != root) {
MPI_Send(buffer, count, datatype,i,0,MPI_COMM_WORLD);
}
}
} else {
MPI_Recv(buffer,count,datatype, root, 0, MPI_COMM_WORLD, &status);
}
}
int main(int argc, char **argv)
{
int nb_proc;
int my_id;
int buffer[BUFLEN];
long int start_time, end_time;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nb_proc);
MPI_Comm_rank(MPI_COMM_WORLD,&my_id);
start_time = ms_time();
my_broadcast(buffer,BUFLEN,MPI_INT,0);
long_computation();
end_time = ms_time();
printf("[proc P%d] Done within %ld milliseconds.\n", my_id, end_time - start_time);
MPI_Finalize();
return (0);
}
Generated by GNU enscript 1.6.3.