-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributions.zig
More file actions
172 lines (142 loc) Β· 5.67 KB
/
distributions.zig
File metadata and controls
172 lines (142 loc) Β· 5.67 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
/// cuRAND Distributions Example
///
/// Demonstrates GPU random number generation with various distributions:
/// 1. Uniform distribution (0, 1]
/// 2. Normal distribution (Gaussian)
/// 3. Log-normal distribution
/// 4. Poisson distribution
/// 5. Seed control for reproducibility
///
/// Reference: CUDALibrarySamples/cuRAND
const std = @import("std");
const cuda = @import("zcuda");
pub fn main() !void {
const allocator = std.heap.page_allocator;
std.debug.print("=== cuRAND Distributions Example ===\n\n", .{});
const ctx = try cuda.driver.CudaContext.new(0);
defer ctx.deinit();
std.debug.print("Device: {s}\n\n", .{ctx.name()});
const stream = ctx.defaultStream();
const rng = try cuda.curand.CurandContext.init(ctx, .default);
defer rng.deinit();
try rng.setSeed(42);
try rng.setStream(stream);
const n: usize = 10000;
// --- Uniform distribution ---
std.debug.print("βββ Uniform Distribution (0, 1] βββ\n", .{});
{
const d_data = try stream.alloc(f32, allocator, n);
defer d_data.deinit();
try rng.fillUniform(d_data);
var h_data: [10000]f32 = undefined;
try stream.memcpyDtoH(f32, &h_data, d_data);
var sum: f64 = 0.0;
var min_val: f32 = std.math.floatMax(f32);
var max_val: f32 = -std.math.floatMax(f32);
for (&h_data) |v| {
sum += @as(f64, v);
min_val = @min(min_val, v);
max_val = @max(max_val, v);
}
const mean = sum / @as(f64, @floatFromInt(n));
std.debug.print(" Samples: {}\n", .{n});
std.debug.print(" Mean: {d:.4} (expected ~0.5)\n", .{mean});
std.debug.print(" Range: [{d:.4}, {d:.4}]\n", .{ min_val, max_val });
std.debug.print(" First 5: ", .{});
for (h_data[0..5]) |v| std.debug.print("{d:.4} ", .{v});
std.debug.print("\n\n", .{});
}
// --- Normal distribution ---
std.debug.print("βββ Normal Distribution (ΞΌ=0, Ο=1) βββ\n", .{});
{
const d_data = try stream.alloc(f32, allocator, n);
defer d_data.deinit();
try rng.fillNormal(d_data, 0.0, 1.0);
var h_data: [10000]f32 = undefined;
try stream.memcpyDtoH(f32, &h_data, d_data);
var sum: f64 = 0.0;
for (&h_data) |v| sum += @as(f64, v);
const mean = sum / @as(f64, @floatFromInt(n));
var var_sum: f64 = 0.0;
for (&h_data) |v| {
const diff = @as(f64, v) - mean;
var_sum += diff * diff;
}
const stddev = @sqrt(var_sum / @as(f64, @floatFromInt(n)));
std.debug.print(" Samples: {}\n", .{n});
std.debug.print(" Mean: {d:.4} (expected ~0.0)\n", .{mean});
std.debug.print(" Stddev: {d:.4} (expected ~1.0)\n", .{stddev});
std.debug.print(" First 5: ", .{});
for (h_data[0..5]) |v| std.debug.print("{d:.4} ", .{v});
std.debug.print("\n\n", .{});
}
// --- Log-Normal distribution ---
std.debug.print("βββ Log-Normal Distribution (ΞΌ=0, Ο=0.5) βββ\n", .{});
{
const d_data = try stream.alloc(f32, allocator, n);
defer d_data.deinit();
try rng.fillLogNormal(d_data, 0.0, 0.5);
var h_data: [10000]f32 = undefined;
try stream.memcpyDtoH(f32, &h_data, d_data);
var sum: f64 = 0.0;
var min_val: f32 = std.math.floatMax(f32);
for (&h_data) |v| {
sum += @as(f64, v);
min_val = @min(min_val, v);
}
const mean = sum / @as(f64, @floatFromInt(n));
std.debug.print(" Samples: {}\n", .{n});
std.debug.print(" Mean: {d:.4} (expected ~{d:.4})\n", .{
mean,
@exp(@as(f64, 0.0) + 0.5 * 0.25),
});
std.debug.print(" Min: {d:.4} (all positive)\n\n", .{min_val});
}
// --- Poisson distribution ---
std.debug.print("βββ Poisson Distribution (Ξ»=5.0) βββ\n", .{});
{
const d_data = try stream.alloc(u32, allocator, n);
defer d_data.deinit();
try rng.fillPoisson(d_data, 5.0);
var h_data: [10000]u32 = undefined;
try stream.memcpyDtoH(u32, &h_data, d_data);
var sum: u64 = 0;
for (&h_data) |v| sum += v;
const mean = @as(f64, @floatFromInt(sum)) / @as(f64, @floatFromInt(n));
std.debug.print(" Samples: {}\n", .{n});
std.debug.print(" Mean: {d:.4} (expected ~5.0)\n", .{mean});
std.debug.print(" First 10: ", .{});
for (h_data[0..10]) |v| std.debug.print("{} ", .{v});
std.debug.print("\n\n", .{});
}
// --- Reproducibility test ---
std.debug.print("βββ Seed Reproducibility βββ\n", .{});
{
const d1 = try stream.alloc(f32, allocator, 5);
defer d1.deinit();
const d2 = try stream.alloc(f32, allocator, 5);
defer d2.deinit();
try rng.setSeed(12345);
try rng.fillUniform(d1);
try rng.setSeed(12345);
try rng.fillUniform(d2);
var h1: [5]f32 = undefined;
var h2: [5]f32 = undefined;
try stream.memcpyDtoH(f32, &h1, d1);
try stream.memcpyDtoH(f32, &h2, d2);
std.debug.print(" Run 1: ", .{});
for (&h1) |v| std.debug.print("{d:.6} ", .{v});
std.debug.print("\n Run 2: ", .{});
for (&h2) |v| std.debug.print("{d:.6} ", .{v});
std.debug.print("\n", .{});
var match = true;
for (&h1, &h2) |a, b| {
if (a != b) {
match = false;
break;
}
}
std.debug.print(" Reproducible: {s}\n", .{if (match) "Yes β" else "No β"});
}
std.debug.print("\nβ cuRAND distributions example complete\n", .{});
}