14

There are some evaluations which are automatically parallelized or multithread if there are cores available.( for example, Eigenvalues)

But I don't want the evaluation to be automatically multithreaded, I want it to use only one core. How can I do it?

Michael E2
  • 235,386
  • 17
  • 334
  • 747
matheorem
  • 17,132
  • 8
  • 45
  • 115
  • Notice that you can run 2 threads on one core too, only if you have multithreaded processor. But it is not the same thing as parallel computing. – Kuba Aug 30 '13 at 13:35
  • @Kuba Yeah! I always noticed this. Sometimes, my parallel evaluations automatically became several 50% cpu running, If there are other people running their calculation on the same remote computer – matheorem Aug 30 '13 at 13:38
  • Is there a list of which functions automatically multithread? – SPPearce Mar 03 '17 at 19:47

1 Answers1

17

For Eigenvalues this will work:

SetSystemOptions["ParallelOptions" -> "ParallelThreadNumber" -> 1]

For functions which are using MKL libraries you can also use:

SetSystemOptions["MKLThreads" -> 1]

Source

It is wrriten there that you can reset it to the defaults by "ParallelThreadNumber" -> $ProcessorCount. But if you have cores that can be multithreaded then this number can be greater than $ProcessorCount.

  • Somewhere in documentation center is example with "MKLThreads" (I know it from there) but I can't find it :) I appreciate link if someone knows it :).
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • MKL libraries? I don't have an idea about "MKL libraries". What is this? – matheorem Aug 30 '13 at 13:40
  • Math kernel library is used by for example Solve. – Kuba Aug 30 '13 at 13:43
  • Its kind of an all in one math antibiotic for your Intel processor kernel ;) – PlatoManiac Aug 30 '13 at 13:44
  • @Kuba It seems that it is undocumented? If I set it to one, After I restart the mathematica, can the value be back to one? – matheorem Aug 30 '13 at 13:45
  • @Kuba I think you must familiar with multithread, can you take a look at this post (http://mathematica.stackexchange.com/questions/31202/whether-nintegrate-evaluation-is-multithreaded-or-not) for me? Unfortunately, somebody down voted it – matheorem Aug 30 '13 at 13:47
  • 1
    @Kuba Oh, SetSystemOptions["MKLThreads" -> 1] actually works for the NIntegrate. My fault. Thank you for your help! But I still wish you could look at the post I mentioned, there are peculiarities which I can't understand. – matheorem Aug 30 '13 at 13:57
  • @matheorem I believe the standard way to set options like this as the default is using the Kernel/init.m file. See (8221) and (1874) – Mr.Wizard Aug 31 '13 at 00:41
  • @matheorem I couldn;t find the second source but I will post it when I will. I saw your other question but I'm not competent enough to answer them :) – Kuba Sep 01 '13 at 11:32
  • @Kuba after examing the answer for the second time. I found that set "ParallelThreadNumber" didn't work for Eigenvalues, set "MKL" works for it. You can check it. The "ParallelThreadNumber" setting works for image processing functions, see http://mathematica.stackexchange.com/questions/3674/are-built-in-mathematica-functions-already-parallelized – matheorem Sep 02 '13 at 14:10
  • @matheorem Try Eigenvalues[ SparseArray[{{x_, y_} /; Abs[x - y] < 3 -> 1}, {100, 100}]] // Timing // First with 1 and 2 ParallelThreadNumber. It works for me. – Kuba Sep 02 '13 at 15:18
  • @matheorem well, have you checked that? – Kuba Sep 10 '13 at 10:29
  • @Kuba Oh, sorry. I have been away from here a couple of days. I tried your code. Yeah, it works. But if you change 1->1.0, that is try Eigenvalues[ SparseArray[{{x_, y_} /; Abs[x - y] < 3 -> 1.0}, {100, 100}]] // Timing // First you will see it multithread again. I don't know why. That is odd. – matheorem Sep 11 '13 at 00:33
  • @matheorem Interesting :) I will check that later. – Kuba Sep 11 '13 at 05:01