Parallel programming in MATLAB to simultaneously perform three different functions

I am writing a molecular dynamics code that requires the calculation of 3 different types of forces using 3 functions, namely Compute2BodyForce, Compute3BodyForce and ComputeOtherForce. Since the functions are independent of each other, and I want to calculate each function separately on 3 different kernels, the correct way to do this would be as follows:

funList = {@Compute2BodyForce,@Compute3BodyForce,@ComputeOtherForce};

dataList = {data1,data2,data3}; %# or pass file names 

parfor i=1:length(funList)

    %# call the function

    funList{i}(dataList{i});

end

Secondly, how to consolidate the results and summarize them together, namely, get TotalForce = 2BodyForce + 3BodyForce + OtherForce?

+3
source share
1 answer

. , , parfor ( , ). , matlab , . :

fileDep = {'Compute2BodyForce',...
'Compute3BodyForce',...
'ComputeOtherForce'};     

num_procs = 3;

matlabpool('open','Mycluster',num_procs,'FileDependencies',fileDep);

parfor iter = 1:3

% iter is passed to the functions so the functions can return NaNs when we don't want computation done
   body_2_dummy{iter} = Compute2BodyForce(data,iter); %assuming data is a variable here, maybe a struct that gets parsed inside the functions

   body_3_dummy{iter} = Compute3BodyForce(data,iter);

   other_dummy{iter} = ComputeOtherForce(data,iter);

end

% resolve and sum up here

total_force = body_2_dummy{1} + body_3_dummy{2} + other_dummy{3};

, body_2_dummy {1}, body_3_dummy {2} other_dummy {3} NaN. Matlab. - :

parfor iter = 1:3
   if iter == 1
      body_2_dummy = Compute2Body(data);
   end
% more ifs for the other forces
end

, body_2_dummy . Matlab , , body_2_dummy .

, . .

, , , . Matlab?

-

:

- - :

spam = @(x) x + 2;

spam(2)

   ans = 4

- . , , @spam.

Sbiotoolbox. , Matlab. , , . Matlab.

, , , Matlab, . Matlab, .

Matlab - , Matlab. , , , , . , , , Matlab.

"" Matlab. , , .

.

+2

All Articles