next up previous

3.1 C Example     continued...

A single message can contain any number of different data types and there is no limit on the complexity of the message. However, you should ensure that the received message is unpacked in the same way it was originally packed.

The function to send a message is pvm_send().

int info = pvm_send(int tid, int msgtag)
This function attaches an integer label of msgtag and immediately sends the contents of the send buffer to the task with the task id of tid. The msgtag can be used to distinguish different ``types'' of messages that a task could send out.

In the example code, within a loop, the master program clears the send buffer for each new message and packs this buffer with two things: 1) the number of array elements that follow in the message and 2) the array portion to be summed. Since each consecutive item from the array a is to be sent, starting with the num_data*i position, the stride for the packing function is 1. The task_ids array that was returned from the pvm_spawn() call is used to address each different task that will receive a portion of the array. The arbitrarily chosen value `4' is the msgtag used to label the messages.

After the array portions have been distributed, the master program must receive a partial sum from each of the worker processes. To receive a message, a task calls the pvm_recv() function.

int bufid = pvm_recv(int tid, int msgtag)
This will receive a message from task tid with label msgtag and place it into the receive buffer with id bufid. If no message is waiting from the given task with the expected label, the function waits until a message from the proper task and the correct label arrives. Values of `-1' for the parameters will match with any task id and/or label. In the example code, the master program is expecting a label value of `7' on messages from the worker tasks. The messages are to be received from tasks in the order that the send messages were issued.