Do more. Code less. Free software for GPU computing.
<scroll to top>
Functions

Parallelized loops: gfor

Basics

Parallel loop iterations: gfor. More...

Functions

array gfor (double n)
 Create gfor variable for sequence {0, 1, ..., n-1}
array gfor (double first, double last)
 Create gfor variable for sequence {first, first+1, first+2, ..., last}
array gfor (double first, double inc, double last)
 Create gfor variable for sequence {first, first+inc, first+2*inc, ..., last}
array local (const array &variable)
 Create local copy of variable for iteration.

Detailed Description

Parallel loop iterations: gfor.

See tutorial for usage and examples.

Run an FFT on every 2D slice of a volume sequentially with a for-loop or in parallel (faster) with a gfor-loop:

   for (int i = 0; i < N; ++i)
       A(span,span,i) = fft2(A(span,span,i)); // runs each FFT in sequence

   gfor (array i, N)
       A(span,span,i) = fft2(A(span,span,i)); // runs N FFTs in parallel

Use local() to declare a variable or expression local for each iteration (see more).

See also:
local()
Tutorial on GFOR

Function Documentation

array af::gfor ( double  n)

Create gfor variable for sequence {0, 1, ..., n-1}

Examples:
examples/image/gfor_hist_demo.cpp, and examples/misc/gfor.cpp.
array af::gfor ( double  first,
double  last 
)

Create gfor variable for sequence {first, first+1, first+2, ..., last}

array af::gfor ( double  first,
double  inc,
double  last 
)

Create gfor variable for sequence {first, first+inc, first+2*inc, ..., last}

array af::local ( const array &  variable)

Create local copy of variable for iteration.

Indicate when a variable is unique to each iteration, i.e. each iteration has its own copy of the data for modification independent of other iterations.

For example, when using the equal sign, ArrayFire thinks you are subscripting into B shared by all iterations:

   int n = 5;
   array A = seq(n), C = zeros(A.dims(), A.type());
   gfor (array ii, n) {
       array B = A; // mistakenly only sharing (fake copy)
       B(ii) = 0; // write zeros in positions '1:n' of original matrix
       C(ii) = sum(B);
   }
   print(C);  // all zeros

Compare that to indicating local copies:

   int n = 5;
   array A = seq(n), D = zeros(A.dims(), A.type());
   gfor (array ii, n) {
       array B = local(A); // create local copy
       B(ii) = 0;  // each tile has its own B with a different index zeroed out
       D(ii) = sum(B); // different sum for each tile
   }
   print(D);  // all unique summations

Produces:

C =
     0
     0
     0
     0
     0
D =
    14
    13
    12
    11
    10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines