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.KernelContext;
30 import static hat.KernelContext.*;
31 import hat.NDRange;
32 import hat.buffer.F32ArrayPadded;
33 import hat.device.DeviceSchema;
34 import hat.device.NonMappableIface;
35 import hat.types.Float4;
36 import jdk.incubator.code.Reflect;
37
38 import hat.backend.Backend;
39 import hat.test.annotation.HatTest;
40 import hat.test.exceptions.HATAsserts;
41
42 import java.lang.invoke.MethodHandles;
43 import java.util.Random;
44
45 public class TestVectorArrayView {
46
47 @Reflect
48 public static void vectorOps01(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
49 if (GIX() < GSX()) {
50 int index = GIX();
51
52 Float4[] vA = a.float4ArrayView();
53 Float4[] vB = b.float4ArrayView();
54 Float4[] vC = c.float4ArrayView();
55 Float4 floatA = vA[index * 4];
56 Float4 floatB = vB[index * 4];
57 Float4 res = Float4.add(floatA, floatB);
58 vC[index * 4] = res;
59 }
60 }
61
62 @Reflect
63 public static void vectorOps01WithFloat4s(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
64 if (GIX() < GSX()) {
65 int index = GIX();
66
67 Float4[] vA = a.float4ArrayView();
68 Float4[] vB = b.float4ArrayView();
69 Float4[] vC = c.float4ArrayView();
70 Float4 vAFloat = vA[index * 4];
71 Float4 vBFloat = vB[index * 4];
72 vC[index * 4] = Float4.add(vAFloat, vBFloat);
73 }
74 }
75
76 @Reflect
77 public static void vectorOps01WithSeparateAdd(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
78 if (GIX() < GSX()) {
79 int index = GIX();
80
81 Float4[] vA = a.float4ArrayView();
82 Float4[] vB = b.float4ArrayView();
83 Float4[] vC = c.float4ArrayView();
84 Float4 res = Float4.add(vA[index * 4], vB[index * 4]);
85 vC[index * 4] = res;
86 }
87 }
88
89 @Reflect
90 public static void vectorOps02(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b) {
91 if (GIX() < GSX()) {
92 int index = GIX();
93
94 Float4.MutableImpl[] vArr = a.float4ArrayView();
95 Float4.MutableImpl[] bArr = b.float4ArrayView();
96 Float4.MutableImpl vA = vArr[index * 4];
97 float scaleX = vA.x() * 10.0f;
98 vA.x(scaleX);
99 bArr[index * 4] = vA;
100 }
101 }
102
103 @Reflect
104 public static void vectorOps03(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b) {
105 if (GIX() < GSX()) {
106 int index = GIX();
107
108 Float4.MutableImpl[] vA = a.float4ArrayView();
109 Float4.MutableImpl[] vB = b.float4ArrayView();
110 Float4.MutableImpl vAFloat = vA[index * 4];
111 float scaleX = vAFloat.x() * 10.0f;
112 float scaleY = vAFloat.y() * 20.0f;
113 float scaleZ = vAFloat.z() * 30.0f;
114 float scaleW = vAFloat.w() * 40.0f;
115 vAFloat.x(scaleX);
116 vAFloat.y(scaleY);
117 vAFloat.z(scaleZ);
118 vAFloat.w(scaleW);
119 vB[index * 4] = vAFloat;
120 }
121 }
122
123 @Reflect
124 public static void vectorOps04(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b) {
125 if (GIX() < GSX()) {
126 int index = GIX();
127
128 Float4.MutableImpl[] vA = a.float4ArrayView();
129 Float4.MutableImpl[] vB = b.float4ArrayView();
130 Float4.MutableImpl vAFloat = vA[index * 4];
131 vAFloat.x(vAFloat.x() * 10.0f);
132 vAFloat.y(vAFloat.y() * 20.0f);
133 vAFloat.z(vAFloat.z() * 30.0f);
134 vAFloat.w(vAFloat.w() * 40.0f);
135 vB[index * 4] = vAFloat;
136 }
137 }
138
139 @Reflect
140 public static void vectorOps05(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
141 if (GIX() < GSX()) {
142 int index = GIX();
143
144 Float4[] vA = a.float4ArrayView();
145 Float4[] vB = b.float4ArrayView();
146 Float4[] vC = c.float4ArrayView();
147 Float4 floatA = vA[index * 4];
148 Float4 floatB = vB[index * 4];
149 Float4 temp = floatA.add(floatB);
150 Float4 res = temp.add(floatB);
151 vC[index * 4] = res;
152 }
153 }
154
155 @Reflect
156 public static void vectorOps06(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
157 if (GIX() < GSX()) {
158 int index = GIX();
159
160 Float4[] vA = a.float4ArrayView();
161 Float4[] vB = b.float4ArrayView();
162 Float4[] vC = c.float4ArrayView();
163 Float4 floatA = vA[index * 4];
164 Float4 floatB = vB[index * 4];
165 // Float4 vD = Float4.sub(floatA, floatB);
166 Float4 vE = Float4.sub(floatA, floatB);
167 vC[index * 4] = vE;
168 }
169 }
170
171 @Reflect
172 public static void vectorOps07(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
173 if (GIX() < GSX()) {
174 int index = GIX();
175
176 Float4[] vAArray = a.float4ArrayView();
177 Float4[] vBArray = b.float4ArrayView();
178 Float4[] vCArray = c.float4ArrayView();
179
180 Float4 vA = vAArray[index * 4];
181 Float4 vB = vBArray[index * 4];
182 Float4 vC = vA.add(vB);
183 Float4 vD = vC.sub(vB);
184 vCArray[index * 4] = vD;
185 }
186 }
187
188 @Reflect
189 public static void vectorOps08(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
190 if (GIX() < GSX()) {
191 int index = GIX();
192
193 Float4[] vAArray = a.float4ArrayView();
194 Float4[] vBArray = b.float4ArrayView();
195 Float4[] vCArray = c.float4ArrayView();
196
197 Float4 vA = vAArray[index * 4];
198 Float4 vB = vBArray[index * 4];
199 Float4 vC = vA.add(vB);
200 Float4 vD = vC.mul(vA);
201 Float4 vE = vD.div(vB);
202 vCArray[index * 4] = vE;
203 }
204 }
205
206 @Reflect
207 public static void vectorOps09(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c) {
208 // Checking composition
209 if (GIX() < GSX()) {
210 int index = GIX();
211 Float4[] vAArray = a.float4ArrayView();
212 Float4[] vBArray = b.float4ArrayView();
213 Float4[] vCArray = c.float4ArrayView();
214
215 Float4 vA = vAArray[index * 4];
216 Float4 vB = vBArray[index * 4];
217 Float4 temp = vA.mul(vB);
218 Float4 vC = vA.add(temp);
219 vCArray[index * 4] = vC;
220 }
221 }
222
223 private interface SharedMemory extends NonMappableIface {
224 void array(long index, float value);
225 float array(long index);
226 DeviceSchema<SharedMemory> deviceSchema = DeviceSchema.of(SharedMemory.class,
227 arr -> arr.array("array", 1024));
228 static SharedMemory createLocal() {
229 return null;
230 }
231 default Float4.MutableImpl[] float4LocalArrayView() {
232 return null;
233 }
234 }
235
236 @Reflect
237 public static void vectorOps10(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b) {
238 SharedMemory sm = SharedMemory.createLocal();
239 if (GIX() < GSX()) {
240 int index = GIX();
241 int lix = LIX();
242
243 Float4[] aArr = a.float4ArrayView();
244 Float4[] bArr = b.float4ArrayView();
245 Float4[] smArr = sm.float4LocalArrayView();
246
247 Float4 vA = aArr[index * 4];
248 smArr[lix * 4] = vA;
249 barrier();
250 Float4 r = smArr[lix * 4];
251 bArr[index * 4] = r;
252 }
253 }
254
255 private interface PrivateMemory extends NonMappableIface {
256 void array(long index, float value);
257 float array(long index);
258 DeviceSchema<PrivateMemory> deviceSchema = DeviceSchema.of(PrivateMemory.class,
259 arr -> arr.array("array", 4));
260 static PrivateMemory createPrivate() {
261 return null;
262 }
263 default Float4[] float4PrivateArrayView() {
264 return null;
265 }
266 }
267
268 @Reflect
269 public static void vectorOps11(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b) {
270 PrivateMemory pm = PrivateMemory.createPrivate();
271 if (GIX() < GSX()) {
272 int index = GIX();
273
274 Float4[] aArr = a.float4ArrayView();
275 Float4[] bArr = b.float4ArrayView();
276 Float4[] pmArr = pm.float4PrivateArrayView();
277
278 Float4 vA = aArr[index * 4];
279 pmArr[0] = vA;
280 barrier();
281 Float4 r = pmArr[0];
282 bArr[index * 4] = r;
283 }
284 }
285
286 @Reflect
287 public static void vectorOps12(KernelContext unused, F32ArrayPadded a, F32ArrayPadded b) {
288 SharedMemory sm = SharedMemory.createLocal();
289 if (GIX() < GSX()) {
290 int index = GIX();
291 int lix = LIX();
292 Float4.MutableImpl[] aArr = a.float4ArrayView();
293 Float4.MutableImpl[] bArr = b.float4ArrayView();
294 Float4.MutableImpl[] smArr = sm.float4LocalArrayView();
295
296 Float4.MutableImpl vA = aArr[index * 4];
297 Float4.MutableImpl smVector = smArr[lix * 4];
298 smVector.x(vA.x());
299 smVector.y(vA.y());
300 smVector.z(vA.z());
301 smVector.w(vA.w());
302 smArr[lix * 4] = smVector;
303 barrier();
304 Float4.MutableImpl r = smArr[lix * 4];
305 bArr[index * 4] = r;
306 }
307 }
308
309 @Reflect
310 public static void computeGraph01( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
311 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
312 cc.dispatchKernel( NDRange.of1D(size/4,128), kernelContext -> vectorOps01(kernelContext, a, b, c));
313 }
314
315 @Reflect
316 public static void computeGraph01WithFloat4s( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
317 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
318 cc.dispatchKernel(NDRange.of1D(size/4,128), kernelContext -> vectorOps01WithFloat4s(kernelContext, a, b, c));
319 }
320
321 @Reflect
322 public static void computeGraph01WithSeparateAdd( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
323 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
324 cc.dispatchKernel(NDRange.of1D(size/4,128), kernelContext -> vectorOps01WithSeparateAdd(kernelContext, a, b, c));
325 }
326
327 @Reflect
328 public static void computeGraph02( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, int size) {
329 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
330 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps02(kernelContext, a, b));
331 }
332
333 @Reflect
334 public static void computeGraph03( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, int size) {
335 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
336 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps03(kernelContext, a, b));
337 }
338
339 @Reflect
340 public static void computeGraph04( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, int size) {
341 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
342 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps04(kernelContext, a, b));
343 }
344
345 @Reflect
346 public static void computeGraph05( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
347 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
348 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps05(kernelContext, a, b, c));
349 }
350
351 @Reflect
352 public static void computeGraph06( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
353 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
354 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps06(kernelContext, a, b, c));
355 }
356
357 @Reflect
358 public static void computeGraph07( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
359 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
360 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps07(kernelContext, a, b, c));
361 }
362
363 @Reflect
364 public static void computeGraph08( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
365 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
366 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps08(kernelContext, a, b, c));
367 }
368
369 @Reflect
370 public static void computeGraph09( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, F32ArrayPadded c, int size) {
371 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
372 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps09(kernelContext, a, b, c));
373 }
374
375 @Reflect
376 public static void computeGraph10( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, int size) {
377 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
378 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps10(kernelContext, a, b));
379 }
380
381 @Reflect
382 public static void computeGraph11( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, int size) {
383 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
384 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps11(kernelContext, a, b));
385 }
386
387 @Reflect
388 public static void computeGraph12( ComputeContext cc, F32ArrayPadded a, F32ArrayPadded b, int size) {
389 // Note: we need to launch N threads / vectorWidth -> size / 4 for this example
390 cc.dispatchKernel(NDRange.of1D(size/4), kernelContext -> vectorOps12(kernelContext, a, b));
391 }
392
393 @HatTest
394 @Reflect
395 public void TestVectorArrayView01() {
396 final int size = 1024;
397 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
398 var arrayA = F32ArrayPadded.create(accelerator, size);
399 var arrayB = F32ArrayPadded.create(accelerator, size);
400 var arrayC = F32ArrayPadded.create(accelerator, size);
401
402 Random r = new Random(19);
403 for (int i = 0; i < size; i++) {
404 arrayA.array(i, r.nextFloat());
405 arrayB.array(i, r.nextFloat());
406 }
407
408 accelerator.compute(cc -> computeGraph01(cc, arrayA, arrayB, arrayC, size));
409
410 for (int i = 0; i < size; i++) {
411 HATAsserts.assertEquals((arrayA.array(i) + arrayB.array(i)), arrayC.array(i), 0.001f);
412 }
413
414 }
415
416 // @HatTest
417 // @Reflect
418 // public void TestVectorArrayView01WithFloat4s() {
419 // final int size = 1024;
420 // var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
421 // var arrayA = F32ArrayPadded.create(accelerator, size);
422 // var arrayB = F32ArrayPadded.create(accelerator, size);
423 // var arrayC = F32ArrayPadded.create(accelerator, size);
424 //
425 // Random r = new Random(19);
426 // for (int i = 0; i < size; i++) {
427 // arrayA.array(i, r.nextFloat());
428 // arrayB.array(i, r.nextFloat());
429 // }
430 //
431 // accelerator.compute(cc -> computeGraph01WithFloat4s(cc, arrayA, arrayB, arrayC, size));
432 //
433 // for (int i = 0; i < size; i++) {
434 // HATAsserts.assertEquals((arrayA.array(i) + arrayB.array(i)), arrayC.array(i), 0.001f);
435 // }
436 //
437 // }
438 //
439 // @HatTest
440 // @Reflect
441 // public void TestVectorArrayView01WithSeparateAdd() {
442 // final int size = 1024;
443 // var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
444 // var arrayA = F32ArrayPadded.create(accelerator, size);
445 // var arrayB = F32ArrayPadded.create(accelerator, size);
446 // var arrayC = F32ArrayPadded.create(accelerator, size);
447 //
448 // Random r = new Random(19);
449 // for (int i = 0; i < size; i++) {
450 // arrayA.array(i, r.nextFloat());
451 // arrayB.array(i, r.nextFloat());
452 // }
453 //
454 // accelerator.compute(cc -> computeGraph01WithSeparateAdd(cc, arrayA, arrayB, arrayC, size));
455 //
456 // for (int i = 0; i < size; i++) {
457 // HATAsserts.assertEquals((arrayA.array(i) + arrayB.array(i)), arrayC.array(i), 0.001f);
458 // }
459 //
460 // }
461
462 @HatTest
463 @Reflect
464 public void TestVectorArrayView02() {
465 final int size = 1024;
466 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
467 var arrayA = F32ArrayPadded.create(accelerator, size);
468 var arrayB = F32ArrayPadded.create(accelerator, size);
469
470 Random r = new Random(19);
471 for (int i = 0; i < size; i++) {
472 arrayA.array(i, r.nextFloat());
473 }
474
475 accelerator.compute(cc -> computeGraph02(cc, arrayA, arrayB, size));
476
477 for (int i = 0; i < size; i += 4) {
478 HATAsserts.assertEquals((arrayA.array(i + 0) * 10.0f), arrayB.array(i + 0), 0.001f);
479 HATAsserts.assertEquals((arrayA.array(i + 1)), arrayB.array(i + 1), 0.001f);
480 HATAsserts.assertEquals((arrayA.array(i + 2)), arrayB.array(i + 2), 0.001f);
481 HATAsserts.assertEquals((arrayA.array(i + 3)), arrayB.array(i + 3), 0.001f);
482 }
483 }
484
485 @HatTest
486 @Reflect
487 public void TestVectorArrayView03() {
488 final int size = 1024;
489 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
490 var arrayA = F32ArrayPadded.create(accelerator, size);
491 var arrayB = F32ArrayPadded.create(accelerator, size);
492
493 Random r = new Random(19);
494 for (int i = 0; i < size; i++) {
495 arrayA.array(i, r.nextFloat());
496 }
497
498 accelerator.compute(cc -> computeGraph03(cc, arrayA, arrayB, size));
499
500 for (int i = 0; i < size; i += 4) {
501 HATAsserts.assertEquals((arrayA.array(i + 0) * 10.0f), arrayB.array(i + 0), 0.001f);
502 HATAsserts.assertEquals((arrayA.array(i + 1) * 20.0f), arrayB.array(i + 1), 0.001f);
503 HATAsserts.assertEquals((arrayA.array(i + 2) * 30.0f), arrayB.array(i + 2), 0.001f);
504 HATAsserts.assertEquals((arrayA.array(i + 3) * 40.0f), arrayB.array(i + 3), 0.001f);
505 }
506 }
507
508 @HatTest
509 @Reflect
510 public void TestVectorArrayView04() {
511 final int size = 1024;
512 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
513 var arrayA = F32ArrayPadded.create(accelerator, size);
514 var arrayB = F32ArrayPadded.create(accelerator, size);
515
516 Random r = new Random(19);
517 for (int i = 0; i < size; i++) {
518 arrayA.array(i, r.nextFloat());
519 }
520
521 accelerator.compute(cc -> computeGraph04(cc, arrayA, arrayB, size));
522
523 for (int i = 0; i < size; i += 4) {
524 HATAsserts.assertEquals((arrayA.array(i + 0) * 10.0f), arrayB.array(i + 0), 0.001f);
525 HATAsserts.assertEquals((arrayA.array(i + 1) * 20.0f), arrayB.array(i + 1), 0.001f);
526 HATAsserts.assertEquals((arrayA.array(i + 2) * 30.0f), arrayB.array(i + 2), 0.001f);
527 HATAsserts.assertEquals((arrayA.array(i + 3) * 40.0f), arrayB.array(i + 3), 0.001f);
528 }
529 }
530
531 @HatTest
532 @Reflect
533 public void TestVectorArrayView05() {
534 final int size = 1024;
535 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
536 var arrayA = F32ArrayPadded.create(accelerator, size);
537 var arrayB = F32ArrayPadded.create(accelerator, size);
538 var arrayC = F32ArrayPadded.create(accelerator, size);
539
540 Random r = new Random(19);
541 for (int i = 0; i < size; i++) {
542 arrayA.array(i, r.nextFloat());
543 arrayB.array(i, r.nextFloat());
544 }
545
546 accelerator.compute(cc -> computeGraph05(cc, arrayA, arrayB, arrayC, size));
547
548 for (int i = 0; i < size; i ++) {
549 HATAsserts.assertEquals((arrayA.array(i) + arrayB.array(i) + arrayB.array(i)), arrayC.array(i), 0.001f);
550 }
551 }
552
553 @HatTest
554 @Reflect
555 public void TestVectorArrayView06() {
556 final int size = 1024;
557 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
558 var arrayA = F32ArrayPadded.create(accelerator, size);
559 var arrayB = F32ArrayPadded.create(accelerator, size);
560 var arrayC = F32ArrayPadded.create(accelerator, size);
561
562 Random r = new Random(19);
563 for (int i = 0; i < size; i++) {
564 arrayA.array(i, r.nextFloat());
565 arrayB.array(i, r.nextFloat());
566 }
567
568 accelerator.compute(cc -> computeGraph06(cc, arrayA, arrayB, arrayC, size));
569
570 for (int i = 0; i < size; i ++) {
571 HATAsserts.assertEquals((arrayA.array(i) - arrayB.array(i)), arrayC.array(i), 0.001f);
572 }
573 }
574
575 @HatTest
576 @Reflect
577 public void TestVectorArrayView07() {
578 final int size = 1024;
579 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
580 var arrayA = F32ArrayPadded.create(accelerator, size);
581 var arrayB = F32ArrayPadded.create(accelerator, size);
582 var arrayC = F32ArrayPadded.create(accelerator, size);
583
584 Random r = new Random(19);
585 for (int i = 0; i < size; i++) {
586 arrayA.array(i, r.nextFloat());
587 arrayB.array(i, r.nextFloat());
588 }
589
590 accelerator.compute(cc -> computeGraph07(cc, arrayA, arrayB, arrayC, size));
591
592 for (int i = 0; i < size; i ++) {
593 HATAsserts.assertEquals(arrayA.array(i), arrayC.array(i), 0.001f);
594 }
595 }
596
597 @HatTest
598 @Reflect
599 public void TestVectorArrayView08() {
600 final int size = 1024;
601 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
602 var arrayA = F32ArrayPadded.create(accelerator, size);
603 var arrayB = F32ArrayPadded.create(accelerator, size);
604 var arrayC = F32ArrayPadded.create(accelerator, size);
605
606 Random r = new Random(19);
607 for (int i = 0; i < size; i++) {
608 arrayA.array(i, r.nextFloat());
609 arrayB.array(i, r.nextFloat());
610 }
611
612 accelerator.compute(cc -> computeGraph08(cc, arrayA, arrayB, arrayC, size));
613
614 for (int i = 0; i < size; i ++) {
615 float val = (((arrayA.array(i) + arrayB.array(i)) * arrayA.array(i)) / arrayB.array(i));
616 HATAsserts.assertEquals(val, arrayC.array(i), 0.001f);
617 }
618 }
619
620 @HatTest
621 @Reflect
622 public void TestVectorArrayView09() {
623 final int size = 1024;
624 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
625 var arrayA = F32ArrayPadded.create(accelerator, size);
626 var arrayB = F32ArrayPadded.create(accelerator, size);
627 var arrayC = F32ArrayPadded.create(accelerator, size);
628
629 Random r = new Random(19);
630 for (int i = 0; i < size; i++) {
631 arrayA.array(i, r.nextFloat());
632 arrayB.array(i, r.nextFloat());
633 }
634
635 accelerator.compute(cc -> computeGraph09(cc, arrayA, arrayB, arrayC, size));
636
637 for (int i = 0; i < size; i ++) {
638 float val = (arrayA.array(i) + (arrayB.array(i)) * arrayA.array(i));
639 HATAsserts.assertEquals(val, arrayC.array(i), 0.001f);
640 }
641 }
642
643 @HatTest
644 @Reflect
645 public void TestVectorArrayView10() {
646 final int size = 1024;
647 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
648 var arrayA = F32ArrayPadded.create(accelerator, size);
649 var arrayB = F32ArrayPadded.create(accelerator, size);
650
651 Random r = new Random(19);
652 for (int i = 0; i < size; i++) {
653 arrayA.array(i, r.nextFloat());
654 arrayB.array(i, r.nextFloat());
655 }
656
657 accelerator.compute(cc -> computeGraph10(cc, arrayA, arrayB, size));
658
659 for (int i = 0; i < size; i ++) {
660 HATAsserts.assertEquals(arrayA.array(i), arrayB.array(i), 0.001f);
661 }
662 }
663
664 @HatTest
665 @Reflect
666 public void TestVectorArrayView11() {
667 final int size = 1024;
668 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
669 var arrayA = F32ArrayPadded.create(accelerator, size);
670 var arrayB = F32ArrayPadded.create(accelerator, size);
671
672 Random r = new Random(19);
673 for (int i = 0; i < size; i++) {
674 arrayA.array(i, r.nextFloat());
675 arrayB.array(i, r.nextFloat());
676 }
677
678 accelerator.compute(cc -> computeGraph11(cc, arrayA, arrayB, size));
679
680 for (int i = 0; i < size; i ++) {
681 HATAsserts.assertEquals(arrayA.array(i), arrayB.array(i), 0.001f);
682 }
683 }
684
685 @HatTest
686 @Reflect
687 public void TestVectorArrayView12() {
688 final int size = 1024;
689 var accelerator = new Accelerator(MethodHandles.lookup(), Backend.FIRST);
690 var arrayA = F32ArrayPadded.create(accelerator, size);
691 var arrayB = F32ArrayPadded.create(accelerator, size);
692
693 Random r = new Random(19);
694 for (int i = 0; i < size; i++) {
695 arrayA.array(i, r.nextFloat());
696 arrayB.array(i, r.nextFloat());
697 }
698
699 accelerator.compute(cc -> computeGraph12(cc, arrayA, arrayB, size));
700
701 for (int i = 0; i < size; i ++) {
702 HATAsserts.assertEquals(arrayA.array(i), arrayB.array(i), 0.001f);
703 }
704 }
705 }