Continue to next iteration in for-loop while saving data
4 views (last 30 days)
I have a for-loop and in each iteration it produces about 1Gb data. Since it takes some time to save this data to hard drive, I want to use a different worker to save the data and let the for-loop continue to the next iteration at the same time on the original worker. Is there any way to do this? Thank you.
Edit: The calculations in each iteration before saving data takes longer time than saving to disk, so the current worker won't reach the save command in the next iteration before the other worker finishes save for the current iteration.
Thomas Falch on 1 Oct 2021
You can use parfeval to run save in the background on a worker like this:
pool = parpool('local', 1) % Start pool with one worker
for i = 1:100
data = generateData()
parfeval(@() save(data), 0); % Sends data to worker to be saved in the background