cuda-rust-wasm 0.1.7

CUDA to Rust transpiler with WebGPU/WASM support
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# CUDA-Rust-WASM API Documentation


## Table of Contents


1. [Core API]#core-api
2. [Transpiler API]#transpiler-api
3. [Runtime API]#runtime-api
4. [Memory Management]#memory-management
5. [Kernel Execution]#kernel-execution
6. [WebGPU Integration]#webgpu-integration
7. [Error Handling]#error-handling
8. [Performance Profiling]#performance-profiling

## Core API


### `transpileCuda(code: string, options?: TranspileOptions): Promise<TranspileResult>`


Transpiles CUDA source code to WebAssembly or WebGPU shader code.

#### Parameters


- `code` (string): The CUDA source code to transpile
- `options` (TranspileOptions): Optional configuration object

#### TranspileOptions


```typescript
interface TranspileOptions {
  target?: 'wasm' | 'webgpu';      // Target platform (default: 'wasm')
  optimize?: boolean;               // Enable optimizations (default: false)
  profile?: boolean;                // Generate profiling data (default: false)
  preserveComments?: boolean;       // Keep comments in output (default: false)
  sourceMaps?: boolean;            // Generate source maps (default: false)
  maxThreadsPerBlock?: number;     // Maximum threads per block (default: 1024)
  sharedMemorySize?: number;       // Shared memory size in bytes (default: 49152)
}
```

#### TranspileResult


```typescript
interface TranspileResult {
  code: string;                    // Transpiled code
  wasmBinary?: Uint8Array;        // Compiled WASM binary
  profile?: ProfileData;          // Profiling information
  sourceMap?: string;             // Source map data
  warnings?: Warning[];           // Transpilation warnings
  metadata?: {
    kernelCount: number;
    sharedMemoryUsage: number;
    registerUsage: number;
    threadConfiguration: ThreadConfig;
  };
}
```

#### Example


```javascript
const result = await transpileCuda(`
  __global__ void vectorAdd(float* a, float* b, float* c, int n) {
    int tid = blockIdx.x * blockDim.x + threadIdx.x;
    if (tid < n) {
      c[tid] = a[tid] + b[tid];
    }
  }
`, {
  target: 'wasm',
  optimize: true,
  profile: true
});

console.log('Transpiled code:', result.code);
console.log('Binary size:', result.wasmBinary.length);
console.log('Parse time:', result.profile.parseTime);
```

### `analyzeKernel(code: string): Promise<KernelAnalysis>`


Analyzes a CUDA kernel for performance characteristics and optimization opportunities.

#### Parameters


- `code` (string): The CUDA kernel source code

#### KernelAnalysis


```typescript
interface KernelAnalysis {
  memoryPattern: 'coalesced' | 'strided' | 'random';
  threadUtilization: number;        // Percentage (0-100)
  sharedMemoryUsage: number;        // Bytes
  registerUsage: number;            // Per thread
  occupancy: number;                // Theoretical occupancy (0-1)
  suggestions: OptimizationSuggestion[];
  bottlenecks: Bottleneck[];
  metrics: {
    arithmeticIntensity: number;
    memoryBandwidth: number;
    computeThroughput: number;
  };
}

interface OptimizationSuggestion {
  type: 'memory' | 'compute' | 'synchronization';
  severity: 'low' | 'medium' | 'high';
  description: string;
  impact: string;
  implementation: string;
}
```

#### Example


```javascript
const analysis = await analyzeKernel(`
  __global__ void matrixMultiply(float* A, float* B, float* C, int N) {
    int row = blockIdx.y * blockDim.y + threadIdx.y;
    int col = blockIdx.x * blockDim.x + threadIdx.x;
    
    if (row < N && col < N) {
      float sum = 0.0f;
      for (int k = 0; k < N; k++) {
        sum += A[row * N + k] * B[k * N + col];
      }
      C[row * N + col] = sum;
    }
  }
`);

console.log('Memory pattern:', analysis.memoryPattern);
console.log('Thread utilization:', analysis.threadUtilization + '%');
analysis.suggestions.forEach(s => {
  console.log(`${s.severity}: ${s.description}`);
});
```

## Transpiler API


### `CudaRust` Class


The main transpiler class for converting CUDA to Rust/WebAssembly.

```typescript
class CudaRust {
  constructor(config?: TranspilerConfig);
  
  // Parse CUDA source code
  parse(source: string): Promise<CudaAST>;
  
  // Transpile to target language
  transpile(source: string, target?: Target): Promise<string>;
  
  // Optimize transpiled code
  optimize(code: string, level?: OptimizationLevel): Promise<string>;
  
  // Validate CUDA code
  validate(source: string): Promise<ValidationResult>;
}
```

### `Parser` Class


Low-level CUDA/PTX parser.

```typescript
class Parser {
  // Parse CUDA C++ code
  parseCuda(source: string): CudaAST;
  
  // Parse PTX assembly
  parsePtx(source: string): PtxAST;
  
  // Extract kernels from source
  extractKernels(source: string): KernelDefinition[];
  
  // Get syntax errors
  getErrors(): SyntaxError[];
}
```

## Runtime API


### `Runtime` Class


Manages kernel execution and device resources.

```typescript
class Runtime {
  constructor(backend?: Backend);
  
  // Device management
  getDevice(id?: number): Promise<Device>;
  getDeviceCount(): Promise<number>;
  setDevice(id: number): Promise<void>;
  
  // Kernel compilation
  compileKernel(code: string, name: string): Promise<Kernel>;
  
  // Memory allocation
  allocate(size: number, type?: MemoryType): Promise<DeviceMemory>;
  
  // Stream management
  createStream(): Promise<Stream>;
  synchronize(): Promise<void>;
  
  // Profiling
  startProfiling(): void;
  stopProfiling(): ProfileReport;
}
```

### `Kernel` Class


Represents a compiled GPU kernel.

```typescript
class Kernel {
  // Launch configuration
  setBlockDim(x: number, y?: number, z?: number): void;
  setGridDim(x: number, y?: number, z?: number): void;
  setSharedMemory(bytes: number): void;
  
  // Parameter binding
  setArg(index: number, value: any): void;
  setBuffer(index: number, buffer: DeviceMemory): void;
  setTexture(index: number, texture: Texture): void;
  
  // Execution
  launch(stream?: Stream): Promise<void>;
  launchAsync(stream?: Stream): void;
  
  // Profiling
  getExecutionTime(): Promise<number>;
  getOccupancy(): number;
}
```

## Memory Management


### `DeviceMemory` Class


Manages GPU memory allocations.

```typescript
class DeviceMemory {
  constructor(size: number, type?: MemoryType);
  
  // Data transfer
  copyFrom(data: ArrayBuffer): Promise<void>;
  copyTo(buffer: ArrayBuffer): Promise<void>;
  copyFromAsync(data: ArrayBuffer, stream: Stream): void;
  copyToAsync(buffer: ArrayBuffer, stream: Stream): void;
  
  // Memory operations
  memset(value: number): Promise<void>;
  memcpy(src: DeviceMemory, size?: number): Promise<void>;
  
  // Properties
  size: number;
  type: MemoryType;
  device: Device;
}
```

### `UnifiedMemory` Class


Provides unified memory accessible from both host and device.

```typescript
class UnifiedMemory extends DeviceMemory {
  // Direct access
  getHostPointer(): ArrayBuffer;
  prefetch(device: Device): Promise<void>;
  advise(advice: MemoryAdvice): void;
}
```

### Memory Types


```typescript
enum MemoryType {
  Device = 'device',        // Device-only memory
  Host = 'host',           // Host-only memory
  Unified = 'unified',     // Unified memory
  Pinned = 'pinned',       // Pinned host memory
  Shared = 'shared'        // Shared memory
}

enum MemoryAdvice {
  ReadMostly = 'read_mostly',
  PreferredLocation = 'preferred_location',
  AccessedBy = 'accessed_by'
}
```

## Kernel Execution


### Launch Configuration


```typescript
interface LaunchConfig {
  gridDim: Dim3;
  blockDim: Dim3;
  sharedMemory?: number;
  stream?: Stream;
}

interface Dim3 {
  x: number;
  y?: number;
  z?: number;
}
```

### Execution Example


```javascript
// Compile kernel
const kernel = await runtime.compileKernel(cudaCode, 'vectorAdd');

// Allocate memory
const n = 1024 * 1024;
const size = n * 4; // float32
const d_a = await runtime.allocate(size);
const d_b = await runtime.allocate(size);
const d_c = await runtime.allocate(size);

// Copy input data
await d_a.copyFrom(hostArrayA);
await d_b.copyFrom(hostArrayB);

// Configure kernel
kernel.setBlockDim(256);
kernel.setGridDim(Math.ceil(n / 256));

// Set kernel arguments
kernel.setBuffer(0, d_a);
kernel.setBuffer(1, d_b);
kernel.setBuffer(2, d_c);
kernel.setArg(3, n);

// Launch kernel
await kernel.launch();

// Copy results back
await d_c.copyTo(hostArrayC);
```

## WebGPU Integration


### `WebGPUKernel` Class


Specialized kernel for WebGPU execution.

```typescript
class WebGPUKernel {
  constructor(device: GPUDevice, code: string);
  
  // Buffer management
  createBuffer(size: number, usage?: GPUBufferUsage): GPUBuffer;
  setBuffer(index: number, buffer: GPUBuffer): void;
  
  // Execution
  dispatch(x: number, y?: number, z?: number): Promise<void>;
  
  // Data transfer
  readBuffer(index: number): Promise<ArrayBuffer>;
  writeBuffer(index: number, data: ArrayBuffer): Promise<void>;
  
  // Pipeline state
  setPushConstants(data: ArrayBuffer): void;
  setBindGroup(index: number, bindGroup: GPUBindGroup): void;
}
```

### WebGPU Example


```javascript
// Initialize WebGPU
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();

// Create kernel
const kernel = await createWebGPUKernel(`
  @group(0) @binding(0) var<storage, read> a: array<f32>;
  @group(0) @binding(1) var<storage, read> b: array<f32>;
  @group(0) @binding(2) var<storage, read_write> c: array<f32>;
  
  @compute @workgroup_size(256)
  fn main(@builtin(global_invocation_id) id: vec3<u32>) {
    let i = id.x;
    if (i < arrayLength(&a)) {
      c[i] = a[i] + b[i];
    }
  }
`);

// Create buffers
const size = 1024 * 4;
const bufferA = kernel.createBuffer(size, GPUBufferUsage.STORAGE);
const bufferB = kernel.createBuffer(size, GPUBufferUsage.STORAGE);
const bufferC = kernel.createBuffer(size, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC);

// Write data
await kernel.writeBuffer(0, dataA);
await kernel.writeBuffer(1, dataB);

// Execute
await kernel.dispatch(1024 / 256);

// Read results
const results = await kernel.readBuffer(2);
```

## Error Handling


### Error Types


```typescript
class CudaRustError extends Error {
  code: ErrorCode;
  location?: SourceLocation;
  suggestions?: string[];
}

enum ErrorCode {
  // Parser errors
  SYNTAX_ERROR = 'SYNTAX_ERROR',
  UNSUPPORTED_FEATURE = 'UNSUPPORTED_FEATURE',
  
  // Runtime errors
  OUT_OF_MEMORY = 'OUT_OF_MEMORY',
  INVALID_KERNEL = 'INVALID_KERNEL',
  LAUNCH_FAILED = 'LAUNCH_FAILED',
  
  // Transpiler errors
  TYPE_MISMATCH = 'TYPE_MISMATCH',
  UNDEFINED_SYMBOL = 'UNDEFINED_SYMBOL'
}
```

### Error Handling Example


```javascript
try {
  const result = await transpileCuda(cudaCode);
} catch (error) {
  if (error instanceof CudaRustError) {
    console.error(`Error: ${error.message}`);
    console.error(`Code: ${error.code}`);
    
    if (error.location) {
      console.error(`Location: ${error.location.line}:${error.location.column}`);
    }
    
    if (error.suggestions) {
      console.error('Suggestions:');
      error.suggestions.forEach(s => console.error(`  - ${s}`));
    }
  }
}
```

## Performance Profiling


### `Profiler` Class


```typescript
class Profiler {
  // Start/stop profiling
  start(): void;
  stop(): ProfileReport;
  
  // Mark events
  markEvent(name: string): void;
  beginRange(name: string): void;
  endRange(name: string): void;
  
  // Get metrics
  getKernelMetrics(kernel: Kernel): KernelMetrics;
  getMemoryMetrics(): MemoryMetrics;
  getOverallMetrics(): OverallMetrics;
}
```

### Profile Report


```typescript
interface ProfileReport {
  totalTime: number;
  kernelTime: number;
  memoryTime: number;
  events: ProfileEvent[];
  kernels: KernelProfile[];
  memory: MemoryProfile;
}

interface KernelProfile {
  name: string;
  calls: number;
  totalTime: number;
  avgTime: number;
  minTime: number;
  maxTime: number;
  occupancy: number;
  throughput: number;
}
```

### Profiling Example


```javascript
// Start profiling
runtime.startProfiling();

// Execute kernels
for (let i = 0; i < 100; i++) {
  profiler.beginRange('iteration');
  
  await kernel1.launch();
  profiler.markEvent('kernel1_complete');
  
  await kernel2.launch();
  profiler.markEvent('kernel2_complete');
  
  profiler.endRange('iteration');
}

// Get report
const report = runtime.stopProfiling();

console.log('Total execution time:', report.totalTime, 'ms');
console.log('Kernel execution time:', report.kernelTime, 'ms');
console.log('Memory transfer time:', report.memoryTime, 'ms');

report.kernels.forEach(k => {
  console.log(`${k.name}: ${k.avgTime.toFixed(3)}ms avg (${k.calls} calls)`);
});
```

## Advanced Features


### Custom Memory Allocators


```typescript
interface MemoryAllocator {
  allocate(size: number): Promise<DeviceMemory>;
  deallocate(memory: DeviceMemory): Promise<void>;
  getUsage(): MemoryUsage;
}

class PoolAllocator implements MemoryAllocator {
  constructor(poolSize: number, blockSize: number);
  // ... implementation
}
```

### Kernel Fusion


```typescript
interface FusionOptions {
  strategy: 'horizontal' | 'vertical' | 'auto';
  maxKernels: number;
  preserveOrder: boolean;
}

async function fuseKernels(
  kernels: Kernel[], 
  options?: FusionOptions
): Promise<Kernel>;
```

### Multi-GPU Support


```typescript
interface MultiGPUConfig {
  devices: number[];
  strategy: 'data_parallel' | 'model_parallel' | 'pipeline';
  syncMode: 'blocking' | 'non_blocking';
}

class MultiGPURuntime extends Runtime {
  constructor(config: MultiGPUConfig);
  
  distributeWork(kernel: Kernel, data: DeviceMemory[]): Promise<void>;
  gather(results: DeviceMemory[]): Promise<DeviceMemory>;
}
```