A program is a sequence of boxes, over/2: each box runs its equations at every point of its
axes, like a loop nest, and an equation writes one element of an array, a name. From the boxes
the compiler derives the kernels, which axes are parallel, what fuses, what lives in registers
or local memory, and runs them on any OpenCL device; eval/3 runs the same boxes in Prolog,
exactly, the reference to check a device against. Shapes and subscripts are clpBNR
constraints, so an extent may stay unknown: one compiled program serves every size, and a
subscript out of bounds is an error before anything runs.
% C = A B, for A of N x M and B of M x P.
matmul(A, B, C, N, M, P) :-
shape(A, [N, M]), shape(B, [M, P]), shape(C, [N, P]),
over([I:N, J:P], [C[I, J] := 0]),
over([I:N, J:P, K:M], [C[I, J] := C[I, J] + A[I, K] * B[K, J]]).
?- matmul(A, B, C, 2, 3, 2),
Data = [A-[1, 2, 3, 4, 5, 6], B-[1, 0, 0, 1, 1, 1]],
eval(C, Data, Ref),
run(C, Data, Values, [device(pocl)]).
Ref = Values, Values = [4.0, 5.0, 10.0, 11.0].
The user-facing predicates of the pack, from the modules under array/:
- names, from array/core: shape/2, kind/2, type/2, elements/2, power_of_two/1,
permutation/1 and device/2 declare and read what a name is; the postfix operator [] makes
its places, `A[I, J]`;
- boxes: over/2, and copy/3, the one box across devices;
- running a program once, from array/handle: run/4, run_all/4 and realize/3, through a
handle kept in a cache; from array/compile, eval/3, the reference; from array/core,
value/2 and unrealize/1, the value of a realized name, and label_kinds/1 and
label_types/1, the kinds and the types of a program resolved;
- handles, a program compiled once and called many times, from array/handle: compile/3,
compile/2, compile_all/3, run/3, signature/3, signature_all/4, release/1 and
flush_handles/0;
- devices and kernels: opencl_device/2, from array/opencl, the devices a program runs on;
from array/compile, program/2, kernels/3, source/2 and device_options/2, the boxes of a
program, its kernels, their OpenCL C and the schedule parameters of a device;
- printing, from array/answer: print_array/1,2, and the view of names in the toplevel's
answers, by the flag array_answer.
The modules under array/ export more, for tools and tests: load them by name. So are
array/api, the functional overlay `Y @= Expr`, and array/grad, the derivatives of a program.
The pack writes caches under $XDG_CACHE_HOME or ~/.cache, each safe to delete: array_eval,
the compiled reference programs of eval/3, the flag eval_cache_directory; array_cl, the
binaries of OpenCL programs, the flag cl_program_cache_directory; and array_tune, the
options tune_device/2 measured for a device, the flag array_tune_file.
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.
- shape(Arg1, Arg2)
- elements(Arg1, Arg2)
- kind(Arg1, Arg2)
- Arg1 type Arg2
- label_kinds(Arg1)
- label_types(Arg1)
- power_of_two(Arg1)
- permutation(Arg1)
- over(Arg1, Arg2)
- copy(Arg1, Arg2, Arg3)
- device(Arg1, Arg2)
- value(Arg1, Arg2)
- unrealize(Arg1)
- program(Arg1, Arg2)
- eval(Arg1, Arg2, Arg3)
- kernels(Arg1, Arg2, Arg3)
- source(Arg1, Arg2)
- device_options(Arg1, Arg2)
- compile(Arg1, Arg2, Arg3)
- compile(Arg1, Arg2)
- compile_all(Arg1, Arg2, Arg3)
- run(Arg1, Arg2, Arg3)
- signature(Arg1, Arg2, Arg3)
- release(Arg1)
- run(Arg1, Arg2, Arg3, Arg4)
- run_all(Arg1, Arg2, Arg3, Arg4)
- realize(Arg1, Arg2, Arg3)
- flush_handles
- signature_all(Arg1, Arg2, Arg3, Arg4)
- opencl_device(Arg1, Arg2)
- print_array(Arg1)
- print_array(Arg1, Arg2)