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