CUDA
C/C++    Fortran   

Parallelized loops: gfor

Parallel loop iterations: gfor. More...

Defines

#define gfor(var,...)
 Do not use this function directly; see Parallel Loops: gfor.

Functions

array local (const array &variable)
 Create local copy of variable for iteration.
array gfor (double n)
 Do not use this function directly; see Parallel Loops: gfor.
array gfor (double first, double last)
 Do not use this function directly; see Parallel Loops: gfor.
array gfor (double first, double inc, double last)
 Do not use this function directly; see Parallel Loops: gfor.

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

Define Documentation

#define gfor (   var,
  ... 
)

Do not use this function directly; see Parallel Loops: gfor.


Function Documentation

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 = constant(0,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 = constant(0,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
array af::gfor ( double  n)
array af::gfor ( double  first,
double  last 
)

Do not use this function directly; see Parallel Loops: gfor.

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

Do not use this function directly; see Parallel Loops: gfor.