why does (ga + parallel + global variables + sub2ind) fail?

When optimizing with ga and parallel computing, a sub2ind call using globals fails (subscript vectors must be of same size), but when doing it in serial, it doesn't fail. Why?

 Akzeptierte Antwort

Matt J
Matt J am 9 Sep. 2016

1 Stimme

Probably because of the dangers of using global variables.

5 Kommentare

Jochen Schuettler
Jochen Schuettler am 10 Sep. 2016
Bearbeitet: Jochen Schuettler am 10 Sep. 2016
Hi Matt, of course I know all about this. There is a bunch of constant data for the fitness function to know. It would really slow down optimization if I unnecessarily copy all that data again and again as a function input. Could you please give a more helpful answer?
Stephen23
Stephen23 am 10 Sep. 2016
Bearbeitet: Stephen23 am 10 Sep. 2016
"It would really slow down optimization if I unnecessarily copy all that data again and again as a function input"
Not so much... MATLAB only copies variables in the memory when they are changed: "MATLAB internally optimizes for some cases and passes by reference and only creates local copies if modifications are being made to these arguments." source:
So as long as those variables are not being changed you can use them as inputs to a million function calls, and yet there will be no copies in MATLAB's memory.
Note that the above discussion is only for serial computing. The considerations are a bit different for parallel computing, where each worker must have a copy of the data because they are different processes.
Matt J
Matt J am 11 Sep. 2016
Bearbeitet: Matt J am 11 Sep. 2016
The discussion still applies to parallel computing if we're not talking about the one-time cost of broadcasting constant data to the workers. The OP seems to think that arguments passed to the fitness function will be copied "again and again" each time the fitness function is called. That is not true, even on parallel workers

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 10 Sep. 2016

1 Stimme

Global variables are never copied to parallel workers.
You might be able to take advantage of parallel.pool.Constant or of parfevalOnAll() to initialize the variable on all of the workers.

1 Kommentar

If you have especially large shared data, you could also use the File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/28572-sharedmatrix to use shared memory. This will only work if the compute nodes are on the same host, though

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Parallel Computing Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by