ArrayFire is a library for GPU and accelerated computing. ArrayFire.jl wraps the ArrayFire library for Julia, and provides a Julia interface.

Install ArrayFire library: either download a binary from the official site, or you can build from source.

In Julia v0.5 and v0.6:

```
Pkg.add("ArrayFire")
Pkg.checkout("ArrayFire")
```

In Julia v0.7:

```
Pkg.add("ArrayFire")
Pkg.checkout("ArrayFire", "julia-v0.7")
```

Congratulations, you've now installed `ArrayFire.jl`

! Now what can you do?

Let's say you have a simple Julia array on the CPU:

```
a = rand(10, 10)
```

You can transfer this array to the device by calling the `AFArray`

constructor on it.

```
using ArrayFire # Don't forget to load the library
ad = AFArray(a)
```

Now let us perform some simple arithmetic on it:

```
bd = (ad + 1) / 5
```

Of course, you can do much more than just add and divide numbers. Check the supported functions section for more information.

Now that you're done with all your device computation, you can bring your array back to the CPU (or host):

```
b = Array(bd)
```

Here are other examples of simple usage:

```
using ArrayFire
# Random number generation
a = rand(AFArray{Float64}, 100, 100)
b = randn(AFArray{Float64}, 100, 100)
# Transfer to device from the CPU
host_to_device = AFArray(rand(100,100))
# Transfer back to CPU
device_to_host = Array(host_to_device)
# Basic arithmetic operations
c = sin(a) + 0.5
d = a * 5
# Logical operations
c = a .> b
any_trues = any(c)
# Reduction operations
total_max = maximum(a)
colwise_min = min(a,2)
# Matrix operations
determinant = det(a)
b_positive = abs(b)
product = a * b
dot_product = a .* b
transposer = a'
# Linear Algebra
lu_fact = lu(a)
cholesky_fact = chol(a*a') # Multiplied to create a positive definite matrix
qr_fact = qr(a)
svd_fact = svd(a)
# FFT
fast_fourier = fft(a)
```

`ArrayFire.jl`

introduces an `AFArray`

type that is a subtype of `AbstractArray`

. Operations on `AFArrays`

create other `AFArrays`

, so data always remains on the device unless it is specifically transferred back. This wrapper provides a simple Julian interface that aims to mimic Base Julia's versatility and ease of use.

**REPL Behaviour**: On the REPL, whenever you create an `AFArray`

, the REPL displays the values, just like in Base Julia. This happens because the `showarray`

method is overloaded to ensure that every time it is needed to display on the REPL, values are transferred from device to host. This means that every single operation on the REPL involves an implicit memory transfer. This may lead to some slowdown while working interactively depending on the size of the data and memory bandwidth available. You can use a semicolon (`;`

) at the end of each statement to disable displaying and avoid that memory transfer. Also, note that in a script, there would be no memory transfer unless a display function is explicitly called (or if you use the `Array`

constructor like in the above example).

**Async Behaviour**: `arrayfire`

is an asynchronous library. This essentially means that whenever you call a particular function in `ArrayFire.jl`

, it would return control to the host almost immediately (which in this case in Julia) and continue executing on the device. This is pretty useful because it would mean that host code that's independent of the device can simply execute while the device computes, resulting in better real world performance.

The library also performs some kernel fusions on elementary arithmetic operations (see the arithmetic section of the Supported Functions). `arrayfire`

has an intelligent runtime JIT compliation engine which converts array expressions into the smallest number of OpenCL/CUDA kernels. Kernel fusion not only decreases the number of kernel calls, but also avoids extraneous global memory operations. This asynchronous behaviour ends only when a non-JIT operation is called or an explicit synchronization barrier `sync(array)`

is called.

**Garbage collection and memory management**: `arrayfire`

is using its own memory management that relies on Julia garbage collector releasing refences to unused arrays. Sometimes it could be a bottleneck as Julia garbage collector can be slow and not even notice the pressure in GPU memory usage. The best way to avoid it is to use `@afgc`

macro that would free all unused `AFArray`

references when leaving the scope of a function or a block. The alternative is to call `afgc()`

periodically.

**A note on benchmarking**: In Julia, one would use the `@time`

macro to time execution times of functions. However, in this particular case, `@time`

would simply time the function call, and the library would execute asynchronously in the background. This would often lead to misleading timings. Therefore, the right way to time individual operations is to run them multiple times, place an explicit synchronization barrier at the end, and take the average of multiple runs.

Also, note that this does not affect how the user writes code. Users can simply write normal Julia code using `ArrayFire.jl`

and this asynchronous behaviour is abstracted out. Whenever the data is needed back onto the CPU, an implicit barrier ensures that the computatation is complete, and the values are transferred back.

**Operations between CPU and device arrays**: Consider the following code. It will return an error:

```
a = rand(Float32, 10, 10)
b = AFArray(a)
a - b # Throws Error
```

This is because the two arrays reside in different regions of memory (host and device), and for any coherent operation to be performed, one array would have to be transferred to other region in memory. `ArrayFire.jl`

does *not* do this automatically for performance considerations. Therefore, to make this work, you would have to manually transfer one of the arrays to the other memory. The following operations would work:

```
a - Array(b) # Works!
AFArray(a) - b # This works too!
```

**A note on correctness**: Sometimes, `ArrayFire.jl`

and Base Julia might return marginally different values from their computation. This is because Julia and `ArrayFire.jl`

sometimes use different lower level libraries for BLAS, FFT, etc. For example, Julia uses OpenBLAS for BLAS operations, but `ArrayFire.jl`

would use clBLAS for the OpenCL backend and CuBLAS for the CUDA backend, and these libraries might not always the exact same values as OpenBLAS after a certain decimal point. In light of this, users are encouraged to keep testing their codes for correctness.

**A note on performance**: Some operations can be slow due to `Base`

's generic implementations. This is intentional, to enable a "make it work, then make it fast" workflow. When you're ready you can disable slow fallback methods:

```
julia> allowslow(AFArray, false)
julia> xs[5]
ERROR: getindex is disabled
```

`rand`

julia-observer-quote-cut-paste-112_*work`julia-observer-quote-cut-paste-28**work`julia-observer-quote-cut-paste-113**work`julia-observer-quote-cut-paste-29**work`julia-observer-quote-cut-paste-115**work`julia-observer-quote-cut-paste-30**work`julia-observer-quote-cut-paste-116**work`julia-observer-quote-cut-paste-31**work`julia-observer-quote-cut-paste-117**work`julia-observer-quote-cut-paste-32**work`julia-observer-quote-cut-paste-118**work`julia-observer-quote-cut-paste-33**work`julia-observer-quote-cut-paste-120**work`julia-observer-quote-cut-paste-34**work`julia-observer-quote-cut-paste-121**work`julia-observer-quote-cut-paste-35**work`julia-observer-quote-cut-paste-123**work`julia-observer-quote-cut-paste-36**work`julia-observer-quote-cut-paste-119**work`julia-observer-quote-cut-paste-37**work`julia-observer-quote-cut-paste-124**work`julia-observer-quote-cut-paste-38**work`julia-observer-quote-cut-paste-125**work`julia-observer-quote-cut-paste-39**work`julia-observer-quote-cut-paste-126**work`julia-observer-quote-cut-paste-40*_work`

`+`

julia-observer-quote-cut-paste-127_*work`julia-observer-quote-cut-paste-42**work`julia-observer-quote-cut-paste-128**work`julia-observer-quote-cut-paste-43**work`julia-observer-quote-cut-paste-130**work`julia-observer-quote-cut-paste-44**work`julia-observer-quote-cut-paste-131**work`julia-observer-quote-cut-paste-45**work`julia-observer-quote-cut-paste-132**work`julia-observer-quote-cut-paste-46**work`julia-observer-quote-cut-paste-133**work`julia-observer-quote-cut-paste-47**work`julia-observer-quote-cut-paste-134**work`julia-observer-quote-cut-paste-48**work`julia-observer-quote-cut-paste-129**work`julia-observer-quote-cut-paste-49**work`julia-observer-quote-cut-paste-135**work`julia-observer-quote-cut-paste-50**work`julia-observer-quote-cut-paste-136**work`julia-observer-quote-cut-paste-51**work`julia-observer-quote-cut-paste-138**work`julia-observer-quote-cut-paste-52**work`julia-observer-quote-cut-paste-139**work`julia-observer-quote-cut-paste-53**work`julia-observer-quote-cut-paste-140**work`julia-observer-quote-cut-paste-54**work`julia-observer-quote-cut-paste-141**work`julia-observer-quote-cut-paste-55**work`julia-observer-quote-cut-paste-143**work`julia-observer-quote-cut-paste-56**work`julia-observer-quote-cut-paste-144**work`julia-observer-quote-cut-paste-57**work`julia-observer-quote-cut-paste-145**work`julia-observer-quote-cut-paste-58**work`julia-observer-quote-cut-paste-137**work`julia-observer-quote-cut-paste-59**work`julia-observer-quote-cut-paste-146**work`julia-observer-quote-cut-paste-60**work`julia-observer-quote-cut-paste-147**work`julia-observer-quote-cut-paste-61**work`julia-observer-quote-cut-paste-148**work`julia-observer-quote-cut-paste-62**work`julia-observer-quote-cut-paste-150**work`julia-observer-quote-cut-paste-63**work`julia-observer-quote-cut-paste-151**work`julia-observer-quote-cut-paste-64**work`julia-observer-quote-cut-paste-153**work`julia-observer-quote-cut-paste-65**work`julia-observer-quote-cut-paste-155**work`julia-observer-quote-cut-paste-66**work`julia-observer-quote-cut-paste-158**work`julia-observer-quote-cut-paste-67**work`julia-observer-quote-cut-paste-159**work`julia-observer-quote-cut-paste-68**work`julia-observer-quote-cut-paste-154**work`julia-observer-quote-cut-paste-69**work`julia-observer-quote-cut-paste-156**work`julia-observer-quote-cut-paste-70*_work` (works only in vectorized form on Julia v0.5 - Ref issue #109)

`chol`

julia-observer-quote-cut-paste-160_*work`julia-observer-quote-cut-paste-72**work`julia-observer-quote-cut-paste-161**work`julia-observer-quote-cut-paste-73**work`julia-observer-quote-cut-paste-162**work`julia-observer-quote-cut-paste-74**work`julia-observer-quote-cut-paste-163**work`julia-observer-quote-cut-paste-75**work`julia-observer-quote-cut-paste-165**work`julia-observer-quote-cut-paste-76**work`julia-observer-quote-cut-paste-167**work`julia-observer-quote-cut-paste-77**work`julia-observer-quote-cut-paste-157**work`julia-observer-quote-cut-paste-78**work`julia-observer-quote-cut-paste-168**work`julia-observer-quote-cut-paste-79**work`julia-observer-quote-cut-paste-169**work`julia-observer-quote-cut-paste-80**work`julia-observer-quote-cut-paste-170**work`julia-observer-quote-cut-paste-81**work`julia-observer-quote-cut-paste-171**work`julia-observer-quote-cut-paste-82**work`julia-observer-quote-cut-paste-172**work`julia-observer-quote-cut-paste-83**work`julia-observer-quote-cut-paste-174**work`julia-observer-quote-cut-paste-84**work`julia-observer-quote-cut-paste-164**work`julia-observer-quote-cut-paste-85**work`julia-observer-quote-cut-paste-175**work`julia-observer-quote-cut-paste-86**work`julia-observer-quote-cut-paste-176**work`julia-observer-quote-cut-paste-87**work`julia-observer-quote-cut-paste-177**work`julia-observer-quote-cut-paste-88**work`julia-observer-quote-cut-paste-166**work`julia-observer-quote-cut-paste-89**work`julia-observer-quote-cut-paste-178**work`julia-observer-quote-cut-paste-90**work`julia-observer-quote-cut-paste-180**work`julia-observer-quote-cut-paste-91**work`julia-observer-quote-cut-paste-181**work`julia-observer-quote-cut-paste-92**work`julia-observer-quote-cut-paste-182**work`julia-observer-quote-cut-paste-93**work`julia-observer-quote-cut-paste-183**work`julia-observer-quote-cut-paste-94**work`julia-observer-quote-cut-paste-185**work`julia-observer-quote-cut-paste-95**work`julia-observer-quote-cut-paste-173**work`julia-observer-quote-cut-paste-96**work`julia-observer-quote-cut-paste-186**work`julia-observer-quote-cut-paste-97**work`julia-observer-quote-cut-paste-187**work`julia-observer-quote-cut-paste-98**work`julia-observer-quote-cut-paste-188**work`julia-observer-quote-cut-paste-99**work`julia-observer-quote-cut-paste-189**work`julia-observer-quote-cut-paste-100*_work`

`fft`

julia-observer-quote-cut-paste-190_*work`julia-observer-quote-cut-paste-102**work`julia-observer-quote-cut-paste-191**work`julia-observer-quote-cut-paste-103**work`julia-observer-quote-cut-paste-192**work`julia-observer-quote-cut-paste-104**work`julia-observer-quote-cut-paste-179**work`julia-observer-quote-cut-paste-105**work`julia-observer-quote-cut-paste-193**work`julia-observer-quote-cut-paste-106*_work``fftC2R`

,`fftR2C`

,`conv3`

,`convolve`

,`fir,`

iir`,`

approx1`,`

approx2`### Statistics *`

mean`,`

median`,`

std`,`

var`,`

cov``meanWeighted`

,`varWeighted`

,`corrcoef`

### Vector Algorithms *`sum`

,`min`

,`max`

,`minimum`

,`maximum`

,`findmax`

,`findmin`

`countnz`

,`any`

,`all`

,`sort`

,`union`

,`find`

,`cumsum`

,`diff`

`sortIndex`

,`sortByKey`

,`diff2`

,`minidx`

,`maxidx`

julia-observer-quote-cut-paste-142_*work*`getActiveBackend`

,`getBackendCount`

,`getAvailableBackends`

,`setBackend`

,`getBackendId`

,`sync`

,`getActiveBackendId`

julia-observer-quote-cut-paste-149*work*_work`getDevice`

,`setDevice`

,`getNumDevices`

julia-observer-quote-cut-paste-152`scale`

,`hist`

`loadImage`

,`saveImage`

`isImageIOAvailable`

`colorspace`

,`gray2rgb`

,`rgb2gray`

,`rgb2hsv`

,`rgb2ycbcr`

,`ycbcr2rgb`

,`hsv2rgb`

`regions`

,`SAT`

`bilateral`

,`maxfilt`

,`meanshift`

,`medfilt`

,`minfilt`

,`sobel`

,`histequal`

`resize`

,`rotate`

,`skew`

,`transform`

,`transformCoordinates`

,`translate`

`dilate`

,`erode`

,`dilate3d`

,`erode3d`

,`gaussiankernel`

julia-observer-quote-cut-paste-184_*work*`orb`

,`sift`

,`gloh`

,`diffOfGaussians`

,`fast`

,`harris`

,`susan`

,`hammingMatcher`

,`nearestNeighbour`

,`matchTemplate`

julia-observer-quote-cut-paste-194*work``` include("benchmark.jl") include("nmf*benchmark.jl") ```

There are three backends in `ArrayFire.jl`

:

- CUDA Backend
- OpenCL Backend
- CPU Backend

There is yet another backend which essentially allows the user to switch backends at runtime. This is called the unified
backend. `ArrayFire.jl`

starts up with the unified backend.

If the backend selected by ArrayFire by default (depends on the available drivers) is not the desired one (depending on the available hardware), you can override the default by setting the environment variable `$JULIA_ARRAYFIRE_BACKEND`

before starting Julia (more specifically, before loading the `ArrayFire`

module). Possible values for `$JULIA_ARRAYFIRE_BACKEND`

are `cpu`

, `cuda`

and `opencl`

.

You may also change the backend at runtime via, e.g., `set_backend(AF_BACKEND_CPU)`

(resp. `AF_BACKEND_CUDA`

or `AF_BACKEND_OPENCL`

).
The unified backend isn't a computational backend by itself but represents an interface to switch
between different backends at runtime. `ArrayFire.jl`

starts up with the unified backend, but `get_active_backend()`

will return either a particular default backend, depending on how you have installed the library. For example, if you have
built `ArrayFire.jl`

with the CUDA backend, `get_active_backend()`

will return `AF_BACKEND_CUDA`

backend.

`ArrayFire.jl`

isn't working! What do I do?

Error loading

`libaf`

Try adding the path to `libaf`

to your `LD_LIBRARY_PATH`

.

`ArrayFire Error (998): Internal Error`

whenever you call`rand`

If you're using the CUDA backend, try checking if `libcudart`

and `libnvvm`

are both in your `LD_LIBRARY_PATH`

. This is because `libafcuda`

will try to link to these libraries when it loads into Julia. If they're not in your system, install CUDA for your platform.

`ArrayFire.jl`

loads, but`a = rand(AFArray{Float32}, 10)`

is stuck.

If you want to use the CUDA backend, check if you have installed CUDA for your platform. If you've installed CUDA, simply downloaded a binary and it still doens't work, try adding `libnvvm`

, `libcudart`

to your path.

`ArrayFire.jl`

does not work with Atom.

Create a file in your home directory called `.juliarc.jl`

and write `ENV["LD_LIBRARY_PATH"] = "/usr/local/lib/"`

(or the path to `libaf`

) in it. Atom should now be able to load it.

`ERROR: ArrayFire Error (401) : Double precision not supported for this device`

This error message pops up on devices that do not support double precision: a good example would be the Iris Pro on Macbooks. If you get this message, you should work with single precision. For example, if you're generating random numbers directly on the device, the correct usage in this scenario would be `rand(AFArray{Float32}, 10)`

instead of `rand(AFArray{Float64}, 10)`

.

05/28/2015

2 days ago

752 commits