0

I need help in parallel programming in MATLAB. I have a list of filenames; for each file I have to do an independent calculation that returns a table row. All the tableRows should be integrated into one table. The order of the rows has no meaning. How do I process all the files in parallel and insert the rows into one table?

samples=dir('*.txt');
for smpl=samples'
   row=processSamples(smpl,prm1,prm2); //should be parallel
   table=[table;row];                  
end

Thanks

1 Answer 1

1

Matlab has a very useful and easy to use implementation of the parallelized for loop called parfor. See e.g. doc parfor.

Code would be similar to the below depending on the dimension and type of your "row" variable. The point is you have to index into table for parfor to be able to work.

samples=dir('*.txt');
parfor k=1:length(samples)
   smpl = samples(k)
   row=processSamples(smpl,prm1,prm2); //should be parallel
   table(k) = row % Preserves order
end

When the code hits the parfor line it will spend some time (seconds) to set up parallel workers on your local computer. Alternatively you can use parpool to set up the workers on your local computer or a cluster fx.

Exemplifying

n = 10;
y = randi(10,1,n);
squaredy = zeros(n,1) % Not necessary for parfor to work
parfor k = 1:n
    squaredy(k) = y(k)^2
end
disp(y)
disp(squaredy)
Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure this usage of a growing array in parfor is legal? It looks fishy to me but I don't have MATLAB right now to test it. Usually parfor doesn't allow use with unexpected outcomes.
the 'parfor' loop can't work like 'foreach' (as it is in the current program), but i changed some lines and it works perfectly thanks a lot.
Yeah you might be right about the growing, probably works with preallocated array and indexing. I'll test that, hold on
Yep, you are right about the growing. You need to use indexing depending on the dimensions and type of your output "row". But good to hear you got it working, I'll update my answer correspondingly

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.