How can I Use C Reducer?

How can I Use C Reducer?

I want to use C Reducer.

I write this code.

-----------------------------------------------------------------------------------------------------------------------------

#include 
#include 
#include 
#include 
#include 
#include 
#include 

void MakeRandData(unsigned int* pData, unsigned int nSize)
{        

	srand((unsigned int)time(NULL));

	for(unsigned int i =0; i < nSize; i++)
		pData[i] = rand()%(nSize-1); 

	unsigned int nMsxPos= rand()%(nSize);
	pData[nMsxPos] = nSize;
	printf("Max value po %d.rn",nMsxPos); 
}

int FindMaxIndex(unsigned int* pData, unsigned int nSize)
{
	int nMax = 0, nFindIndex = 0;
	CILK_C_REDUCER_MAX_INDEX(re_nIndex,int, -1);
	CILK_C_REDUCER_MAX(re_nMax, int, 0);

	cilk_for(int i =0; i  < nSize; i++)
	{
		if(pData[i] > REDUCER_VIEW(re_nMax))
		{
			REDUCER_VIEW(re_nMax) = pData[i];
			REDUCER_VIEW(re_nIndex) = i; // error
		}
	}

	re_nIndex = REDUCER_VIEW(re_nIndex); //error
	nMax = REDUCER_VIEW(re_nMax);

	return nFindIndex;
}

int _tmain(int argc, _TCHAR* argv[])
{
	const unsigned int MAX_COUNT = 100000000;
	unsigned int* pNData;
	unsigned int nFindMaxPos;

	pNData = new unsigned int [MAX_COUNT];

	MakeRandData(&pNData[0], MAX_COUNT);
	nFindMaxPos = FindMaxIndex(&pNData[0], MAX_COUNT);
	printf("max value posision is %d.rn",nFindMaxPos);

	delete [] pNData;

	return 0;
}

-----------------------------------------------------------------------------------------------------------------------------

this program occur error!

I don't understand. why do this program occur error?

How can I successfully this program

3 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione
Best Reply

Hello,

a working version would look like this:

#include 
#include 
#include 
#include 
#include 
#include 

void MakeRandData(unsigned int* pData, unsigned int nSize)
{

    srand((unsigned int)time(NULL));

    for(unsigned int i =0; i < nSize; i++)
        pData[i] = rand()%(nSize-1);

    unsigned int nMsxPos= rand()%(nSize);
    pData[nMsxPos] = nSize;
    printf("Max value po %d.rn",nMsxPos);
}

int FindMaxIndex(unsigned int* pData, unsigned int nSize)
{
    int nMax = 0, nFindIndex = 0;
    CILK_C_REDUCER_MAX_INDEX(re_nMaxIndex, uint, 0); // max. value with index!
    CILK_C_REGISTER_REDUCER(re_nMaxIndex);

    cilk_for(int i =0; i  < nSize; i++)
    {
        if(pData[i] > REDUCER_VIEW(re_nMaxIndex).value)
        {
            CILK_C_REDUCER_MAX_INDEX_CALC(re_nMaxIndex, i, pData[i]);
        }
    }

    nFindIndex = REDUCER_VIEW(re_nMaxIndex).index;

    CILK_C_UNREGISTER_REDUCER(re_nMaxIndex);
    return nFindIndex;
}

int main(int argc, char* argv[])
{
    const unsigned int MAX_COUNT = 100000000;
    unsigned int* pNData;
    unsigned int nFindMaxPos;

    pNData = new unsigned int [MAX_COUNT];

    MakeRandData(&pNData[0], MAX_COUNT);
    nFindMaxPos = FindMaxIndex(&pNData[0], MAX_COUNT);
    printf("max value posision is %d.rn",nFindMaxPos);

    delete [] pNData;

    return 0;
}

Please note:
i) Reducers need to be registered and unregistered.
ii) CILK_C_REDUCER_MAX_INDEX contains both index & value.
iii) Basic reducer types are already predefined for basic C numeric types (unsigned int for values -> uint as reducer type). See headers (reducer_*.h) for list of all types.
iv) This is not real plain C99 code because of new/delete. Anyways, the exercise was for understanding C reducers if I get it right.

You'll find more information here: http://software.intel.com/en-us/articles/using-an-intel-cilk-plus-reducer-in-c/.

Best regards,

Georg Zitzlsberger

Thank you very so much.

I solve this problem!

Lascia un commento

Eseguire l'accesso per aggiungere un commento. Non siete membri? Iscriviti oggi