# cuda { #tvbo.codegen.cuda }

`codegen.cuda`

CUDA Adapter for TVBO.

Run generated CUDA kernels using PyCUDA.

## Usage {.doc-section .doc-section-usage}

from tvbo import SimulationExperiment

exp = SimulationExperiment.from_file("experiment.yaml")
result = exp.run('cuda')

## Functions

| Name | Description |
| --- | --- |
| [compile_cuda](#tvbo.codegen.cuda.compile_cuda) | Compile CUDA kernel for experiment. |
| [run_cuda](#tvbo.codegen.cuda.run_cuda) | Run CUDA simulation for experiment. |
| [save_cuda](#tvbo.codegen.cuda.save_cuda) | Save CUDA source to file. |

### compile_cuda { #tvbo.codegen.cuda.compile_cuda }

```python
codegen.cuda.compile_cuda(experiment)
```

Compile CUDA kernel for experiment.

Compiled with `no_extern_c=True`. The kernel includes `<curand_kernel.h>` for its noise, and the block `SourceModule` otherwise wraps a whole source in gives those headers C linkage — which their templates may not have, so every compile failed with 33 errors out of curand rather than anything to do with the model. The kernels declare `extern "C"` themselves instead, which is what keeps `get_function` able to find them under their unmangled names.

#### Parameters {.doc-section .doc-section-parameters}

| Name       | Type                 | Description                   | Default    |
|------------|----------------------|-------------------------------|------------|
| experiment | SimulationExperiment | SimulationExperiment instance | _required_ |

#### Returns {.doc-section .doc-section-returns}

| Name   | Type              | Description                    |
|--------|-------------------|--------------------------------|
|        | tuple\[Any, Any\] | Tuple of (module, kernel_func) |

### run_cuda { #tvbo.codegen.cuda.run_cuda }

```python
codegen.cuda.run_cuda(
    experiment,
    n_steps=None,
    dt=None,
    n_work_items=1,
    global_speed=1.0,
    global_coupling=0.1,
    buffer_length=None,
    swept_params=None,
)
```

Run CUDA simulation for experiment.

All configuration comes from experiment metadata.

The kernel integrates in the model's own time unit — it multiplies *dt* straight into the model's equations — while indexing the delay ring as ``length / speed / dt``, which is millimetres over metres-per-second and so is milliseconds. The two agree only for a model whose time unit is ``ms``; anything else gets the right trajectory on the wrong delays, so *dt* is passed through in model units rather than converted onto either.

#### Parameters {.doc-section .doc-section-parameters}

| Name            | Type                            | Description                                                                   | Default    |
|-----------------|---------------------------------|-------------------------------------------------------------------------------|------------|
| experiment      | SimulationExperiment            | SimulationExperiment instance                                                 | _required_ |
| n_steps         | int \| None                     | Number of integration steps (default from experiment.integration)             | `None`     |
| dt              | float \| None                   | Integration time step in the model's own time unit (default: its `step_size`) | `None`     |
| n_work_items    | int                             | Number of parallel parameter configurations                                   | `1`        |
| global_speed    | float                           | Conduction speed (m/s)                                                        | `1.0`      |
| global_coupling | float                           | Global coupling strength                                                      | `0.1`      |
| buffer_length   | int \| None                     | History buffer length (auto-calculated if None)                               | `None`     |
| swept_params    | dict\[str, np.ndarray\] \| None | Dict mapping param names to arrays of values per work item                    | `None`     |

#### Returns {.doc-section .doc-section-returns}

| Name   | Type                    | Description                                 |
|--------|-------------------------|---------------------------------------------|
|        | dict\[str, np.ndarray\] | Dict with 'tavg', 'n_node', 'n_steps', 'dt' |

### save_cuda { #tvbo.codegen.cuda.save_cuda }

```python
codegen.cuda.save_cuda(experiment, path)
```

Save CUDA source to file.

#### Parameters {.doc-section .doc-section-parameters}

| Name       | Type                 | Description                   | Default    |
|------------|----------------------|-------------------------------|------------|
| experiment | SimulationExperiment | SimulationExperiment instance | _required_ |
| path       | str                  | Output file path (.cu)        | _required_ |

#### Returns {.doc-section .doc-section-returns}

| Name   | Type   | Description        |
|--------|--------|--------------------|
|        | str    | Path to saved file |