# Compute functions and execution tags

compute defines a portable data-parallel kernel. [auto], [cpu], or [gpu] states the admitted execution preference.

Last updated: 2026-09-09

Package: language | Status: experimental

Tags: [auto], [cpu], [gpu], auto, compute, compute.index, cpu, gameplay, gpu, hybrid, language, pipeline, yeho

Use compute for bounded element-wise or parallel work that can obey the restricted portable kernel contract.

```yh
compute Name(buffer of Type values, ...) { ... }
[gpu] compute Name(...) { ... }
```


## CPU · [cpu]

Apply damage to a small batch of health values on the explicit CPU route. Each invocation owns one element.

Verification: executed

```yh
[cpu]
compute ApplyDamage(buffer of int health, int damage)
{
    int index = compute.index
    if index >= health.count { return }
    health[index] = health[index] - damage
}

buffer of int health
health.Add(100)
health.Add(80)
health.Add(60)
computeTask job = dispatch ApplyDamage(health, 10)
wait job
if job.failed { Console.Error(job.error.message) Process.Exit(1) }
for index from 0 < health.count { Console.Log(Text.From(health[index])) }

```

Save this beside start.yh as project.mech:

```toml
manifestVersion = "2"
package = "api.example.language__compute_functions"
version = "0.1.0"
languageEdition = "yeho-core-2026.1"

[app]
kind = "headless"

```

Expected output:

```text
90
70
50
```

Executed backend: cpu-oracle

Observed output:

```text
90
70
50
```

Extract the example archive beside the yeho and dolphin checkouts. This example requires those source dependencies and a current developer compiler.

```sh
./yeho/build/dolphin-metal/yehoc ./api-examples/language--compute-functions/cpu --backend cpu-oracle -o /tmp/yeho-example
/tmp/yeho-example
```


## GPU · [gpu]

Double a batch of reward values on native Metal. The [gpu] tag requires an accelerator backend; CPU oracle cannot silently satisfy it.

Verification: executed

```yh
[gpu]
compute DoubleRewards(buffer of int rewards, int multiplier)
{
    int index = compute.index
    if index >= rewards.count { return }
    rewards[index] = rewards[index] * multiplier
}

buffer of int rewards
rewards.Add(100)
rewards.Add(80)
rewards.Add(60)
computeTask job = dispatch DoubleRewards(rewards, 2)
wait job
if job.failed { Console.Error(job.error.message) Process.Exit(1) }
for index from 0 < rewards.count { Console.Log(Text.From(rewards[index])) }

```

Save this beside start.yh as project.mech:

```toml
manifestVersion = "2"
package = "api.example.language__compute_functions"
version = "0.1.0"
languageEdition = "yeho-core-2026.1"

[app]
kind = "headless"

```

Expected output:

```text
200
160
120
```

Executed backend: metal

Observed output:

```text
200
160
120
```

Extract the example archive beside the yeho and dolphin checkouts. This example requires those source dependencies and a current developer compiler.

```sh
./yeho/build/dolphin-metal/yehoc ./api-examples/language--compute-functions/gpu --backend metal -o /tmp/yeho-example
/tmp/yeho-example
```


## Automatic · [auto]

Keep the same portable source for separate CPU and GPU builds. Choose --backend cpu-oracle or --backend metal explicitly; [auto] is not proof of GPU execution.

Verification: executed

```yh
[auto]
compute ApplyDamage(buffer of int health, int damage)
{
    int index = compute.index
    if index >= health.count { return }
    health[index] = health[index] - damage
}

buffer of int health
health.Add(100)
health.Add(80)
health.Add(60)
computeTask job = dispatch ApplyDamage(health, 10)
wait job
if job.failed { Console.Error(job.error.message) Process.Exit(1) }
for index from 0 < health.count { Console.Log(Text.From(health[index])) }

```

Save this beside start.yh as project.mech:

```toml
manifestVersion = "2"
package = "api.example.language__compute_functions"
version = "0.1.0"
languageEdition = "yeho-core-2026.1"

[app]
kind = "headless"

```

Expected output:

```text
90
70
50
```

Executed backend: metal

Observed output:

```text
90
70
50
```

Extract the example archive beside the yeho and dolphin checkouts. This example requires those source dependencies and a current developer compiler.

```sh
./yeho/build/dolphin-metal/yehoc ./api-examples/language--compute-functions/auto --backend metal -o /tmp/yeho-example
/tmp/yeho-example
```


## CPU + GPU · one pipeline

The CPU prepares rewards, the GPU doubles them, then the CPU totals the finished buffer. There is no [both] tag. Ordinary host code and a [gpu] kernel cooperate explicitly.

Verification: executed

```yh
[gpu]
compute DoubleRewards(buffer of int rewards)
{
    int index = compute.index
    if index >= rewards.count { return }
    rewards[index] = rewards[index] * 2
}

// CPU: prepare a small game reward batch.
buffer of int rewards
for level from 1 <= 4 { rewards.Add(level * 10) }

// GPU: process independent elements.
computeTask job = dispatch DoubleRewards(rewards)
wait job
if job.failed { Console.Error(job.error.message) Process.Exit(1) }

// CPU: consume only after the GPU has finished.
int total = 0
for index from 0 < rewards.count { total = total + rewards[index] }
Console.Log(Text.From(total))

```

Save this beside start.yh as project.mech:

```toml
manifestVersion = "2"
package = "api.example.language__compute_functions"
version = "0.1.0"
languageEdition = "yeho-core-2026.1"

[app]
kind = "headless"

```

Expected output:

```text
200
```

Executed backend: metal

Observed output:

```text
200
```

Extract the example archive beside the yeho and dolphin checkouts. This example requires those source dependencies and a current developer compiler.

```sh
./yeho/build/dolphin-metal/yehoc ./api-examples/language--compute-functions/hybrid --backend metal -o /tmp/yeho-example
/tmp/yeho-example
```


Exactly one execution tag is allowed.

A [gpu] request is not proof of GPU execution. Promotion evidence must name the actual backend and device path.

Source: yeho/parser_core.cpp parseTopLevelCompute; tests/compiler/accelerator/vulkan-positive/start.yh

AI training permission: https://demonhunterlabs.com/ai-use
