function MultiTaskTest(nCore)
cluster = parcluster();
job = createJob(cluster);
for i=1:nCore
job.createTask(@LargeTask, 1,{1000000,500},'CaptureDiary',true);
end
job.submit();
DispProg(job);
job.wait();
results = job.fetchOutputs();
end
function res=LargeTask(n,m)
t1=cputime;
for i=1:200
srcArr=zeros(n,2);
% create a value array
srcArr(:,1)=rand(n,1);
% create an indexarray
srcArr(:,2)= randi(m,n,1);
% accumulate the values by class indices.
res=accumarray(srcArr(:,2),srcArr(:,1));
end
t2=cputime;
disp([num2str(t2-t1),'s'])
end
function DispProg(job)
tasks=job.Tasks;
nTasks=length(tasks);
indices=zeros(nTasks,1);
while ~strcmp(job.State, 'finished')
bEmpty=true;
for i=1:nTasks
if ~isempty(tasks(i).Diary)
bEmpty=false;
end
end
if bEmpty
continue;
end
for i=1:nTasks
text=tasks(i).Diary;
if ~(indices(i)==length(text))
disp(text(indices(i)+1:end));
indices(i)=length(text);
end
end
end
end
I pasted the sample code above so that anyone can test on his/her own computer. The only parameter of the entrance function is the number of cores you decide to use.
My testing screenshot is, >> MultiTaskTest(1) 10.9513s >> MultiTaskTest(2) 12.3085s 12.3709s >> MultiTaskTest(3) 13.3225s
13.4941s
13.4161s
>> MultiTaskTest(4) 15.1945s
15.3349s
15.3037s
15.3193s
>> MultiTaskTest(5) 16.5985s
16.8169s
16.9261s
16.7545s
16.8169s
>> MultiTaskTest(6) 18.6733s
18.6265s
18.7981s
18.8137s
18.7825s
18.8137s
>> MultiTaskTest(7) 19.0477s
18.7513s
18.9541s
18.9697s
19.1257s
18.2209s
16.7077s
The time consumed by the same single task obviously increase by the number of cores I used.