views:

34

answers:

2

I'm doing some time trials on my code, and logically it seems really easy to parallelize with OpenMP as each trial is independent of the others. As it stands, my code looks something like this:

for(int size = 30; size < 50; ++size) {
    #pragma omp parallel for
    for(int trial = 0; trial < 8; ++trial) {
        time_t start, end;
        //initializations
        time(&start);

        //perform computation

        time(&end);

        output << size << "\t" << difftime(end,start) << endl;
    }
    output << endl;
}

I have a sneaking suspicion that this is kind of a faux pas, however, as two threads may simultaneously write values to the output, thus screwing up the formatting. Is this a problem, and if so, will surrounding the output << size << ... code with a #pragma omp critical statement fix it?

+2  A: 

Never mind whether your output will be screwed up (it likely will). Unless you're really careful to assign your OpenMP threads to different processors that don't share resources like memory bandwidth, your time trials aren't very meaningful either. Different runs will be interfering with each other.

The solution to the problem you're asking about is to write the result times into designated elements of an array, with one slot for each trial, and ouput the results after the fact.

Novelocrat
I'm not so much interested in the absolute timing data, I'm actually just trying to see the relative scaling between different sizes. Will that be as heavily impacted?The initial parallel test runs looked convincingly enough like the serial results for me to proceed, but I am still very much a noob and looks can be deceiving :)
ConnorG
I can think of several scenarios in which different problem sizes would be differently impacted by the issues I mentioned. For instance, there will be a different cache pressure cutoff depending on how much parallelism you run with.
Novelocrat
A: 

As long as you don't mind the individual lines being out of order you'll be fine. OpenMP should make sure a whole line is printed at a time.

However, you will need to declare start and end as private in the pragma otherwise the threads will overwrite them and mess up your timings.

Kevin Stock
Huh. Why is that? I just kind of assumed that since the declarations came after the initial thread-creating pragma, each thread would do their own declarations. I tested it out, and gcc yelled as they had not been declared yet.
ConnorG