Skip to content

Commit 5cf4774

Browse files
authored
(Too) many changes... (#65)
* Improve `CTaskException`, add `CTask` struct, add comments, and refactor `ctask.jl` * Add iteration interface for `CTask` * Clean and update current tests and include TRef * Update README * Fix tests on older Julia versions * Remove superfluous forwarding * Add `let` block * Improve comparisons * Reintroduce `_libtask_state`
1 parent 2ca2a89 commit 5cf4774

11 files changed

+543
-453
lines changed

Diff for: README.md

+51-36
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,85 @@ C shim for [task copying](https://github.com/JuliaLang/julia/issues/4085) in Tur
77

88
## Getting Started
99

10+
Stack allocated objects are deep copied:
11+
1012
```julia
1113
using Libtask
1214

13-
# Stack allocated objects are deep copied.
14-
function f_ct()
15-
t = 0;
15+
function f()
16+
t = 0
1617
while true
1718
produce(t)
1819
t = 1 + t
1920
end
2021
end
2122

22-
t = CTask(f_ct)
23+
ctask = CTask(f)
24+
25+
@show consume(ctask) # 0
26+
@show consume(ctask) # 1
27+
28+
a = copy(ctask)
29+
@show consume(a) # 2
30+
@show consume(a) # 3
31+
32+
@show consume(ctask) # 2
33+
@show consume(ctask) # 3
34+
```
2335

24-
consume(t) == 0
25-
consume(t) == 1
26-
a = copy(t);
27-
consume(a) == 2
28-
consume(a) == 3
29-
consume(t) == 2
30-
consume(t) == 3
36+
Heap allocated objects are shallow copied:
3137

32-
# Heap allocated objects are shallow copied.
38+
```julia
39+
using Libtask
3340

34-
function f_ct2()
35-
t = [0 1 2];
41+
function f()
42+
t = [0 1 2]
3643
while true
3744
produce(t[1])
3845
t[1] = 1 + t[1]
3946
end
4047
end
4148

42-
t = CTask(f_ct2)
49+
ctask = CTask(f)
4350

44-
consume(t) == 0
45-
consume(t) == 1
46-
a = copy(t);
47-
consume(a) == 2
48-
consume(a) == 3
49-
consume(t) == 4
50-
consume(t) == 5
51+
@show consume(ctask) # 0
52+
@show consume(ctask) # 1
5153

52-
# `TArray` implements a copy-on-write array. This is useful for task copying.
53-
# In constrast to standard arrays, which are only shallow copied during task copying,
54-
# `TArray` are deep copied after task copying.
54+
a = copy(t)
55+
@show consume(a) # 2
56+
@show consume(a) # 3
5557

56-
function f_cta()
57-
t = TArray(Int, 1);
58-
t[1] = 0;
58+
@show consume(ctask) # 4
59+
@show consume(ctask) # 5
60+
```
61+
62+
`TArray` implements a copy-on-write array. This is useful for task copying.
63+
In constrast to standard arrays, which are only shallow copied during task copying,
64+
`TArray` are deep copied after task copying:
65+
66+
```julia
67+
using Libtask
68+
69+
function f()
70+
t = TArray(Int, 1)
71+
t[1] = 0
5972
while true
6073
produce(t[1])
6174
t[1] = 1 + t[1]
6275
end
6376
end
6477

65-
t = CTask(f_cta)
78+
ctask = CTask(f)
79+
80+
@show consume(ctask) # 0
81+
@show consume(ctask) # 1
82+
83+
a = copy(ctask)
84+
@show consume(a) # 2
85+
@show consume(a) # 3
6686

67-
consume(t) == 0
68-
consume(t) == 1
69-
a = copy(t);
70-
consume(a) == 2
71-
consume(a) == 3
72-
consume(t) == 2
73-
consume(t) == 3
87+
@show consume(ctask) # 2
88+
@show consume(ctask) # 3
7489
```
7590

7691
Note: The [Turing](https://github.com/TuringLang/Turing.jl) probabilistic programming language uses this task copying feature in an efficient implementation of the [particle filtering](https://en.wikipedia.org/wiki/Particle_filter) sampling algorithm for arbitary order [Markov processes](https://en.wikipedia.org/wiki/Markov_model#Hidden_Markov_model).

Diff for: src/Libtask.jl

+1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ end
2525

2626
include("ctask.jl")
2727
include("tarray.jl")
28+
include("tref.jl")
2829

2930
end

0 commit comments

Comments
 (0)