1 /*
  2  * Copyright (c) 2026, 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.Accelerator.Compute;
 29 import hat.ComputeContext;
 30 import hat.HATMath;
 31 
 32 import static hat.KernelContext.*;
 33 import hat.NDRange;
 34 import hat.backend.Backend;
 35 import hat.device.DeviceSchema;
 36 import hat.device.NonMappableIface;
 37 import hat.test.annotation.HatTest;
 38 import hat.test.exceptions.HATAsserts;
 39 import jdk.incubator.code.Reflect;
 40 import optkl.ifacemapper.BoundSchema;
 41 import optkl.ifacemapper.Buffer;
 42 import optkl.ifacemapper.Schema;
 43 
 44 import java.lang.invoke.MethodHandles;
 45 
 46 import static hat.test.TestDFT.ArrayComplex.Complex;
 47 import static hat.test.TestDFT.ArrayComplex.create;
 48 
 49 public class TestDFT {
 50 
 51     public interface ArrayComplex extends Buffer {
 52         int length();
 53 
 54         interface Complex extends Struct {
 55             float real();
 56             float imag();
 57             void real(float real);
 58             void imag(float imag);
 59         }
 60 
 61         Complex complex(long index);
 62 
 63         Schema<ArrayComplex> schema = Schema.of(ArrayComplex.class,
 64                 complex ->
 65                         complex.arrayLen("length")
 66                                 .array("complex",
 67                                         array -> array.fields("real", "imag")));
 68 
 69         static ArrayComplex create(Accelerator accelerator, int length) {
 70             return BoundSchema.of(accelerator, schema, length).allocate();
 71         }
 72     }
 73 
 74     @Reflect
 75     public static void dftKernel(
 76             ArrayComplex input,
 77             ArrayComplex output) {
 78         int size = input.length();
 79         int idx = GIX();
 80         if (idx < GSX()) {
 81             float sumReal = 0.0f;
 82             float sumImag = 0.0f;
 83             for (int k = 0; k < size; k++) {
 84                 float angle = -2 * HATMath.PI * ((k * idx) % size) / size;
 85                 Complex complexInput = input.complex(k);
 86                 float cReal = HATMath.cosf(angle);
 87                 float cImag = HATMath.sinf(angle);
 88                 sumReal += (complexInput.real() * cReal) - (complexInput.imag() * cImag);
 89                 sumImag += (complexInput.real() * cImag) + (complexInput.imag() * cReal);
 90             }
 91             Complex complexOutput = output.complex(idx);
 92             complexOutput.real(sumReal);
 93             complexOutput.imag(sumImag);
 94         }
 95     }
 96 
 97     private static void dftJava(ArrayComplex input, ArrayComplex output) {
 98         int size = input.length();
 99         for (int k = 0; k < size; k++) {
100             Complex complexOutput = output.complex(k);
101             complexOutput.real(0.0f);
102             complexOutput.imag(0.0f);
103             float sumReal = 0.0f;
104             float sumImag = 0.0f;
105             for (int j = 0; j < size; j++) {
106                 float angle = -2 * HATMath.PI * ((j * k) % size) / size;
107                 Complex complexInput = input.complex(j);
108                 float cReal = HATMath.cosf(angle);
109                 float cImag = HATMath.sinf(angle);
110                 sumReal += (complexInput.real() * cReal) - (complexInput.imag() * cImag);
111                 sumImag += (complexInput.real() * cImag) + (complexInput.imag() * cReal);
112             }
113             complexOutput.real(sumReal);
114             complexOutput.imag(sumImag);
115         }
116     }
117 
118     @Reflect
119     private static void dftCompute(ComputeContext cc, ArrayComplex input, ArrayComplex output) {
120         var range = NDRange.of1D(input.length(), 128);
121         cc.dispatchKernel(range, () -> dftKernel( input, output));
122     }
123 
124     @HatTest
125     public void testDFTWithOwnDS() {
126         var lookup = MethodHandles.lookup();
127         var accelerator = new Accelerator(lookup, Backend.FIRST);
128         final int size = 8192;
129         ArrayComplex input = create(accelerator, size);
130         ArrayComplex outputSeq = create(accelerator, size);
131         ArrayComplex outputHAT = create(accelerator, size);
132         accelerator.compute((@Reflect Compute) computeContext -> dftCompute(computeContext, input, outputHAT));
133         dftJava(input, outputSeq);
134         for (int i = 0; i < outputSeq.length(); i++) {
135             HATAsserts.assertEquals(outputSeq.complex(i).real(), outputHAT.complex(i).real(), 0.001f);
136             HATAsserts.assertEquals(outputSeq.complex(i).imag(), outputHAT.complex(i).imag(), 0.001f);
137         }
138     }
139 
140     // Simple test to check User Data Structures in Private Memory
141     public interface ArrayComplexPrivate extends NonMappableIface {
142         interface PrivateComplex extends NonMappableIface {
143             DeviceSchema<PrivateComplex> deviceSchema =
144                     DeviceSchema.of(PrivateComplex.class,
145                             complex -> complex.fields("real", "imag"));
146             float real();
147             float imag();
148             void real(float real);
149             void imag(float imag);
150         }
151         PrivateComplex complex(long index);
152         DeviceSchema<ArrayComplexPrivate> deviceSchema =
153                 DeviceSchema.of(ArrayComplexPrivate.class,
154                         complex -> complex.array("complex", 128, c2 -> c2.fields("real", "imag")));
155         static ArrayComplexPrivate createPrivate() {
156             return null;
157         }
158     }
159 
160     @Reflect
161     public static void testPrivateDS(
162             ArrayComplex input, // HOW come this is unused?
163             ArrayComplex output) {
164         int idx = GIX();
165         ArrayComplexPrivate priv = ArrayComplexPrivate.createPrivate();
166         ArrayComplexPrivate.PrivateComplex complex = priv.complex(0);
167         complex.real(1.0f);
168         complex.imag(2.0f);
169         Complex complexOutput = output.complex(idx);
170         complexOutput.real(complex.real());
171         complexOutput.imag(complex.imag());
172     }
173 
174 
175     @Reflect
176     private static void complexNumbersInPrivate(ComputeContext cc, ArrayComplex input, ArrayComplex output) {
177         var range = NDRange.of1D(input.length(), 128);
178         cc.dispatchKernel(range, () -> testPrivateDS( input, output));
179     }
180 
181     @HatTest
182     public void complexNumbersInPrivate() {
183         var lookup = MethodHandles.lookup();
184         var accelerator = new Accelerator(lookup, Backend.FIRST);
185         final int size = 8192;
186         ArrayComplex input = create(accelerator, size);
187         ArrayComplex outputSeq = create(accelerator, size);
188         ArrayComplex outputHAT = create(accelerator, size);
189         accelerator.compute((@Reflect Compute) computeContext -> complexNumbersInPrivate(computeContext, input, outputHAT));
190         for (int i = 0; i < outputSeq.length(); i++) {
191             HATAsserts.assertEquals(1.0f, outputHAT.complex(i).real(), 0.001f);
192             HATAsserts.assertEquals(2.0f, outputHAT.complex(i).imag(), 0.001f);
193         }
194     }
195 
196 }