help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multicore process - rewriting a for-loop


From: Ismael Diego Nunez-Riboni
Subject: Re: Multicore process - rewriting a for-loop
Date: Mon, 21 Jan 2013 11:37:07 +0100

-------- Original-Nachricht --------
> Datum: Mon, 21 Jan 2013 01:28:13 -0800 (PST)
> Von: lyvic <address@hidden>
> An: address@hidden
> Betreff: Multicore process - rewriting a for-loop

> Hello dear Forum,
> 
> I'm proud I got this far, but now I'm stuck and need your help:
> 

Hi Max, I also tried once the multicore package but could not really get too 
far with it and changed to the general package (general-1.3.0), which works 
fine for me. I use the function PARCELLFUN. So, unless somebody else in the 
list helps you with the multicore package, I suggest to use parcellfun from the 
general package. Here you just have to divide your input vector in various 
vectors, equally long (as much vectors as processes you want to use) and then 
call parcellfun. Here is an example (you must save your input vectors in an 
cell array, in the example is called INPUTVECT):

------------------
      % Dividing the input vectors in NOP *equally* long vectors:

      le = floor(length(YOURINPUTDATA)./NOP);

      % Building the parallel input vectors:
      for aa = 1:NOP
         INPUTVECT{aa}= YOURINPUTDATA([(aa-1)*le+1 : aa*le]);
      end

      % Calling the function in parallel! :

      YOUROUTPUT  = parcellfun (NOP, @YOURFUNCTION, INPUTVECT);

-------------------

NOP=Number of processes to use. Do not forget the @ next to the function's 
name! It's easy and works like a charm, the hardest part is to divide your 
input data in equally length vectors... Some notes:

1) After calling the function you will have to join the various output vectors 
in one.

2) If you have a "rest vector" simply call the function with this rest vector 
as input in a single core. Append the rest vector to your output vector.

3) Instead of passing the data itself to YOURFUNCTION you can define the data 
as "global" and then pass only the indices of the data you want to process in 
each core... I believe this should be faster.

4) Probably you already know this but I had to learn it the hard way: sometimes 
you make your program to run faster by simply coding more efficiently than by 
using many cores. Check first if you can reduce for loops to matrix algebra, 
double for loops to single loops, etc.

I hope this help.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]