Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

A general question: For using matlabpool, I write the line

matlabpool open local 12;

as the first line in my code and the line:

matlabpool close;

as the last. Is this correct?

share|improve this question

3 Answers 3

Seems to be fine according to the documentation.

The only things you need to try are whether 12 is the best number for you, and whether it makes sense to close the matlabpool earlier (you might not gain anything from it for a postprocessing part). Furthermore it is of course always good to check whether it actually gives the speed increase that you are looking for.

share|improve this answer

This is correct, but I would recommend you to use matlabpool open local. This lets matlab decide how many workers are used, 12 might require to much memory.

share|improve this answer

My answer is very late, but I'd like to suggest a couple of improvements to your code.

Firstly, I would use the functional form, rather than the command-dual form:

matlabpool('open', 'local', 12);

and

matlabpool('close');

as it's easier (if you later need to) to parameterize the number of workers or the cluster used.

The next suggestion assumes that the instructions for opening and closing the pool are within a function. If you're just writing some quick code, this doesn't really matter, but for larger applications it's typically better to be writing reusable, modular functions with their own workspace rather than scripts.

I would typically write two consecutive lines:

matlabpool('open', 'local', 12);    
cleaner = onCleanup(@()matlabpool('close'));

This creates an onCleanup object cleaner that will execute the supplied code (in other words matlabpool('close')) when it is deleted. It will be deleted when it goes out of scope, which happens either when the function completes and exits, or if the function exits due to an error or Ctrl-C.

This pattern has two advantages:

  1. You are assured that the pool will always be closed, however the function terminates - if you leave the closing in a separate statement at the end, this may not always be reached due to an error earlier on.
  2. The commands for opening and closing the pool are right next to each other in the code, so when you're reading through it, you're more likely to be sure that the right thing is being done.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.