这是我正在尝试使用的功能:
#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] );
}
这是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";
预期的输出是Exponential Moving average
处理后必须是某个值的值。但在输出中,我得到的值为0.0000
.
这不是预期值。
甚至没有给出输出的printf()
函数。cl_src
我的猜测是该过程未收到该值。我不明白我错过了什么。
请告诉我我能做什么。或者我在内存管理方面做了什么?我怎样才能使它多核可行?