Info

This question is closed. Reopen it to edit or answer.

Is there a way to index a task from within the task?

2 views (last 30 days)
Steven
Steven on 18 Dec 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi everyone,
I am working on using the local scheduler to run simulation code that I have written. Each job will consist of about 25 tasks and each task consists of a single call to my simulation code. If the simulation were run entirely in Matlab, I would have no problem. However, the code calls an external program, which does some computation and generates some files, then my code runs some analysis. To deal with this, I've created four directories (\lab1, \lab2, \lab3, and \lab4) corresponding to the cluster size (quad-core CPU). In each folder is a copy of the executable that I need. In principle, then, I'd like Matlab to run four simultaneous tasks (or simulations), one in each folder. In order to do this, though, I need a way for the task to know which core or worker it's running on. I had assumed that labindex would be all that I needed to accomplish this, but it doesn't work as I had expected. For instance, I would have expected the following code to return 1, 2, 3, and 4 because the scheduler runs the four tasks simultaneously, one on each core. Instead, it returns 1, 1, 1, 1.
sched = findResource('scheduler','type','local');
job1 = createJob(sched);
for i = 1:4
createTask(job1, @() labindex, 1, {});
end
submit(job1)
waitForState(job1)
results = getAllOutputArguments(job1);
I've also tried variations based on the documentation such as
sched = findResource('scheduler','type','local');
job1 = createJob(sched);
for i = 1:4
createTask(job1, @() get(getCurrentWorker,'Name'), 1, {});
end
submit(job1)
waitForState(job1)
results = getAllOutputArguments(job1);
without any luck. (The previous code returns four empty vectors.)
My question: Is there a way to know within a task which worker it's running on, similar to the function labindex? In other words, I would like to execute within a directory ['lab', num2str(index)]. How can I figure out what 'index' is?
As a side note, my simulations run as I want them to if I run 4 tasks and manually assign the tasks the indices 1, 2, 3, and 4. Each simulation runs in one of the four directories. However, this work-around only works for up to 4 simulations. If I run 5 tasks and assign the indices 1, 2, 3, 4, 1 then the code would fail if, for instance, task 4 ended before task 1. This is because when task 4 finished in the directory \lab4, then task 5 would try to run in \lab1, but that directory is still.
Thanks in advance for any help!
Cheers, Steve

Answers (1)

Jessica M
Jessica M on 6 Sep 2013
I used t=getCurrentTask; id=num2str(t.ID) to do this before, but it stopped working for me in 2012b.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!