Main Content

canUseParallelPool

Verify that parallel functions can use a parallel pool

Since R2020b

    Description

    example

    tf = canUseParallelPool() returns a logical value indicating if parallel functions can create and use a parallel pool.

    The function returns logical 1 (true) if Parallel Computing Toolbox™ is installed and licensed for use, a default parallel pool is configured and supported, and automatic creation of parallel pools is enabled. Otherwise, the function returns logical 0 (false). This function does not create a parallel pool.

    Use canUseParallelPool to check if parallel functions such as parfor (Parallel Computing Toolbox) and parfeval (Parallel Computing Toolbox) can use a parallel pool. This enables you to avoid executing code that requires a parallel pool if one is not available.

    Even if this function returns true, you can still receive an error when you try to create a parallel pool if your parallel cluster is not properly configured or cannot be contacted.

    Examples

    collapse all

    Verify that you can use a parallel pool before executing code that runs in parallel. If a pool is not available, run the code in serial.

    N=100;
    
    if canUseParallelPool
        f(1:N) = parallel.FevalFuture;
        for i = 1:N
            f(i) = parfeval(@rank,1,magic(i));
        end
        results = fetchOutputs(f);
    else
        results = zeros(1,N);
        for i = 1:N
            results(i) = rank(magic(i));
        end
    end

    If canUseParallelPool returns true, then parfeval creates a parallel pool and the code runs in parallel. Otherwise, the code runs in a normal for-loop.

    Version History

    Introduced in R2020b

    See Also

    | (Parallel Computing Toolbox)

    Topics