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 hat.ifacemapper.MappableIface.RO;
 34 import jdk.incubator.code.Reflect;
 35 import hat.test.annotation.HatTest;
 36 import hat.test.engine.HATAsserts;
 37 
 38 import java.lang.invoke.MethodHandles;
 39 
 40 import static hat.ifacemapper.MappableIface.RW;
 41 
 42 public class TestParenthesis {
 43 
 44     @Reflect
 45     public static void compute(@RO KernelContext context, @RW S32Array data) {
 46         final int TN = 2;
 47         final int TF = 128;
 48         final int MAX = 1024;
 49         int c = MAX / (TN * TF);
 50         data.array(context.gix, c);
 51     }
 52 
 53     @Reflect
 54     public static void compute2(@RO KernelContext context, @RW S32Array data) {
 55         final int TN = 2;
 56         final int TF = 128;
 57         final int MAX = 1024;
 58         int c = MAX / ((TN * TF) / (TN * TN));
 59         data.array(context.gix, c);
 60     }
 61 
 62     @Reflect
 63     public static void compute3(@RO KernelContext context, @RW S32Array data) {
 64         final int TN = 2;
 65         final int TF = 128;
 66         final int MAX = 1024;
 67         int c = MAX * (TF + 2) / ((TN * TF) / (TN * TN));
 68         data.array(context.gix, c);
 69     }
 70 
 71     @Reflect
 72     public static void compute(@RO ComputeContext cc, @RW S32Array data) {
 73         cc.dispatchKernel(NDRange.of1D(data.length()),kc -> compute(kc, data));
 74     }
 75 
 76     @Reflect
 77     public static void compute2(@RO ComputeContext cc, @RW S32Array data) {
 78         cc.dispatchKernel(NDRange.of1D(data.length()),kc -> compute2(kc, data));
 79     }
 80 
 81     @Reflect
 82     public static void compute3(@RO ComputeContext cc, @RW S32Array data) {
 83         cc.dispatchKernel(NDRange.of1D(data.length()),kc -> compute3(kc, data));
 84     }
 85 
 86     @HatTest
 87     @Reflect
 88     public static void testParenthesis01() {
 89         final int size = 1;
 90         var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
 91         var data = S32Array.create(accelerator, size);
 92 
 93         // Initialize array
 94         data.fill(_ -> 0);
 95 
 96         accelerator.compute(cc -> TestParenthesis.compute(cc, data));
 97 
 98         final int TN = 2;
 99         final int TF = 128;
100         final int MAX = 1024;
101         int c = MAX / (TN * TF);
102         HATAsserts.assertEquals(c, data.array(0));
103     }
104 
105     @HatTest
106     @Reflect
107     public static void testParenthesis02() {
108         final int size = 1;
109         var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
110         var data = S32Array.create(accelerator, size);
111 
112         // Initialize array
113         data.fill(_ -> 0);
114 
115         accelerator.compute(cc -> TestParenthesis.compute2(cc, data));
116 
117         final int TN = 2;
118         final int TF = 128;
119         final int MAX = 1024;
120         int c = MAX / ((TN * TF) / (TN * TN));
121         HATAsserts.assertEquals(c, data.array(0));
122     }
123 
124     @HatTest
125     @Reflect
126     public static void testParenthesis03() {
127         final int size = 1;
128         var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
129         var data = S32Array.create(accelerator, size);
130 
131         // Initialize array
132         data.fill(_ -> 0);
133 
134         accelerator.compute(cc -> TestParenthesis.compute3(cc, data));
135 
136         final int TN = 2;
137         final int TF = 128;
138         final int MAX = 1024;
139         int c = MAX * (TF + 2) / ((TN * TF) / (TN * TN));
140         HATAsserts.assertEquals(c, data.array(0));
141     }
142 
143 }