1:- module(array, []).

Multidimensional arrays as boxes of equations, compiled to OpenCL

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/:

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.

   55:- reexport(library(array/core)).   56:- reexport(library(array/compile), [program/2, eval/3, kernels/3, source/2, device_options/2]).   57:- reexport(library(array/handle), [compile/3, compile/2, compile_all/3, run/3, signature/3, release/1, run/4, run_all/4, realize/3, flush_handles/0, signature_all/4]).   58:- reexport(library(array/opencl), [opencl_device/2]).   59:- reexport(library(array/answer), [print_array/1, print_array/2]).