0

There is my question:

I have many files (more than 1000) of the same size (several Mb). I have to read them and extract some information. This step of the information extraction requires some time, so I can use (at least I hope so) this time to read another file. What I try to do is:

#pragma omp parallel for
for (int i=0; i<FilesCount; i++)
{
    myData Data;

    #pragma omp critical
    {
        Data.ReadDataFromFile (FileNames[i]);
    }

    //Operate with the Data and extract some information
}

It doesn't work as I expect. I also tried to use:

#pragma omp ordered

and the result is the same - only one thread is used. Other OpenMP stuff works fine. Maybe the problem is that I use fstream for the reading?

What is the problem with it and how to do that correctly?

Shiela
  • 500
  • 1
  • 7
  • 20
  • What do you mean by as expected? The code above will block each thread in the OMP task pool reading data (see @Frank's answer below. – Ade Miller Feb 25 '14 at 16:09
  • Did you notice a commented line below the 'critical' section? Or you mean, that while processed it blocks activities of other threads even if they have passed this section? Then what about 'ordered' case? Could you suggest any solution? – user3351574 Feb 25 '14 at 20:10
  • In your environment, what number does `omp_get_max_threads()` return? – yohjp Feb 26 '14 at 06:46
  • omp_get_max_threads() returns 8. – user3351574 Feb 26 '14 at 10:20
  • Read the file in with `fread`, process the data in parallel. See the highest voted answer here http://stackoverflow.com/questions/16812302/openmp-while-loop-for-text-file-reading-and-using-a-pipeline – Z boson Feb 26 '14 at 12:34
  • I thought about such thing, but the point is that I want to read and process different files in different threads. I do not see any restriction for that, that's why I'm asking for help. The issue I faced with is how to open and read a file in each thread, while other do not address file operations? If it is impossible, then I would see an explanation. – user3351574 Feb 26 '14 at 13:23

1 Answers1

0

Everything in a block marked with

#pragma omp critical

will be executed by one thread at a time only. All other threads have to wait until the current thread leaves that block.

Section about 'critical' in an OpenMP tutorial

Frank
  • 1,565
  • 1
  • 10
  • 9
  • That's why I put there only reading function. After this block 'Data' contains an information that I can operate with, and while doing so, another thread can start to read a new file. – user3351574 Feb 25 '14 at 15:46
  • OK, sorry for misunderstanding your question. I thought that ReadDataFromFile also contains the expensive stuff that you wanted to execute concurrently, and I overlooked the comment below that block. – Frank Feb 25 '14 at 15:56