Why is MATLAB job taking a long time running?

Girardi

I have a function (a convolution) which can get very slow if it operates on matrices of many many columns (function code below). I hence want to parallelize the code.

Example MATLAB code:

x = zeros(1,100);
x(rand(1,100)>0.8) = 1;
x = x(:);
c = convContinuous(1:100,x,@(t,p)p(1)*exp(-(t-p(2)).*(t-p(2))./(2*p(3).*p(3))),[1,0,3],false)
plot(1:100,x,1:100,c)

if x is a matrix of many columns, the code gets very slow... My first attempt was to change for to parfor statement, but it went wrong (see Concluding remarks below).

My second attempt was to follow this example, which shows how to schedule tasks in a job and then submit the job to a local server. That example is implemented in my function below by letting the last argument isParallel being true.

The example MATLAB code would be:

x = zeros(1,100);
x(rand(1,100)>0.8) = 1;
x = x(:);
c = convContinuous(1:100,x,@(t,p)p(1)*exp(-(t-p(2)).*(t-p(2))./(2*p(3).*p(3))),[1,0,3],true)

Now, MATLAB tells me:

Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.

Warning: This job will remain queued until the Parallel Pool is closed.

And MATLAB terminal keeps on hold, waiting for something to finish. I then open Jobs Monitor by Home -> Parallel -> Monitor jobs and see there are two jobs, one of which has the state running. But none of them will ever finish.

Questions

  • Why is it taking too long to run, given it is a really simple task?

  • What would be the best way to parallelize my function below? (the "heavy" part is in the separated function convolveSeries)

File convContinuous.m

function res = convContinuous(tData, sData, smoothFun, par, isParallel)
% performs the convolution of a series of delta with a smooth function of parameters par
% tData = temporal space
% sData = matrix of delta series (each column is a different series that will be convolved with smoothFunc)
% smoothFun = function used to convolve with each column of sData
%             must be of the form smoothFun(t, par)
% par = parameters to smoothing function
    if nargin < 5 || isempty(isParallel)
        isParallel = false;
    end
    if isvector(sData)
        [mm,nn] = size(sData);
        sData = sData(:);
    end
    res = zeros(size(sData));
    [ ~, n ] = size(sData);
    if ~isParallel
        %parfor i = 1:n % uncomment this and comment line below for strange error
        for i = 1:n
            res(:,i) = convolveSeries(tData, sData(:,i), smoothFun, par);
        end
    else
        myPool = gcp; % creates parallel pool if needed
        sched = parcluster; % creates scheduler
        job = createJob(sched);
        task = cell(1,n);
        for i = 1:n
            task{i} = createTask(job, @convolveSeries, 1, {tData, sData(:,i), smoothFun, par});
        end
        submit(job);
        wait(job);
        jobRes = fetchOutputs(job);
        for i = 1:n
            res(:,i) = jobRes{i,1}(:);
        end
        delete(job);
    end
    if isvector(sData)
        res = reshape(res, mm, nn);
    end
end

function r = convolveSeries(tData, s, smoothFun, par)
    r = zeros(size(s));
    tSpk = s == 1;
    j = 1;
    for t = tData
        for tt = tData(tSpk)
            if (tt > t)
                break;
            end
            r(j) = r(j) + smoothFun(t - tt, par);
        end
        j = j + 1;
    end
end

Concluding remarks

As a side note, I was not able to do it using parfor because MATLAB R2015a gave me a strange error:

Error using matlabpool (line 27) matlabpool has been removed.

To query the size of an already started parallel pool, query the 'NumWorkers' property of the pool.

To check if a pool is already started use 'isempty(gcp('nocreate'))'.

Error in parallel_function (line 317) Nworkers = matlabpool('size');

Error in convContinuous (line 18) parfor i = 1:n

My version command outputs

Parallel Computing Toolbox                            Version 6.6        (R2015a)

which is compatible with my MATLAB version. Almost all other tests I have done are OK. I am then compelled to think that this is a MATLAB bug.

I tried changing matlabpool to gcp and then retrieving the number of workers by parPoolObj.NumWorkers, and after altering this detail in two different built-in functions, I received another error:

Error in convContinuous>makeF%1/F% (line 1)

function res = convContinuous(tData, sData, smoothFun, par)

Output argument "res" (and maybe others) not assigned during call to "convContinuous>makeF%1/F%".

Error in parallel_function>iParFun (line 383) output.data = processInfo.fun(input.base, input.limit, input.data);

Error in parProcess (line 167) data = processFunc(processInfo, data);

Error in parallel_function (line 358) stateInfo = parProcess(@iParFun, @iConsume, @iSupply, ...

Error in convContinuous (line 14) parfor i = 1:numel(sData(1,:))

I suspect that this last error is generated because the function call inside parfor loop requires many arguments, but I don't really know it.

Solving the errors

Thanks to wary comments of people here (saying they could not reproduce my errors), I went on looking for the source of the error. I realized it was a local error due to having pforfun in my pathdef.m which I downloaded long ago from File Exchange.

Once I removed pforfun from my pathdef.m, parfor (line 18 in convContinuous function) started working well.

Thank you in advance!

Thomas Ibbotson

The parallel pool you created is blocking your job from running. When you are using the jobs and tasks API you do not need (and must not have) a pool open. When you looked in Job Monitor, the running job you saw was the job that backs the parallel pool, that only finishes when the pool is deleted.

If you delete the line in convContinuous that says myPool = gcp, then it should work. As an optimization you can use the vectorised form of createTask, which is much more efficient than creating tasks in a loop i.e.

inputCell = cell(1, n);
for i = 1:n
    inputCell{i} = {tData, sData(:,i), smoothFun, par};
end
task = createTask(job, @convolveSeries, 1, inputCell);

However, having said all that, you should be able to make this code work using parfor. The first error you encountered was due to matlabpool being removed, it has now been replaced by parpool.

The second error appears to be caused by your function not returning the correct outputs, but the error message does not appear to correspond to the code you posted, so I'm not sure. Specifically I don't know what convContinuous>makeF%1/F% (line 1) refers to.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Ubuntu taking way too long time to start

분류에서Dev

Graphx EdgeRDD count taking long time to compute

분류에서Dev

Timeout transaction when processing long running job in javaee

분류에서Dev

Run multiple cron jobs where one job takes a long time

분류에서Dev

Interrupting an unknown long-running function after a certain time period

분류에서Dev

All java threads are running on a single core, ultimately taking too much time to execute

분류에서Dev

dosfsck of pendrive taking too long

분류에서Dev

Why would a solid wallpaper cause a Windows PC a long time to login?

분류에서Dev

Very slow boot time ubuntu - A start job is running for Raise network interfaces

분류에서Dev

Monitor Process and echo if taking too long

분류에서Dev

Identify if a website is taking too long to respond

분류에서Dev

How do I find out long time a service has been up and running for in CentOS 6?

분류에서Dev

Taking action after timeout during the command running

분류에서Dev

Running cron job in specific directory

분류에서Dev

launchd job not on list, but still running

분류에서Dev

An issue running fmincon in matlab

분류에서Dev

Taking time to load the image from URL to UIImageview

분류에서Dev

Long running query timing out

분류에서Dev

Optimizing long running WebAPI method

분류에서Dev

Oracle SQL query taking too long like 60 minutes to execute

분류에서Dev

Windows 10 Browser taking abnormally long to open external URLs

분류에서Dev

visual studio 2013 update 2 taking to long to configur

분류에서Dev

Ubuntu upgrade from 16.04 to 18.04 is taking too long

분류에서Dev

time part of a script (running time)

분류에서Dev

Cron job still running when deleted

분류에서Dev

Shut Down Quickly - A stop job is running

분류에서Dev

Is there a running time limitation?

분류에서Dev

Time on y axis - Matlab

분류에서Dev

Why my scheduled job is not working?

Related 관련 기사

  1. 1

    Ubuntu taking way too long time to start

  2. 2

    Graphx EdgeRDD count taking long time to compute

  3. 3

    Timeout transaction when processing long running job in javaee

  4. 4

    Run multiple cron jobs where one job takes a long time

  5. 5

    Interrupting an unknown long-running function after a certain time period

  6. 6

    All java threads are running on a single core, ultimately taking too much time to execute

  7. 7

    dosfsck of pendrive taking too long

  8. 8

    Why would a solid wallpaper cause a Windows PC a long time to login?

  9. 9

    Very slow boot time ubuntu - A start job is running for Raise network interfaces

  10. 10

    Monitor Process and echo if taking too long

  11. 11

    Identify if a website is taking too long to respond

  12. 12

    How do I find out long time a service has been up and running for in CentOS 6?

  13. 13

    Taking action after timeout during the command running

  14. 14

    Running cron job in specific directory

  15. 15

    launchd job not on list, but still running

  16. 16

    An issue running fmincon in matlab

  17. 17

    Taking time to load the image from URL to UIImageview

  18. 18

    Long running query timing out

  19. 19

    Optimizing long running WebAPI method

  20. 20

    Oracle SQL query taking too long like 60 minutes to execute

  21. 21

    Windows 10 Browser taking abnormally long to open external URLs

  22. 22

    visual studio 2013 update 2 taking to long to configur

  23. 23

    Ubuntu upgrade from 16.04 to 18.04 is taking too long

  24. 24

    time part of a script (running time)

  25. 25

    Cron job still running when deleted

  26. 26

    Shut Down Quickly - A stop job is running

  27. 27

    Is there a running time limitation?

  28. 28

    Time on y axis - Matlab

  29. 29

    Why my scheduled job is not working?

뜨겁다태그

보관