0

这是我的内核:

 const string cl_src = 
//" #pragma OPENCL EXTENSION cl_khr_fp64 : enable                       \r\n"
"__kernel void CalculateSimpleMA(                                     \r\n"
                                 "int rates_total,                     \r\n"
                                 "int prev_calculated,                \r\n"
                                 "int begin,                          \r\n"
                                 "int InpMAPeriod,                    \r\n"
                                 "__global float *price,              \r\n"
                                 "__global float *storeValue          \r\n"
                                 ")                                    \r\n"
         "{                                                           \r\n"
            " int i,limit;                                               \r\n"
         " if(prev_calculated==0)// first calculation                 \r\n"
         "{                                                           \r\n" 
            "limit=InpMAPeriod+begin;                                 \r\n"
            "for(i=0;i<limit-1;i++) storeValue[i]=0.0;                \r\n"
            "double firstValue=0;                                     \r\n"
            "for(i=begin;i<limit;i++) firstValue+=price[i];           \r\n"
            "firstValue/=InpMAPeriod;                                 \r\n"
            "storeValue[limit-1]=firstValue;                          \r\n"
         "}                                                           \r\n"
         "else limit=prev_calculated-1;                               \r\n"
         "for(i= 0;i<rates_total;i++)                              \r\n"
            "storeValue[i]=storeValue[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;\r\n"

         "}                                                           \r\n";

以下是我的 OnCalculation():

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//--- check for bars count
   if(rates_total<InpMAPeriod-1+begin)
      return(0);// not enough bars for calculation
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
      ArrayInitialize(ExtLineBuffer,0);
//--- sets first bar from what index will be draw
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod-1+begin);


 string str;   
int cl_ctx = CLContextCreate(CL_USE_GPU_ONLY);
   int cl_prg=CLProgramCreate(cl_ctx,cl_src,str);
   int cl_krn=CLKernelCreate(cl_prg,"CalculateSimpleMA");
   int cl_mem=CLBufferCreate(cl_ctx,ArraySize(price)*sizeof(float ),CL_MEM_READ_WRITE), 
       cl_price=CLBufferCreate(cl_ctx,ArraySize(price)*sizeof(float ),CL_MEM_READ_WRITE);

   if(cl_ctx==INVALID_HANDLE)
     {
      Print("OpenCL not found: ", GetLastError() );
      return(0);

     }

   //if(CLGetInfoString(cl_ctx,CL_DEVICE_NAME,str)) 
   //   Print("OpenCL device name: ",str);
   if(cl_prg==INVALID_HANDLE)
     {
      CLContextFree(cl_ctx);

      Print("OpenCL program create failed: ", str);
      return(0);
     }
     if(cl_prg==ERR_INVALID_PARAMETER )
     {
      CLContextFree(cl_ctx);

      Print("OpenCL program create failed: ", str);
      return(0);
     }
     if(cl_prg==ERR_NOT_ENOUGH_MEMORY )
     {
      CLContextFree(cl_ctx);

      Print("OpenCL program create failed: ", str);
      return(0);
     }
     if(cl_prg==ERR_OPENCL_PROGRAM_CREATE )
     {
      CLContextFree(cl_ctx);

      Print("OpenCL program create failed: ", str);
      return(0);
     }
           if(cl_krn==ERR_OPENCL_INVALID_HANDLE )
           {
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               Print("OpenCL kernel create failed : ERR_OPENCL_INVALID_HANDLE");
               return(0);
           }
           if(cl_krn==ERR_INVALID_PARAMETER )
           {
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               Print("OpenCL kernel create failed : ERR_INVALID_PARAMETER");
               return(0);
           }
           if(cl_krn==ERR_OPENCL_TOO_LONG_KERNEL_NAME  )
           {
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               Print("OpenCL kernel create failed : ERR_OPENCL_TOO_LONG_KERNEL_NAME");
               return(0);
           }
           if(cl_krn==ERR_OPENCL_KERNEL_CREATE )
           {
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               Print("OpenCL kernel create failed : ERR_OPENCL_KERNEL_CREATE");
               return(0);
           }

           if(cl_mem==INVALID_HANDLE)
           {
               CLKernelFree(cl_krn);
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               Print("OpenCL buffer create failed: cl_mem INVALID_HANDLE");
               return(0);
           }
           if(cl_mem==ERR_NOT_ENOUGH_MEMORY )
           {
               CLKernelFree(cl_krn);
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               Print("OpenCL buffer create failed: cl_mem ERR_NOT_ENOUGH_MEMORY ");
               return(0);
           }
           if(cl_mem==ERR_OPENCL_BUFFER_CREATE )
           {
               CLKernelFree(cl_krn);
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               Print("OpenCL buffer create failed: cl_mem ERR_OPENCL_BUFFER_CREATE ");
               return(0);
           }

           if(cl_price==INVALID_HANDLE)
           {
               CLKernelFree(cl_krn);
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               CLBufferFree(cl_mem);
               Print("OpenCL buffer create failed: cl_price ");
               return(0);
           }
           if(cl_price==ERR_NOT_ENOUGH_MEMORY)
           {
               CLKernelFree(cl_krn);
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               CLBufferFree(cl_mem);
               Print("OpenCL buffer create failed: cl_price ERR_NOT_ENOUGH_MEMORY");
               return(0);
           }
           if(cl_price==ERR_OPENCL_BUFFER_CREATE)
           {
               CLKernelFree(cl_krn);
               CLProgramFree(cl_prg);
               CLContextFree(cl_ctx);
               CLBufferFree(cl_mem);
               Print("OpenCL buffer create failed: cl_price ERR_OPENCL_BUFFER_CREATE ");
               return(0);
           }
//--- calculation
int  offset[2]={0,0};
   int  work  [2]={512,512};
   //float test[];
   //        ArrayResize(test,ArraySize(price));
   //        ArrayCopy(test,price);
   //uint inpMAPeriod = InpMAPeriod;
   switch(InpMAMethod)
     {
      case MODE_EMA:  CalculateEMA(rates_total,prev_calculated,begin,price);        break;
      case MODE_LWMA: CalculateLWMA(rates_total,prev_calculated,begin,price);       break;
      case MODE_SMMA: CalculateSmoothedMA(rates_total,prev_calculated,begin,price);break;
      case MODE_SMA:  
           if(!CLBufferWrite(cl_price, price))
           Print("Could not copy Input buffer"); 
           else Print("Copied: ",cl_price);               
           if(!CLSetKernelArgMem(cl_krn,4,cl_price))
           Print("Input Bufer Not Set");
          //else Print("Input Buffer Set");
           if(!CLSetKernelArgMem(cl_krn,5,cl_mem))
           Print("Output Bufer Not Set");
           //else Print("Output Buffer Set");

           //else Print("Input Buffer Copied");
           if(!CLSetKernelArg(cl_krn,0,rates_total))
           Print("Could Not Set Arg 0");
           //else Print("Set Arg 0");
           if(!CLSetKernelArg(cl_krn,1,prev_calculated))
           Print("Could Not Set Arg 1");
           //else Print("Set Arg 1");
           if(!CLSetKernelArg(cl_krn,2,begin))
           Print("Could Not Set Arg2");
           //else Print("Set Arg 2");
           if(!CLSetKernelArg(cl_krn,3,InpMAPeriod))
           Print("Could Not Set Arg3: ",GetLastError());


           //else Print("Set Arg 3");
           //CLSetKernelArg(cl_krn,4,cl_price);

           if(!CLExecute(cl_krn,2,offset,work))
           Print("Kernel not executed",GetLastError());
           //else Print("Executing Now!");
           if(CLExecutionStatus(cl_krn) == 0) Print("Completed");
           if(CLExecutionStatus(cl_krn) == 1) Print("CL_RUNNING");
           if(CLExecutionStatus(cl_krn) == 2) Print("CL_SUBMITTED");
           if(CLExecutionStatus(cl_krn) == 3) Print("CL_QUEUED");
           if(CLExecutionStatus(cl_krn) == -1) Print("Error Occurred:", GetLastError());

           if(!CLBufferRead(cl_mem,ExtLineBuffer))
           Print("Buffer Copy Nothing: ", GetLastError());
           break;
     }
      CLBufferFree(cl_price);
            CLBufferFree(cl_mem);
            CLKernelFree(cl_krn);
            CLProgramFree(cl_prg);

            CLContextFree(cl_ctx);

    return(rates_total);
  }

以上是我的ull代码。

给出问题的行是:

if(!CLSetKernelArg(cl_krn,3,inpMAPeriod))
           Print("Could Not Set Arg3: ",GetLastError());  

我得到的错误是:

Could Not Set Arg3: 5108  

伙计们,请任何人帮助我..我没有找到与我的问题有关的任何东西,请帮助我。

编辑:
这些是行:

"for(i= 0;i<rates_total;i++) \r\n" 

"storeValue[i]=storeValue[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;\r\n"

当我取消注释这些行时,找不到 OpenCL 或未创建上下文成为问题。我得到的错误是:

OpenCL: GPU device 'GeForce GT 730' selected
CL_QUEUED
Buffer Copy Nothing: 0

请告诉我我提到的 ilnes 是否有问题,以及我是否可以解决它。

4

1 回答 1

1

CLSetKernelArgMemLocal不能用于将数据从主机传递到设备。所以下面的说法是错误的:CLSetKernelArgMemLocal(cl_krn,4,cl_price). CLSetKernelArgMemLocal只能用于设置要在内核内部使用的本地缓冲区的大小。请参阅手册

local_mem_size [in] 缓冲区大小,以字节为单位。

如果要将数据从主机传递到设备,则必须通过值(CLSetKernelArg)或全局内存缓冲区(CLSetKernelArgMem)。

我对MQL5不熟悉。我怀疑代码应该是这样的:

CLBufferWrite(cl_price, price); // pass data to buffer
CLSetKernelArgMem(cl_krn,4,cl_price);

并且内核参数应更改为:__global float *price.

于 2018-04-21T08:54:10.333 回答