Porting callback code
In a vanilla CSL program, the code that follows an asynchronous operation is in another task, which the operation activates, or in another state of a state machine. This guide turns such code into an async task. Step 1 of the tutorial shows the conversion for a chain of three tasks, and the conjugate gradient example shows the result for a state machine.
Conversion in steps
Section titled “Conversion in steps”-
Find the sequence. Collect the tasks that continue each other through
.activateor.unblock, or the states of a state machine and the order in which its state variable selects them. Together they form one sequence of steps. -
Write the sequence as one async task. Declare an
async taskthat contains the steps in order. After each asynchronous operation that used to activate the next task, activate the current task instead and suspend:@fadds(x_dsd, x_dsd, in_dsd, .{ .async = true, .activate = @current_task() });suspend;The code of the next task follows the
suspend. Bind the async task with@bind_local_taskand remove the tasks that it replaces. -
Replace the state variable with control flow. Transitions that set the state back to an earlier step become loops, and transitions that depend on a condition become
ifstatements. -
Move values into local variables. A global that only carries a value from one step to the next can be a local variable of the async task. Local variables keep their values and their addresses across
suspend. -
Turn helper functions into async functions. A function that starts an operation and takes the task to continue with as a parameter becomes an
async fnthat activates@current_task()and suspends. Its callers call it with@await. -
Initialize the runtime. Call
@coro_runtime_init()once before the first async task is activated, usually in the function that the host launches. -
Claim the resources. Replace fixed colors, task IDs and queues with
@claim_color(),@claim_local_task(),@claim_input_queue()and so on; see Claims.
Libraries that activate a task
Section titled “Libraries that activate a task”A library function that takes a task to activate on completion can be called from async code without a change to the library. Pass @current_task() and suspend:
callback_lib.start_transfer(&buffer, @current_task());suspend;Events that call a function
Section titled “Events that call a function”Some events do not activate a task but call a function, for example a handler that the hardware calls when a control wavelet arrives. An async function can wait for such a call as well. The handler activates the task that waits. The allreduce library of csl++ waits in this way for the teardown wavelet that ends each of its phases:
var td_waiter_task: local_task_id;var td_received: bool = false;var td_awaiting: bool = false;
fn teardown_allreduce() void { td_received = true; if (td_awaiting) { @activate(td_waiter_task); }}An async function of the library resets the two flags, starts its DSD operations, and then waits for the handler:
if (!td_received) { td_waiter_task = @current_task(); td_awaiting = true; suspend; td_awaiting = false;}The function stores @current_task() for the handler and suspends. It checks td_received first, because the event can occur before the function gets to this point, and then there is nothing to wait for. The handler activates the task only if the function already waits. An earlier activation would resume the task at another suspend, where it waits for a DSD operation.
Pitfalls
Section titled “Pitfalls”- A suspended task resumes on any activation. If other code activates the same task ID, for example a library that was given the ID for another purpose, the task resumes too early. Give such code a task ID of its own.
- An operation that ends with
.unblockinstead of.activatedoes not resume a suspended task. Use.activate = @current_task(). sys_mod.unblock_cmd_stream()returns control to the host. Call it after the last step of the async task, as the last task of the chain did before.- Two operations that run at the same time must not share a queue or a microthread, as in vanilla CSL. With
@await_parallel, this applies to the operations of all its calls.