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 import hat.KernelContext;
 31 import hat.backend.Backend;
 32 import hat.buffer.S32Array;
 33 import jdk.incubator.code.Reflect;
 34 import hat.test.annotation.HatTest;
 35 import hat.test.exceptions.HATAsserts;
 36 import optkl.ifacemapper.MappableIface.RO;
 37 import optkl.ifacemapper.MappableIface.RW;
 38 import optkl.ifacemapper.MappableIface.WO;
 39 
 40 import java.lang.invoke.MethodHandles;
 41 
 42 public class TestConstants {
 43 
 44     public static final int CONSTANT = 100;
 45 
 46     @Reflect
 47     public static void vectorWithConstants(@RO KernelContext kc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
 48         final int BM = 100;
 49         if (kc.gix < kc.gsx) {
 50             final int valueA = arrayA.array(kc.gix);
 51             final int valueB = arrayB.array(kc.gix);
 52             arrayC.array(kc.gix, (BM + valueA + valueB));
 53         }
 54     }
 55 
 56     @Reflect
 57     public static void vectorWithConstants(@RO ComputeContext cc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
 58         cc.dispatchKernel(NDRange.of1D(arrayA.length()), kc -> vectorWithConstants(kc, arrayA, arrayB, arrayC));
 59     }
 60 
 61     /**
 62      * Test to check if final values are represented in the generated code.
 63      */
 64     @HatTest
 65     @Reflect
 66     public static void testConstants01() {
 67         final int size = 1024;
 68         var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
 69         var arrayA = S32Array.create(accelerator, size);
 70         var arrayB = S32Array.create(accelerator, size);
 71         var arrayC = S32Array.create(accelerator, size);
 72 
 73         arrayA.fill(i -> i);
 74         arrayB.fill(i -> 100 + i);
 75 
 76         accelerator.compute(cc ->
 77                 TestConstants.vectorWithConstants(cc, arrayA, arrayB, arrayC));
 78 
 79         S32Array test = S32Array.create(accelerator, size);
 80 
 81         for (int i = 0; i < test.length(); i++) {
 82             test.array(i, CONSTANT + arrayA.array(i) + arrayB.array(i));
 83         }
 84 
 85         for (int i = 0; i < test.length(); i++) {
 86             HATAsserts.assertEquals(test.array(i), arrayC.array(i));
 87         }
 88     }
 89 
 90     @Reflect
 91     public static int compute(final int valueA, final int valueB) {
 92         final int BM = 100;
 93         return BM + valueA + valueB;
 94     }
 95 
 96     @Reflect
 97     public static void vectorWithConstants2(@RO KernelContext kc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
 98         if (kc.gix < kc.gsx) {
 99             final int valueA = arrayA.array(kc.gix);
100             final int valueB = arrayB.array(kc.gix);
101             final int result = compute(valueA, valueB);
102             arrayC.array(kc.gix, result);
103         }
104     }
105 
106     @Reflect
107     public static void vectorWithConstants2(@RO ComputeContext cc, @RO S32Array arrayA, @RO S32Array arrayB, @RW S32Array arrayC) {
108         cc.dispatchKernel(NDRange.of1D(arrayA.length()), kc -> vectorWithConstants2(kc, arrayA, arrayB, arrayC));
109     }
110 
111     /**
112      * Test to check multiple method calls that contains constants.
113      * This triggers the code model analysis for each of the reachable method before the
114      * final code gen.
115      */
116     @HatTest
117     @Reflect
118     public static void testConstants02() {
119         final int size = 1024;
120         var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
121         var arrayA = S32Array.create(accelerator, size);
122         var arrayB = S32Array.create(accelerator, size);
123         var arrayC = S32Array.create(accelerator, size);
124 
125         arrayA.fill(i -> i);
126         arrayB.fill(i -> 100 + i);
127 
128         accelerator.compute(cc ->
129                 TestConstants.vectorWithConstants2(cc, arrayA, arrayB, arrayC));
130 
131         S32Array test = S32Array.create(accelerator, size);
132 
133         for (int i = 0; i < test.length(); i++) {
134             test.array(i, CONSTANT + arrayA.array(i) + arrayB.array(i));
135         }
136 
137         for (int i = 0; i < test.length(); i++) {
138             HATAsserts.assertEquals(test.array(i), arrayC.array(i));
139         }
140     }
141 }