1 # HAT's Programming Model
2
3 ----
4 * [Contents](hat-00.md)
5 * Build Babylon and HAT
6 * [Quick Install](hat-01-quick-install.md)
7 * [Building Babylon with jtreg](hat-01-02-building-babylon.md)
8 * [Building HAT with jtreg](hat-01-03-building-hat.md)
9 * [Enabling the NVIDIA CUDA Backend](hat-01-05-building-hat-for-cuda.md)
10 * [Testing Framework](hat-02-testing-framework.md)
11 * [Running Examples](hat-03-examples.md)
12 * [HAT Programming Model](hat-03-programming-model.md)
13 * Interface Mapping
14 * [Interface Mapping Overview](hat-04-01-interface-mapping.md)
15 * [Cascade Interface Mapping](hat-04-02-cascade-interface-mapping.md)
16 * Development
17 * [Project Layout](hat-01-01-project-layout.md)
18 * Implementation Details
19 * [Walkthrough Of Accelerator.compute()](hat-accelerator-compute.md)
20 * [How we minimize buffer transfers](hat-minimizing-buffer-transfers.md)
21 * [Running HAT with Docker on NVIDIA GPUs](hat-07-docker-build-nvidia.md)
22 ---
23
24 # HAT's Programming model
25
26 Let's consider a trivial opencl kernel which squares each element in an int buffer
27
28 ```java
29 int square(int value){
30 return value*value;
31 }
32
33 __kernel void squareKernel( __global int* s32Array){
34 int value = s32Array[get_global_id(0)];
35 s32Array[get_global_id(0)]=square(value);
36 return;
37 }
38
39 ```
40
41 We implement this in HAT by collecting the kernel(s) and compute method(s) in a `Compute` class.
42
43 ```java
44 public class SquareCompute {
45 @Reflect
46 public static int square(int v) {
47 return v * v;
48 }
49
50 @Reflect
51 public static void squareKernel(KernelContext kc, S32Array s32Array) {
52 int value = s32Array.array(kc.x); // arr[cc.x]
53 s32Array.array(kc.x, square(value)); // arr[cc.x]=value*value
54 }
55
56 @Reflect
57 public static void square(ComputeContext cc, S32Array s32Array) {
58 cc.dispatchKernel(s32Array.length(),
59 kc -> squareKernel(kc, s32Array)
60 );
61 }
62 }
63 ```
64 And we dispatch by creating the appropriate data buffer and then asking an `Accelerator` (bound to a typical vendor backend) to execute the compute method.. which in turn coordinates the dispatch of the various kernels.
65
66 ```java
67 // Create an accelerator bound to a particular backend
68
69 var accelerator = new Accelerator(
70 MethodHandles.lookup(), Backend.FIRST // Predicate<Backend>
71 );
72
73 // Ask the accelerator/backend to allocate an S32Array
74 var s32Array = S32Array.create(accelerator, 32);
75
76 // Fill it with data
77 for (int i = 0; i < s32Array.length(); i++) {
78 s32Array.array(i, i);
79 }
80
81 // Tell the accelerator to execute the square() compute entrypoint
82
83 accelerator.compute(
84 cc -> SquareCompute.square(cc, s32Array)
85 );
86
87 // Check the data
88 for (int i = 0; i < arr.length(); i++) {
89 System.out.println(i + " " + arr.array(i));
90 }
91 ```
92
93 ## Programming model notes
94
95 The most important concept here is that we separate `normal java` code,
96 from `compute` code from `kernel` code
97
98 We must not assume that Compute or Kernel code are ever executed by the JVM
99
100 ### Kernel Code (kernel entrypoints and kernel reachable methods)
101 Kernel's and any kernel reachable methods will naturally be restricted to subset of Java.
102
103 * No exceptions (no exceptions! :) )
104 * No heap access (no `new`)
105 * No access to static or instance fields from this or any other classes )
106 * Except `final static primitives` (which generally get constant pooled)
107 * Except fields of `KernelContext` (thread identity `.x`, `.maxX`, `.groups`... )
108 - We may even decide to access these via methods (`.x()`);
109 * The only methods that can be called are either :-
110 * Kernel reachable methods
111 - Technically you can call a kernel entrypoint, but must pass your KernelContext
112 * `ifaceMappedSegment` accessor/mutators (see later)
113 * Calls on `KernelContext` (backend kernel features)
114 - `KernelContext.barrier()`
115 - `kernelContext.I32.hypot(x,y)`
116 #### Kernel Entrypoints
117 * Declared `@Reflect static public void`
118 * Later we may allow reductions to return data...
119 * Parameters
120 * 0 is always a `KernelContext` (KernelContext2D, KernelContext3D logically follow)
121 * 1..n are restricted to uniform primitive values and Panama FFM `ifaceMappedSegments`
122
123 #### Kernel Reachable Methods
124 * Declared `@Reflect static public`
125 * All Parameters are restricted to uniform primitive values and Panama FFM `ifaceMappedSegments`
126
127 ### Compute Code (Compute entry points and compute reachable methods)
128 Code within the `compute entrypoint` and `compute reachable
129 methods` have much fewer Java restrictions than kernels but generally...
130
131 * Exceptions are discouraged
132 * Java Synchronization is discouraged
133 * Don't assume any allocation of local `ifaceMappedSegmants` are allocated
134 * Java accesses/mutations to `ifaceMappedSegment` will likely impact performance
135 * Code should ideally just contain simple plyTable flow and kernel dispatches.
136 * Data movements (to and from backend) will automatically be derived from plyTable flow and `ifaceMappedSegment` accesses
137 - We hope to never have to add `cc.moveToDevice(hatBuffer)`
138 * All methods reachable from a `compute entrypoint` are either :-
139 * Compute Reachable Methods
140 - Technically methods can be compute reachable and kernel reachable.
141 * `ifaceMappedSegment` accessor/mutators (see later)
142 * Calls on the `ComputeContext` to generate ranges, or dispatch kernels.
143
144 #### Compute Entry Points
145 * Declared `@Reflect static public void`
146 * Parameter 0 is `ComputeContext`
147
148
149 #### Compute Reachable Methods
150 * Declared `@Reflect static public `