How to save and load data in parallel while optimizing?
8 次查看(过去 30 天)
显示 更早的评论
Hello every one,
I am optimizing 30 equations with Genetic Algorithm and need to save data in each time step and load it in the next time step. This makes my optimization too long, which is not efficient. I ran it in parallel, but it messed up the data. Would you help me with solving this issue if you have any similar experiences?
here is the part of the code that shows how I save data and reload them:
Data = [t ; y(15) ; y(22) ; y(29) ; y(30)];
if t ==0
fileID = fopen('Data.txt','w'); fprintf(fileID, '%f %f %f %f %f \n', Data); fclose(fileID);
else
fileID = fopen('Data.txt','a'); fprintf(fileID, '%f %f %f %f %f \n', Data); fclose(fileID);
end
if t >Tau_D
Data = load ('Data.txt','Data');
for i = 1 : size (Data,1)-1
if t - Tau_D < Data(i+1,1) && t - Tau_D >= Data(i,1)
CD = Data(i,2);
end
end
end
if t > Tau_t
for i = 1 : size (Data,1)-1
if t - Tau_t < Data(i+1,1) && t - Tau_t >= Data(i,1)
CD8T = Data(i,3);
IgM = Data(i,4);
IgG = Data(i,5);
end
end
end
6 个评论
Mohammad Sami
2020-7-8
I would suggest you need to centralise the reading and writing of the data to the file.
Multiple threads trying to read and write from the same file will cause errors.
Use the function parfeval to call the processing in the parallel and pass in all the data the function needs to work. Ideally your function will not read nor write to the file.
The functions will then compute the values on parallel processes. You can then fetch the results on the main thread and update your files.
More information is available in the documentation on how to use parfeval.
回答(1 个)
Rohith Nomula
2020-7-7
Use parfor - parallel for loop in MATLAB
Then use a cell array to store all the values assigned in the looping state
% declare variables outside parallel for loop
parfor i=1:fileLength
% your condition
CD8T{i} = Data(i,3);
IgM{i} = Data(i,4);
IgG{i} = Data(i,5);
end
This works well for optimization
You can use it in both for loops
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!