tilework-hip 0.18.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,840 @@
1
+ #include <ruby.h>
2
+ #include <stdint.h>
3
+ #include <stdlib.h>
4
+
5
+ /* The C ABI the backend bridge exports. Mechanism only; policy lives in Ruby. */
6
+ extern char *gpu_bridge_device_name(void);
7
+ extern int gpu_bridge_device_available(void);
8
+ extern void *gpu_bridge_buffer_alloc(long bytes);
9
+ extern void gpu_bridge_buffer_write(void *handle, const void *src, long bytes, long offset);
10
+ extern void gpu_bridge_buffer_read(void *handle, void *dst, long bytes, long offset);
11
+ extern void gpu_bridge_buffer_free(void *handle);
12
+ extern void *gpu_bridge_pipeline_create(const char *src, const char *fn_name, char **error_out);
13
+ extern long gpu_bridge_pipeline_max_threads(void *handle);
14
+ extern void gpu_bridge_pipeline_free(void *handle);
15
+ extern int gpu_bridge_dispatch(void *pipeline,
16
+ const int32_t *arg_kinds, void *const *arg_ptrs,
17
+ const long *arg_lens, int32_t arg_count,
18
+ long gx, long gy, long gz,
19
+ long bx, long by, long bz,
20
+ int32_t wait, char **error_out);
21
+ extern void gpu_bridge_synchronize(void);
22
+ /* Optional: synchronize and report a failed asynchronous command. */
23
+ extern int gpu_bridge_synchronize_checked(char **err) __attribute__((weak));
24
+
25
+ /* Optional ABI. Weakly referenced, so linking a bridge that does not
26
+ implement it leaves the symbol NULL instead of unresolved, and Ruby gets
27
+ nil and falls back. */
28
+ extern int gpu_bridge_device_capabilities(int32_t *warp_size, int32_t *cc_major,
29
+ int32_t *cc_minor, int32_t *integrated,
30
+ long *memory_budget_bytes) __attribute__((weak));
31
+ extern char *gpu_bridge_device_architecture(void) __attribute__((weak));
32
+ extern int gpu_bridge_device_index(void) __attribute__((weak));
33
+ extern char *gpu_bridge_last_error(void) __attribute__((weak));
34
+ extern void gpu_bridge_shutdown(void) __attribute__((weak));
35
+
36
+ /* Optional ABI: offload transfers, two synchronous calls. Without them Ruby
37
+ falls back to the plain D2H -> String -> File.write path. */
38
+ extern int gpu_bridge_buffer_save(void *handle, const char *path, long bytes,
39
+ long off, char **err) __attribute__((weak));
40
+ extern int gpu_bridge_buffer_load(void *handle, const char *path, long bytes,
41
+ long off, char **err) __attribute__((weak));
42
+
43
+ /* Optional ABI: three asynchronous transfer calls, used to hide prefetch.
44
+ The bridge has no scheduler; what to prefetch in which order is Ruby's policy. */
45
+ extern void *gpu_bridge_transfer_begin(void *handle, const char *path, long bytes,
46
+ long off, int dir) __attribute__((weak));
47
+ extern int gpu_bridge_transfer_poll(void *t) __attribute__((weak));
48
+ extern int gpu_bridge_transfer_end(void *t, char **err) __attribute__((weak));
49
+
50
+ /* Optional ABI: delegated GEMM. Without it the kernel path is used. */
51
+ extern int gpu_bridge_gemm(void *a, long a_off, void *b, long b_off, void *c, long c_off,
52
+ long m, long k, long n,
53
+ int allow_tf32, int in_dtype, char **err) __attribute__((weak));
54
+ extern int gpu_bridge_gemm_available(void) __attribute__((weak));
55
+
56
+ /* optional ABI: executable matrix-instruction path (rocWMMA etc.). */
57
+ extern int gpu_bridge_matrix_core_available(void) __attribute__((weak));
58
+ extern int gpu_bridge_matrix_core_gemm(void *a, long a_off, void *b, long b_off,
59
+ void *c, long c_off, long m, long k, long n,
60
+ char **err) __attribute__((weak));
61
+
62
+ /* Optional ABI: issue `batch` GEMMs of the same shape at once. Without it
63
+ Ruby falls back to one gemm per batch. */
64
+ extern int gpu_bridge_gemm_strided_batched(void *a, long a_off, void *b, long b_off,
65
+ void *c, long c_off,
66
+ long m, long k, long n,
67
+ long lda, long ldb, long ldc,
68
+ long stride_a, long stride_b, long stride_c,
69
+ int trans_a, int trans_b,
70
+ long batch, double alpha,
71
+ int allow_tf32, int in_dtype,
72
+ char **err) __attribute__((weak));
73
+
74
+ /* Optional ABI: random generation (cuRAND). Without it Ruby's Random is used. */
75
+ extern int gpu_bridge_random(void *handle, long n, int kind, unsigned long long seed,
76
+ float mean, float stddev, char **err) __attribute__((weak));
77
+ extern int gpu_bridge_random_available(void) __attribute__((weak));
78
+
79
+ /* Optional ABI: FFT (cuFFT). Without it Ruby raises. */
80
+ extern int gpu_bridge_fft(void *in, void *out, long n, long batch, int kind,
81
+ int direction, char **err) __attribute__((weak));
82
+ extern int gpu_bridge_fft_available(void) __attribute__((weak));
83
+
84
+ /* Optional ABI: 2D convolution (cuDNN). Without it Ruby raises. */
85
+ extern int gpu_bridge_conv2d(void *x, void *w, void *y,
86
+ int n, int cin, int h, int width,
87
+ int cout, int kh, int kw,
88
+ int pad_h, int pad_w, int stride_h, int stride_w,
89
+ int dilation_h, int dilation_w, int groups,
90
+ int dtype, char **err) __attribute__((weak));
91
+ extern int gpu_bridge_conv2d_available(void) __attribute__((weak));
92
+
93
+ /* Optional ABI: convolution backward. mode 0 = input gradient, 1 = weight
94
+ gradient. Descriptors are shared with the forward plan, so the forward
95
+ pass must have run first. */
96
+ extern int gpu_bridge_conv2d_backward(void *a, void *b, void *out,
97
+ int n, int cin, int h, int width,
98
+ int cout, int kh, int kw,
99
+ int pad_h, int pad_w, int stride_h, int stride_w,
100
+ int dilation_h, int dilation_w, int groups,
101
+ int dtype, int mode, char **err) __attribute__((weak));
102
+ extern int gpu_bridge_conv2d_backward_available(void) __attribute__((weak));
103
+
104
+ /* optional ABI: opaque pageable host backing, never a filesystem path. */
105
+ extern int gpu_bridge_host_backing_available(void) __attribute__((weak));
106
+ extern void *gpu_bridge_host_backing_alloc(long bytes) __attribute__((weak));
107
+ extern void gpu_bridge_host_backing_free(void *handle) __attribute__((weak));
108
+ extern int gpu_bridge_host_backing_store(void *host, void *device, long bytes, long offset, char **err) __attribute__((weak));
109
+ extern int gpu_bridge_host_backing_restore(void *host, void *device, long bytes, long offset, char **err) __attribute__((weak));
110
+ extern void *gpu_bridge_host_backing_transfer_begin(void *host, void *device, long bytes, long offset, char **err) __attribute__((weak));
111
+ extern int gpu_bridge_host_backing_transfer_poll(void *token) __attribute__((weak));
112
+ extern int gpu_bridge_host_backing_transfer_end(void *token, char **err) __attribute__((weak));
113
+
114
+ /* --- TypedData: buffer handle --- */
115
+
116
+ typedef struct {
117
+ void *handle; /* NULL after explicit free */
118
+ long offset; /* in bytes; nonzero only for a view */
119
+ int owned; /* 0 for a view: must not be freed */
120
+ VALUE parent; /* the BufferHandle a view points into (kept alive against GC) */
121
+ } bridge_buffer_t;
122
+
123
+ static void
124
+ buffer_dfree(void *ptr)
125
+ {
126
+ bridge_buffer_t *data = ptr;
127
+ /* A view only points at someone else's memory, so it frees nothing */
128
+ if (data->handle && data->owned) gpu_bridge_buffer_free(data->handle);
129
+ xfree(data);
130
+ }
131
+
132
+ static void
133
+ buffer_dmark(void *ptr)
134
+ {
135
+ bridge_buffer_t *data = ptr;
136
+ if (data->parent != Qnil) rb_gc_mark(data->parent);
137
+ }
138
+
139
+ static const rb_data_type_t buffer_data_type = {
140
+ "Tilework::Bridge::BufferHandle",
141
+ { buffer_dmark, buffer_dfree, NULL },
142
+ NULL, NULL, 0 /* dmark rules out FREE_IMMEDIATELY */
143
+ };
144
+
145
+ /* For delegated libraries (cuBLAS/cuDNN/cuRAND/cuFFT). They take raw
146
+ pointers, so pass the address with the view's offset added. */
147
+ static void *buffer_ptr(bridge_buffer_t *data)
148
+ {
149
+ return (void *)((char *)data->handle + data->offset);
150
+ }
151
+
152
+ static bridge_buffer_t *
153
+ buffer_unwrap(VALUE obj)
154
+ {
155
+ bridge_buffer_t *data;
156
+ TypedData_Get_Struct(obj, bridge_buffer_t, &buffer_data_type, data);
157
+ if (data->handle == NULL) rb_raise(rb_eRuntimeError, "buffer already freed");
158
+ return data;
159
+ }
160
+
161
+ /* --- TypedData: pipeline handle --- */
162
+
163
+ typedef struct {
164
+ void *handle;
165
+ } bridge_pipeline_t;
166
+
167
+ static void
168
+ pipeline_dfree(void *ptr)
169
+ {
170
+ bridge_pipeline_t *data = ptr;
171
+ if (data->handle) gpu_bridge_pipeline_free(data->handle);
172
+ xfree(data);
173
+ }
174
+
175
+ static const rb_data_type_t pipeline_data_type = {
176
+ "Tilework::Bridge::PipelineHandle",
177
+ { NULL, pipeline_dfree, NULL },
178
+ NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
179
+ };
180
+
181
+ static bridge_pipeline_t *
182
+ pipeline_unwrap(VALUE obj)
183
+ {
184
+ bridge_pipeline_t *data;
185
+ TypedData_Get_Struct(obj, bridge_pipeline_t, &pipeline_data_type, data);
186
+ if (data->handle == NULL) rb_raise(rb_eRuntimeError, "pipeline already freed");
187
+ return data;
188
+ }
189
+
190
+ static VALUE cBufferHandle;
191
+ static VALUE cPipelineHandle;
192
+ static VALUE cHostBackingHandle;
193
+
194
+ typedef struct { void *handle; } bridge_host_backing_t;
195
+ static void host_backing_dfree(void *ptr) {
196
+ bridge_host_backing_t *data = ptr;
197
+ if (data->handle && gpu_bridge_host_backing_free != NULL) gpu_bridge_host_backing_free(data->handle);
198
+ xfree(data);
199
+ }
200
+ static const rb_data_type_t host_backing_data_type = {
201
+ "Tilework::Bridge::HostBackingHandle", { NULL, host_backing_dfree, NULL }, NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
202
+ };
203
+ static bridge_host_backing_t *host_backing_unwrap(VALUE obj) {
204
+ bridge_host_backing_t *data;
205
+ TypedData_Get_Struct(obj, bridge_host_backing_t, &host_backing_data_type, data);
206
+ if (!data->handle) rb_raise(rb_eRuntimeError, "host backing already freed");
207
+ return data;
208
+ }
209
+
210
+ #ifndef TILEWORK_BRIDGE_MODULE
211
+ #define TILEWORK_BRIDGE_MODULE "HIPBridge"
212
+ #endif
213
+
214
+ static VALUE
215
+ raise_bridge_error(const char *fallback)
216
+ {
217
+ char *error = gpu_bridge_last_error != NULL ? gpu_bridge_last_error() : NULL;
218
+ VALUE message = rb_utf8_str_new_cstr(error ? error : fallback);
219
+ if (error) free(error);
220
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, message));
221
+ return Qnil;
222
+ }
223
+
224
+ static void
225
+ bridge_shutdown(VALUE _unused)
226
+ {
227
+ if (gpu_bridge_shutdown != NULL) gpu_bridge_shutdown();
228
+ }
229
+
230
+ /* --- module functions --- */
231
+
232
+ static VALUE
233
+ bridge_device_name(VALUE self)
234
+ {
235
+ char *name = gpu_bridge_device_name();
236
+ if (name == NULL) return raise_bridge_error("no Tilework device available");
237
+ VALUE str = rb_utf8_str_new_cstr(name);
238
+ free(name);
239
+ return str;
240
+ }
241
+
242
+ static VALUE
243
+ bridge_device_available_p(VALUE self)
244
+ {
245
+ return gpu_bridge_device_available() ? Qtrue : Qfalse;
246
+ }
247
+
248
+ /* Diagnostic observations. Unlike Capabilities, never used for path selection. */
249
+ static VALUE
250
+ bridge_device_info(VALUE self)
251
+ {
252
+ VALUE info = rb_hash_new();
253
+ char *name = gpu_bridge_device_name();
254
+ if (name != NULL) {
255
+ rb_hash_aset(info, ID2SYM(rb_intern("name")), rb_utf8_str_new_cstr(name));
256
+ free(name);
257
+ }
258
+ if (gpu_bridge_device_architecture != NULL) {
259
+ char *architecture = gpu_bridge_device_architecture();
260
+ if (architecture != NULL) {
261
+ rb_hash_aset(info, ID2SYM(rb_intern("architecture")), rb_utf8_str_new_cstr(architecture));
262
+ free(architecture);
263
+ }
264
+ }
265
+ if (gpu_bridge_device_index != NULL) {
266
+ int index = gpu_bridge_device_index();
267
+ if (index >= 0) rb_hash_aset(info, ID2SYM(rb_intern("index")), INT2NUM(index));
268
+ }
269
+ if (gpu_bridge_last_error != NULL) {
270
+ char *error = gpu_bridge_last_error();
271
+ if (error != NULL) {
272
+ rb_hash_aset(info, ID2SYM(rb_intern("last_error")), rb_utf8_str_new_cstr(error));
273
+ free(error);
274
+ }
275
+ }
276
+ return info;
277
+ }
278
+
279
+ static VALUE
280
+ bridge_buffer_alloc(VALUE self, VALUE bytesize)
281
+ {
282
+ long bytes = NUM2LONG(bytesize);
283
+ if (bytes <= 0) rb_raise(rb_eArgError, "buffer size must be positive");
284
+ void *handle = gpu_bridge_buffer_alloc(bytes);
285
+ if (handle == NULL) return raise_bridge_error("failed to allocate Tilework buffer");
286
+
287
+ bridge_buffer_t *data = ALLOC(bridge_buffer_t);
288
+ data->handle = handle;
289
+ data->offset = 0;
290
+ data->owned = 1;
291
+ data->parent = Qnil;
292
+ return TypedData_Wrap_Struct(cBufferHandle, &buffer_data_type, data);
293
+ }
294
+
295
+ /* Creates another handle to the same memory. It frees nothing, so it is valid
296
+ while the original handle lives; holding parent protects that from GC. */
297
+ static VALUE
298
+ bridge_buffer_view(VALUE self, VALUE buf, VALUE byte_offset)
299
+ {
300
+ bridge_buffer_t *base = buffer_unwrap(buf);
301
+
302
+ bridge_buffer_t *data = ALLOC(bridge_buffer_t);
303
+ data->handle = base->handle;
304
+ data->offset = base->offset + NUM2LONG(byte_offset);
305
+ data->owned = 0;
306
+ data->parent = buf;
307
+ return TypedData_Wrap_Struct(cBufferHandle, &buffer_data_type, data);
308
+ }
309
+
310
+ static VALUE bridge_host_backing_available_p(VALUE self) {
311
+ return (gpu_bridge_host_backing_available != NULL && gpu_bridge_host_backing_available()) ? Qtrue : Qfalse;
312
+ }
313
+ static VALUE bridge_host_backing_alloc(VALUE self, VALUE bytesize) {
314
+ if (gpu_bridge_host_backing_alloc == NULL) rb_raise(rb_eNotImpError, "host backing is not available");
315
+ void *handle = gpu_bridge_host_backing_alloc(NUM2LONG(bytesize));
316
+ if (!handle) return raise_bridge_error("failed to allocate host backing");
317
+ bridge_host_backing_t *data = ALLOC(bridge_host_backing_t); data->handle = handle;
318
+ return TypedData_Wrap_Struct(cHostBackingHandle, &host_backing_data_type, data);
319
+ }
320
+ static VALUE bridge_host_backing_free(VALUE self, VALUE backing) {
321
+ bridge_host_backing_t *data = host_backing_unwrap(backing);
322
+ if (gpu_bridge_host_backing_free != NULL) gpu_bridge_host_backing_free(data->handle);
323
+ data->handle = NULL; return Qnil;
324
+ }
325
+ static VALUE bridge_host_backing_copy(VALUE self, VALUE backing, VALUE buffer, VALUE bytes, VALUE offset, int restore) {
326
+ bridge_host_backing_t *host = host_backing_unwrap(backing); bridge_buffer_t *device = buffer_unwrap(buffer);
327
+ char *error = NULL;
328
+ int status = restore ? gpu_bridge_host_backing_restore(host->handle, buffer_ptr(device), NUM2LONG(bytes), NUM2LONG(offset), &error)
329
+ : gpu_bridge_host_backing_store(host->handle, buffer_ptr(device), NUM2LONG(bytes), NUM2LONG(offset), &error);
330
+ if (status != 0) { VALUE msg = rb_utf8_str_new_cstr(error ? error : "host backing transfer failed"); if (error) free(error); rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg)); }
331
+ return Qnil;
332
+ }
333
+ static VALUE bridge_host_backing_store(VALUE self, VALUE backing, VALUE buffer, VALUE bytes, VALUE offset) { return bridge_host_backing_copy(self, backing, buffer, bytes, offset, 0); }
334
+ static VALUE bridge_host_backing_restore(VALUE self, VALUE backing, VALUE buffer, VALUE bytes, VALUE offset) { return bridge_host_backing_copy(self, backing, buffer, bytes, offset, 1); }
335
+ static VALUE bridge_host_backing_async_available_p(VALUE self) {
336
+ return (gpu_bridge_host_backing_transfer_begin != NULL && gpu_bridge_host_backing_transfer_poll != NULL && gpu_bridge_host_backing_transfer_end != NULL) ? Qtrue : Qfalse;
337
+ }
338
+ static VALUE bridge_host_backing_transfer_begin(VALUE self, VALUE backing, VALUE buffer, VALUE bytes, VALUE offset) {
339
+ if (gpu_bridge_host_backing_transfer_begin == NULL) rb_raise(rb_eNotImpError, "async host backing is not available");
340
+ bridge_host_backing_t *host = host_backing_unwrap(backing); bridge_buffer_t *device = buffer_unwrap(buffer); char *error = NULL;
341
+ void *token = gpu_bridge_host_backing_transfer_begin(host->handle, buffer_ptr(device), NUM2LONG(bytes), NUM2LONG(offset), &error);
342
+ if (!token) { VALUE msg = rb_utf8_str_new_cstr(error ? error : "async host backing transfer failed"); if (error) free(error); rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg)); }
343
+ return ULL2NUM((unsigned long long)(uintptr_t)token);
344
+ }
345
+ static VALUE bridge_host_backing_transfer_poll(VALUE self, VALUE token) { return INT2NUM(gpu_bridge_host_backing_transfer_poll((void *)(uintptr_t)NUM2ULL(token))); }
346
+ static VALUE bridge_host_backing_transfer_end(VALUE self, VALUE token) {
347
+ char *error = NULL; if (gpu_bridge_host_backing_transfer_end((void *)(uintptr_t)NUM2ULL(token), &error) != 0) { VALUE msg = rb_utf8_str_new_cstr(error ? error : "async host backing transfer failed"); if (error) free(error); rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg)); } return Qnil;
348
+ }
349
+
350
+ static VALUE
351
+ bridge_buffer_write(VALUE self, VALUE buf, VALUE bytes, VALUE offset)
352
+ {
353
+ bridge_buffer_t *data = buffer_unwrap(buf);
354
+ Check_Type(bytes, T_STRING);
355
+ gpu_bridge_buffer_write(data->handle, RSTRING_PTR(bytes), RSTRING_LEN(bytes),
356
+ data->offset + NUM2LONG(offset));
357
+ return Qnil;
358
+ }
359
+
360
+ static VALUE
361
+ bridge_buffer_read(VALUE self, VALUE buf, VALUE bytesize, VALUE offset)
362
+ {
363
+ bridge_buffer_t *data = buffer_unwrap(buf);
364
+ long bytes = NUM2LONG(bytesize);
365
+ VALUE str = rb_str_new(NULL, bytes);
366
+ gpu_bridge_buffer_read(data->handle, RSTRING_PTR(str), bytes, data->offset + NUM2LONG(offset));
367
+ return str;
368
+ }
369
+
370
+ static VALUE
371
+ bridge_buffer_free(VALUE self, VALUE buf)
372
+ {
373
+ bridge_buffer_t *data;
374
+ TypedData_Get_Struct(buf, bridge_buffer_t, &buffer_data_type, data);
375
+ if (data->handle && data->owned) gpu_bridge_buffer_free(data->handle);
376
+ data->handle = NULL; /* mark it unusable even for a view */
377
+ return Qnil;
378
+ }
379
+
380
+ static VALUE
381
+ bridge_pipeline_create(VALUE self, VALUE src, VALUE fn_name)
382
+ {
383
+ Check_Type(src, T_STRING);
384
+ Check_Type(fn_name, T_STRING);
385
+ char *error = NULL;
386
+ void *handle = gpu_bridge_pipeline_create(StringValueCStr(src), StringValueCStr(fn_name), &error);
387
+ if (handle == NULL) {
388
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "unknown kernel compile error");
389
+ if (error) free(error);
390
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
391
+ }
392
+
393
+ bridge_pipeline_t *data = ALLOC(bridge_pipeline_t);
394
+ data->handle = handle;
395
+ return TypedData_Wrap_Struct(cPipelineHandle, &pipeline_data_type, data);
396
+ }
397
+
398
+ static VALUE
399
+ bridge_pipeline_max_threads(VALUE self, VALUE pipeline)
400
+ {
401
+ bridge_pipeline_t *data = pipeline_unwrap(pipeline);
402
+ return LONG2NUM(gpu_bridge_pipeline_max_threads(data->handle));
403
+ }
404
+
405
+ /* args: a Ruby Array of BufferHandle (setBuffer) or String (setBytes). */
406
+ static VALUE
407
+ bridge_dispatch(VALUE self, VALUE pipeline, VALUE args, VALUE grid, VALUE block, VALUE wait)
408
+ {
409
+ bridge_pipeline_t *pso = pipeline_unwrap(pipeline);
410
+ Check_Type(args, T_ARRAY);
411
+ long argc = RARRAY_LEN(args);
412
+
413
+ int32_t *kinds = ALLOCA_N(int32_t, argc > 0 ? argc : 1);
414
+ void **ptrs = ALLOCA_N(void *, argc > 0 ? argc : 1);
415
+ long *lens = ALLOCA_N(long, argc > 0 ? argc : 1);
416
+
417
+ for (long i = 0; i < argc; i++) {
418
+ VALUE arg = rb_ary_entry(args, i);
419
+ if (rb_typeddata_is_kind_of(arg, &buffer_data_type)) {
420
+ bridge_buffer_t *data = buffer_unwrap(arg);
421
+ kinds[i] = 0;
422
+ ptrs[i] = data->handle;
423
+ /* arg_lens was unused for buffer arguments, so it carries the
424
+ view's byte offset here. CUDA adds it to the pointer, Metal
425
+ takes it via setBuffer(offset:) — no C ABI change needed. */
426
+ lens[i] = data->offset;
427
+ } else if (RB_TYPE_P(arg, T_STRING)) {
428
+ kinds[i] = 1;
429
+ ptrs[i] = RSTRING_PTR(arg);
430
+ lens[i] = RSTRING_LEN(arg);
431
+ } else {
432
+ rb_raise(rb_eTypeError, "dispatch argument %ld must be a BufferHandle or packed String", i);
433
+ }
434
+ }
435
+
436
+ Check_Type(grid, T_ARRAY);
437
+ Check_Type(block, T_ARRAY);
438
+ long gx = NUM2LONG(rb_ary_entry(grid, 0));
439
+ long gy = NUM2LONG(rb_ary_entry(grid, 1));
440
+ long gz = NUM2LONG(rb_ary_entry(grid, 2));
441
+ long bx = NUM2LONG(rb_ary_entry(block, 0));
442
+ long by = NUM2LONG(rb_ary_entry(block, 1));
443
+ long bz = NUM2LONG(rb_ary_entry(block, 2));
444
+
445
+ char *error = NULL;
446
+ int rc = gpu_bridge_dispatch(pso->handle, kinds, ptrs, lens, (int32_t)argc,
447
+ gx, gy, gz, bx, by, bz,
448
+ RTEST(wait) ? 1 : 0, &error);
449
+ RB_GC_GUARD(args);
450
+ if (rc != 0) {
451
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "kernel dispatch failed");
452
+ if (error) free(error);
453
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
454
+ }
455
+ return Qnil;
456
+ }
457
+
458
+ static VALUE
459
+ bridge_synchronize(VALUE self)
460
+ {
461
+ if (gpu_bridge_synchronize_checked == NULL) {
462
+ gpu_bridge_synchronize();
463
+ return Qnil;
464
+ }
465
+
466
+ char *error = NULL;
467
+ if (gpu_bridge_synchronize_checked(&error) != 0) {
468
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "asynchronous work failed");
469
+ if (error) free(error);
470
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
471
+ }
472
+ return Qnil;
473
+ }
474
+
475
+ /* Whether the bridge has a native offload path; Ruby uses it to pick a path. */
476
+ static VALUE
477
+ bridge_offload_available_p(VALUE self)
478
+ {
479
+ return (gpu_bridge_buffer_save != NULL && gpu_bridge_buffer_load != NULL) ? Qtrue : Qfalse;
480
+ }
481
+
482
+ static VALUE
483
+ bridge_buffer_save(VALUE self, VALUE buf, VALUE path, VALUE bytesize, VALUE offset)
484
+ {
485
+ if (gpu_bridge_buffer_save == NULL) rb_raise(rb_eNotImpError, "native offload is not available");
486
+
487
+ bridge_buffer_t *data = buffer_unwrap(buf);
488
+ char *error = NULL;
489
+ if (gpu_bridge_buffer_save(buffer_ptr(data), StringValueCStr(path),
490
+ NUM2LONG(bytesize), NUM2LONG(offset), &error) != 0) {
491
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "offload save failed");
492
+ if (error) free(error);
493
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
494
+ }
495
+ return Qnil;
496
+ }
497
+
498
+ static VALUE
499
+ bridge_buffer_load(VALUE self, VALUE buf, VALUE path, VALUE bytesize, VALUE offset)
500
+ {
501
+ if (gpu_bridge_buffer_load == NULL) rb_raise(rb_eNotImpError, "native offload is not available");
502
+
503
+ bridge_buffer_t *data = buffer_unwrap(buf);
504
+ char *error = NULL;
505
+ if (gpu_bridge_buffer_load(buffer_ptr(data), StringValueCStr(path),
506
+ NUM2LONG(bytesize), NUM2LONG(offset), &error) != 0) {
507
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "offload load failed");
508
+ if (error) free(error);
509
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
510
+ }
511
+ return Qnil;
512
+ }
513
+
514
+ static VALUE
515
+ bridge_transfer_available_p(VALUE self)
516
+ {
517
+ return (gpu_bridge_transfer_begin != NULL && gpu_bridge_transfer_poll != NULL &&
518
+ gpu_bridge_transfer_end != NULL) ? Qtrue : Qfalse;
519
+ }
520
+
521
+ /* A transfer handle is an opaque pointer. Ruby only touches it at the start
522
+ and the end, so it is returned as an Integer rather than TypedData (GC
523
+ freeing it would pull it out from under the running worker thread). */
524
+ static VALUE
525
+ bridge_transfer_begin(VALUE self, VALUE buf, VALUE path, VALUE bytesize, VALUE offset, VALUE dir)
526
+ {
527
+ if (gpu_bridge_transfer_begin == NULL) rb_raise(rb_eNotImpError, "async offload is not available");
528
+
529
+ bridge_buffer_t *data = buffer_unwrap(buf);
530
+ void *t = gpu_bridge_transfer_begin(buffer_ptr(data), StringValueCStr(path),
531
+ NUM2LONG(bytesize), NUM2LONG(offset), NUM2INT(dir));
532
+ if (t == NULL) rb_raise(rb_eRuntimeError, "failed to start the offload transfer");
533
+ return ULL2NUM((unsigned long long)(uintptr_t)t);
534
+ }
535
+
536
+ static VALUE
537
+ bridge_transfer_poll(VALUE self, VALUE t)
538
+ {
539
+ if (gpu_bridge_transfer_poll == NULL) rb_raise(rb_eNotImpError, "async offload is not available");
540
+
541
+ return INT2NUM(gpu_bridge_transfer_poll((void *)(uintptr_t)NUM2ULL(t)));
542
+ }
543
+
544
+ static VALUE
545
+ bridge_transfer_end(VALUE self, VALUE t)
546
+ {
547
+ if (gpu_bridge_transfer_end == NULL) rb_raise(rb_eNotImpError, "async offload is not available");
548
+
549
+ char *error = NULL;
550
+ if (gpu_bridge_transfer_end((void *)(uintptr_t)NUM2ULL(t), &error) != 0) {
551
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "offload transfer failed");
552
+ if (error) free(error);
553
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
554
+ }
555
+ return Qnil;
556
+ }
557
+
558
+ static VALUE
559
+ bridge_random_available_p(VALUE self)
560
+ {
561
+ return (gpu_bridge_random != NULL &&
562
+ (gpu_bridge_random_available == NULL || gpu_bridge_random_available())) ? Qtrue : Qfalse;
563
+ }
564
+
565
+ static VALUE
566
+ bridge_random(VALUE self, VALUE buf, VALUE n, VALUE kind, VALUE seed, VALUE mean, VALUE stddev)
567
+ {
568
+ if (gpu_bridge_random == NULL) rb_raise(rb_eNotImpError, "no RNG library is available");
569
+
570
+ bridge_buffer_t *data = buffer_unwrap(buf);
571
+ char *error = NULL;
572
+ if (gpu_bridge_random(buffer_ptr(data), NUM2LONG(n), NUM2INT(kind), NUM2ULL(seed),
573
+ (float)NUM2DBL(mean), (float)NUM2DBL(stddev), &error) != 0) {
574
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "random generation failed");
575
+ if (error) free(error);
576
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
577
+ }
578
+ return Qnil;
579
+ }
580
+
581
+ static VALUE
582
+ bridge_conv2d_available_p(VALUE self)
583
+ {
584
+ return (gpu_bridge_conv2d != NULL &&
585
+ (gpu_bridge_conv2d_available == NULL || gpu_bridge_conv2d_available())) ? Qtrue : Qfalse;
586
+ }
587
+
588
+ static VALUE
589
+ bridge_conv2d_backward_available_p(VALUE self)
590
+ {
591
+ return (gpu_bridge_conv2d_backward != NULL &&
592
+ (gpu_bridge_conv2d_backward_available == NULL || gpu_bridge_conv2d_backward_available())) ? Qtrue : Qfalse;
593
+ }
594
+
595
+ /* Many arguments, so Ruby passes an Array (dims has 14 entries: pad/stride
596
+ followed by dilation_h, dilation_w, groups). */
597
+ static VALUE
598
+ bridge_conv2d(VALUE self, VALUE x, VALUE w, VALUE y, VALUE dims, VALUE dtype)
599
+ {
600
+ if (gpu_bridge_conv2d == NULL) rb_raise(rb_eNotImpError, "no convolution library is available");
601
+
602
+ Check_Type(dims, T_ARRAY);
603
+ if (RARRAY_LEN(dims) != 14) rb_raise(rb_eArgError, "dims must have 14 elements");
604
+ int d[14];
605
+ for (int i = 0; i < 14; i++) d[i] = NUM2INT(rb_ary_entry(dims, i));
606
+
607
+ bridge_buffer_t *dx = buffer_unwrap(x);
608
+ bridge_buffer_t *dw = buffer_unwrap(w);
609
+ bridge_buffer_t *dy = buffer_unwrap(y);
610
+ char *error = NULL;
611
+ if (gpu_bridge_conv2d(buffer_ptr(dx), buffer_ptr(dw), buffer_ptr(dy),
612
+ d[0], d[1], d[2], d[3], d[4], d[5], d[6],
613
+ d[7], d[8], d[9], d[10], d[11], d[12], d[13],
614
+ NUM2INT(dtype), &error) != 0) {
615
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "conv2d failed");
616
+ if (error) free(error);
617
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
618
+ }
619
+ return Qnil;
620
+ }
621
+
622
+ static VALUE
623
+ bridge_conv2d_backward(VALUE self, VALUE a, VALUE b, VALUE out, VALUE dims, VALUE dtype, VALUE mode)
624
+ {
625
+ if (gpu_bridge_conv2d_backward == NULL) {
626
+ rb_raise(rb_eNotImpError, "no convolution library is available");
627
+ }
628
+
629
+ Check_Type(dims, T_ARRAY);
630
+ if (RARRAY_LEN(dims) != 14) rb_raise(rb_eArgError, "dims must have 14 elements");
631
+ int d[14];
632
+ for (int i = 0; i < 14; i++) d[i] = NUM2INT(rb_ary_entry(dims, i));
633
+
634
+ bridge_buffer_t *da = buffer_unwrap(a);
635
+ bridge_buffer_t *db = buffer_unwrap(b);
636
+ bridge_buffer_t *dout = buffer_unwrap(out);
637
+ char *error = NULL;
638
+ if (gpu_bridge_conv2d_backward(buffer_ptr(da), buffer_ptr(db), buffer_ptr(dout),
639
+ d[0], d[1], d[2], d[3], d[4], d[5], d[6],
640
+ d[7], d[8], d[9], d[10], d[11], d[12], d[13],
641
+ NUM2INT(dtype), NUM2INT(mode), &error) != 0) {
642
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "conv2d_backward failed");
643
+ if (error) free(error);
644
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
645
+ }
646
+ return Qnil;
647
+ }
648
+
649
+ static VALUE
650
+ bridge_fft_available_p(VALUE self)
651
+ {
652
+ return (gpu_bridge_fft != NULL &&
653
+ (gpu_bridge_fft_available == NULL || gpu_bridge_fft_available())) ? Qtrue : Qfalse;
654
+ }
655
+
656
+ static VALUE
657
+ bridge_fft(VALUE self, VALUE in, VALUE out, VALUE n, VALUE batch, VALUE kind, VALUE direction)
658
+ {
659
+ if (gpu_bridge_fft == NULL) rb_raise(rb_eNotImpError, "no FFT library is available");
660
+
661
+ bridge_buffer_t *di = buffer_unwrap(in);
662
+ bridge_buffer_t *dobj = buffer_unwrap(out);
663
+ char *error = NULL;
664
+ if (gpu_bridge_fft(buffer_ptr(di), buffer_ptr(dobj), NUM2LONG(n), NUM2LONG(batch),
665
+ NUM2INT(kind), NUM2INT(direction), &error) != 0) {
666
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "fft failed");
667
+ if (error) free(error);
668
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
669
+ }
670
+ return Qnil;
671
+ }
672
+
673
+ static VALUE
674
+ bridge_gemm_available_p(VALUE self)
675
+ {
676
+ return (gpu_bridge_gemm != NULL &&
677
+ (gpu_bridge_gemm_available == NULL || gpu_bridge_gemm_available())) ? Qtrue : Qfalse;
678
+ }
679
+
680
+ static VALUE
681
+ bridge_gemm(VALUE self, VALUE a, VALUE b, VALUE c, VALUE m, VALUE k, VALUE n,
682
+ VALUE tf32, VALUE in_dtype)
683
+ {
684
+ if (gpu_bridge_gemm == NULL) rb_raise(rb_eNotImpError, "no GEMM library is available");
685
+
686
+ bridge_buffer_t *da = buffer_unwrap(a);
687
+ bridge_buffer_t *db = buffer_unwrap(b);
688
+ bridge_buffer_t *dc = buffer_unwrap(c);
689
+ char *error = NULL;
690
+ if (gpu_bridge_gemm(da->handle, da->offset, db->handle, db->offset, dc->handle, dc->offset,
691
+ NUM2LONG(m), NUM2LONG(k), NUM2LONG(n),
692
+ RTEST(tf32) ? 1 : 0, NUM2INT(in_dtype), &error) != 0) {
693
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "gemm failed");
694
+ if (error) free(error);
695
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
696
+ }
697
+ return Qnil;
698
+ }
699
+
700
+ static VALUE
701
+ bridge_gemm_batched_available_p(VALUE self)
702
+ {
703
+ return gpu_bridge_gemm_strided_batched != NULL ? Qtrue : Qfalse;
704
+ }
705
+
706
+ static VALUE
707
+ bridge_matrix_core_available_p(VALUE self)
708
+ {
709
+ return gpu_bridge_matrix_core_available != NULL && gpu_bridge_matrix_core_available() ? Qtrue : Qfalse;
710
+ }
711
+
712
+ static VALUE
713
+ bridge_matrix_core_gemm(VALUE self, VALUE a, VALUE b, VALUE c, VALUE m, VALUE k, VALUE n)
714
+ {
715
+ if (gpu_bridge_matrix_core_gemm == NULL) rb_raise(rb_eNotImpError, "no matrix-core implementation is available");
716
+ bridge_buffer_t *da = buffer_unwrap(a);
717
+ bridge_buffer_t *db = buffer_unwrap(b);
718
+ bridge_buffer_t *dc = buffer_unwrap(c);
719
+ char *error = NULL;
720
+ if (gpu_bridge_matrix_core_gemm(da->handle, da->offset, db->handle, db->offset,
721
+ dc->handle, dc->offset, NUM2LONG(m), NUM2LONG(k), NUM2LONG(n),
722
+ &error) != 0) {
723
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "matrix-core GEMM failed");
724
+ if (error) free(error);
725
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
726
+ }
727
+ return Qnil;
728
+ }
729
+
730
+ /* 17 arguments exceed rb_define_module_function's fixed arity limit (15),
731
+ so this takes argc/argv. The order matches the bridge's declaration. */
732
+ static VALUE
733
+ bridge_gemm_batched(int argc, VALUE *argv, VALUE self)
734
+ {
735
+ if (gpu_bridge_gemm_strided_batched == NULL) {
736
+ rb_raise(rb_eNotImpError, "no batched GEMM library is available");
737
+ }
738
+ if (argc != 18) rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 18)", argc);
739
+
740
+ bridge_buffer_t *da = buffer_unwrap(argv[0]);
741
+ bridge_buffer_t *db = buffer_unwrap(argv[1]);
742
+ bridge_buffer_t *dc = buffer_unwrap(argv[2]);
743
+ char *error = NULL;
744
+ if (gpu_bridge_gemm_strided_batched(da->handle, da->offset, db->handle, db->offset,
745
+ dc->handle, dc->offset,
746
+ NUM2LONG(argv[3]), NUM2LONG(argv[4]), NUM2LONG(argv[5]),
747
+ NUM2LONG(argv[6]), NUM2LONG(argv[7]), NUM2LONG(argv[8]),
748
+ NUM2LONG(argv[9]), NUM2LONG(argv[10]), NUM2LONG(argv[11]),
749
+ RTEST(argv[12]) ? 1 : 0, RTEST(argv[13]) ? 1 : 0,
750
+ NUM2LONG(argv[14]), NUM2DBL(argv[15]),
751
+ RTEST(argv[16]) ? 1 : 0,
752
+ NUM2INT(argv[17]), &error) != 0) {
753
+ VALUE msg = rb_utf8_str_new_cstr(error ? error : "batched gemm failed");
754
+ if (error) free(error);
755
+ rb_exc_raise(rb_exc_new_str(rb_eRuntimeError, msg));
756
+ }
757
+ return Qnil;
758
+ }
759
+
760
+ /* Returns raw device facts only. Turning them into the Tilework.capabilities
761
+ contract is Ruby's policy (lib/tilework.rb). */
762
+ static VALUE
763
+ bridge_device_capabilities(VALUE self)
764
+ {
765
+ if (gpu_bridge_device_capabilities == NULL) return Qnil;
766
+
767
+ int32_t warp_size = 0, cc_major = 0, cc_minor = 0, integrated = 0;
768
+ long memory_budget = 0;
769
+ if (gpu_bridge_device_capabilities(&warp_size, &cc_major, &cc_minor,
770
+ &integrated, &memory_budget) != 0) {
771
+ return Qnil;
772
+ }
773
+
774
+ VALUE h = rb_hash_new();
775
+ rb_hash_aset(h, ID2SYM(rb_intern("warp_size")), INT2NUM(warp_size));
776
+ rb_hash_aset(h, ID2SYM(rb_intern("compute_capability")),
777
+ rb_ary_new_from_args(2, INT2NUM(cc_major), INT2NUM(cc_minor)));
778
+ rb_hash_aset(h, ID2SYM(rb_intern("unified_memory")), integrated ? Qtrue : Qfalse);
779
+ rb_hash_aset(h, ID2SYM(rb_intern("memory_budget_bytes")), LONG2NUM(memory_budget));
780
+ return h;
781
+ }
782
+
783
+ void
784
+ Init_bridge(void)
785
+ {
786
+ VALUE mTilework = rb_define_module("Tilework");
787
+ VALUE mBridge = rb_define_module_under(mTilework, TILEWORK_BRIDGE_MODULE);
788
+
789
+ cBufferHandle = rb_define_class_under(mBridge, "BufferHandle", rb_cObject);
790
+ rb_undef_alloc_func(cBufferHandle);
791
+ cPipelineHandle = rb_define_class_under(mBridge, "PipelineHandle", rb_cObject);
792
+ rb_undef_alloc_func(cPipelineHandle);
793
+ cHostBackingHandle = rb_define_class_under(mBridge, "HostBackingHandle", rb_cObject);
794
+ rb_undef_alloc_func(cHostBackingHandle);
795
+
796
+ rb_define_module_function(mBridge, "device_name", bridge_device_name, 0);
797
+ rb_define_module_function(mBridge, "device_available?", bridge_device_available_p, 0);
798
+ rb_define_module_function(mBridge, "device_info", bridge_device_info, 0);
799
+ rb_define_module_function(mBridge, "buffer_alloc", bridge_buffer_alloc, 1);
800
+ rb_define_module_function(mBridge, "buffer_write", bridge_buffer_write, 3);
801
+ rb_define_module_function(mBridge, "buffer_read", bridge_buffer_read, 3);
802
+ rb_define_module_function(mBridge, "buffer_free", bridge_buffer_free, 1);
803
+ rb_define_module_function(mBridge, "buffer_view", bridge_buffer_view, 2);
804
+ rb_define_module_function(mBridge, "host_backing_available?", bridge_host_backing_available_p, 0);
805
+ rb_define_module_function(mBridge, "host_backing_alloc", bridge_host_backing_alloc, 1);
806
+ rb_define_module_function(mBridge, "host_backing_free", bridge_host_backing_free, 1);
807
+ rb_define_module_function(mBridge, "host_backing_store", bridge_host_backing_store, 4);
808
+ rb_define_module_function(mBridge, "host_backing_restore", bridge_host_backing_restore, 4);
809
+ rb_define_module_function(mBridge, "host_backing_async_available?", bridge_host_backing_async_available_p, 0);
810
+ rb_define_module_function(mBridge, "host_backing_transfer_begin", bridge_host_backing_transfer_begin, 4);
811
+ rb_define_module_function(mBridge, "host_backing_transfer_poll", bridge_host_backing_transfer_poll, 1);
812
+ rb_define_module_function(mBridge, "host_backing_transfer_end", bridge_host_backing_transfer_end, 1);
813
+ rb_define_module_function(mBridge, "pipeline_create", bridge_pipeline_create, 2);
814
+ rb_define_module_function(mBridge, "pipeline_max_threads", bridge_pipeline_max_threads, 1);
815
+ rb_define_module_function(mBridge, "dispatch", bridge_dispatch, 5);
816
+ rb_define_module_function(mBridge, "synchronize", bridge_synchronize, 0);
817
+ rb_define_module_function(mBridge, "device_capabilities", bridge_device_capabilities, 0);
818
+ rb_define_module_function(mBridge, "offload_available?", bridge_offload_available_p, 0);
819
+ rb_define_module_function(mBridge, "buffer_save", bridge_buffer_save, 4);
820
+ rb_define_module_function(mBridge, "buffer_load", bridge_buffer_load, 4);
821
+ rb_define_module_function(mBridge, "transfer_available?", bridge_transfer_available_p, 0);
822
+ rb_define_module_function(mBridge, "transfer_begin", bridge_transfer_begin, 5);
823
+ rb_define_module_function(mBridge, "transfer_poll", bridge_transfer_poll, 1);
824
+ rb_define_module_function(mBridge, "transfer_end", bridge_transfer_end, 1);
825
+ rb_define_module_function(mBridge, "conv2d_available?", bridge_conv2d_available_p, 0);
826
+ rb_define_module_function(mBridge, "conv2d", bridge_conv2d, 5);
827
+ rb_define_module_function(mBridge, "conv2d_backward", bridge_conv2d_backward, 6);
828
+ rb_define_module_function(mBridge, "conv2d_backward_available?", bridge_conv2d_backward_available_p, 0);
829
+ rb_define_module_function(mBridge, "fft_available?", bridge_fft_available_p, 0);
830
+ rb_define_module_function(mBridge, "fft", bridge_fft, 6);
831
+ rb_define_module_function(mBridge, "random_available?", bridge_random_available_p, 0);
832
+ rb_define_module_function(mBridge, "random", bridge_random, 6);
833
+ rb_define_module_function(mBridge, "gemm_available?", bridge_gemm_available_p, 0);
834
+ rb_define_module_function(mBridge, "gemm", bridge_gemm, 8);
835
+ rb_define_module_function(mBridge, "gemm_batched_available?", bridge_gemm_batched_available_p, 0);
836
+ rb_define_module_function(mBridge, "gemm_batched", bridge_gemm_batched, -1);
837
+ rb_define_module_function(mBridge, "matrix_core_available?", bridge_matrix_core_available_p, 0);
838
+ rb_define_module_function(mBridge, "matrix_core_gemm", bridge_matrix_core_gemm, 6);
839
+ rb_set_end_proc(bridge_shutdown, Qnil);
840
+ }