GFOR: Getting Started

From Jacket Wiki

Jump to: navigation, search

This page serves as an introduction to using the GFOR loop. We assume you have basic knowledge of Jacket's GFOR loop. If not, please read GFOR documentation before reading this page.

Things to remember

Converting most basic for-loops is as easy as replacing for and end by gfor and gend. In quite a few cases, however, some thought is required before converting the loop to GFOR. Some questions that need to be answered are:

  • Is the GFOR loop necessary? Remember that Jacket and GPGPU Programming in general, are best suited to deal with large chunks of data at once. This means that the overhead associated with GFOR loop creation and launch may well be avoided in some cases, by vectorized calculation.
  • Can I perform the same operation in a vectorized (non-GFOR-loop) fashion? Typically some or all of a GFOR loop can be 'vectorized', that is to say, operations that would ordinarily be performed on each element (or group of elements) of an array, can be executed all at once.
  • Does my for-loop have any data dependencies? Loops where each iteration requires values from the previous iteration are more difficult to convert to GFOR and require careful thought.
  • Am I following all the rules of proper GFOR Usage? Are all the restrictions on GFOR use as discussed in GFOR Usage met?
  • Are all functions within my loop gfor-supported? Check this page for a list of GFOR-supported functions.

Example

Let's take a simple example. Suppose the following is some FOR-loop code that you need to convert to GFOR.

Nx = 20;
n = 20;
Df = zeros(n,Nx);
X = ones(n, Nx);
 
for ii = 1:Nx
  Df(1,ii) = X(1,ii);
  Df(2,ii) = X(2,ii);
end

In this case, we are lucky - conversion to a GFOR-loop is as simple as it can get.

Df = gzeros(n,Nx);
gfor ii = 1:Nx
  Df(1,ii) = X(1,ii);
  Df(2,ii) = X(2,ii);
gend

How not to test GFOR

A for-loop that computes the FFT as shown will perform the same operation N times.

for ii = 1:N
A = fft(B)
end

A GFOR-loop on the other hand, if written as shown, will not actually go into the GFOR-section at all. Jacket requires the iterator to be involved in subscripting operations, for the GFOR-construct to take over. Thus, the following code is an incorrect way of constructing a GFOR-loop.

gfor ii = 1:N
A = fft(B)
gend
Views
Personal tools