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