1
votes

Suppose that I have this code in MATLAB:

% Predefined data
SX = [1, 2, 3, 4];
    parfor xx = 1:4
        naming2 = SX(xx);
        [BestM, BestX{xx},  fina_M{xx}, final_D{xx}, BestAA{xx}, final_Data{xx}] = Optmz(naming2,  v_data); 
    end

Optmz is an optimization algorithm. This optimization algorithm should run to optimize a regression model (with different output and optimized inputs-feature selection). As you know heuristic optimization algorithms are based on random numbers. We have different initial random numbers in every parlor loop? Is this a proper structure to decrease time of my application? I'm currently using for loop in above structure.

This is a part of printed output. Iterations are not sorted. Any problem? Based on above code we should have four iterations with same number. I need total different calculations in all 4 workers with different initial random number generator. For example, like the way we run our calculations in sequence without parallel computing. Run first one, second one, third one and finally fourth one.

******Iteration 24,   Best Cost = 0.041201******
******Iteration 26,   Best Cost = 0.034994******
******Iteration 26,   Best Cost = 0.036624******
******Iteration 26,   Best Cost = 0.039317******
******Iteration 25,   Best Cost = 0.039584******
******Iteration 27,   Best Cost = 0.034994******
******Iteration 27,   Best Cost = 0.036624******
******Iteration 27,   Best Cost = 0.039317******
******Iteration 28,   Best Cost = 0.034994******
******Iteration 26,   Best Cost = 0.039242******
******Iteration 28,   Best Cost = 0.036624******
******Iteration 28,   Best Cost = 0.03931******
******Iteration 29,   Best Cost = 0.034994******
******Iteration 29,   Best Cost = 0.036624******
******Iteration 27,   Best Cost = 0.039048******
******Iteration 29,   Best Cost = 0.03931******
******Iteration 30,   Best Cost = 0.034994******
******Iteration 30,   Best Cost = 0.036624******
******Iteration 28,   Best Cost = 0.039048******
1
@MZimmerman6 We have different initial random numbers in every parlor loop? Is this a proper structure to decrease time of my application? Iterations are not sorted. Any problem? - Eghbal
In parfor loops, the accessed indices are never guaranteed to be in order. Guaranteeing order and parallelization is usually an oxymoron. - rayryeng
If you are asking if it is okay for the iterations to be out of order, then, as @rayryeng said, it is almost impossible to guarantee any sort of order. Because of this, your code has to be designed to not care if things are not done in a specific order. - MZimmerman6
thank you for both comments. The second question is about random numbers in my optimization algorithm (here I'm using genetic algorithm). We need have same random number generator with same initial condition in every worker? I'm predicting 4 different outputs in every worker. Suppose that it is high temperature, low temperature, median temperature and mean temperature of a system. - Eghbal
If you need the same stream of random numbers, just generate an array of random numbers before you go into the loop, and give each loop the same array. This will negate any necessity to make sure your random generation is synchronized. Also this doesn't make much sense. Why would you want the same set of random numbers anyway. Them being the same set defeats the whole "random" nature of it. - MZimmerman6

1 Answers

1
votes

Maybe this is what you are asking, maybe not, but if you want different seeds for each loop you can seed the random number generator with a time stamp or something like that. This will make sure that each loop has a different seed and therefor a different set of random numbers

nLoops = 10
parfor ii = 1:nLoops
     timeVals = strsplit(sprintf('%.9f',now),'.')
     rng(str2double(timeVals{2}))
     % do some stuff
end

Or you can use MATLAB's built in shuffle function for rng

nLoops = 10
parfor ii = 1:nLoops
     rng('shuffle')
     % do some stuff
end

If you don't want it to be random in every loops, just create an array before you go into the loops, of whatever size you need, and have each loop access this same information. It is highly recommended that none of the loops edit anything about this array though

nLoops = 10;
randNums = rand(1,100)
parfor ii = 1:nLoops
     %do something with randNums(someNum)
end

Either option is relatively easy. If you are doing an optimization problem, you likely want to ensure that your random numbers are different though, that is kind of the point of optimization.