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