Hi, first I write an introduction to explain what I do. If you are not interested, jump to the question (in bold).
I am running a code in parallel that uses GA (but I guess with a simple parfor would be the same). Inside the loop, the code has to solve several times a problem with certain input variables. Since the input variables might happen to be the same as a previous run and the solution is computationally expensive, I build a lookup table along the execution. The code assigns a unique numeric identifier to each combination of input variables for which it has to solve the problem (I have about 1e40 combinations, so the identifier is made by several doubles), then it checks in the lookup table if the problem has already been solved for that combination input variables. If yes, it just retrieves the solution from the lookup table, otherwise it solves the problem and adds the new numeric identifier and the associated solution at the end of the lookup table. To make sure that GA don't mess up the table, the table is stored in the main stack, and retrieved by the fitness function every time. This works smoothly as long as I don't run in parallel. When I run in parallel, I have this issue: what happens if two CPUs of the pool try to update the lookup table in the stack simultaneously?
Can I have a CPU of the pool "lock" a variable in the main stack, update it, and then unlock it, to make sure that there are no conflicts with other CPUs that might try to update it simultaneously?
PS: don't say "preallocate the space for all your combinations": I can't, I have 1e40 combinations, I will actually use only some thousands of them, but I cannot know which ones in advance.