title: Malloc performance in a multithreaded environment
body: <p>I've been running some experiments with the openmp framework and found some odd results I'm not sure I know how to explain.</p>
  
  <p>My goal is to create this huge matrix and then fill it with values. I made some parts of my code like parallel loops in order to gain performance from my multithreaded enviroment. I'm running this in a machine with 2 quad-core xeon processors, so I can safely put up to 8 concurrent threads in there.</p>
  
  <p>Everything works as expected, but for some reason the for loop actually allocating the rows of my matrix have an odd peak performance when running with only 3 threads. From there on, adding some more threads just makes my loop take longer. With 8 threads taking actually more time that it would need with only one.</p>
  
  <p>This is my parallel loop:</p>
  
  <pre><code> int width = 11;
   int height = 39916800;
   vector&lt;vector&lt;int&gt; &gt; matrix;
   matrix.resize(height);    
   #pragma omp parallel shared(matrix,width,height) private(i) num_threads(3)
   {
     #pragma omp for schedule(dynamic,chunk)
     for(i = 0; i &lt; height; i++){
       matrix[i].resize(width);
     }
   } /* End of parallel block */
  </code></pre>
  
  <p>This made me wonder: is there a known performance problem when calling malloc (which I suppose is what the resize method of the vector template class is actually calling) in a multithreaded enviroment? I found some articles saying something about performance loss in freeing heap space in a mutithreaded enviroment, but nothing specific about allocating new space as in this case.</p>
  
  <p>Just to give you an example, I'm placing below a graph of the time it takes for the loop to finish as a function of the number of threads for both the allocation loop, and a normal loop that just reads data from this huge matrix later on.</p>
  
  <p><img src="https://i.stack.imgur.com/kzHOi.png" alt="Parallel region 1, where there is allocation"></p>
  
  <p><img src="https://i.stack.imgur.com/PiCCH.png" alt="Parallel region 2, where there is no allocation"></p>
  
  <p>Both times where measured using the gettimeofday function and seem to return very similar and accurate results across different execution instances. So, anyone has a good explanation?</p>