luna_thread
This manual documents version 0.1.0 of Luna-Flow/luna_thread as it stands on
the current branch.
Overview
luna_thread lets MoonBit programs describe parallel work as data and run part
of it on a native C runtime. A program builds plans (map, reduce, scan and
map-reduce over a typed input domain) and workflows (task graphs whose
nodes use capabilities such as channels, mutexes and barriers), validates them
in MoonBit, and executes integer kernels and workflow graphs through the C
foreign function interface. A JavaScript path through a Node.js addon is
scaffolded but does not execute anything yet.
The repository also holds the normative specification that the implementation is moving towards. This manual describes what the current branch implements; where the code is narrower than the specification, the pages say so.
Version 0.1.0 implements a first, deliberately small subset (“v1”):
- Plans and workflows are validated in MoonBit, and only the native backend in synchronous mode passes validation.
- The executable kernels are element-wise doubling, sum, minimum and maximum
reductions, and inclusive prefix sums over
IntandInt64. The facade exposes theIntversions of doubling, sum and prefix sum. - Workflow graphs run on a pthread scheduler in C that executes the synchronisation protocol of each node. Compute nodes are scheduled but do not yet run their plan.
- The facade and
backend/nativebuild only for thenativetarget;plan,shared,workflowandbackend/jsbuild for every target.
Install
moon add Luna-Flow/luna_thread@0.1.0
Then import the facade in the moon.pkg of a package that builds for the
native target:
import {
"Luna-Flow/luna_thread",
}
supported_targets = "native"
The facade is then available as @luna_thread. Packages that only build plans
or workflows can import Luna-Flow/luna_thread/plan or
Luna-Flow/luna_thread/workflow instead and build on every target.
The module uses the moon.mod and moon.pkg manifest syntax and needs the
MoonBit toolchain 0.10 or later (moonc ≥ 0.10). Running the facade needs the
native target and a C compiler; the build links the C runtime from the
package’s native stubs and uses POSIX threads. Building the standalone C
runtime with OpenMP uses CMake, and the Node.js addon uses node-gyp;
scripts/check-env.sh checks for all of them.
Pages
The MoonBit module lives in the luna_thread/ directory of the repository. It
has six packages, and the root package Luna-Flow/luna_thread is documented as
core.
| Part | Tutorial | API | Design |
|---|---|---|---|
core: the facade, Luna-Flow/luna_thread | tutorial | API | design |
plan: data-parallel plans and their validation | tutorial | API | design |
workflow: task graphs, capabilities and their validation | tutorial | API | design |
shared: policies, backend targets, status codes, C record mirrors | tutorial | API | design |
backend/native: the C FFI backend, kernels and workflow runtime | tutorial | API | design |
backend/js: the placeholder for the JavaScript backend | tutorial | API | design |
The architecture guide spans the packages: it shows how the
MoonBit packages, the C runtime in native/ and the Node.js addon in js/
fit together, and lists the integer codes that cross the foreign function
interface.
Exported entry points
The facade re-exports the common names; each package page lists the rest.
- Policies:
make_policy,default_policy,default_backend,runtime_capabilities, and insharedExecutionPolicy,make_execution_policyandvalidate_policy - Plans:
map,reduce,scan,map_reduce,validate,is_ready, the value typesi32_type…opaque_typeand the kernelssum_reduction,min_reductionandmax_reduction - Workflows:
workflow,compute_task,spawn_task,join_task,channel_capability,mutex_capability,shared_read_capability,workflow_validate,workflow_is_readyandsubmit_workflow; the full node, edge and capability vocabulary is inworkflow
Execution
- Direct kernels:
execute_map_i32,execute_reduce_sum_i32andexecute_scan_sum_i32onFixedArray[Int]; theInt64, minimum and maximum kernels are theffi_*functions ofbackend/native - Asynchronous workflows:
submit_workflow_async,poll_workflow,wait_workflowanddrop_workflow - Plans translate into typed native requests with
map_request_from_plan,reduce_request_from_planandscan_request_from_plan; no function executes a plan yet
Where to read next
If you want to run something, start with the core tutorial: it doubles, sums and scans an array on the native backend and builds a small workflow.
- New to the package: read the core tutorial, then the plan tutorial and the workflow tutorial to see what a plan or a workflow means before you build one.
- Using it in a library: keep the core API, the
plan API and the workflow API at hand.
Write library code against
planandworkflow, which build on every target, and leave execution to the application. - Contributing: read the architecture guide and the design pages, in particular the native backend design for the threading and memory model, then the specification above.
Validation
Run the release checks from the module directory:
moon -C luna_thread check --target all
moon -C luna_thread test --target native
native/tests/smoke.c, built by make native-configure native-build, checks
the C kernels on their own.