source: CIVL/examples/mpi/routines/probe.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[5773b57e]1/*
2
3An example using MPI_Probe to find the size of a message
4
5The program consists of one sender and one receiver process.
6The sender processes draws a randum number and sends a message
7of this size to process one. Process one uses MPI_Probe to find
8out how large the message is before it is received. It then allocates
9a receive-buffer of this size, and receices the message.
10
11Compile the program with 'mpicc -O3 probe.c -o probe'
12Run it on 2 processes.
13
14*/
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <time.h>
19#include <mpi.h>
20
21int main(int argc, char *argv[]) {
22 const int tag = 42; /* Message tag */
23 int id, ntasks;
24
25 MPI_Init(&argc, &argv); /* Initialize MPI */
26 MPI_Comm_size(MPI_COMM_WORLD, &ntasks); /* Get nr of tasks */
27 MPI_Comm_rank(MPI_COMM_WORLD, &id); /* Get id of this process */
28
29 /* Check that we run on two processors */
30 if (ntasks != 2) {
31 printf("You have to use 2 processes to run this program\n");
32 MPI_Finalize(); /* Quit if there is only one processor */
33 exit(0);
34 }
35
36 /* Process 0 does this */
37 if (id == 0) {
38 const int MAX_SIZE = 1000;
39 int message_size, i;
40 int *numbers;
41 float randomnumber;
42
43 /*Pick a random amount of integers to send to process one */
44 srandom(time(NULL)); /* Set random seed based on time */
45 /* Draw a random number between 0 and 1 */
46 randomnumber = (float)random()/(float)RAND_MAX;
47 /* Message size will be between 0 and MAX_SIZE */
48 message_size = (int)(randomnumber * MAX_SIZE);
49 /* printf("message_size = %d\n", message_size); */
50
51 /* Allocate an array of this size */
52 numbers = (int *) malloc(message_size * sizeof(int));
53 /* Initialize the message to some numbers */
54 for (i=0; i<message_size; i++) {
55 numbers[i] = random() % 100;
56 /* printf("%d ", numbers[i]); */ /* Print the numbers */
57 }
58 printf("\n");
59
60 /* Send the array of integers to process one */
61 MPI_Send(numbers, message_size, MPI_INT, 1, tag, MPI_COMM_WORLD);
62 printf("0 sent %d numbers to process 1\n", message_size);
63
64 /* Process 1 does this */
65 } else if (id == 1) {
66 MPI_Status status;
67 int *message_buf;
68 int my_size;
69 /* Probe for an incoming message from process zero */
70 MPI_Probe(0, tag, MPI_COMM_WORLD, &status);
71
72 /* When probe returns, the status object has the size and other
73 attributes of the incoming message. Get the size of the message */
74 MPI_Get_count(&status, MPI_INT, &my_size);
75 printf("Process %d got a message of size %d\n", id, my_size);
76
77 /* Allocate a buffer just big enough to store the numbers */
78 message_buf = (int*)malloc(sizeof(int) * my_size);
79
80 /* Now receive the message into the allocated buffer */
81 MPI_Recv(message_buf, my_size, MPI_INT, 0, tag, MPI_COMM_WORLD,
82 MPI_STATUS_IGNORE);
83 printf("Process %d received %d numbers\n", id, my_size);
84 free(message_buf);
85 }
86 MPI_Finalize();
87 exit(0);
88}
89
Note: See TracBrowser for help on using the repository browser.