Static Analysis Problem Type Reference

Unordered use of I/O operation

Unordered use of I/O operation.

Performing I/O operations in a parallel region but outside of an ORDERED, MASTER, or SINGLE block can result in unpredictable order of input and output operations. This can cause different execution results in parallel and sequential mode.

ID

Code Location

Description

1

Call site

The place where the I/O function was called

Example


#include <stdio.h>

int main(int argc, char **argv)
{
    int i;
    FILE *fp;
    fp = fopen("random.txt", "w");
    #pragma omp parallel for
    for (i = 0; i < 10; i++) {
        // bad: order is not guaranteed
        fprintf(fp, "i = %d\n", i);
    }
    fclose(fp);
    
    fp = fopen("ordered.txt", "w");
    #pragma omp parallel for ordered
    for (i = 0; i < 10; i++) {
        // this is OK
        #pragma omp ordered
        fprintf(fp, "i = %d\n", i);
    }
    fclose(fp);
    
    return 0;
}