User`s guide

Cray XMT Programming Environment Users Guide
Example 7. Calling UNIX I/O functions from parallel code
In serial code, the low-level UNIX functions behave as specified by the Posix
standard. In parallel code, all calls are executed atomically. In this case, you must
explicitly manage access to a particular file by a sequence of calls, to prevent races.
For example:
#pragma mta assert parallel
for (i = 0; i < n; i++)
{
char line[80];
int len = sprintf(line, "this is iteration %d\n", i);
write(fd, line, len);
}
The previous code asserts that the loop is parallel. In general, it is not safe for the
compiler to parallelize a loop that contains procedure calls, especially calls to I/O
functions. The assertion tells the compiler that, in this case, it is safe to parallelize
the loop. The atomicity guarantee ensures that each line written to
fd by this loop is
of the form that follows:
this is iteration i
Two lines are never mixed together, so the following never occurs:
this is this is iteration j
The actual sequence of lines is random because the different iterations are all
executed in parallel. However, for a sequence of calls such as the following:
char part1[80];
int len1 = sprintf(part1, "this is iteration ");
#pragma mta assert parallel
for (i = 0; i < n; i++)
{
char part2[80];
int len2 = sprintf(part2,"%d\n", i);
write(fd, part1, len1);
write(fd, part2, len2);
}
The output may look like the following, because only the individual calls to write
are atomic:
this is iteration this is iteration i
k
this is iteration j
40 S247920