42 lines
No EOL
2.6 KiB
HTML
42 lines
No EOL
2.6 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>Job.parallel_for — Ludic</title>
|
||
<meta name="description" content="Run work(i, ctx) for every i in [0, count) across all cores; returns when every call is done.">
|
||
<link rel="stylesheet" href="base.css">
|
||
<link rel="stylesheet" href="docs.css">
|
||
</head>
|
||
<body>
|
||
<header class="nav"><div class="wrap nav-in"><a class="brand" href="index.html"><span class="logo">L</span> Ludic</a><button class="nav-toggle" aria-label="Toggle menu" aria-expanded="false">☰</button><nav class="nav-links"><a href="index.html">Home</a><a href="api.html">API Reference</a><a class="nav-cta" href="https://git.workshopsoft.io/workshopsoft/ludic">Source ↗</a></nav></div></header>
|
||
<main class="wrap item">
|
||
<div class="crumbs"><a href="api.html">API Reference</a> <span>›</span> <a href="ns-job.html">Job</a> <span>›</span> <span class="here">Job.parallel_for</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-method">method</span>
|
||
<h1 id="top">Job.parallel_for</h1>
|
||
</div>
|
||
<code class="sig">Job.parallel_for(count, work, ctx) -> void</code>
|
||
<div class="desc"><p>Run <code>work(i, ctx)</code> for every <code>i</code> in <code>[0, count)</code> on real OS threads, and return when every call has
|
||
finished. The indices are split into chunks shared by a worker pool (one thread per core but one,
|
||
started on first use) and the calling thread, so each index runs exactly once, in no particular order.</p><p><code>work</code> is a top-level function taking <code>(i: int, ctx)</code>, named with <code>fn</code>; <code>ctx</code> is whatever it computes
|
||
on (<code>words</code>, <code>bytes</code> or a <code>pointer</code>). A worker computes on what it was handed and writes only its own
|
||
index's results: it must not <code>spawn</code>, <code>despawn</code>, <code>push</code> onto a list another thread can see, or use
|
||
Http or Audio. <code>spawn</code> and <code>despawn</code> on a worker stop the program with a located message. Shared
|
||
counters go through <code>Sync.add</code>, shared totals behind a <code>Sync.mutex</code>.</p></div>
|
||
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Squares {
|
||
function square(i: int, out: words) -> void { out[i] = i * i }
|
||
|
||
entry {
|
||
let sq = words(20000)
|
||
Job.parallel_for(20000, fn square, sq)
|
||
print(sq[141])
|
||
}
|
||
}</pre></div>
|
||
|
||
<a class="back" href="api.html">← All symbols</a>
|
||
</main>
|
||
<script src="ludic-highlight.js"></script>
|
||
<script>Ludic.highlightAll(); Ludic.installCards(); Ludic.flashTarget();</script>
|
||
</body></html> |