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 hat.test;
26 
27 import hat.Accelerator;
28 import hat.ComputeContext;
29 import hat.NDRange;
30 
31 import static hat.KernelContext.*;
32 import hat.backend.Backend;
33 import hat.buffer.F32Array;
34 import hat.device.DeviceSchema;
35 import hat.device.NonMappableIface;
36 import jdk.incubator.code.Reflect;
37 import hat.test.annotation.HatTest;
38 import hat.test.exceptions.HATAsserts;
39 
40 import java.lang.invoke.MethodHandles;
41 
42 public class TestLocal {
43 
44     private interface MySharedArray extends NonMappableIface {
45         void array(long index, float value);
46         float array(long index);
47 
48         DeviceSchema<MySharedArray> deviceSchema = DeviceSchema.of(MySharedArray.class,
49                 builder -> builder.array("array", 16));
50 
51         static MySharedArray create(Accelerator accelerator) {
52             return null;
53         }
54 
55         static MySharedArray createLocal() {
56             return create(new Accelerator(MethodHandles.lookup(), Backend.FIRST));
57         }
58     }
59 
60     @Reflect
61     private static void compute( F32Array data) {
62         MySharedArray mySharedArray = MySharedArray.createLocal();
63         int lix = LIX();
64         int blockId = BIX();
65         int blockSize = LSX();
66         mySharedArray.array(lix, lix);
67         barrier();
68         data.array(lix + (long) blockId * blockSize, mySharedArray.array(lix));
69     }
70 
71     @Reflect
72     private static void myCompute(ComputeContext computeContext, F32Array data) {
73         computeContext.dispatchKernel(NDRange.of1D(32,16), () -> compute( data));
74     }
75 
76     @HatTest
77     @Reflect
78     public void testLocal() {
79         Accelerator accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
80         F32Array data = F32Array.create(accelerator, 32);
81         accelerator.compute(computeContext -> TestLocal.myCompute(computeContext, data));
82 
83         // Check result
84         boolean isCorrect = true;
85         int jIndex = 0;
86         for (int i = 0; i < data.length(); i++) {
87             if (data.array(i) != jIndex) {
88                 isCorrect = false;
89                 break;
90             }
91             jIndex++;
92             if (jIndex == 16) {
93                 jIndex = 0;
94             }
95         }
96         HATAsserts.assertTrue(isCorrect);
97     }
98 
99 }