0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As I mentioned earlier the size of the file used in the encryption process large and can not move it directly to the matrix. sot the instruction "fread(Store,sizeof(char),icount,Rfile);// read" , Not be useful, Is there another option???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.