Hello!
I have a following problem: I have two nested for loops that go through some 2D matrices that contain data structutes with some data. It is possible in principle to replace the "inner" for loop with parfor for the efficacy: the processing of the elements of matrix within the "inner" loop is independent from each other. However, the processing routine includes some stochasticity: drafting from the random distributions, random permutations of the vectors, and so on. All these processes are happening within the external self-made functions that are called from within the "inner" loop. I want to make the whole thing reproducible, so I want to control the rng seeds. To handle this, I pre-generate the seeds before entering the loop. The code is quite bulky and includes a small tree of downstream functions, I'll try to make a pseudocodish extract of it and illustrate the problem.
Seeds=randi(1000000,O_N,I_N);
Results_slice=Results(O,:);
Running_results(I)=external_function(Results_slice(I),local_seeds(I));
Results(O+1,1:I_N)=Running_results;
So, basically, every iteration of the "inner" loop, the results of the previous iteration are used as an input for the external_function(), together with the pregenerated seed. The rng(local_seed) line is pretty much the first one within the external_function().
After some code arrangement, it all boiled down to the code in which I can comment/incomment a single line: it is either parfor I=1:I_N or for I=1:I_N launching the "inner" loop.
And now the problem: when I run it with parfor I=1:I_N, I get some result. This result is perfectly reproducible: all the seed values as well as the output of the external_function and, therefore the final Results table are identical on the repetitive runs of the script with the parfor loop. The same is true for the version with the for loop. But the results with for loop and parfor loop are different from each other despite same functions receive the same seeds as the input.
What is worse: to my undersatnding, if I have the Results table and all the seeds, in theory I should be able to rerun the external_function for any place in the middle of the tables just by providing the indices of the input. But when I do it, the result is not the same as during the parfor loop. To me it looks like the rngs of the independent processes within the parfor interact somehow, although from the manuals I had an impresion that it should not happen.
Could you please help me with figuring out what is going on and what am I doing wrong. Again, my idea (and I have high hopes for it) is that if I set the rngs correctly, than it should not matter from where I call the external_function: from within the for loop, within the parfor loop, or as a standalone command. But so far looks like this is not the case. Can I control it somehow?
Thank you