MATLAB: How to run several tasks in a job in parallel

parallel computingParallel Computing Toolbox

I have a problem about functions createJob and createTask. The problem is, I created 3 tasks in a job, I expected that they can be run in parallel. But actually they can't.
I have a function jobTest2.m, which returns labindex of worker.
code of jobTest2.m:
function [a] = jobTest2
% fprintf('labindex: %d\n',labindex);
a = labindex;
pause(20);
end
code run in matlab command window:
parallel.defaultClusterProfile('local');
c = parcluster();
j = createJob(c)
createTask(j, @jobTest2, 1, {});
createTask(j, @jobTest2, 1, {});
createTask(j, @jobTest2, 1, {});
submit(j);
wait(j);
results = fetchOutputs(j)
output:
results =
[1]
[1]
[1]
It takes 3 times longer than just running a task. And the labindex shows that it runs in serial. I've read a matlab answers Parallel programming with createJob and followed its proposal. But the problem is still not solved.
Before I run these code, I've delete all jobs on the cluster.
>> delete(c.Jobs)
>> c
c =
Local Cluster
Properties:
Profile: local
Modified: false
Host: localhost
NumWorkers: 4
JobStorageLocation: /home/drinkcor/.matlab/local_cluster_jobs/R2015b
RequiresMathWorksHostedLicensing: false
Associated Jobs:
Number Pending: 0
Number Queued: 0
Number Running: 0
Number Finished: 0
Can anyone help me? Thanks in advance!

Best Answer

I've found the solution. As @Edric Ellis reply in matlab ask Why is createJob / createTask so much slower than parfor? ,
"The thing to remember about your timings is that when using createJob and createTask with the local cluster type, each task runs in a separate MATLAB process. This takes much more time to launch than a parfor loop.
I suggest looking into parfeval, which has a similar interface to createTask, but uses the workers in a parallel pool, and so is much more efficient."
So, the solution is,
function parfevalTest
p = gcp();
tstart = tic;
f1 = parfeval(p,@jobTest2,1);
f2 = parfeval(p,@jobTest2,1);
value1 = fetchOutputs(f1);
value2 = fetchOutputs(f2);
aaaatime = toc(tstart);
fprintf('Time Consuming: %f\n',aaaatime);
fprintf('labIndex of job1: %f\n',value1);
fprintf('labIndex of job2: %f\n',value2);
end
The result is,
>> parfevalTest
Time Consuming: 20.063211
labIndex of job1: 1.000000
labIndex of job2: 1.000000
Although their labindex are the same, they do run in parallel according to the time consuming. The value of labindex is always 1 both in parfor and parfeval.