1 /*
  2  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package experiments;
 26 
 27 import hat.Accelerator;
 28 import hat.ComputeContext;
 29 import hat.NDRange;
 30 import hat.KernelContext;
 31 import hat.backend.Backend;
 32 import hat.buffer.S32Array;
 33 import hat.ifacemapper.MappableIface.RO;
 34 import hat.ifacemapper.MappableIface.RW;
 35 import jdk.incubator.code.CodeReflection;
 36 
 37 import java.lang.invoke.MethodHandles;
 38 import java.util.stream.IntStream;
 39 
 40 /**
 41  * How to test?
 42  * <code>
 43  *     HAT=SHOW_CODE java -cp job.jar hat.java exp ffi-opencl LocalIds
 44  * </code>
 45  */
 46 public class LocalIds {
 47 
 48     private static boolean PRINT_RESULTS = false;
 49 
 50     @CodeReflection
 51     private static void assign(@RO KernelContext context, @RW S32Array arrayA, @RW S32Array arrayB, @RW S32Array arrayC) {
 52         int gx = context.gix;
 53         int lx = context.lix;
 54         int lsx = context.lsx;
 55         int bix = context.bix;
 56         arrayA.array(gx, lx);
 57         arrayB.array(gx, lsx);
 58         arrayC.array(gx, bix);
 59     }
 60 
 61     private static final int BLOCK_SIZE = 16;
 62 
 63     @CodeReflection
 64     private static void mySimpleCompute(@RO ComputeContext cc,  @RW S32Array arrayA, @RW S32Array arrayB, @RW S32Array arrayC) {
 65         // 2 groups of 16 threads each
 66         NDRange ndRange = NDRange.of(NDRange.Global1D.of(32), NDRange.Local1D.of(BLOCK_SIZE));
 67         cc.dispatchKernel(ndRange, kc -> assign(kc, arrayA, arrayB, arrayC));
 68     }
 69 
 70     public static void main(String[] args) {
 71         System.out.println("Experiment: local IDs and local groups");
 72 
 73         Accelerator accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
 74         final int size = 32;
 75         S32Array arrayA = S32Array.create(accelerator, size);
 76         S32Array arrayB = S32Array.create(accelerator, size);
 77         S32Array arrayC = S32Array.create(accelerator, size);
 78 
 79         // Set initial value to 0
 80         arrayA.fill(i -> 0);
 81         arrayB.fill(i -> 0);
 82         arrayC.fill(i -> 0);
 83 
 84         // Compute on the accelerator
 85         accelerator.compute( cc -> LocalIds.mySimpleCompute(cc, arrayA, arrayB, arrayC));
 86 
 87         int[] expectedIds = new int[size];
 88         int j = 0;
 89         for (int i = 0; i < size; i++) {
 90             expectedIds[i] = j++;
 91             if (j == BLOCK_SIZE) {
 92                 j = 0;
 93             }
 94         }
 95 
 96         System.out.println("Execution finished");
 97 
 98         if (PRINT_RESULTS) {
 99             System.out.println("Result Locals: ");
100             for (int i = 0; i < arrayA.length(); i++) {
101                 System.out.println(arrayA.array(i));
102             }
103             System.out.println("Result Blocks: ");
104             for (int i = 0; i < arrayB.length(); i++) {
105                 System.out.println(arrayB.array(i));
106             }
107             System.out.println("Result Block ID: ");
108             for (int i = 0; i < arrayC.length(); i++) {
109                 System.out.println(arrayC.array(i));
110             }
111         }
112 
113         boolean correct = true;
114         for (int i = 0; i < arrayA.length(); i++) {
115             if (expectedIds[i] != arrayA.array(i)) {
116                 System.out.println("Mismatch local ids");
117                 correct = false;
118             }
119         }
120         if (correct) {
121             System.out.println("Local IDs are correct");
122         }
123 
124 
125         correct = true;
126         for (int i = 0; i < arrayB.length(); i++) {
127             if (BLOCK_SIZE != arrayB.array(i)) {
128                 System.out.println("Mismatch group Sizes");
129                 correct = false;
130             }
131         }
132         if (correct) {
133             System.out.println("Group Size are correct");
134         }
135 
136         IntStream.range(0, size).forEach(i -> {
137             int v = i < BLOCK_SIZE ? 0 : 1;
138             expectedIds[i] = v;
139         });
140         for (int i = 0; i < arrayC.length(); i++) {
141             if (expectedIds[i] != arrayC.array(i)) {
142                 System.out.println("Mismatch group IDs");
143                 correct = false;
144             }
145         }
146         if (correct) {
147             System.out.println("Group IDs are correct");
148         }
149     }
150 
151 }