Parallel Programming Guide for HP-UX Systems

UPC
Running
Chapter 5 103
Example 5-3 below illustrates a method to partition output files into separate streams, one for
each thread, under program control. A given file name is appended with the thread number,
and a separate output file is opened for each thread. A similar approach could also be used for
input files. Note that the fopen function calls occur in parallel, one on each thread.
Example 5-3 Partitioning Output Streams
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *open_thread_output_file(char *base_name) {
char fname[100];
sprintf(fname, “%s.%d”, base_name, MYTHREAD);
return fopen(fname, “w”);
}
The -t option to the prun command causes the output lines from each thread to be prefixed
with the thread number when the output lines are sent to the terminal. Note that this will not
happen when the output lines from each thread are redirected to separate files.