matlabparallel-processingpoolspmd

Matlab: Shut down parallel pool - only working for parfeval() but not spmd


I would like to shut down my parallel pool by a button press in a Matlab GUI to stop the execution of functions running on these pool workers.

Unfortunately this only works when starting the functions with "parfeval()". In this case, as soon as I press the button, my parallel pool is shutting down and therefore the functions called with parfeval() stop running.

As I prefer using "spmd" over "parfeval" to establish communication between the workers, I tried the same but it failed.

Nothing is happening on a button press and the parallel pool is only shutting down as soon as I cancel the whole script with ctrl+c.

Hope someone can assist me with this problem.

Working:

function StartButtonPushed2(app,event)
    pool = gcp();
    parfeval(pool, @dosomething, 0, app);
end

Not working:

function StartButtonPushed1(app,event)
    pool = gcp();
    spmd 
        dosomething(app);
    end
end

function StopButtonPushed(app,event)
    delete(gcp); %shutdown pool
end

Goal:

My final goal is, to start different functions in parallel by using a start button.

These functions are supposed to collect data from SPS, OPC UA Server and different Sensors and continuously write the collected data to a SQL Database.

To write the data to the DB I thought about having another parallel function - let's call it "writeDB" - which is receiving chunks of data from the data-collecting functions and upload it.

The stop button should end the data collection by interrupting all functions - currently I am doing this by deleting the parpool.

Thanks in advance!


Solution

  • For your use-case, asynchronous execution of the parallel tasks is critical.

    The asynchronous evaluation of fcn does not block MATLAB (from doc parfeval)

    When using parfeval your primary MATLAB instance is not blocked, allowing the GUI to execute code. Synchronous interfaces like spmd or parfor are not suitable for your situation. While the workers are busy, your primary instance is blocked and unable to execute any code.

    Related (same question asking for parfor): https://mathworks.com/matlabcentral/answers/401838-how-to-halt-parfor-execution-from-a-ui