/* An example using MPI_Probe to find the size of a message The program consists of one sender and one receiver process. The sender processes draws a randum number and sends a message of this size to process one. Process one uses MPI_Probe to find out how large the message is before it is received. It then allocates a receive-buffer of this size, and receices the message. Compile the program with 'mpicc -O3 probe.c -o probe' Run it on 2 processes. */ #include #include #include #include int main(int argc, char *argv[]) { const int tag = 42; /* Message tag */ int id, ntasks; MPI_Init(&argc, &argv); /* Initialize MPI */ MPI_Comm_size(MPI_COMM_WORLD, &ntasks); /* Get nr of tasks */ MPI_Comm_rank(MPI_COMM_WORLD, &id); /* Get id of this process */ /* Check that we run on two processors */ if (ntasks != 2) { printf("You have to use 2 processes to run this program\n"); MPI_Finalize(); /* Quit if there is only one processor */ exit(0); } /* Process 0 does this */ if (id == 0) { const int MAX_SIZE = 1000; int message_size, i; int *numbers; float randomnumber; /*Pick a random amount of integers to send to process one */ srandom(time(NULL)); /* Set random seed based on time */ /* Draw a random number between 0 and 1 */ randomnumber = (float)random()/(float)RAND_MAX; /* Message size will be between 0 and MAX_SIZE */ message_size = (int)(randomnumber * MAX_SIZE); /* printf("message_size = %d\n", message_size); */ /* Allocate an array of this size */ numbers = (int *) malloc(message_size * sizeof(int)); /* Initialize the message to some numbers */ for (i=0; i