GPU Kernels and Parallel

A Function or a Routine can be run more than once for one call, at the same time: on the graphics card, once for every pixel of a picture, every item of a list or every element of a volume - a GPU kernel - or on the processor, once for every item of a list - Parallel. Which, is the graph's Runs as, in its Properties with nothing selected: Function or Routine - the graph itself, as usual - Parallel or GPU Kernel. Changing it is one step of the undo history, and the status line says so - Set Estimate to run as an ordinary Routine. GPU kernels come with Divooka Compute Standard.

Glowing Circle: the kernel called for a picture of 640 by 360, and its preview, an orange disc with a bright rim on night blue

Parallel

A Function or a Routine that runs as Parallel and is called with a list where it expects one item runs once for each item, on several processor threads at once, and answers a list of its results in the list's order. Its call takes the list on the pin that would take one item. Nothing about the graph itself changes, so a graph that already works on one item works on many.

Its copies run at the same moment, so a Parallel graph that writes a data member of the document is an error: give each copy what it needs as an input, and gather what they answer. Divooka Programs › Parallel Estimates estimates π with sixteen seeds at once and averages the estimates.

GPU kernels

A graph that runs as a GPU Kernel is run on the graphics card once for every element of what it is given, and each run works out one element of what it answers:

  • given a picture or a field, or a width and a height, it runs over 2D, once per pixel, and answers a picture or a field;
  • given a list, it runs once per item and answers lists;
  • given a volume, it runs once per element, over 3D, and answers a volume.

Inside a kernel, Index X, Index Y and Index Z say which element a run is working on, UV where it stands in the picture from 0 to 1, and Time the time of the call. Read At reads any item of any list, not only the one at this index, and Sample List and List Length read a list as a whole. Random (Kernel) gives random numbers that are the same on the graphics card and the processor for each seed, element and draw, so a result can be checked against the processor's. The Number, Vector and Color operations - Smooth Step, Step, Mix Colors, Vector Length, Scale and the rest - work on whole vectors and colours, so a shader needs no Split or Make node.

A kernel takes numbers, vectors, colours, fields, pictures, lists and volumes, and answers the same kinds. Lists it reads at each item must be equally long: given lists of different lengths it stops and names each list and its length. A list it only looks items up in, with Read At, may be any length.

Wires a kernel cannot run are refused as they are drawn, in the kernel's words - A GPU kernel cannot turn numbers into truths · hold Shift to connect anyway - and Shift connects anyway. The tray offers a kernel only what it can run, and says why a node is missing: search while and it answers No node matches 'while' in a GPU Kernel.

A Routine as a kernel

A Routine can be a kernel too, with loops that count: it may hold local variables, Branch, Switch on numbers, Sequence, For, Repeat, Break and Continue, and calls to Functions a kernel can run, and every path through it must reach a Return. While, Try, Print, data members and calls to Routines are not offered, and the editor says what to use instead - A GPU kernel counts its loops: use For or Repeat., A GPU kernel cannot read or set a data member: give it the value as an input. A graphics card cannot stop a loop that never ends, and Windows resets a card that runs one for more than about two seconds. A kernel with a loop runs in slices of about a tenth of a second, so Stop works between them.

Its preview

Select a call of a kernel and press F6: the Active Preview shows what it answers, drawn again live as you change its inputs, and the size under the picture - 640 × 360. The drop-down at the right of the pane's header chooses the resolution of the preview: As called (the size the call asks for), fixed sizes such as 1024 × 1024 and 2048 × 2048, or Fit to pane, which follows the pane's size. It is a view, not part of the document, and it redraws with no run.

Inspecting one element

A breakpoint cannot stop a kernel partway, so it is looked into an element at a time. With the kernel's graph in front and nothing selected, click a pixel of its preview - Picked (320, 80) - or, on the Debug face, type the element's place under Element and press Inspect. The Debug face shows the element's place and the time, what it answered as the graphics card worked it out and as Divooka's own evaluator did, which agree to about two decimals, the kernel's local variables, and every node's value for that element. Nothing is run.

A Julia set kernel with the element at (60, 100) inspected on the Debug face: its colour from the graphics card beside the evaluator's, its local variables and each node's value

Colours

A colour on a pin or in Properties opens the colour picker: Spectrum, Palette and Values, and Confirm to take the colour, as one step of the undo history. A Function's colour input or output has a colour field with a swatch for its default.

Examples

Under GPU Kernels in New from examples:

  • Glowing Circle - an orange disc with a bright rim on night blue, breathing with Time, from a shader's vector maths.
  • Julia Set - a Julia set drawn by a Routine kernel with a counted loop and a Break.
  • Expected Tail Loss - a Monte Carlo risk estimate: 5,000 scenarios of 252 trading days over a ten-year index, run on the graphics card, giving the average return, the value at risk and the expected tail loss.

And under Divooka Programs, Parallel Estimates, the Parallel way of running.