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 import hat.KernelContext;
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(KernelContext unused, 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 kc -> TestMandel.mandel(kc, s32Array2D, pallete, x, y, scale)
69 );
70 }
71
72 public static void mandelSeq(S32Array2D s32Array2D, S32Array pallette, float offsetx, float offsety, float scale) {
73 for (int i = 0; i < pallette.length(); i++) {
74 float width = s32Array2D.width();
75 float height = s32Array2D.height();
76 float x = ((i % s32Array2D.width()) * scale - (scale / 2f * width)) / width + offsetx;
77 float y = (((float) i / s32Array2D.width()) * scale - (scale / 2f * height)) / height + offsety;
78 float zx = x;
79 float zy = y;
80 float new_zx;
81 int colorIdx = 0;
82 while ((colorIdx < pallette.length()) && (((zx * zx) + (zy * zy)) < 4f)) {
83 new_zx = ((zx * zx) - (zy * zy)) + x;
84 zy = (2f * zx * zy) + y;
85 zx = new_zx;
86 colorIdx++;
87 }
88 int color = colorIdx < pallette.length() ? pallette.array(colorIdx) : 0;
89 s32Array2D.array(i, color);
90 }
91
92 }
93
94 @HatTest
95 @Reflect
96 public void testMandel() {
97 Accelerator accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
98 final int width = 1024;
99 final int height = 1024;
100 final float defaultScale = 3f;
101 final float originX = -1f;
102 final float originY = 0;
103 final int maxIterations = 64;
104
105 S32Array2D s32Array2D = S32Array2D.create(accelerator, width, height);
106 S32Array2D check = S32Array2D.create(accelerator, width, height);
107
108 int[] palletteArray = new int[maxIterations];
109 for (int i = 1; i < maxIterations; i++) {
110 palletteArray[i]=(i/8+1);// 0-7?
111 }
112 palletteArray[0]=0;
113 S32Array pallette = S32Array.createFrom(accelerator, palletteArray);
114
115 accelerator.compute(cc -> TestMandel.compute(cc, pallette, s32Array2D, originX, originY, defaultScale));
116
117 // Check
118 TestMandel.mandelSeq(check, pallette, originX, originY, defaultScale);
119
120 int subsample = 16;
121 for (int y = 0; y<height/subsample; y++) {
122 for (int x = 0; x<width/subsample; x++) {
123 int palletteValue = s32Array2D.get(x * subsample, y * subsample); // so 0->8
124 int palletteValueSeq = s32Array2D.get(x * subsample, y * subsample); // so 0->8
125 HATAsserts.assertEquals(palletteValueSeq, palletteValue);
126 }
127 }
128 }
129 }