user3540874
user3540874

Reputation: 1

multithreading using openmp in c++

What is the best solution if I just want to parallelize the loop only and sequential saving to file using openmp. I have a file with a large volume of information I would like to split into equal chunks (16 bytes each) , encrypted using openmp (multithreading programming in C++). After completion of encryption process these chunks store in a single file, but the same sequence of the original.

 i_count=meta->subchunk2_size-meta->subchunk2_size%16;// TO GET THE EXACT LENTH MODE 16
   // Get the number of processors in this system
         int iCPU = omp_get_num_procs();
       // Now set the number of threads
         omp_set_num_threads(iCPU);  
       #pragma omp parallel for ordered
    for( i=0;i<i_count;i+=16)
    {
         fread(Store,sizeof(char),16,Rfile);// read 
         ENCRYPT();
         #pragma omp ordered
         fwrite(Store,sizeof(char),16,Wfile) // write
    }

The program it supposed works parallel but the saving to file work sequential, but the implementation of the program shows it work in sequential order.

Upvotes: 0

Views: 196

Answers (1)

Z boson
Z boson

Reputation: 33699

You're much better off reading the whole file into a buffer in one thread, processing the buffer in parallel without using ordered, and then writing the buffer in one thread. Something like this

fread(Store,sizeof(char),icount,Rfile);// read
#pragma omp parallel for schedule(static)
for( i=0;i<i_count;i+=16) {
     ENCRYPT(&Store[i]);  //ENCRYPT acts on 16 bytes at a time    
}
fwrite(Store,sizeof(char),icount,Wfile) // write

If the file is too big to read in all at once then do it in chunks in a loop. The main point is that the ENCRYPT function should be much slower than reading writing files. Otherwise, there is no point in using multiple threads anyway because you can't really speed up reading writing files with multiple threads.

Upvotes: 1

Related Questions