|
2 | 2 |
|
3 | 3 | [](https://github.com/TuringLang/Libtask.jl/actions?branch=main)
|
4 | 4 |
|
5 |
| -Tape based task copying in Turing |
6 | 5 |
|
7 |
| -## Getting Started |
| 6 | +Resumable and copyable functions in Julia, with optional function-specific globals. |
| 7 | +See the docs for example usage. |
8 | 8 |
|
9 |
| -Stack allocated objects are always deep copied: |
10 |
| - |
11 |
| -```julia |
12 |
| -using Libtask |
13 |
| - |
14 |
| -function f() |
15 |
| - t = 0 |
16 |
| - for _ in 1:10 |
17 |
| - produce(t) |
18 |
| - t = 1 + t |
19 |
| - end |
20 |
| -end |
21 |
| - |
22 |
| -ttask = TapedTask(f) |
23 |
| - |
24 |
| -@show consume(ttask) # 0 |
25 |
| -@show consume(ttask) # 1 |
26 |
| - |
27 |
| -a = copy(ttask) |
28 |
| -@show consume(a) # 2 |
29 |
| -@show consume(a) # 3 |
30 |
| - |
31 |
| -@show consume(ttask) # 2 |
32 |
| -@show consume(ttask) # 3 |
33 |
| -``` |
34 |
| - |
35 |
| -Heap-allocated Array and Ref objects are deep copied by default: |
36 |
| - |
37 |
| -```julia |
38 |
| -using Libtask |
39 |
| - |
40 |
| -function f() |
41 |
| - t = [0 1 2] |
42 |
| - for _ in 1:10 |
43 |
| - produce(t[1]) |
44 |
| - t[1] = 1 + t[1] |
45 |
| - end |
46 |
| -end |
47 |
| - |
48 |
| -ttask = TapedTask(f) |
49 |
| - |
50 |
| -@show consume(ttask) # 0 |
51 |
| -@show consume(ttask) # 1 |
52 |
| - |
53 |
| -a = copy(ttask) |
54 |
| -@show consume(a) # 2 |
55 |
| -@show consume(a) # 3 |
56 |
| - |
57 |
| -@show consume(ttask) # 2 |
58 |
| -@show consume(ttask) # 3 |
59 |
| -``` |
60 |
| - |
61 |
| -Other heap-allocated objects (e.g., `Dict`) are shallow copied, by default: |
62 |
| - |
63 |
| -```julia |
64 |
| -using Libtask |
65 |
| - |
66 |
| -function f() |
67 |
| - t = Dict(1=>10, 2=>20) |
68 |
| - while true |
69 |
| - produce(t[1]) |
70 |
| - t[1] = 1 + t[1] |
71 |
| - end |
72 |
| -end |
73 |
| - |
74 |
| -ttask = TapedTask(f) |
75 |
| - |
76 |
| -@show consume(ttask) # 10 |
77 |
| -@show consume(ttask) # 11 |
78 |
| - |
79 |
| -a = copy(ttask) |
80 |
| -@show consume(a) # 12 |
81 |
| -@show consume(a) # 13 |
82 |
| - |
83 |
| -@show consume(ttask) # 14 |
84 |
| -@show consume(ttask) # 15 |
85 |
| -``` |
86 |
| - |
87 |
| -Notes: |
88 |
| - |
89 |
| -- The [Turing](https://github.com/TuringLang/Turing.jl) probabilistic |
90 |
| - programming language uses this task copying feature in an efficient |
91 |
| - implementation of the [particle |
92 |
| - filtering](https://en.wikipedia.org/wiki/Particle_filter) sampling |
93 |
| - algorithm for arbitrary order [Markov |
94 |
| - processes](https://en.wikipedia.org/wiki/Markov_model#Hidden_Markov_model). |
95 |
| - |
96 |
| -- From v0.6.0, Libtask is implemented by recording all the computing |
97 |
| - to a tape and copying that tape. Before that version, it is based on |
98 |
| - a tricky hack on the Julia internals. You can check the commit |
99 |
| - history of this repo to see the details. |
| 9 | +Used in the [Turing](https://github.com/TuringLang/Turing.jl) probabilistic programming language to implement various particle-based inference methods, for example those in [AdvancedPS.jl](https://github.com/TuringLang/AdvancedPS.jl/). |
0 commit comments