|
| 1 | +package com.arrayfire; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +public class Array implements AutoCloseable { |
| 6 | + |
| 7 | + static { |
| 8 | + System.loadLibrary("arrayfire"); |
| 9 | + } |
| 10 | + |
| 11 | + private native static long createArray(int[] dims); |
| 12 | + private native static long createArrayElems(int[] dims, float[] elems); |
| 13 | + private native static void destroyArray(long ref); |
| 14 | + private native static float[] host(long ref); |
| 15 | + private native static int[] getDims(long ref); |
| 16 | + |
| 17 | + // Binary operations |
| 18 | + private native static long add(long a, long b); |
| 19 | + private native static long sub(long a, long b); |
| 20 | + private native static long mul(long a, long b); |
| 21 | + private native static long div(long a, long b); |
| 22 | + private native static long le (long a, long b); |
| 23 | + private native static long lt (long a, long b); |
| 24 | + private native static long ge (long a, long b); |
| 25 | + private native static long gt (long a, long b); |
| 26 | + private native static long eq (long a, long b); |
| 27 | + private native static long ne (long a, long b); |
| 28 | + |
| 29 | + // Unary operations |
| 30 | + private native static long sin (long a); |
| 31 | + private native static long cos (long a); |
| 32 | + private native static long tan (long a); |
| 33 | + private native static long asin (long a); |
| 34 | + private native static long acos (long a); |
| 35 | + private native static long atan (long a); |
| 36 | + private native static long sinh (long a); |
| 37 | + private native static long cosh (long a); |
| 38 | + private native static long tanh (long a); |
| 39 | + private native static long asinh(long a); |
| 40 | + private native static long acosh(long a); |
| 41 | + private native static long atanh(long a); |
| 42 | + private native static long exp (long a); |
| 43 | + private native static long log (long a); |
| 44 | + private native static long abs (long a); |
| 45 | + private native static long sqrt (long a); |
| 46 | + |
| 47 | + // Scalar return operations |
| 48 | + private native static float sum(long a); |
| 49 | + private native static float max(long a); |
| 50 | + private native static float min(long a); |
| 51 | + |
| 52 | + // Scalar operations |
| 53 | + private native static long addf(long a, float b); |
| 54 | + private native static long subf(long a, float b); |
| 55 | + private native static long mulf(long a, float b); |
| 56 | + private native static long divf(long a, float b); |
| 57 | + private native static long lef (long a, float b); |
| 58 | + private native static long ltf (long a, float b); |
| 59 | + private native static long gef (long a, float b); |
| 60 | + private native static long gtf (long a, float b); |
| 61 | + private native static long eqf (long a, float b); |
| 62 | + private native static long nef (long a, float b); |
| 63 | + private native static long pow (long a, float b); |
| 64 | + private native static long fsub(float a, long b); |
| 65 | + private native static long fdiv(float a, long b); |
| 66 | + private native static long fle (float a, long b); |
| 67 | + private native static long flt (float a, long b); |
| 68 | + private native static long fge (float a, long b); |
| 69 | + private native static long fgt (float a, long b); |
| 70 | + |
| 71 | + // Global reference to JVM object |
| 72 | + // to persist between JNI calls |
| 73 | + long ref; |
| 74 | + |
| 75 | + public Array() { |
| 76 | + ref = 0; |
| 77 | + } |
| 78 | + |
| 79 | + // Below version of constructor |
| 80 | + // allocates space on device and initializes |
| 81 | + // all elemets to zero |
| 82 | + public Array(int[] dims) throws Exception { |
| 83 | + if( dims == null ) { |
| 84 | + throw new Exception("Null dimensions object provided"); |
| 85 | + } else if ( dims.length > 3 ) { |
| 86 | + throw new Exception("Upto 3 dimensions only supported for now."); |
| 87 | + } |
| 88 | + this.ref = createArray(dims); |
| 89 | + } |
| 90 | + |
| 91 | + public Array(int[] dims, float[] elems) throws Exception { |
| 92 | + if( dims == null || elems == null ) { |
| 93 | + throw new Exception("Null object provided"); |
| 94 | + } else if ( dims.length > 3 ) { |
| 95 | + throw new Exception("Upto 3 dimensions only supported for now."); |
| 96 | + } |
| 97 | + int total_size = 1; |
| 98 | + for (int i = 0; i < dims.length; i++) total_size *= dims[i]; |
| 99 | + |
| 100 | + if( elems.length > total_size || elems.length < total_size ) { |
| 101 | + throw new Exception("Mismatching dims and array size"); |
| 102 | + } |
| 103 | + Log.i("Array", "============ Array class =============== "); |
| 104 | + this.ref = createArrayElems(dims,elems); |
| 105 | + } |
| 106 | + |
| 107 | + public int[] dims() { |
| 108 | + return getDims(ref); |
| 109 | + } |
| 110 | + |
| 111 | + public float[] host() throws Exception { |
| 112 | + return host(ref); |
| 113 | + } |
| 114 | + |
| 115 | + // Binary operations |
| 116 | + public static Array add(Array a, Array b) throws Exception { |
| 117 | + Array ret_val = new Array(); |
| 118 | + ret_val.ref = add(a.ref,b.ref); |
| 119 | + return ret_val; |
| 120 | + } |
| 121 | + |
| 122 | + public static Array sub(Array a, Array b) throws Exception { |
| 123 | + Array ret_val = new Array(); |
| 124 | + ret_val.ref = sub(a.ref,b.ref); |
| 125 | + return ret_val; |
| 126 | + } |
| 127 | + |
| 128 | + public static Array mul(Array a, Array b) throws Exception { |
| 129 | + Array ret_val = new Array(); |
| 130 | + ret_val.ref = mul(a.ref,b.ref); |
| 131 | + return ret_val; |
| 132 | + } |
| 133 | + |
| 134 | + public static Array div(Array a, Array b) throws Exception { |
| 135 | + Array ret_val = new Array(); |
| 136 | + ret_val.ref = div(a.ref,b.ref); |
| 137 | + return ret_val; |
| 138 | + } |
| 139 | + |
| 140 | + public static Array le(Array a, Array b) throws Exception { |
| 141 | + Array ret_val = new Array(); |
| 142 | + ret_val.ref = le(a.ref,b.ref); |
| 143 | + return ret_val; |
| 144 | + } |
| 145 | + |
| 146 | + public static Array lt(Array a, Array b) throws Exception { |
| 147 | + Array ret_val = new Array(); |
| 148 | + ret_val.ref = lt(a.ref,b.ref); |
| 149 | + return ret_val; |
| 150 | + } |
| 151 | + |
| 152 | + public static Array ge(Array a, Array b) throws Exception { |
| 153 | + Array ret_val = new Array(); |
| 154 | + ret_val.ref = ge(a.ref,b.ref); |
| 155 | + return ret_val; |
| 156 | + } |
| 157 | + |
| 158 | + public static Array gt(Array a, Array b) throws Exception { |
| 159 | + Array ret_val = new Array(); |
| 160 | + ret_val.ref = gt(a.ref,b.ref); |
| 161 | + return ret_val; |
| 162 | + } |
| 163 | + |
| 164 | + public static Array eq(Array a, Array b) throws Exception { |
| 165 | + Array ret_val = new Array(); |
| 166 | + ret_val.ref = eq(a.ref,b.ref); |
| 167 | + return ret_val; |
| 168 | + } |
| 169 | + |
| 170 | + public static Array ne(Array a, Array b) throws Exception { |
| 171 | + Array ret_val = new Array(); |
| 172 | + ret_val.ref = ne(a.ref,b.ref); |
| 173 | + return ret_val; |
| 174 | + } |
| 175 | + |
| 176 | + // Unary operations |
| 177 | + public static Array sin(Array a) throws Exception { |
| 178 | + Array ret_val = new Array(); |
| 179 | + ret_val.ref = sin(a.ref); |
| 180 | + return ret_val; |
| 181 | + } |
| 182 | + |
| 183 | + public static Array cos(Array a) throws Exception { |
| 184 | + Array ret_val = new Array(); |
| 185 | + ret_val.ref = cos(a.ref); |
| 186 | + return ret_val; |
| 187 | + } |
| 188 | + |
| 189 | + public static Array tan(Array a) throws Exception { |
| 190 | + Array ret_val = new Array(); |
| 191 | + ret_val.ref = tan(a.ref); |
| 192 | + return ret_val; |
| 193 | + } |
| 194 | + |
| 195 | + public static Array asin(Array a) throws Exception { |
| 196 | + Array ret_val = new Array(); |
| 197 | + ret_val.ref = asin(a.ref); |
| 198 | + return ret_val; |
| 199 | + } |
| 200 | + |
| 201 | + public static Array acos(Array a) throws Exception { |
| 202 | + Array ret_val = new Array(); |
| 203 | + ret_val.ref = acos(a.ref); |
| 204 | + return ret_val; |
| 205 | + } |
| 206 | + |
| 207 | + public static Array atan(Array a) throws Exception { |
| 208 | + Array ret_val = new Array(); |
| 209 | + ret_val.ref = atan(a.ref); |
| 210 | + return ret_val; |
| 211 | + } |
| 212 | + |
| 213 | + public static Array sinh(Array a) throws Exception { |
| 214 | + Array ret_val = new Array(); |
| 215 | + ret_val.ref = sinh(a.ref); |
| 216 | + return ret_val; |
| 217 | + } |
| 218 | + |
| 219 | + public static Array cosh(Array a) throws Exception { |
| 220 | + Array ret_val = new Array(); |
| 221 | + ret_val.ref = cosh(a.ref); |
| 222 | + return ret_val; |
| 223 | + } |
| 224 | + |
| 225 | + public static Array tanh(Array a) throws Exception { |
| 226 | + Array ret_val = new Array(); |
| 227 | + ret_val.ref = tanh(a.ref); |
| 228 | + return ret_val; |
| 229 | + } |
| 230 | + |
| 231 | + public static Array asinh(Array a) throws Exception { |
| 232 | + Array ret_val = new Array(); |
| 233 | + ret_val.ref = asinh(a.ref); |
| 234 | + return ret_val; |
| 235 | + } |
| 236 | + public static Array acosh(Array a) throws Exception { |
| 237 | + Array ret_val = new Array(); |
| 238 | + ret_val.ref = acosh(a.ref); |
| 239 | + return ret_val; |
| 240 | + } |
| 241 | + public static Array atanh(Array a) throws Exception { |
| 242 | + Array ret_val = new Array(); |
| 243 | + ret_val.ref = atanh(a.ref); |
| 244 | + return ret_val; |
| 245 | + } |
| 246 | + public static Array exp(Array a) throws Exception { |
| 247 | + Array ret_val = new Array(); |
| 248 | + ret_val.ref = exp(a.ref); |
| 249 | + return ret_val; |
| 250 | + } |
| 251 | + public static Array log(Array a) throws Exception { |
| 252 | + Array ret_val = new Array(); |
| 253 | + ret_val.ref = log(a.ref); |
| 254 | + return ret_val; |
| 255 | + } |
| 256 | + public static Array abs(Array a) throws Exception { |
| 257 | + Array ret_val = new Array(); |
| 258 | + ret_val.ref = abs(a.ref); |
| 259 | + return ret_val; |
| 260 | + } |
| 261 | + public static Array sqrt(Array a) throws Exception { |
| 262 | + Array ret_val = new Array(); |
| 263 | + ret_val.ref = sqrt(a.ref); |
| 264 | + return ret_val; |
| 265 | + } |
| 266 | + |
| 267 | + // Scalar return operations |
| 268 | + public static float sum(Array a) throws Exception { return sum(a.ref); } |
| 269 | + |
| 270 | + public static float max(Array a) throws Exception { return max(a.ref); } |
| 271 | + |
| 272 | + public static float min(Array a) throws Exception { return min(a.ref); } |
| 273 | + |
| 274 | + // Scalar operations |
| 275 | + public static Array add(Array a, float b) throws Exception { |
| 276 | + Array res = new Array(); |
| 277 | + res.ref = addf(a.ref,b); |
| 278 | + return res; |
| 279 | + } |
| 280 | + |
| 281 | + public static Array sub(Array a, float b) throws Exception { |
| 282 | + Array res = new Array(); |
| 283 | + res.ref = subf(a.ref,b); |
| 284 | + return res; |
| 285 | + } |
| 286 | + |
| 287 | + public static Array mul(Array a, float b) throws Exception { |
| 288 | + Array res = new Array(); |
| 289 | + res.ref = mulf(a.ref,b); |
| 290 | + return res; |
| 291 | + } |
| 292 | + |
| 293 | + public static Array div(Array a, float b) throws Exception { |
| 294 | + Array res = new Array(); |
| 295 | + res.ref = divf(a.ref,b); |
| 296 | + return res; |
| 297 | + } |
| 298 | + |
| 299 | + public static Array le(Array a, float b) throws Exception { |
| 300 | + Array res = new Array(); |
| 301 | + res.ref = lef(a.ref,b); |
| 302 | + return res; |
| 303 | + } |
| 304 | + |
| 305 | + public static Array lt(Array a, float b) throws Exception { |
| 306 | + Array res = new Array(); |
| 307 | + res.ref = ltf(a.ref,b); |
| 308 | + return res; |
| 309 | + } |
| 310 | + |
| 311 | + public static Array ge(Array a, float b) throws Exception { |
| 312 | + Array res = new Array(); |
| 313 | + res.ref = gef(a.ref,b); |
| 314 | + return res; |
| 315 | + } |
| 316 | + |
| 317 | + public static Array gt(Array a, float b) throws Exception { |
| 318 | + Array res = new Array(); |
| 319 | + res.ref = gtf(a.ref,b); |
| 320 | + return res; |
| 321 | + } |
| 322 | + |
| 323 | + public static Array eq(Array a, float b) throws Exception { |
| 324 | + Array res = new Array(); |
| 325 | + res.ref = eqf(a.ref,b); |
| 326 | + return res; |
| 327 | + } |
| 328 | + |
| 329 | + public static Array ne(Array a, float b) throws Exception { |
| 330 | + Array res = new Array(); |
| 331 | + res.ref = nef(a.ref,b); |
| 332 | + return res; |
| 333 | + } |
| 334 | + |
| 335 | + public static Array pow(Array a, float b) throws Exception { |
| 336 | + Array res = new Array(); |
| 337 | + res.ref = pow(a.ref,b); |
| 338 | + return res; |
| 339 | + } |
| 340 | + |
| 341 | + public static Array sub(float a, Array b) throws Exception { |
| 342 | + Array res = new Array(); |
| 343 | + res.ref = fsub(a,b.ref); |
| 344 | + return res; |
| 345 | + } |
| 346 | + |
| 347 | + public static Array div(float a, Array b) throws Exception { |
| 348 | + Array res = new Array(); |
| 349 | + res.ref = fdiv(a,b.ref); |
| 350 | + return res; |
| 351 | + } |
| 352 | + |
| 353 | + public static Array le(float a, Array b) throws Exception { |
| 354 | + Array res = new Array(); |
| 355 | + res.ref = fle(a,b.ref); |
| 356 | + return res; |
| 357 | + } |
| 358 | + |
| 359 | + public static Array lt(float a, Array b) throws Exception { |
| 360 | + Array res = new Array(); |
| 361 | + res.ref = flt(a,b.ref); |
| 362 | + return res; |
| 363 | + } |
| 364 | + |
| 365 | + public static Array ge(float a, Array b) throws Exception { |
| 366 | + Array res = new Array(); |
| 367 | + res.ref = fge(a,b.ref); |
| 368 | + return res; |
| 369 | + } |
| 370 | + |
| 371 | + public static Array gt(float a, Array b) throws Exception { |
| 372 | + Array res = new Array(); |
| 373 | + res.ref = fgt(a,b.ref); |
| 374 | + return res; |
| 375 | + } |
| 376 | + |
| 377 | + @Override |
| 378 | + public void close() throws Exception { |
| 379 | + if (ref != 0) destroyArray(ref); |
| 380 | + } |
| 381 | + |
| 382 | +} |
0 commit comments