Origin of threads for compute-only Task graph


the new async support encourages non-blocking patterns enable compose elaborate task dependency graphs based on async/await , taskcompletionsource<t> only. what's best way launch graph proper number of tasks spun up?

is case calling 1 of nodes run whole thing on calling thread?

or, if instead parallel.invoke on the entire set of non-dependent nodes (where may more prolific others), tpl work-stealing take care of balancing? in compute-only graph work stealing opportunity occur?

hi glenn-

forgetting new async feature moment, let's using parallel.invoke run tree of processing in parallel, e.g.

void process(int depth)
{
    if (depth <= 0) return;
    parallel.invoke(
        () => process(depth-1),
        () => process(depth-1),
        () => dowork());
}

that's logical equivalent of:

void process(int depth)
{
    if (depth <= 0) return;
    task t1 = task.factory.startnew(() => process(depth-1));
    task t2 = task.factory.startnew(() => process(depth-1));
    task t3 = task.factory.startnew(() => dowork());
    task.waitall(t1, t2, t3);
}

now, using new async ctp support, rewrite follows:

task process(int depth)
{
    if (depth <= 0) return;
    task t1 = task.run(() => process(depth-1)); // taskex.runex in the async ctp
    task t2 = task.run(() => process(depth-1));
    task t3 = task.run(() => dowork());
    return task.whenall(t1, t2, t3); // taskex.whenall in async ctp
}

this identical previous snippet, except when invoke it, task represents of processing.

a key thing notice here i'm still kicking off tasks myself using task.run; that's what's introducing parallelism.  you'll notice in example did use new async or await keywords; that's because they're useful when doing sequential control flow, , you've explicitly asked introduce parallelism: since 'await' prevents forward progress of method until awaited task completes, achieve parallelism want kick off of relevant tasks before awaiting things.

we change problem , see async , await become handy in case this.  let's don't want know when of tasks done, want compute based on them, e.g. want sum values of node's result , descendent's results, , dowork method returns integer.  can accomplish async , await follows:

async task<int> process(int depth)
{
    if (depth <= 0) return 0;
    task<int> t1 = task.run(() => process(depth-1)); // taskex.runex in the async ctp
    task<int> t2 = task.run(() => process(depth-1));
    task<int> t3 = task.run(() => dowork());
    int [] results = await task.whenall(t1, t2, t3); // taskex.whenall in async ctp
    return results.sum();
}

i hope answers question.  if not, let me know.



Archived Forums V  >  Visual Studio Async CTP



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

Catalog Staging

How to Share webservice object to all user