0

In my Matlab script, I run an algorithm with a for loop in which, at each iteration, I need to do a gradient descent. All the gradient descent are independent. Here is the structure of my script :

for i=1:p

x=gradient_descent(x_init,grad_g,opts(i));

end

where opts(i) is a structure which contains variables necessary to the gradient descent. In this case, $p=145$. My script runs in $8$ seconds on my machine (without using any parallel trick). I have the impression that my script can be parallelized since each gradient descent in independent. When I un matlabpool on my computer, I can use up to 4 labs. Simultaneously, each lab could run a gradient descent and, instead of doing one gradient descent at a time, I would be able to do 4. But I do not know how I could parallelize my script. From what I have found on Internet, I could use the batch function, right ?

5
  • 1
    if you have the parallel toolbox you can use parfor instead of for. Maybe have a look here: mathworks.de/de/help/distcomp/getting-started-with-parfor.html Commented Sep 3, 2014 at 12:25
  • Parfor won't work because of the way 'opts' is used. Commented Sep 3, 2014 at 12:40
  • @jibounet Post a minimal example in which switching from for to parfor would cause Matlab to show the error that you get. (In your example, we are missing the definition of opts) Commented Sep 3, 2014 at 12:58
  • Did you check with setting only one core (maxNumCompThreads) for you system? If your performance drops by a factor of 4-ish, then it is already paralellized. Commented Sep 3, 2014 at 13:01
  • @zinjaai : I succeeded in using a parfor loop, thanks ! Commented Sep 3, 2014 at 13:24

1 Answer 1

4

I think this should work:

parfor i=1:p
    x{i}=gradient_descent(x_init,grad_g,opts(i));
end
Sign up to request clarification or add additional context in comments.

1 Comment

I adapted your answer to my script and I eventually got the parfor loop to work ! Everything works fine. Thanks !!

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.