forked from arrayfire/arrayfire-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.java
More file actions
660 lines (539 loc) · 20.1 KB
/
Array.java
File metadata and controls
660 lines (539 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package com.arrayfire;
public class Array implements AutoCloseable {
public static final int FloatType = 0;
public static final int FloatComplexType = 1;
public static final int DoubleType = 2;
public static final int DoubleComplexType = 3;
public static final int BooleanType = 4;
public static final int IntType = 5;
static {
System.loadLibrary("af_java");
}
public native static void info();
private native static long createEmptyArray(int[] dims, int type);
private native static long createArrayFromFloat(int[] dims, float[] elems);
private native static long createArrayFromDouble(int[] dims, double[] elems);
private native static long createArrayFromFloatComplex(int[] dims, FloatComplex[] elems);
private native static long createArrayFromDoubleComplex(int[] dims, DoubleComplex[] elems);
private native static long createArrayFromInt(int[] dims, int[] elems);
private native static long createArrayFromBoolean(int[] dims, boolean[] elems);
private native static long createRanduArray(int[] dims, int type);
private native static long createRandnArray(int[] dims, int type);
private native static long createConstantsArray(double val, int[] dims, int type);
private native static void destroyArray(long ref);
private native static int[] getDims(long ref);
private native static int getType(long ref);
private native static float [] getFloatFromArray(long ref);
private native static double [] getDoubleFromArray(long ref);
private native static int [] getIntFromArray(long ref);
private native static boolean [] getBooleanFromArray(long ref);
private native static FloatComplex [] getFloatComplexFromArray(long ref);
private native static DoubleComplex [] getDoubleComplexFromArray(long ref);
private native static long fft (long a);
private native static long fft2 (long a);
private native static long fft3 (long a);
private native static long ifft (long a);
private native static long ifft2(long a);
private native static long ifft3(long a);
// Binary operations
private native static long add(long a, long b);
private native static long sub(long a, long b);
private native static long mul(long a, long b);
private native static long div(long a, long b);
private native static long le (long a, long b);
private native static long lt (long a, long b);
private native static long ge (long a, long b);
private native static long gt (long a, long b);
private native static long eq (long a, long b);
private native static long ne (long a, long b);
// Unary operations
private native static long sin (long a);
private native static long cos (long a);
private native static long tan (long a);
private native static long asin (long a);
private native static long acos (long a);
private native static long atan (long a);
private native static long sinh (long a);
private native static long cosh (long a);
private native static long tanh (long a);
private native static long asinh(long a);
private native static long acosh(long a);
private native static long atanh(long a);
private native static long exp (long a);
private native static long log (long a);
private native static long abs (long a);
private native static long sqrt (long a);
// Scalar return operations
private native static double sumAll(long a);
private native static double maxAll(long a);
private native static double minAll(long a);
private native static long sum(long a, int dim);
private native static long max(long a, int dim);
private native static long min(long a, int dim);
// Scalar operations
private native static long addf(long a, float b);
private native static long subf(long a, float b);
private native static long mulf(long a, float b);
private native static long divf(long a, float b);
private native static long lef (long a, float b);
private native static long ltf (long a, float b);
private native static long gef (long a, float b);
private native static long gtf (long a, float b);
private native static long eqf (long a, float b);
private native static long nef (long a, float b);
private native static long pow (long a, float b);
private native static long fsub(float a, long b);
private native static long fdiv(float a, long b);
private native static long fle (float a, long b);
private native static long flt (float a, long b);
private native static long fge (float a, long b);
private native static long fgt (float a, long b);
// Global reference to JVM object
// to persist between JNI calls
long ref;
public Array() {
ref = 0;
}
public int[] dims() {
return getDims(ref);
}
public int type() {
return getType(ref);
}
public String typeName(int ty) throws Exception {
if (ty == FloatType) return "float";
if (ty == DoubleType) return "double";
if (ty == IntType) return "int";
if (ty == BooleanType) return "boolean";
if (ty == FloatComplexType) return "FloatComplex";
if (ty == DoubleComplexType) return "DoubleComplex";
throw new Exception("Unknown type");
}
private static int[] dim4(int[] dims) throws Exception {
if( dims == null ) {
throw new Exception("Null dimensions object provided");
} else if ( dims.length > 4 ) {
throw new Exception("ArrayFire supports up to 4 dimensions only");
}
int[] adims;
adims = new int[] {1, 1, 1, 1};
for (int i = 0; i < dims.length; i++) adims[i] = dims[i];
return adims;
}
private void assertType(int ty) throws Exception {
int myType = type();
if( myType != ty ) {
String str = "Type mismatch: ";
str = str + "Requested " + typeName(ty);
str = str + ". Found " + typeName(myType);
throw new Exception(str);
}
return;
}
// Below version of constructor
// allocates space on device and initializes
// all elemets to zero
public Array(int[] dims, int type) throws Exception {
int[] adims = dim4(dims);
ref = createEmptyArray(adims, type);
if (ref == 0) throw new Exception("Failed to create Array");
}
public Array(int[] dims) throws Exception {
int[] adims = dim4(dims);
ref = createEmptyArray(adims, FloatType);
if (ref == 0) throw new Exception("Failed to create Array");
}
public Array(int[] dims, float[] elems) throws Exception {
int[] adims = dim4(dims);
int total_size = 1;
for (int i = 0; i < adims.length; i++) total_size *= adims[i];
if(elems == null) {
throw new Exception("Null elems object provided");
}
if( elems.length > total_size || elems.length < total_size ) {
throw new Exception("Mismatching dims and array size");
}
ref = createArrayFromFloat(adims, elems);
if (ref == 0) throw new Exception("Failed to create Array");
}
public Array(int[] dims, double[] elems) throws Exception {
int[] adims = dim4(dims);
int total_size = 1;
for (int i = 0; i < adims.length; i++) total_size *= adims[i];
if(elems == null) {
throw new Exception("Null elems object provided");
}
if( elems.length > total_size || elems.length < total_size ) {
throw new Exception("Mismatching dims and array size");
}
ref = createArrayFromDouble(adims, elems);
if (ref == 0) throw new Exception("Failed to create Array");
}
public Array(int[] dims, int[] elems) throws Exception {
int[] adims = dim4(dims);
int total_size = 1;
for (int i = 0; i < adims.length; i++) total_size *= adims[i];
if(elems == null) {
throw new Exception("Null elems object provided");
}
if( elems.length > total_size || elems.length < total_size ) {
throw new Exception("Mismatching dims and array size");
}
ref = createArrayFromInt(adims, elems);
if (ref == 0) throw new Exception("Failed to create Array");
}
public Array(int[] dims, FloatComplex[] elems) throws Exception {
int[] adims = dim4(dims);
int total_size = 1;
for (int i = 0; i < adims.length; i++) total_size *= adims[i];
if(elems == null) {
throw new Exception("Null elems object provided");
}
if( elems.length > total_size || elems.length < total_size ) {
throw new Exception("Mismatching dims and array size");
}
ref = createArrayFromFloatComplex(adims, elems);
if (ref == 0) throw new Exception("Failed to create Array");
}
public Array(int[] dims, DoubleComplex[] elems) throws Exception {
int[] adims = dim4(dims);
int total_size = 1;
for (int i = 0; i < adims.length; i++) total_size *= adims[i];
if(elems == null) {
throw new Exception("Null elems object provided");
}
if( elems.length > total_size || elems.length < total_size ) {
throw new Exception("Mismatching dims and array size");
}
ref = createArrayFromDoubleComplex(adims, elems);
if (ref == 0) throw new Exception("Failed to create Array");
}
public float[] getFloatArray() throws Exception {
assertType(FloatType);
return getFloatFromArray(ref);
}
public double[] getDoubleArray() throws Exception {
assertType(DoubleType);
return getDoubleFromArray(ref);
}
public FloatComplex[] getFloatComplexArray() throws Exception {
assertType(FloatComplexType);
return getFloatComplexFromArray(ref);
}
public DoubleComplex[] getDoubleComplexArray() throws Exception {
assertType(DoubleComplexType);
return getDoubleComplexFromArray(ref);
}
public int[] getIntArray() throws Exception {
assertType(IntType);
return getIntFromArray(ref);
}
public boolean[] getBooleanArray() throws Exception {
assertType(BooleanType);
return getBooleanFromArray(ref);
}
// Binary operations
public static Array randu(int[] dims, int type) throws Exception {
int[] adims = dim4(dims);
long ref = createRanduArray(adims, type);
if (ref == 0) throw new Exception("Failed to create Array");
Array ret_val = new Array();
ret_val.ref = ref;
return ret_val;
}
public static Array randn(int[] dims, int type) throws Exception {
int[] adims = dim4(dims);
long ref = createRandnArray(adims, type);
if (ref == 0) throw new Exception("Failed to create Array");
Array ret_val = new Array();
ret_val.ref = ref;
return ret_val;
}
public static Array constant(double val, int[] dims, int type) throws Exception {
int[] adims = dim4(dims);
long ref = createConstantsArray(val, adims, type);
if (ref == 0) throw new Exception("Failed to create Array");
Array ret_val = new Array();
ret_val.ref = ref;
return ret_val;
}
public static Array add(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = add(a.ref,b.ref);
return ret_val;
}
public static Array sub(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = sub(a.ref,b.ref);
return ret_val;
}
public static Array mul(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = mul(a.ref,b.ref);
return ret_val;
}
public static Array div(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = div(a.ref,b.ref);
return ret_val;
}
public static Array le(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = le(a.ref,b.ref);
return ret_val;
}
public static Array lt(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = lt(a.ref,b.ref);
return ret_val;
}
public static Array ge(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = ge(a.ref,b.ref);
return ret_val;
}
public static Array gt(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = gt(a.ref,b.ref);
return ret_val;
}
public static Array eq(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = eq(a.ref,b.ref);
return ret_val;
}
public static Array ne(Array a, Array b) throws Exception {
Array ret_val = new Array();
ret_val.ref = ne(a.ref,b.ref);
return ret_val;
}
// Unary operations
public static Array sin(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = sin(a.ref);
return ret_val;
}
public static Array cos(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = cos(a.ref);
return ret_val;
}
public static Array tan(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = tan(a.ref);
return ret_val;
}
public static Array asin(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = asin(a.ref);
return ret_val;
}
public static Array acos(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = acos(a.ref);
return ret_val;
}
public static Array atan(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = atan(a.ref);
return ret_val;
}
public static Array sinh(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = sinh(a.ref);
return ret_val;
}
public static Array cosh(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = cosh(a.ref);
return ret_val;
}
public static Array tanh(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = tanh(a.ref);
return ret_val;
}
public static Array asinh(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = asinh(a.ref);
return ret_val;
}
public static Array acosh(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = acosh(a.ref);
return ret_val;
}
public static Array atanh(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = atanh(a.ref);
return ret_val;
}
public static Array exp(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = exp(a.ref);
return ret_val;
}
public static Array log(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = log(a.ref);
return ret_val;
}
public static Array abs(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = abs(a.ref);
return ret_val;
}
public static Array sqrt(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = sqrt(a.ref);
return ret_val;
}
// Scalar return operations
public static double sumAll(Array a) throws Exception { return sumAll(a.ref); }
public static double maxAll(Array a) throws Exception { return maxAll(a.ref); }
public static double minAll(Array a) throws Exception { return minAll(a.ref); }
public static Array fft(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = fft(a.ref);
return ret_val;
}
public static Array fft2(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = fft2(a.ref);
return ret_val;
}
public static Array fft3(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = fft3(a.ref);
return ret_val;
}
public static Array ifft(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = ifft(a.ref);
return ret_val;
}
public static Array ifft2(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = ifft2(a.ref);
return ret_val;
}
public static Array ifft3(Array a) throws Exception {
Array ret_val = new Array();
ret_val.ref = ifft3(a.ref);
return ret_val;
}
public static Array sum(Array a, int dim) throws Exception {
Array ret_val = new Array();
ret_val.ref = sum(a.ref, dim);
return ret_val;
}
public static Array max(Array a, int dim) throws Exception {
Array ret_val = new Array();
ret_val.ref = max(a.ref, dim);
return ret_val;
}
public static Array min(Array a, int dim) throws Exception {
Array ret_val = new Array();
ret_val.ref = min(a.ref, dim);
return ret_val;
}
public static Array sum(Array a) throws Exception {
return sum(a, -1);
}
public static Array max(Array a) throws Exception {
return max(a, -1);
}
public static Array min(Array a) throws Exception {
return min(a, -1);
}
// Scalar operations
public static Array add(Array a, float b) throws Exception {
Array res = new Array();
res.ref = addf(a.ref,b);
return res;
}
public static Array sub(Array a, float b) throws Exception {
Array res = new Array();
res.ref = subf(a.ref,b);
return res;
}
public static Array mul(Array a, float b) throws Exception {
Array res = new Array();
res.ref = mulf(a.ref,b);
return res;
}
public static Array div(Array a, float b) throws Exception {
Array res = new Array();
res.ref = divf(a.ref,b);
return res;
}
public static Array le(Array a, float b) throws Exception {
Array res = new Array();
res.ref = lef(a.ref,b);
return res;
}
public static Array lt(Array a, float b) throws Exception {
Array res = new Array();
res.ref = ltf(a.ref,b);
return res;
}
public static Array ge(Array a, float b) throws Exception {
Array res = new Array();
res.ref = gef(a.ref,b);
return res;
}
public static Array gt(Array a, float b) throws Exception {
Array res = new Array();
res.ref = gtf(a.ref,b);
return res;
}
public static Array eq(Array a, float b) throws Exception {
Array res = new Array();
res.ref = eqf(a.ref,b);
return res;
}
public static Array ne(Array a, float b) throws Exception {
Array res = new Array();
res.ref = nef(a.ref,b);
return res;
}
public static Array pow(Array a, float b) throws Exception {
Array res = new Array();
res.ref = pow(a.ref,b);
return res;
}
public static Array sub(float a, Array b) throws Exception {
Array res = new Array();
res.ref = fsub(a,b.ref);
return res;
}
public static Array div(float a, Array b) throws Exception {
Array res = new Array();
res.ref = fdiv(a,b.ref);
return res;
}
public static Array le(float a, Array b) throws Exception {
Array res = new Array();
res.ref = fle(a,b.ref);
return res;
}
public static Array lt(float a, Array b) throws Exception {
Array res = new Array();
res.ref = flt(a,b.ref);
return res;
}
public static Array ge(float a, Array b) throws Exception {
Array res = new Array();
res.ref = fge(a,b.ref);
return res;
}
public static Array gt(float a, Array b) throws Exception {
Array res = new Array();
res.ref = fgt(a,b.ref);
return res;
}
@Override
public void close() throws Exception {
if (ref != 0) destroyArray(ref);
}
}