title: OpenMP - accessing individual file by individual thread
body: <p>I have 4 cores == 4 threads (<code>omp_get_max_threads()</code>) so I created 4 sections (OMP sections) to break that one huge file into 4 sub-partitions so then to feed them to 4 different threads and do the further processing in each thread (other approaches were there but I wanted to try this). In each section I opened same large file independently assuming that each section will have their own independent file descriptor.
  The problem is that out of 4 sections only 3 sections are writing to their 3 partition files and 1 of the section is not writing a single byte at all. And this happens randomly to any section upon different runs.</p>
  <p>I also tried to share same file pointer among sections but the problem persisted. In addition performance deteriorated so I made file pointer in each section.</p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;omp.h&gt;
  #include&lt;fstream&gt;
  #include&lt;string&gt;
  #include&lt;sstream&gt;
  using namespace std;
  
  int main()
  {
      int no_of_threads = omp_get_max_threads();
      int partition_size = 1000000000/no_of_threads;
      long double word_length_counter;
      string temp_word_holder;
      word_length_counter=0;
      
      cout&lt;&lt;&quot;\nno of threads &quot;&lt;&lt;no_of_threads;
      #pragma omp parallel private(word_length_counter,temp_word_holder) shared(partition_size)
      {
          #pragma omp sections
          {
              #pragma omp section
              {
                  ifstream main_file;
                  ofstream temp_file_holder;
                  main_file.open(&quot;generated.txt&quot;,ios::in);
                  temp_file_holder.open(&quot;partition0.txt&quot;,ios::out);
  
                  while(word_length_counter &lt;= partition_size)
                  {
                      main_file&gt;&gt;temp_word_holder; 
                      word_length_counter += temp_word_holder.length();
                      temp_file_holder&lt;&lt;temp_word_holder&lt;&lt;endl;
                  }
                  main_file.close();
                  temp_file_holder.close();
              }
  
              #pragma omp section
              {
                  ifstream main_file1;
                  ofstream temp_file_holder1;
                  main_file1.open(&quot;generated.txt&quot;,ios::in);
                  temp_file_holder1.open(&quot;partition1.txt&quot;,ios::out);
                  main_file1.seekg(partition_size);
                  while(word_length_counter &lt;= partition_size)
                  {
                      main_file1&gt;&gt;temp_word_holder; 
                      word_length_counter += temp_word_holder.length();
                      temp_file_holder1&lt;&lt;temp_word_holder&lt;&lt;endl;
                  }
                  main_file1.close();
                  temp_file_holder1.close();
              }
  
              #pragma omp section
              {
                  ifstream main_file2;
                  ofstream temp_file_holder2;
                  main_file2.open(&quot;generated.txt&quot;,ios::in);
                  temp_file_holder2.open(&quot;partition2.txt&quot;,ios::out);
                  main_file2.seekg((partition_size*2));
                  while(word_length_counter &lt;= partition_size)
                  {
                      main_file2&gt;&gt;temp_word_holder; 
                      word_length_counter += temp_word_holder.length();
                      temp_file_holder2&lt;&lt;temp_word_holder&lt;&lt;endl;
                  }
                  main_file2.close();
                  temp_file_holder2.close();
              }
  
              #pragma omp section
              {
                  ifstream main_file3;
                  ofstream temp_file_holder3;
                  main_file3.open(&quot;generated.txt&quot;,ios::in);
                  temp_file_holder3.open(&quot;partition3.txt&quot;,ios::out);
                  main_file3.seekg((partition_size-1)*3);
                  while(word_length_counter &lt;= partition_size &amp;&amp; !main_file3.eof())
                  {
                      main_file3&gt;&gt;temp_word_holder; 
                      word_length_counter += temp_word_holder.length();
                      temp_file_holder3&lt;&lt;temp_word_holder&lt;&lt;endl;
                  }
                  main_file3.close();
                  temp_file_holder3.close();
              }
          }
          
      }
      #pragma omp barrier
      cout&lt;&lt;&quot;\npartitions generated&quot;;
  }
  </code></pre>