2.4.2 ffor


Contents

Name

ffor

Description

A multithreaded faster for statement.

Syntax

ffor(int <variable name>; <start valuue>;  <end value>){}

Remark

Inside ffor can call these functions

External VC functions
#pragma ffunc() functions (see example below)

For thread-safe, these interlocked functions should use for increment/decrement

int  AtomicLoad( const int * p ) // return value
void AtomicStore( int * p, int value) // store value in p
int  AtomicExchange( int * p, int value ) // store value in p, and return old value
int AtomicInc( int * p) // +1, return value
int AtomicDec( int * p ) // -1, return value
int AtomicAdd( int * p, int n ) // +n, returns old value
// double
double AtomicLoadDbl( const double * p ) // return value
void   AtomicStoreDbl( double * p, double value ) // store value in p
double AtomicExchangeDbl( double * p, double value ) // store value in p, and return old value
double AtomicAddDbl( double* p, double d ) // +d, returns old value

Examples

EX1

//active matrix sheet should have 3 matrix object
// this will multiply the first two and put results on third one
void ffor_ex()
{
	MatrixPage mPage = Project.Pages();
	if(!mPage)
		return;
	MatrixLayer mlyr1 = mPage.Layers(0);
	Matrix matLayer1(mlyr1, 0);
	Matrix matLayer2(mlyr1, 1);
	Matrix matLayer3(mlyr1, 2);
	matrix mat1, mat2, mat3;
	mat1 = matLayer1;
	mat2 = matLayer2;
	mat3 = matLayer3;
	int nCols = mat1.GetNumCols();
	int nRows = mat1.GetNumRows();
	if(nRows != nCols)
		return;
	
	ffor(int nR; 0;  nRows)
	{
		ffor(int nC; 0; nRows)
		{
			double temp = 0;
			ffor(int k; 0; nRows)
			{
				double dmat = mat1[nR][k] * mat2[k][nC];
				AtomicAddDbl(&temp, dmat);
			}
			mat3[nR][nC] = temp;
		}
	}
	matLayer3 = mat3;
}


EX2

#pragma ffunc(1)
int myAdd(int &n1, int &n2)
{
	myDuplicate(n1);
	myDuplicate(n2);
	return n1 + n2;
}
void myDuplicate(int &n)
{  
	n *= 2;
}
#pragma ffunc(0) 

void ffor_ffunc_ex(int n = 5)
{
 	Profiler  junk;
 	
    vector v1;
    vector<int> v2;;
    vector<int> v3;;
	v1.Data(5, n+5);
	v2.Data(5, n+5);	
	v3.Data(5, n+5);	      
  	ffor(int ii; 0;  n)
  	{
   		v1[ii] = myAdd(v2[ii], v3[ii]);
   	}          
}


Header to Include

See Also

for

Reference