title: Optimizing N-queen with openmp
body: <p>I am learning OPENMP and wrote the following code to solve nqueens problem.</p>
  
  <pre><code>//Full Code: https://github.com/Shafaet/Codes/blob/master/OPENMP/Parallel%20N-  Queen%20problem.cpp
  int n;
  
  int call(int col,int rowmask,int dia1,int dia2)
  {
      if(col==n) 
      {
          return 1;
  
      }
      int row,ans=0;
      for(row=0;row&lt;n;row++)
      {
          if(!(rowmask &amp; (1&lt;&lt;row)) &amp; !(dia1 &amp; (1&lt;&lt;(row+col))) &amp; !(dia2 &amp; (1&lt;&lt;((row+n-1)-col))))
          {           
              ans+=call(col+1,rowmask|1&lt;&lt;row,dia1|(1&lt;&lt;(row+col)), dia2|(1&lt;&lt;((row+n-1)-col)));
          }
      }
      return ans;
  
  }
  
  double parallel()
  {
      double st=omp_get_wtime();
      int ans=0;
      int i;
      int rowmask=0,dia1=0,dia2=0;
       #pragma omp parallel for reduction(+:ans) shared(i,rowmask)
      for(i=0;i&lt;n;i++)
      {
          rowmask=0;
          dia1=0,dia2=0;
          int col=0,row=i;
          ans+=call(1,rowmask|1&lt;&lt;row,dia1|(1&lt;&lt;(row+col)), dia2|(1&lt;&lt;((row+n-1)-col)));
      }
      printf("Found %d configuration for n=%d\n",ans,n);
      double en=omp_get_wtime();
      printf("Time taken using openmp %lf\n",en-st);
      return en-st;
  
  }
  double serial()
  {
  
      double st=omp_get_wtime();
      int ans=0;
      int i;
      int rowmask=0,dia1=0,dia2=0;
      for(i=0;i&lt;n;i++)
      {
          rowmask=0;
          dia1=0,dia2=0;
          int col=0,row=i;
          ans+=call(1,rowmask|1&lt;&lt;row,dia1|(1&lt;&lt;(row+col)), dia2|(1&lt;&lt;((row+n-1)-col)));
      }
      printf("Found %d configuration for n=%d\n",ans,n);
      double en=omp_get_wtime();
      printf("Time taken without openmp %lf\n",en-st);
      return en-st;
  
  }
  int main()
  {
      double average=0;
      int count=0;
      for(int i=2;i&lt;=13;i++)
      {
          count++;
          n=i;
  
          double stime=serial();
          double ptime=parallel();
          printf("OpenMP is %lf times faster for n=%d\n",stime/ptime,n);
          average+=stime/ptime;
          puts("===============");
      }
      printf("On average OpenMP is %lf times faster\n",average/count);
      return 0;
  
  }
  </code></pre>
  
  <p>Parallel code is already faster than normal one but i wonder how can i optimize it more using openmp pragmas. I want to know what i should do for better performance and what i should not do.</p>
  
  <p>Thanks in advance.</p>
  
  <p>(Please dont suggest any optimizations which are non-related to parallel programming)</p>