没有从MQL5中获得OpenCL内核的预期输出

时间:2018-04-03 11:47:13

标签: opencl mql5 metatrader5

以下是我尝试使用的功能:

#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 函数未提供输出。我的猜测是该过程没有收到该值。我不明白我错过了什么 请建议我能做些什么。或者我在内存管理方面做了什么?我如何才能使其多核心可行?

1 个答案:

答案 0 :(得分:1)

OpenCL内核,与CUDA / GPU内核完全相同,代码在OpenCL设备内编译和传输并“远程”执行。因此,除非某些特定于平台的技巧作为OpenCL驱动程序扩展存在,否则无法使“远程”执行引擎“交付” - printf() - 输出(s )回到主要断开连接/去同步的异步异构分布式计算生态系统(MetaTrader终端平台与“远程”流程流无关,反之亦然)。

 if (  INVALID_HANDLE == ( cl_prg = CLProgramCreate( cl_ctx,
                                                     cl_src,
                                                     str
                                                     )
                           )
       )
  {                        Print( "CLProgramCreate() failed: ", str );
                                   CLContextFree( cl_ctx );
                           return( 0 );
  }
  Print( "DEBUG[OpenCL]: compiler log:\nDEBUG[OpenCL]INFO:[[", str, "]]\n" );
  ...

因此,除非经过平台验证的工具可用,否则最好避免任何内核“打印

接下来的接口:OpenCL / MQL5互操作性被记录为仅使用 缓冲区 映射工具来移动数据并返回数据输出。

最好的下一步:

重新编译简化的OpenCL内核,无需任何打印,并重新测试最简单的步骤:将一个简单的简单值(使用-1的简单值)移动到单个数组单元中,以验证工作流程并且所有的呼叫签名首先匹配,然后进入更多细节,好吗?

const string
cl_src = "__kernel void Calculation(          int    period,     \r\n" // [0]__GPU_call-interface_ORDINAL
         "                                    float  price,      \r\n" // [1]__GPU_call-interface_ORDINAL
         "                                    float  prev_value, \r\n" // [2]__GPU_call-interface_ORDINAL
         "                           __global float *result      \r\n" // [3]__GPU_call-interface_ORDINAL
         "                           )                           \r\n"
         "{                                                      \r\n"
         "     result[0] = -1;                                   \r\n"
         "}                                                      \r\n";
...                                                               // OpenCL SETUP
// -----------------------------------------------<ASYNC>.LAUNCHER
CLExecute(         cl_krn, 2, offset, work );
// -----------------------------------------------<ASYNC>.MONITOR
do
while ( getOpenCL_KernelExecutionSTATE( cl_krn ) >     CL_COMPLETE );
// -----------------------------------------------<ASYNC>.COMPLETE
CLBufferRead(      cl_mem, result );
// -----------------------------------------------<ASYNC>.TRY-READ-MAPPING-BUFFER
int getOpenCL_KernelExecutionSTATE(                 int aKernelHANDLE = 0 )
{   int    cl_KernelExecutionSTATE = CLExecutionStatus( aKernelHANDLE );
    switch(cl_KernelExecutionSTATE )
    { 
      case( CL_COMPLETE  ): Print( "DEBUG[OpenCL]:program[, aKernelHANDLE, "] complete." );
                            return( CL_COMPLETE );

      case( CL_RUNNING   ): Print( "DEBUG[OpenCL]:program[, aKernelHANDLE, "] in running phase." );
                            return( CL_RUNNING );

      case( CL_SUBMITTED ): Print( "DEBUG[OpenCL]:program[, aKernelHANDLE, "] submitted for execution." );
                            return( CL_SUBMITTED );

      case( CL_QUEUED    ): Print( "DEBUG[OpenCL]:program[, aKernelHANDLE, "] waiting in a queue." );
                            return( CL_QUEUED );

      case( WRONG_VALUE  ): Print( "DEBUG[OpenCL]:program[, aKernelHANDLE, "] returned Error upon last CLExecutionStatus() call." );
                            return( WRONG_VALUE );

      default:              Print( "Ought never get here... Check updated MQL5 documentation." );
                            return( WRONG_VALUE );
    }
}