Matlab parfor equivalent
- Posted by Jerome Oct 22, 2012
- 1366 views
I have some EU code that I would like to run in parallel on mutliple cores. I've been spoiled by things like Matlab where I can use the 'parfor' command to have a for loop run in parallel. While investigating how this might work in Euphoria, I came across this post: http://openeuphoria.org/forum/113531.wc#113531 .
Out of curiosity, I wanted to see if I could come up with a very basic fix for my situation. I wrapped some of the basic pthreads commands and have something like this for EU code:
-- Allocate data to be passed to function in thread atom msg1 = allocate(16), msg2 = allocate(16) -- Pass in which chunk of the for loop to execute along with SieveBenchmark inputs poke4(msg1,{1,2,8192,50_000}) poke4(msg2,{3,4,8192,50_000}) -- Pthread code pthreadCreate(0,call_back(routine_id("parallelCode")),msg1) pthreadCreate(1,call_back(routine_id("parallelCode")),msg2) pthreadJoin(0) pthreadJoin(1) function parallelCode(atom msg) -- Get parameter data passed in from pthread_create integer start,finish,size,iterations {start, finish, size, iterations} = peek4u({msg,4}) -- For loop I wish to turn into a parfor equivalent for i = start to finish do SieveBenchmark({size,iterations}) end for return 0 end function
As an example, I'm running the SieveBenchmark multiple times in parallel. This does the trick nicely when translated to C; I get full usage of two cores. It fails miserably when iterpreted though with machine-level exceptions. Is something along these lines easily possible with interpreted euphoria?
Thanks,
Ira