title: Serial version slower than parallel with MKL_DYNAMIC=TRUE
body: <p>I have implemented Conjugate Gradient in FORTRAN by replacing the Linear Algebra subroutines in the <a href="http://en.wikipedia.org/wiki/Conjugate_gradient_method#Example_code_in_GNU_Octave" rel="nofollow">wikipedia example</a> by (Fortran) Intel MKL subroutines. (DGEMV, DAXPY and DNRM only. It turns out that a=b is faster than DCOPY and a=2*a is faster than DSCAL)</p>
  
  <p>The answers are correct and there is no problem with the implementation. However, when I compile it as <code>ifort CG.f90 -mkl</code> The results are :</p>
  
  <p>MKL_SET_DYNAMIC = TRUE ; 140 seconds</p>
  
  <p>MKL_SET_DYNAMIC = FALSE, MKL_SET_NUM_THREADS=1 ; 70 seconds.</p>
  
  <p>MKL_SET_DYNAMIC = FALSE, MKL_SET_NUM_THREADS=2 ; ~100 seconds.</p>
  
  <p>A few points:</p>
  
  <ol>
  <li>I have 2 real cores and 2 virtual cores through hyperthreading. I am not trying to run 16 threads on a 2 core machine.</li>
  <li>Profiling has yielded abstruse references to a <code>M16_LAY_GAS16</code> which after a lot of searching came down to <code>multpd</code> ASM. Nothing useful came out otherwise (or maybe, I didn't know where to look) FWIW, I used VTune.</li>
  <li>The problem size is not small. The above examples are for matrix sizes proportional to the size of my RAM (Roughly 13k x 13k for my 4 GB System).</li>
  <li><code>KMP_AFFINITY</code> maps one thread to one processor in serial case and 2 threads to 2 processors in parallel.</li>
  </ol>
  
  <p>My question is : <strong>Why isn't MKL_DYNAMIC setting number of threads as 1 if that is optimal? I don't necessarily need to use 2 threads if the same work (in lesser time) is done by 1.</strong></p>
  
  <p>Am I doing something wrong or is something wrong with Intel MKL?</p>