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.backend.Backend;
29 import jdk.incubator.code.Reflect;
30 import optkl.ifacemapper.BoundSchema;
31 import optkl.ifacemapper.Buffer;
32 import optkl.ifacemapper.MappableIface;
33 import optkl.ifacemapper.Schema;
34 import optkl.util.carriers.ArenaAndLookupCarrier;
35 
36 import java.lang.foreign.GroupLayout;
37 import java.lang.invoke.MethodHandles;
38 
39 public class S32Array2DNewSchemaTest implements Buffer {
40     public interface S32Arr2D extends Buffer {
41          @Reflect default void schema(){array(width()*height());};
42          Schema<S32Arr2D> schema = Schema.of(S32Arr2D.class);
43         // Schema<S32Arr2D> oldSchema = Schema.of(S32Arr2D.class, s32Array -> s32Array.arrayLen("width", "height").array("array"));
44         int width();
45         int height();
46         int array(long idx);
47         void array(long idx, int i);
48         static S32Arr2D create(ArenaAndLookupCarrier cc, int width, int height) {
49             return BoundSchema.of(cc ,schema, width, height).allocate();
50         }
51     }
52     public static void main(String[] args) {
53         var lookup = MethodHandles.lookup();
54         Accelerator accelerator = new Accelerator(lookup, Backend.FIRST);
55 
56         S32Arr2D s32Arr2D  = S32Arr2D.create(accelerator, 100,200);
57         GroupLayout groupLayout = (GroupLayout) MappableIface.getLayout(s32Arr2D);
58         System.out.println("Layout from buffer "+groupLayout);
59 
60         if ( MappableIface.getBoundSchema(s32Arr2D)
61                 .rootBoundSchemaNode()
62                 .getName("array") instanceof BoundSchema.ArrayFieldLayout arrayFieldLayout){
63             arrayFieldLayout.elementOffset(0);
64             arrayFieldLayout.elementLayout(0);
65             if (arrayFieldLayout instanceof BoundSchema.BoundArrayFieldLayout boundArrayFieldLayout){
66                 boundArrayFieldLayout.dimFields.forEach(dimLayout->{
67                     System.out.println(dimLayout.field.name + " offset=@"+dimLayout.offset());
68                 });
69             }
70         }
71         System.out.println("-----");
72         S32Arr2D.schema.toText(System.out::print);
73     }
74 
75 }