1 /*
 2  * Copyright (c) 2024, 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 experiments;
26 
27 import hat.Accelerator;
28 import hat.Accelerator.Compute;
29 import hat.ComputeContext;
30 import hat.NDRange;
31 import hat.KernelContext;
32 import hat.buffer.S32Array;
33 import optkl.ifacemapper.MappableIface.RO;
34 import optkl.ifacemapper.MappableIface.RW;
35 import optkl.ifacemapper.MappableIface.WO;
36 import static hat.backend.Backend.FIRST;
37 
38 import jdk.incubator.code.Reflect;
39 
40 import java.lang.invoke.MethodHandles;
41 
42 public class MinBufferTest {
43 
44 
45     public static class ComputeApp {
46         @Reflect
47         public static void inc(@RO KernelContext kc, @RW S32Array s32Array, int len) {
48             if (kc.gix < kc.gsx) {
49                 s32Array.array(kc.gix, s32Array.array(kc.gix) + 1);
50             }
51         }
52 
53         @Reflect
54         public static void add(ComputeContext cc, @RW S32Array s32Array, int len, int n) {
55             for (int i = 0; i < n; i++) {
56                 cc.dispatchKernel(NDRange.of1D(len), kc -> inc(kc, s32Array, len));
57                 System.out.println(i);//s32Array.array(0));
58             }
59         }
60     }
61 
62     public static void main(String[] args) {
63         Accelerator accelerator = new Accelerator(MethodHandles.lookup(),FIRST);
64         int len = 10000000;
65         int valueToAdd = 10;
66         S32Array s32Array = S32Array.create(accelerator, len,i->i);
67         accelerator.compute((@Reflect Compute)
68                 cc -> ComputeApp.add(cc, s32Array, len, valueToAdd));
69         // Quite an expensive way of adding 20 to each array element
70         for (int i = 0; i < 20; i++) {
71             System.out.println(i + "=" + s32Array.array(i));
72         }
73     }
74 
75 }