{"id":"language--compute-functions","name":"Compute functions and execution tags","owner":"yeho","package":"language","kind":"language","signature":"compute Name(buffer of Type values, ...) { ... }\n[gpu] compute Name(...) { ... }","description":"compute defines a portable data-parallel kernel. [auto], [cpu], or [gpu] states the admitted execution preference.","context":"Use compute for bounded element-wise or parallel work that can obey the restricted portable kernel contract.","parameters":[],"returns":"","members":[],"tags":["[auto]","[cpu]","[gpu]","auto","compute","compute.index","cpu","gameplay","gpu","hybrid","language","pipeline","yeho"],"updated":"2026-09-09","source":{"repository":"yeho","path":"parser_core.cpp parseTopLevelCompute; tests/compiler/accelerator/vulkan-positive/start.yh"},"status":"experimental","notes":["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."],"examples":[{"id":"cpu","title":"CPU · [cpu]","description":"Apply damage to a small batch of health values on the explicit CPU route. Each invocation owns one element.","code":"[cpu]\ncompute ApplyDamage(buffer of int health, int damage)\n{\n    int index = compute.index\n    if index >= health.count { return }\n    health[index] = health[index] - damage\n}\n\nbuffer of int health\nhealth.Add(100)\nhealth.Add(80)\nhealth.Add(60)\ncomputeTask job = dispatch ApplyDamage(health, 10)\nwait job\nif job.failed { Console.Error(job.error.message) Process.Exit(1) }\nfor index from 0 < health.count { Console.Log(Text.From(health[index])) }\n","tags":["cpu","compute","gameplay"],"run":"cpu-oracle","expected":"90\n70\n50","verifyRun":true,"manifest":"manifestVersion = \"2\"\npackage = \"api.example.language__compute_functions\"\nversion = \"0.1.0\"\nlanguageEdition = \"yeho-core-2026.1\"\n\n[app]\nkind = \"headless\"\n","verification":"executed","sha256":"27976cb7d10ff1b403ad898b85f39a2795a345a1ae22876ea1c76f59504b569c","checked":"2026-09-09","diagnostic":"","observedOutput":"90\n70\n50","checkedBackend":"cpu-oracle"},{"id":"gpu","title":"GPU · [gpu]","description":"Double a batch of reward values on native Metal. The [gpu] tag requires an accelerator backend; CPU oracle cannot silently satisfy it.","code":"[gpu]\ncompute DoubleRewards(buffer of int rewards, int multiplier)\n{\n    int index = compute.index\n    if index >= rewards.count { return }\n    rewards[index] = rewards[index] * multiplier\n}\n\nbuffer of int rewards\nrewards.Add(100)\nrewards.Add(80)\nrewards.Add(60)\ncomputeTask job = dispatch DoubleRewards(rewards, 2)\nwait job\nif job.failed { Console.Error(job.error.message) Process.Exit(1) }\nfor index from 0 < rewards.count { Console.Log(Text.From(rewards[index])) }\n","tags":["gpu","compute","gameplay"],"run":"metal","expected":"200\n160\n120","verifyRun":true,"manifest":"manifestVersion = \"2\"\npackage = \"api.example.language__compute_functions\"\nversion = \"0.1.0\"\nlanguageEdition = \"yeho-core-2026.1\"\n\n[app]\nkind = \"headless\"\n","verification":"executed","sha256":"7edba9b28a0875d6957a212cf209734095e1acb4c6593e801adbd1722ff22fba","checked":"2026-09-09","diagnostic":"","observedOutput":"200\n160\n120","checkedBackend":"metal"},{"id":"auto","title":"Automatic · [auto]","description":"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.","code":"[auto]\ncompute ApplyDamage(buffer of int health, int damage)\n{\n    int index = compute.index\n    if index >= health.count { return }\n    health[index] = health[index] - damage\n}\n\nbuffer of int health\nhealth.Add(100)\nhealth.Add(80)\nhealth.Add(60)\ncomputeTask job = dispatch ApplyDamage(health, 10)\nwait job\nif job.failed { Console.Error(job.error.message) Process.Exit(1) }\nfor index from 0 < health.count { Console.Log(Text.From(health[index])) }\n","tags":["auto","cpu","gpu","compute"],"run":"metal","expected":"90\n70\n50","verifyRun":true,"manifest":"manifestVersion = \"2\"\npackage = \"api.example.language__compute_functions\"\nversion = \"0.1.0\"\nlanguageEdition = \"yeho-core-2026.1\"\n\n[app]\nkind = \"headless\"\n","verification":"executed","sha256":"a3224053d612ae8cbd66a9c4ac7b1d2c61c900fdd28bd983f4a7590452438fe0","checked":"2026-09-09","diagnostic":"","observedOutput":"90\n70\n50","checkedBackend":"metal"},{"id":"hybrid","title":"CPU + GPU · one pipeline","description":"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.","code":"[gpu]\ncompute DoubleRewards(buffer of int rewards)\n{\n    int index = compute.index\n    if index >= rewards.count { return }\n    rewards[index] = rewards[index] * 2\n}\n\n// CPU: prepare a small game reward batch.\nbuffer of int rewards\nfor level from 1 <= 4 { rewards.Add(level * 10) }\n\n// GPU: process independent elements.\ncomputeTask job = dispatch DoubleRewards(rewards)\nwait job\nif job.failed { Console.Error(job.error.message) Process.Exit(1) }\n\n// CPU: consume only after the GPU has finished.\nint total = 0\nfor index from 0 < rewards.count { total = total + rewards[index] }\nConsole.Log(Text.From(total))\n","tags":["hybrid","cpu","gpu","compute","pipeline"],"run":"metal","expected":"200","verifyRun":true,"manifest":"manifestVersion = \"2\"\npackage = \"api.example.language__compute_functions\"\nversion = \"0.1.0\"\nlanguageEdition = \"yeho-core-2026.1\"\n\n[app]\nkind = \"headless\"\n","verification":"executed","sha256":"c205eef620a9c3f0251c9d452900597fc9d58e20e29624230e9a824267fcdb03","checked":"2026-09-09","diagnostic":"","observedOutput":"200","checkedBackend":"metal"}],"searchTerms":[],"url":"https://demonhunterlabs.com/academy/api?item=language--compute-functions","json":"https://demonhunterlabs.com/reference/items/language--compute-functions.json","markdown":"https://demonhunterlabs.com/reference/text/language--compute-functions.md"}
