Skip to content

Conversation

jjfumero
Copy link
Member

@jjfumero jjfumero commented Aug 22, 2025

WIP: This patch enables a new extension for HAT to allow the use of local memory (as in OpenCL) / shared memory (CUDA). This extension is preliminary, and the implementation can be improved to use a new HAT dialect with new Ops from code reflection (Babylon).

This extension brings two new calls to the API:
a) Access to local memory
b) Access to local barriers

Example:

@CodeReflection
private static void reduceLocal(@RO KernelContext context, @RW S32Array input, @RW S32Array partialSums) {
    int localId = context.lix;
    int localSize = context.lsx;
    int blockId = context.bix;

    // Prototype: allocate in shared memory an array of 16 ints
    S32Array sharedArray = context.createLocalS32Array(16);

    // Copy from global to shared memory
    sharedArray.array(localId, input.array(context.gix));

    // Reduction using local memory
    for (int offset = localSize / 2; offset > 0; offset /= 2) {
        context.barrier();
        if (localId < offset) {
            sharedArray.array(localId,  sharedArray.array(localId) +  sharedArray.array(localId + offset));
        }
    }
    if (localId == 0) {
        // copy from shared memory to global memory
        partialSums.array(blockId,  sharedArray.array(0));
    }
}

This brings low-level GPU programming to HAT, but for now this is just experimentation.

Running a few examples:

a) Reductions:

java -cp job.jar hat.java exp ffi-opencl Barriers

java -cp job.jar hat.java exp ffi-cuda Barriers

b) Fast Matrix Mult using loop tiling and local memory:

java @hat/run ffi-opencl matmul 2DTILING 

java @hat/run ffi-cuda matmul 2DTILING 

I will keep this PR as a drat to start iterating, while others developers can start testing and providing feedback.


Progress

  • Change must not contain extraneous whitespace

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/babylon.git pull/531/head:pull/531
$ git checkout pull/531

Update a local copy of the PR:
$ git checkout pull/531
$ git pull https://git.openjdk.org/babylon.git pull/531/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 531

View PR using the GUI difftool:
$ git pr show -t 531

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/babylon/pull/531.diff

jjfumero and others added 29 commits August 8, 2025 14:43
@bridgekeeper
Copy link

bridgekeeper bot commented Aug 22, 2025

👋 Welcome back jfumero! A progress list of the required criteria for merging this PR into code-reflection will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 22, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Aug 22, 2025

@jjfumero this pull request can not be integrated into code-reflection due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout hat/api/shared
git fetch https://git.openjdk.org/babylon.git code-reflection
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge code-reflection"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Aug 22, 2025
@openjdk
Copy link

openjdk bot commented Aug 22, 2025

⚠️ @jjfumero This pull request contains merges that bring in commits not present in the target repository. Since this is not a "merge style" pull request, these changes will be squashed when this pull request in integrated. If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change the title of this pull request to Merge <project>:<branch> where <project> is the name of another project in the OpenJDK organization (for example Merge jdk:master).

@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Aug 22, 2025
@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-conflict Pull request has merge conflict with target branch
Development

Successfully merging this pull request may close these issues.

1 participant