[code-reflection] RFR: [feature][HAT] 2D and 3D ranges for the NDRange API

Juan Fumero duke at openjdk.org
Thu Jul 24 07:08:19 UTC 2025


On Wed, 23 Jul 2025 15:25:54 GMT, Chen Liang <liach at openjdk.org> wrote:

>> This patch introduces the concept of 2D and 3D ranges. 
>> 
>> This allows developers to do the following:
>> - Accessing 1D, 2D and 3D thread indexing within the kernels. The `KernelContext` API has been extended to do this.
>> - Dispatching 1D, 2D and 3D kernels via the `ComputeContext`.
>> 
>> **IMPORTANT**: This patch has been tested for the OpenCL backend, the CUDA backend generates correct code, but for some reason I can't dispatch it due to CUDA incompatibilities (seems to be orthogonal to this PR). I will investigate this in a separate issue. 
>> 
>> An example of use is provided in the `matmul/Main.java` class.
>> 
>> Example of 1D kernel in HAT:
>> 
>> 
>> @CodeReflection
>>     public static void matrixMultiplyKernel1D(@RO KernelContext kc, @RO F32Array matrixA, @RO F32Array matrixB, @RW F32Array matrixC, int size) {
>>         if (kc.x < kc.maxX) {
>>             for (int j = 0; j < size; j++) {
>>                 float acc = 0;
>>                 for (int k = 0; k < size; k++) {
>>                     acc += (matrixA.array(kc.x * size + k) * matrixB.array(k * size + j));
>>                 }
>>                 matrixC.array(kc.x * size + j, acc);
>>             }
>>         }
>>     }
>> 
>> 
>> Example of a 2D kernel:
>> 
>> 
>> @CodeReflection
>>     public static void matrixMultiplyKernel2D(@RO KernelContext kc, @RO F32Array matrixA, @RO F32Array matrixB, @RW F32Array matrixC, int size) {
>>         if (kc.x < kc.maxX) {
>>             if (kc.y < kc.maxY) {
>>                 float acc = 0;
>>                 for (int k = 0; k < size; k++) {
>>                     acc += (matrixA.array(kc.x * size + k) * matrixB.array(k * size + kc.y));
>>                 }
>>                 matrixC.array(kc.x * size + kc.y, acc);
>>             }
>>         }
>>     }
>> 
>> 
>> How to dispatch?
>> 
>> The dispatcher now has more parameters to setup the max thread size for each dimension. In the case of a 2D:
>> 
>> 
>> computeContext.dispatchKernel(maxX, maxY,
>>     kernelContext -> matrixMultiplyKernel2D(kernelContext, matrixA, matrixB, matrixC, size)
>> );
>> 
>> 
>> ### How to test this patch?
>> 
>> I extended the test-case for the matrix multiplication. 
>> 
>> Run with 1D:
>> 
>> 
>> HAT=SHOW_CODE java @hat/run ffi-opencl matmul 1D
>> 
>> 
>> Run with 2D
>> 
>> 
>> HAT=SHOW_CODE java @hat/run ffi-opencl matmul 2D
>
> hat/core/src/main/java/hat/ComputeContext.java line 152:
> 
>> 150:                 case 2 -> ndRange = accelerator.range(rangeX, rangeY);
>> 151:                 case 3 -> ndRange = accelerator.range(rangeX, rangeY, rangeZ);
>> 152:             }
> 
> Should we add a default branch that throws an exception instead of initializing `ndRange` to null?

I thought about this. However, this method is method is private, and only the public dispatch method sets the value. I will add the exception though.

-------------

PR Review Comment: https://git.openjdk.org/babylon/pull/496#discussion_r2227621008


More information about the babylon-dev mailing list