next up previous

3.1 C Example     continued...

Once a message has been received the data within must be unpacked. The unpacking functions are pvm_upkXXXX() where the XXXX corresponds to the type of data that is to be unpacked. The same XXXX extensions used in the pvm_pkXXXX() packing functions are valid for pvm_upkXXXX(). For example, since the master program is receiving an integer from its worker processes, it calls the integer unpacking function

int info = pvm_upkint(int *np, int nitem, int stride)
The first parameter is a pointer to where the first item unpacked is to be stored. The second and third give the number of items to be unpacked and the stride to be used. Our example code unpacks each partial result received into a different element of the results array and adds it to the running sum.

After the sum is computed and printed the master task informs the PVM daemon that it is withdrawing from the virtual machine. This is done by executing the function

int info = pvm_exit()

As for the worker program, after enrolling in the virtual machine, the worker tasks wait to receive their portion of the array to be summed. Using the `-1' values in the pvm_recv() call indicates that the task does not care what task the message was sent from nor what label was used. Since the size of the array being sent may not be known ahead of time, after unpacking the number of data items from the message, the worker code allocates enough memory to hold the rest of the data contained in the message. The array fragment is summed up and the total is sent back to the parent. The task id of the task that spawned the current task is returned by the pvm_parent() function.

int parent_id = pvm_parent()

Note that since the master program is expecting a msgtag of `7' from the worker tasks, this value must be used in the pvm_send() call.

Helpful Hint: If the set of tasks spawned will need to communicate amongst themselves, the parent task needs to send the task array tids that is generated in the pvm_spawn() function to each.