You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/task-spawning.md
+148Lines changed: 148 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -318,3 +318,151 @@ In comparison, the `outer_low_occupancy` snippet should show results like this:
318
318
0.109904 seconds (55.03 k allocations: 3.631 MiB)
319
319
0.117239 seconds (87.95 k allocations: 5.372 MiB)
320
320
```
321
+
322
+
## Different ways to spawn tasks
323
+
324
+
Beyond the standard function call syntax `Dagger.@spawn f(args...)`, Dagger also supports several other convenient ways to spawn tasks, mirroring Julia's own syntax variations.
325
+
326
+
### Broadcast
327
+
328
+
Tasks can be spawned using Julia's broadcast syntax. This is useful for applying an operation element-wise to collections.
329
+
330
+
```julia
331
+
using Dagger
332
+
A =rand(4)
333
+
B =rand(4)
334
+
335
+
# Spawn a task to compute A .+ B
336
+
add_task = Dagger.@spawn A .+ B
337
+
@assertfetch(add_task) ≈ A .+ B
338
+
339
+
x =randn(100)
340
+
abs_task = Dagger.@spawnabs.(x)
341
+
@assertfetch(abs_task) ==abs.(x)
342
+
```
343
+
344
+
### Do block
345
+
346
+
Dagger supports spawning tasks using Julia's `do` block syntax, which is often used for functions that take another function as an argument, especially anonymous functions.
347
+
348
+
```julia
349
+
using Dagger
350
+
A =rand(4)
351
+
352
+
# Spawn a task using a do block with sum
353
+
sum_do_task = Dagger.@spawnsum(A) do a
354
+
a +1
355
+
end
356
+
@assertfetch(sum_do_task) ≈sum(a -> a +1, A)
357
+
358
+
# Spawn a task with a function that accepts a do block
359
+
do_f = f ->f(42)
360
+
do_task = Dagger.@spawndo_f() do x
361
+
x +1
362
+
end
363
+
@assertfetch(do_task) ==43
364
+
```
365
+
366
+
### Anonymous direct call
367
+
368
+
Tasks can be spawned directly from anonymous function definitions.
369
+
370
+
```julia
371
+
using Dagger
372
+
A =rand(4)
373
+
374
+
# Spawn a task from an anonymous function
375
+
anon_task = Dagger.@spawn A ->sum(A)
376
+
@assertfetch(anon_task) ==sum(A)
377
+
378
+
# Anonymous function with closed-over arguments
379
+
dims =1
380
+
anon_kwargs_task = Dagger.@spawn A ->sum(A; dims=dims)
Similarly, tasks can be spawned to modify elements of mutable collections (such as arrays). The object being modified must be running under Datadeps, or wrapped with `Dagger.@mutable`, to ensure that its contents can be mutated correctly.
414
+
415
+
```julia
416
+
using Dagger
417
+
A = Dagger.@mutablerand(4, 4)
418
+
419
+
# Spawn a task to set A[1, 2] = 3.0
420
+
setindex_task1 = Dagger.@spawn A[1, 2] =3.0
421
+
fetch(setindex_task1) # Wait for the setindex! to complete
422
+
@assertfetch(Dagger.@spawn A[1, 2]) ==3.0
423
+
424
+
# Spawn a task to set A[2] = 4.0 (linear indexing)
425
+
setindex_task2 = Dagger.@spawn A[2] =4.0
426
+
fetch(setindex_task2)
427
+
@assertfetch(Dagger.@spawn A[2]) ==4.0
428
+
429
+
R = Dagger.@mutableRef(42)
430
+
# Spawn a task to set R[] = 43
431
+
ref_setindex_task = Dagger.@spawn R[] =43
432
+
fetch(ref_setindex_task)
433
+
@assertfetch(Dagger.@spawn R[]) ==43
434
+
```
435
+
436
+
### NamedTuple
437
+
438
+
Tasks can be spawned to conveniently create `NamedTuple`s.
439
+
440
+
```julia
441
+
using Dagger
442
+
443
+
# Spawn a task to create a NamedTuple
444
+
nt_task = Dagger.@spawn (;a=1, b=2)
445
+
@assertfetch(nt_task) == (;a=1, b=2)
446
+
447
+
# Spawn a task to create an empty NamedTuple
448
+
empty_nt_task = Dagger.@spawn (;)
449
+
@assertfetch(empty_nt_task) == (;)
450
+
```
451
+
452
+
### Getproperty
453
+
454
+
Tasks can be spawned to access properties of `NamedTuple`s (or other objects supporting `getproperty`).
0 commit comments