The Benefits of Array Element-Wise Operations

Oded GreenArrayFire 1 Comment

In this blog we will review the benefit of using element-wise operations for your computations. Element-wise operations are operations that are applied to every element in an array and allow the user to avoid coding loops and nested loops for rudimentary operations. In a simple example of an element-wise operation, we use both the addition (+) and multiply (*)operations:

array A = randu(1024, 1024), B = randu(1024, 1024);
array C=A*A+B*B;

An element-wise operator that is applied to a single element is unary operator. An operator that works on two elements is a binary operator. ArrayFire has implemented a large number element-wise operations that are applied to the elements of an array. These operators can help reduce the programming overhead for an application designer as:

  • Performance – the application designed need not worry about implementing the operator. As these are implemented in OpenCL (with a focus on achieving high utilization), the programmer does not have deal with the partitioning of the work and load-balancing.
  • Completeness – the array data type is no different than any other C scalar data type. This means that you can user operators such as < or <=.
  • Readability and maintenance – high performance computing becomes as simple as writing a script.

Our element-wise operations also include operations between an array and a scalar value. For example:

array A=randu(1024, 1024),B; 
B=A+1;

A small subset of our operators include:

  • sin – computes the sine for each element in the array.
  • cosine – computes the sine for each element in the array.
  • abs – computes the absolute value for each element in the array.
  • trunc – truncates every element in the array.

A full list of element-wise operators can also be found in our document under: element-wise operators and operator-overloading functions. These include trigonometric, arithmetic, logical,and utility functions. We will extend the discussion on these in future posts.

 

 

 

 

Comments 1

  1. Pingback: avoid coding loops and nested loops for rudimentary operations

Leave a Reply

Your email address will not be published. Required fields are marked *