Skip to content

Multi-threaded MultipleShooting and TrapezoidalCollocation #257

@franckgaga

Description

@franckgaga

@baggepinnen

I want to add conditional multi-threading for MultipleShooting and TrapezoidalCollocation, similar to DyadControl. I managed to get multi-threaded computations for the nonlinear equality constraints (the state defects over the prediction horizon) by using:

Threads.@threads for j=1:Hp
    # I fill the the equality constraint vectors `geq` here.
    # I never use Threads.threadid() and there is seperate
    # memory space for each iteration, so race conditions
    # are theoretically impossible.
end

Now what's the best way to do condtionnal multi-threading without code duplication ? The copilot suggested:

if use_threads
    Threads.@threads for j in 1:Hp
        # loop body
    end
else
    for j in 1:Hp
        # loop body
    end
end

but the loop body will be duplicated. I also tried to create this macro:

macro maybe_threads(use_threads, loop)
    if use_threads
        return esc(:(Threads.@threads $loop))
    else
        return esc(loop)
    end
end

but the only way to use it is with boolean literals, that is @maybe_threads true for j=1:Hp or @maybe_threads false for j=1:Hp. It does not work with variable nor parametric struct with "value-as-parameter".

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions