0

I'm getting poorer performance than I expected when multithreading work on a large RawArray in Python (2.7.12+ on Kubuntu 16.10). The array is shared among threads, and each thread works on its own region without fear of contention with other threads, so I don't need or want any synchronization/locking to happen with the array.

If I run my test code with two threads, I get about twice the performance as with one thread, as expected. Three threads is not great (~37% as much execution time, as opposed to the expected ~33-35), but improvement happens. But four threads offers no improvement at all (even slightly worse performance) and adding more threads also offers little or none.

This is an eight-thread four-core CPU (i4770K), so perhaps I shouldn't expect any more performance past 4 threads -- is that correct?

Nonetheless, the lack of improvement from 3 to 4 threads confuses me, and the gap between expected and actual performance (25% vs 37%) is so large that it seems like something must be going wrong.

The documentation for RawArray hedges about whether or not underlying access is synchronized (e.g. "setting and getting an element is potentially non-atomic"). Is it possible that there is some underlying synchronization or other inefficiency happening in RawArray? Or am I misunderstanding something fundamental?

Here are results I get with my test code, showing execution time as a function of the number of threads; four runs were done, and the coordinates graphed over each other:

Thread Performance Graph

...in that graph you can clearly see it hit a wall at 3 threads. Here is the code I'm using to test; it creates a big array, breaks it up into numthreads pieces, and hands it off to the threads to do some simple math on it:

import math
from multiprocessing import Process
from multiprocessing.sharedctypes import RawArray
import multiprocessing
import datetime as dt

def doMath(mpa, startidx, endidx):
    for i in range(startidx, endidx):
        mpa[i] = (math.pow(2.12354, 5.1341234)*1.234845)/4.1234234 + 1.345345

mpa = RawArray('f', 200000000)

for numthreads in range(1, 9):

    threads = []
    chunkwidth = int(math.floor(float(len(mpa))/float(numthreads)))

    for tc in range(numthreads - 1):
        threads.append(Process(target=doMath, args=(mpa, tc*chunkwidth, (tc+1)*chunkwidth)))
    threads.append(Process(target=doMath, args=(mpa, (numthreads-1)*chunkwidth, len(mpa))))

    starttime = dt.datetime.now()

    for i in range(numthreads):
        threads[i].start()

    for i in range(numthreads):
        threads[i].join()

    proctime = dt.datetime.now() - starttime

    print('num threads: ' + str(numthreads) + ' time: {:.4f}'.format(proctime.total_seconds()) + ' secs')
6
  • Just out of curiosity, if you change the code to not assign to mpa (still doing the math, but not storing it), does the scaling issue disappear? That would help rule out synchronization issues (whether at the Python level or the OS trying to synchronize the shared memory amongst the processes that map it). My system is only 2 hyperthreaded cores (4 virtual), so scaling issues would hit me too early to reproduce your problem exactly. I see it take 70 seconds for one process, 44 for two, and 36 seconds for three, and 35 for four or more. Dec 9, 2016 at 1:21
  • And having tested my own suggestion (where the child processes receive the RawArray but don't actually write anything to it), I get timings of 61, 33, 31, 29 for 1, 2, 3 and 4+ worker processes. If it's the same for you (as noted, I don't have enough "real" cores to be sure the drop-off isn't just hyperthreaded virtual cores being terrible), it's something else that is causing your issue; if you never write the the RawArray, it can't be the result of any locking on the RawArray, Python or OS level. Dec 9, 2016 at 1:29
  • Lastly, rather important side-note: range is slower and more memory intensive than xrange on Py2, particularly for large ranges (making a range of 200000000 values for your one process case means that process is going to create and store 200M int values in a list; on a 64 bit system, at 24 bytes per Python int and 8 bytes for the list to store the pointer, you need to allocate and initialize ~6 GB of RAM before your loop processes even a single value; kind of ruins your memory profile. Dec 9, 2016 at 1:44
  • You might consider using xrange, which still has to generate the numbers, but creates and discards them one at a time as you iterate (and Python 2 has an int free list, so few actual allocations occur), reducing the peak memory usage (and associated loss of memory locality) dramatically. Dec 9, 2016 at 1:45
  • Thanks SR, and great idea: removing the mpa write results in scaling as I'd expect it: 100%, 50%, 33%, 25%, 25%, 25% (well, more like 100, 50, 34, 27, or whatever, but close enough)... so perhaps that points a finger at RawArray... any other ideas on how to proceed? And thanks for the range() tip. I suppose iterating an integer counter in a while loop would be even faster than xrange?
    – user272901
    Dec 9, 2016 at 5:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.