-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope_spawn.zig
More file actions
207 lines (179 loc) · 6.18 KB
/
Copy pathscope_spawn.zig
File metadata and controls
207 lines (179 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Scope and Join Sample
//
// Demonstrates structured concurrency patterns in ZigParallel.
// Shows join() for binary fork-join parallelism.
//
// This sample shows:
// - Binary parallelism with join()
// - Recursive parallel algorithms
// - Different return types from parallel branches
const std = @import("std");
const zigparallel = @import("loom");
const join = zigparallel.join;
pub fn main() !void {
std.debug.print("=== ZigParallel Scope/Join Sample ===\n\n", .{});
// join() automatically uses the global thread pool
// ========================================================================
// Example 1: Binary fork-join with join()
// ========================================================================
std.debug.print("--- Example 1: Binary Fork-Join ---\n", .{});
{
const sumLeft = struct {
fn compute() u64 {
var sum: u64 = 0;
for (0..500_000) |i| {
sum +%= i;
}
return sum;
}
}.compute;
const sumRight = struct {
fn compute() u64 {
var sum: u64 = 0;
for (500_000..1_000_000) |i| {
sum +%= i;
}
return sum;
}
}.compute;
const left, const right = join(sumLeft, .{}, sumRight, .{});
const total = left + right;
std.debug.print("Left sum (0..500K): {d}\n", .{left});
std.debug.print("Right sum (500K..1M): {d}\n", .{right});
std.debug.print("Total sum: {d}\n\n", .{total});
}
// ========================================================================
// Example 2: Different return types
// ========================================================================
std.debug.print("--- Example 2: Different Return Types ---\n", .{});
{
const countEven = struct {
fn compute() i32 {
var count: i32 = 0;
for (0..1000) |i| {
if (i % 2 == 0) count += 1;
}
return count;
}
}.compute;
const computeAverage = struct {
fn compute() f64 {
var sum: f64 = 0;
for (0..100) |i| {
sum += @as(f64, @floatFromInt(i));
}
return sum / 100.0;
}
}.compute;
const even_count, const average = join(countEven, .{}, computeAverage, .{});
std.debug.print("Even count (0..1000): {d}\n", .{even_count});
std.debug.print("Average (0..100): {d:.2}\n\n", .{average});
}
// ========================================================================
// Example 3: Join with arguments
// ========================================================================
std.debug.print("--- Example 3: Functions with Arguments ---\n", .{});
{
const multiply = struct {
fn compute(a: i32, b: i32) i32 {
return a * b;
}
}.compute;
const sumRange = struct {
fn compute(start: u64, end: u64) u64 {
var sum: u64 = 0;
for (start..end) |i| {
sum += i;
}
return sum;
}
}.compute;
const product, const range_sum = join(
multiply,
.{ 7, 8 },
sumRange,
.{ @as(u64, 1), @as(u64, 101) },
);
std.debug.print("7 * 8 = {d}\n", .{product});
std.debug.print("sum(1..100) = {d}\n\n", .{range_sum});
}
// ========================================================================
// Example 4: Recursive parallelism - Parallel Fibonacci
// ========================================================================
std.debug.print("--- Example 4: Recursive Fibonacci ---\n", .{});
{
const n: u32 = 20;
const result = parallelFib(n);
std.debug.print("Parallel fib({d}) = {d}\n\n", .{ n, result });
}
// ========================================================================
// Example 5: Parallel array processing
// ========================================================================
std.debug.print("--- Example 5: Parallel Array Sum ---\n", .{});
{
var data: [10000]i64 = undefined;
for (&data, 0..) |*item, i| {
item.* = @intCast(i + 1);
}
// Split array and sum each half in parallel
const half = data.len / 2;
const first_half: []const i64 = data[0..half];
const second_half: []const i64 = data[half..];
const sumSlice = struct {
fn compute(slice: []const i64) i64 {
var sum: i64 = 0;
for (slice) |v| {
sum += v;
}
return sum;
}
}.compute;
const left_sum, const right_sum = join(
sumSlice,
.{first_half},
sumSlice,
.{second_half},
);
const total = left_sum + right_sum;
const n: i64 = @intCast(data.len);
const expected = @divExact(n * (n + 1), 2);
std.debug.print("Left half sum: {d}\n", .{left_sum});
std.debug.print("Right half sum: {d}\n", .{right_sum});
std.debug.print("Total: {d} (expected: {d})\n\n", .{ total, expected });
}
std.debug.print("=== All examples completed ===\n", .{});
}
fn parallelFib(n: u32) u64 {
if (n <= 1) return n;
// For small n, compute sequentially
if (n < 15) {
return sequentialFib(n);
}
// For larger n, use parallel join
const left, const right = join(
struct {
fn compute(m: u32) u64 {
return parallelFib(m);
}
}.compute,
.{n - 1},
struct {
fn compute(m: u32) u64 {
return parallelFib(m);
}
}.compute,
.{n - 2},
);
return left + right;
}
fn sequentialFib(n: u32) u64 {
if (n <= 1) return n;
var a: u64 = 0;
var b: u64 = 1;
for (2..n + 1) |_| {
const c = a + b;
a = b;
b = c;
}
return b;
}