# Examples of Using Multi-Threading for FFT Computation

The following sample program shows how to employ internal threading in Intel MKL for FFT computation (see case "a" in “Number of user threads”).

To specify the number of threads inside Intel MKL, use the following settings:

set `MKL_NUM_THREADS = 1` for one-threaded mode;

set `MKL_NUM_THREADS = 4` for multi-threaded mode.

Note that the configuration parameter DFTI_NUMBER_OF_USER_THREADS must be equal to its default value 1.

## Using Intel MKL Internal Threading Mode

#include "mkl_dfti.h"

void main () {

float x[200][100];

DFTI_DESCRIPTOR_HANDLE my_desc1_handle;

long status, len[2];

//...put input data into x[j][k] 0<=j<=199, 0<=k<=99

len[0] = 200; len[1] = 100;

status = DftiCreateDescriptor( &my_desc1_handle, DFTI_SINGLE,DFTI_REAL, 2,

len);

status = DftiCommitDescriptor(

my_desc1_handle);

status = DftiComputeForward(

my_desc1_handle, x);

status = DftiFreeDescriptor(&my_desc1_handle);

}

The following Example “Using Parallel Mode with Multiple Descriptors Initialized in a Parallel Region” and Example “Using Parallel Mode with Multiple Descriptors Initialized in One Thread” illustrate a parallel customer program with each descriptor instance used only in a single thread (see cases "b" and "c" in Number of user threads).

Specify the number of threads for Example “Using Parallel Mode with Multiple Descriptors Initialized in a Parallel Region” like this:

set `MKL_SERIAL = yes` (or `YES`) for Intel MKL to work in the single-threaded mode (recommended);

set `OMP_NUM_THREADS = 4` for the customer program to work in the multi-threaded mode.

The configuration parameter `DFTI_NUMBER_OF_USER_THREADS` must have its default value of 1.

## Using Parallel Mode with Multiple Descriptors Initialized in a Parallel Region

Note that in this example, the program can be transformed to become single-threaded at the customer level but using parallel mode within Intel MKL (case "a"). To achieve this, you need to set the parameter `DFTI_NUMBER_OF_TRANSFORMS = 4` and to set the corresponding parameter `DFTI_INPUT_DISTANCE = 5000.`

#include "mkl_dfti.h"

#include "omp.h"

void main ()

{

float _Complex x[200][100];

long len[2];

//...put input data into x[j][k] 0<=j<=199, 0<=k<=99

len[0] = 50; len[1] = 100;

// each thread calculates real FFT for matrix (50*100)

#pragma omp parallel

{

DFTI_DESCRIPTOR_HANDLE my_desc_handle;

long myStatus;

int myID = omp_get_thread_num ();

myStatus = DftiCreateDescriptor (&my_desc_handle, DFTI_SINGLE,

DFTI_COMPLEX, 2, len);

myStatus = DftiCommitDescriptor (my_desc_handle);

myStatus = DftiComputeForward (my_desc_handle, &x[myID * len[0] * len[1]]);

myStatus = DftiFreeDescriptor (&my_desc_handle);

} /* End OpenMP parallel region */

}

Specify the number of threads for Example “Using Parallel Mode with Multiple Descriptors Initialized in One Thread” like this:

set `MKL_NUM_THREADS` = 1 for Intel MKL to work in the single-threaded mode (obligatory);

set `OMP_NUM_THREADS` = 4 for the customer program to work in the multi-threaded mode.

The configuration parameter `DFTI_NUMBER_OF_USER_THREADS` must have the default value of 1.

## Using Parallel Mode with Multiple Descriptors Initialized in One Thread

#include "mkl_dfti.h"

#include "omp.h"

void main ()

{

float _Complex x[200][100];

long len[2];

long i;

//...put input data into x[j][k] 0<=j<=199, 0<=k<=99

len[0] = 50; len[1] = 100;

DFTI_DESCRIPTOR_HANDLE my_desc_handle[4];

long myStatus;

for (i=0;i<3;i++) myStatus = DftiCreateDescriptor (&my_desc_handle[i], DFTI_SINGLE, DFTI_COMPLEX, 2, len);

// each thread calculates real FFT for matrix (50*100)

#pragma omp parallel

{

int myID = omp_get_thread_num ();

myStatus = DftiCommitDescriptor (my_desc_handle[myID]);

myStatus = DftiComputeForward (my_desc_handle[myID], &x[myID * len[0] *len[1]]);

} /* End OpenMP parallel region */

for (i=0;i<3;i++) myStatus = DftiFreeDescriptor (&my_desc_handle[i]);

}

## Using Parallel Mode with a Common Descriptor

The following Example “Using Parallel Mode with a Common Descriptor” illustrates a parallel customer program with a common descriptor used in several threads (see case "d" in “Number of user threads”.

In this case, the number of threads, as well as any other configuration parameter, must not be changed after FFT initialization by the DftiCommitDescriptor() function is done.

// set number of threads inside Intel MKL:

//rem set MKL_SERIAL = YES - is not required

// since one-threaded mode for Intel MKL is forced automatically

// set OMP_NUM_THREADS = 4 - multi-threaded mode for customer

#include "mkl_dfti.h"

#include "omp.h"

void main ()

{

float _Complex x[200][100];

long status;

DFTI_DESCRIPTOR_HANDLE desc_handle;

int nThread = omp_get_max_threads ();

long len[2];

//...put input data into x[j][k] 0<=j<=199, 0<=k<=99

len[0] = 50; len[1] = 100;

status =

DftiCreateDescriptor (&desc_handle, DFTI_SINGLE, DFTI_COMPLEX, 2, len);

status = DftiSetValue (desc_handle,

DFTI_NUMBER_OF_USER_THREADS, nThread);

status = DftiCommitDescriptor (desc_handle);

// each thread calculates real FFT for matrix (50*100)

#pragma omp parallel num_threads(nThread)

{

long myStatus;

int myID = omp_get_thread_num ();

myStatus = DftiComputeForward (desc_handle, &x[myID * len[0] * len[1]]);

} /* End OpenMP parallel region */

status = DftiFreeDescriptor (&desc_handle);

}

Parent topic: FFT Code Examples