Here is the function that I am trying to work with:
#define SIZE_X 512
#define SIZE_Y 512
int cl_ctx;
int cl_prg;
int cl_krn;
int cl_mem;
float ExponentialMA( const int position,
const int period,
const double prev_value,
const double &price[]
)
{
//---
float result[];
ArrayResize(result,1);
float pr=2.0/(period+1.0);
Print( "Reality: :) ", (float)price[position]*pr+prev_value*(1-pr) );
if ( period > 0 ) //--- calculate value
{
//--- initializing OpenCL objects
if((cl_ctx=CLContextCreate(CL_USE_GPU_ONLY))==INVALID_HANDLE)
{
Print("OpenCL not found: ", GetLastError() );
return(0);
}
string str;
if((cl_prg=CLProgramCreate(cl_ctx,cl_src,str))==INVALID_HANDLE)
{
CLContextFree( cl_ctx ); Print("OpenCL program create failed: ", str);
return(0);
}
if((cl_krn=CLKernelCreate(cl_prg,"Calculation"))==INVALID_HANDLE)
{
CLProgramFree( cl_prg );
CLContextFree( cl_ctx ); Print("OpenCL kernel create failed");
return(0);
}
if((cl_mem=CLBufferCreate(cl_ctx,SIZE_X*SIZE_Y*sizeof(int),CL_MEM_READ_WRITE))==INVALID_HANDLE)
{
CLKernelFree( cl_krn );
CLProgramFree( cl_prg );
CLContextFree( cl_ctx ); Print("OpenCL buffer create failed");
return(0);
}
int offset[2]={0,0};
int work [2]={SIZE_X,SIZE_Y};
CLSetKernelArg( cl_krn, 0, period );
CLSetKernelArg( cl_krn, 1, (float) price[position] );
CLSetKernelArg( cl_krn, 2, (float)prev_value );
CLSetKernelArgMem( cl_krn, 3, cl_mem );
Print( period, " ", price[position], " ", prev_value );
//--- rendering the frame
CLExecute( cl_krn, 2, offset, work );
CLBufferRead( cl_mem, result );
ArrayPrint( result );
CLBufferFree( cl_mem );
CLKernelFree( cl_krn );
CLProgramFree( cl_prg );
CLContextFree( cl_ctx );
}
//---
return( result[0] );
}
This is the cl_src
:
const string cl_src=
"__kernel void Calculation(int period, \r\n"
"float price, \r\n"
"float prev_value, \r\n"
"__global float *result) \r\n"
"{ \r\n"
" float pr=2.0/(period+1.0); \r\n"
" printf(\"Functional value: %f\",price); \r\n"
" result[0]=price*pr+prev_value*(1-pr); \r\n"
"} \r\n";
The output expected is the value of the Exponential Moving average
which must be some value after process. But in the output I am getting the value as 0.0000
.
Which is not the expected value.
Even the printf()
function in the cl_src
not giving the output. My guess is that the value is not received by the process. I don't understand what I have missed.
Kindly, suggest me what I can do. Or is there anything I did in memory management? How I can make it multiple core workable??