/usr/share/dynare/matlab/parallel/distributeJobs.m is in dynare-common 4.4.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | function [nCPU, totCPU, nBlockPerCPU, totSLAVES] = distributeJobs(Parallel, fBlock, nBlock)
% PARALLEL CONTEXT
% In parallel context this function is used to determine the total number of available CPUs,
% and the number of threads to run on each CPU.
%
% INPUTS
% o Parallel [struct vector] copy of options_.parallel
% o fBlock [int] index number of the first job (e.g. MC iteration or MH block)
% (between 1 and nBlock)
% o nBlock [int] index number of the last job.
%
% OUTPUT
% o nBlockPerCPU [int vector] for each CPU used, indicates the number of
% threads run on that CPU
% o totCPU [int] total number of CPU used (can be lower than
% the number of CPU declared in "Parallel", if
% the number of required threads is lower!)
% o nCPU the number of CPU in user format.
% o totSLAVES the number of cluster's node currently
% involved in parallel computing step.
% It is a number between 1 and length(Parallel).
% Copyright (C) 2010-2011 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% The Parallel vector has already been sorted
% (in accord with the CPUWeight values) in DESCENDING order in
% InitializeComputationalEnvironment!
totCPU=0;
lP=length(Parallel);
CPUWeight=ones(1,length(Parallel))*(-1);
for j=1:lP,
nCPU(j)=length(Parallel(j).CPUnbr);
totCPU=totCPU+nCPU(j);
CPUWeight(j)=str2num(Parallel(j).NodeWeight);
end
% Copy of original nCPU.
nCPUoriginal=nCPU;
nCPU=cumsum(nCPU);
% Number of Nodes in Cluster.
nC=lP;
% Numbers of Jobs.
NumbersOfJobs=nBlock-fBlock+1;
SumOfJobs=0;
JobsForNode=zeros(1,nC);
for j=1:lP,
CPUWeight(j)=str2num(Parallel(j).NodeWeight)*nCPUoriginal(j);
end
CPUWeight=CPUWeight./sum(CPUWeight);
% Redistributing the jobs among the cluster nodes according to the
% CPUWeight.
for i=1:nC
JobsForNode(i)=CPUWeight(i)*NumbersOfJobs;
% Many choices are possible:
% JobsForNode(i)=round(JobsForNode(i));
% JobsForNode(i)=floor(JobsForNode(i));
JobsForNode(i)=ceil(JobsForNode(i));
end
% Check if there are more (fewer) jobs.
% This can happen when we use ceil, round, ... functions.
SumOfJobs=sum(JobsForNode);
if SumOfJobs~=NumbersOfJobs
if SumOfJobs>NumbersOfJobs
% Many choices are possible:
% - Remove the excess works at the node that has the greatest
% number of jobs.
% - Remove the excess works at the node slower.
VerySlow=nC;
while SumOfJobs>NumbersOfJobs
if JobsForNode(VerySlow)==0
VerySlow=VerySlow-1;
continue
end
JobsForNode(VerySlow)=JobsForNode(VerySlow)-1;
SumOfJobs=SumOfJobs-1;
end
end
if SumOfJobs<NumbersOfJobs
% Many choices are possible:
% - ... (see above).
[NonServe VeryFast]= min(CPUWeight);
while SumOfJobs<NumbersOfJobs
JobsForNode(VeryFast)=JobsForNode(VeryFast)+1;
SumOfJobs=SumOfJobs+1;
end
end
end
% Redistributing the jobs among the cpu/core nodes.
JobsForCpu=zeros(1,nCPU(nC));
JobAssignedCpu=0;
RelativePosition=1;
for i=1:nC
% Many choices are possible:
% - ... (see above).
JobAssignedCpu=max(1,floor(JobsForNode(i)/nCPUoriginal(i)));
ChekOverFlow=0;
for j=RelativePosition:nCPU(i)
JobsForCpu(j)=JobAssignedCpu;
ChekOverFlow=ChekOverFlow+JobAssignedCpu;
if ChekOverFlow>=JobsForNode(i)
break;
end
end
% Check if there are more (fewer) jobs.
% This can happen when we use ceil, round, ... functions.
if ChekOverFlow ~=(JobsForNode(i))
if ChekOverFlow >(JobsForNode(i))
while ChekOverFlow>JobsForNode(i)
JobsForCpu(nCPU(i))=JobsForCpu(nCPU(i))-1;
ChekOverFlow=ChekOverFlow-1;
end
end
if ChekOverFlow <(JobsForNode(i))
while ChekOverFlow<JobsForNode(i)
JobsForCpu(nCPU(i))=JobsForCpu(nCPU(i))+1;
ChekOverFlow=ChekOverFlow+1;
end
end
end
RelativePosition=nCPU(i)+1;
end
% Reshape the variables totCPU,totSLAVES and nBlockPerCPU in accord with
% the syntax rquired by a previous version of parallel package ...
totCPU=0;
totSLAVES=0;
nBlockPerCPU=[];
for i=1:nCPU(nC)
if JobsForCpu(i)~=0
totCPU=totCPU+1;
end
end
for i=1:nC
if JobsForNode(i)~=0;
totSLAVES=totSLAVES+1;
end
end
RelativeCounter=1;
for i=1:nCPU(nC)
if JobsForCpu(i)~=0
nBlockPerCPU(RelativeCounter)=JobsForCpu(i);
RelativeCounter=RelativeCounter+1;
end
end
|