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 
 31 import static hat.KernelContext.*;
 32 import hat.backend.Backend;
 33 import hat.buffer.S32Array;
 34 import hat.buffer.S32Array2D;
 35 import jdk.incubator.code.Reflect;
 36 import hat.test.annotation.HatTest;
 37 import hat.test.exceptions.HATAsserts;
 38 
 39 import java.lang.invoke.MethodHandles;
 40 
 41 public class TestMandel {
 42 
 43     @Reflect
 44     public static void mandel( S32Array2D s32Array2D, S32Array pallette, float offsetx, float offsety, float scale) {
 45         if (GIX() < GSX()) {
 46             float width = s32Array2D.width();
 47             float height = s32Array2D.height();
 48             float x = ((GIX() % s32Array2D.width()) * scale - (scale / 2f * width)) / width + offsetx;
 49             float y = ((GIX() / s32Array2D.width()) * scale - (scale / 2f * height)) / height + offsety;
 50             float zx = x;
 51             float zy = y;
 52             float new_zx;
 53             int colorIdx = 0;
 54             while ((colorIdx < pallette.length()) && (((zx * zx) + (zy * zy)) < 4f)) {
 55                 new_zx = ((zx * zx) - (zy * zy)) + x;
 56                 zy = (2f * zx * zy) + y;
 57                 zx = new_zx;
 58                 colorIdx++;
 59             }
 60             int color = colorIdx < pallette.length() ? pallette.array(colorIdx) : 0;
 61             s32Array2D.array(GIX(), color);
 62         }
 63     }
 64 
 65     @Reflect
 66     static public void compute(final ComputeContext computeContext, S32Array pallete, S32Array2D s32Array2D, float x, float y, float scale) {
 67         computeContext.dispatchKernel(NDRange.of1D(s32Array2D.width() * s32Array2D.height()),
 68                 () -> TestMandel.mandel( s32Array2D, pallete, x, y, scale));
 69     }
 70 
 71     public static void mandelSeq(S32Array2D s32Array2D, S32Array pallette, float offsetx, float offsety, float scale) {
 72         for (int i = 0; i < pallette.length(); i++) {
 73             float width = s32Array2D.width();
 74             float height = s32Array2D.height();
 75             float x = ((i % s32Array2D.width()) * scale - (scale / 2f * width)) / width + offsetx;
 76             float y = (((float) i / s32Array2D.width()) * scale - (scale / 2f * height)) / height + offsety;
 77             float zx = x;
 78             float zy = y;
 79             float new_zx;
 80             int colorIdx = 0;
 81             while ((colorIdx < pallette.length()) && (((zx * zx) + (zy * zy)) < 4f)) {
 82                 new_zx = ((zx * zx) - (zy * zy)) + x;
 83                 zy = (2f * zx * zy) + y;
 84                 zx = new_zx;
 85                 colorIdx++;
 86             }
 87             int color = colorIdx < pallette.length() ? pallette.array(colorIdx) : 0;
 88             s32Array2D.array(i, color);
 89         }
 90 
 91     }
 92 
 93     @HatTest
 94     @Reflect
 95     public void testMandel() {
 96         Accelerator accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
 97         final int width = 1024;
 98         final int height = 1024;
 99         final float defaultScale = 3f;
100         final float originX = -1f;
101         final float originY = 0;
102         final int maxIterations = 64;
103 
104         S32Array2D s32Array2D = S32Array2D.create(accelerator, width, height);
105         S32Array2D check = S32Array2D.create(accelerator, width, height);
106 
107         int[] palletteArray = new int[maxIterations];
108         for (int i = 1; i < maxIterations; i++) {
109             palletteArray[i]=(i/8+1);// 0-7?
110         }
111         palletteArray[0]=0;
112         S32Array pallette = S32Array.createFrom(accelerator, palletteArray);
113 
114         accelerator.compute(cc -> TestMandel.compute(cc, pallette, s32Array2D, originX, originY, defaultScale));
115 
116         // Check
117         TestMandel.mandelSeq(check, pallette, originX, originY, defaultScale);
118 
119         int subsample = 16;
120         for (int y = 0; y<height/subsample; y++) {
121             for (int x = 0; x<width/subsample; x++) {
122                 int palletteValue = s32Array2D.get(x * subsample, y * subsample); // so 0->8
123                 int palletteValueSeq = s32Array2D.get(x * subsample, y * subsample); // so 0->8
124                 HATAsserts.assertEquals(palletteValueSeq, palletteValue);
125             }
126         }
127     }
128 }