title: Sequential is faster than Multi threaded - OpenMp - C++
body: <p>I am using C++ &amp; OpenMP to parallelize an algorithm to find the convex hull.
  But I am not able to get the expected speedup. In fact, the sequential algorithm is faster.
  The input &amp; output set of points are stored in arrays.</p>
  
  <p>Could you please look into the code and let me know the corrections? </p>
  
  <pre><code>Point *points = new Point[inp_size]; // contains the input
    int th_id;
  
    omp_set_num_threads(nthreads);
    clock_t t1,t2;
        t1=clock();
    #pragma omp parallel private(th_id)
    {
      th_id = omp_get_thread_num();
  ///////////// …. Only Function called ….///////////////////////////////////
      findParallelUCHWOUP(points,th_id+1, nthreads, inp_size);
  
    }
    t2=clock();
    float diff ((float)t2-(float)t1);
    float seconds = diff / CLOCKS_PER_SEC;
    std::cout &lt;&lt; "Time Elapsed in seconds:" &lt;&lt; seconds &lt;&lt; '\n';
  </code></pre>
  
  <p>///////////////////////////////////////////////////////////////</p>
  
  <pre><code>int findParallelUCHWOUP(Point iv[],int id, int thread_num, int inp_size){
  
      int numElems = inp_size/thread_num;
          int first = (id-1) * numElems;;
          int last;
          if(id == thread_num){
              last = inp_size-1;
          }
          else{
              last = id*numElems-1;
          }
  
          output[first]=iv[first];
            std::stack&lt;int&gt; s;
            s.push(first);
            int i=first+1;
          while(i&lt;last){
              if ( crossProduct(iv, i, first, last) &gt; 0){
                  s.push(i);
                  i++;
                  break;
              }else{
                  i++;
              }
          }
  
          if(i==last){
              s.push(last);
              return 0;
          }
  
          for(;i&lt;=last;i++){
              if ( crossProduct(iv, i, first, last) &gt;= 0){
                    while ( s.size()&gt;1 &amp;&amp; crossProduct(iv, s.top(), second(s), i) &lt;= 0){
                        s.pop();
                    }
                    s.push(i);
              }
  
          }
            int count=s.size();
            sizes[id-1] = count;
            while(!s.empty()){
                output[first+count-1]=iv[s.top()];
                s.pop();
                count--;
            }
  
      return 0;
  }
  </code></pre>
  
  <p>///////////tested on these machines/////</p>
  
  <p>Sequential Time:0.016466
  Using two threads:0.022979
  Using four threads:0.035213
  Using 8 threads: 0.03315</p>
  
  <p>Machine used: Mac Book Pro
  Processor: 2.5 GHz Intel Core i5(at least 4 logical cores)
  Memory: 4GB 1600 MHz
  Compiler: Mac OSX Compiler</p>