Question: How to limit the number of processors used by Threads?

How can I limit the number of processors that are used by a multi-threaded operation? The help pages ?multithreaded and ?kernelopts both suggest, vaguely, that this can be controlled with kernelopts(numcpus). The following worksheet shows, however, that this doesn't work: While Maple does remember what you set numcpus to, this setting has no effect on the number processors used by Threads. And, if this doesn't work, what purpose is there in being able to set the value of numcpus?

restart:

First, warm-up and stretch the memory. Otherwise, the timings are invalid.

L:= RandomTools:-Generate(list(integer, 2^18)):

CodeTools:-Usage(Threads:-Mul(x, x= L)):

memory used=1.21GiB, alloc change=0.98GiB, cpu time=18.66s, real time=3.75s, gc time=27.54m

Now this is the actual test.

L:= RandomTools:-Generate(list(integer, 2^18)):

gc();

for n to kernelopts(numcpus) do
     print(kernelopts(numcpus= n));
     CodeTools:-Usage(Threads:-Mul(x, x= L), iterations= 4)
end do:

8

memory used=1.21GiB, alloc change=322.34MiB, cpu time=4.04s, real time=933.75ms, gc time=566.41ms

1

memory used=1.21GiB, alloc change=-2.00MiB, cpu time=4.22s, real time=859.50ms, gc time=656.25ms

2

memory used=1.21GiB, alloc change=-1.17MiB, cpu time=4.03s, real time=841.75ms, gc time=566.41ms

3

memory used=1.21GiB, alloc change=256.00MiB, cpu time=4.47s, real time=911.25ms, gc time=738.28ms

4

memory used=1.21GiB, alloc change=0 bytes, cpu time=4.33s, real time=852.50ms, gc time=605.47ms

5

memory used=1.21GiB, alloc change=0 bytes, cpu time=4.34s, real time=853.50ms, gc time=628.91ms

6

memory used=1.21GiB, alloc change=0 bytes, cpu time=4.27s, real time=826.75ms, gc time=535.16ms

7

memory used=1.21GiB, alloc change=0 bytes, cpu time=4.19s, real time=841.50ms, gc time=308.59ms

 

Download numcpus.mw

You'll get equivalent results if you replace Mul by Add and increase the size of L to about 2^24.

Please Wait...