User`s guide
Developing an Application [3]
If many parallel calls refer to the same file, locking forces a serial execution order.
For example, in the following code, it makes little sense to run the loop in parallel
because the calls to
fprintf are serialized by the lock on the FILE object referred
to by g. However, the interpretation of the format string is controlled by the lock.
#pragma mta assert parallel
for (i = 0; i < n; i++)
{
fprintf(g,"this is iteration %d\n", i);
}
If the loop contains significant computations, such as in the following example, you
may want to parallelize the loop.
#pragma mta assert parallel
for (i = 0; i < n; i++)
{
int j = expensive_function(i);
fprintf(g,"f(%d) = %d\n", i, j);
}
You cannot use the stdio functions to support concurrent file access. For example,
consider the following code:
#pragma mta assert parallel
for (i = 0; i < n; i++)
{
Buf buffer;
FILE *f = fopen(file_name, "r");
fseek(f, i*sizeof(Buf), SEEK_SET);
fread(buffer, sizeof(Buf), 1, f);
fclose(f);
}
There are two problems in this example:
• If n is large, the system cannot support so many open files.
• The file position (set by fseek) is shared among all open versions of the file, so
races may occur.
3.7.2 System-level I/O
There are a number of low-level functions provided by the operating system to
support more flexible and efficient I/O. However, you should avoid accessing a
given file using both the high-level language-dependent methods and the low-level
functions. The high-level functions use buffering that may interact with the low-level
functions in unpredictable ways.
S–2479–20 39