Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 无法在内核程序OpenCL中声明静态变量_Function_Static_Opencl_Mql5 - Fatal编程技术网

Function 无法在内核程序OpenCL中声明静态变量

Function 无法在内核程序OpenCL中声明静态变量,function,static,opencl,mql5,Function,Static,Opencl,Mql5,以下是内核的代码: "__kernel void CalculateLWMA( \r\n" "int rates_total, \r\n" "int prev_calculated,

以下是内核的代码:

"__kernel void CalculateLWMA(                                           \r\n"
                            "int rates_total,                                 \r\n"
                            "int prev_calculated,                             \r\n"
                            "int begin,                                       \r\n"
                            "int InpMAPeriod,                                 \r\n"
                            "__global double *price,                          \r\n"
                            "__global double *ExtLineBuffer                   \r\n"
                            ")                                                 \r\n"
        "{                                                              \r\n"
            "int        i,limit;                                           \r\n"
            "static int weightsum;                                                \r\n"
            "double     sum;                                               \r\n"
            "if(prev_calculated==0)                                        \r\n"
            "{                                                             \r\n"                                   
            "weightsum=0;                                                  \r\n"
            "limit=InpMAPeriod+begin;                                      \r\n"
            "for(i=0;i<limit;i++) ExtLineBuffer[i]=0.0;                    \r\n"
            "double firstValue=0;                                          \r\n"
            "for(i=begin;i<limit;i++)                                      \r\n"
            "{                                                             \r\n"
                "int k=i-begin+1;                                          \r\n"
                "weightsum+=k;                                             \r\n"
                "firstValue+=k*price[i];                                   \r\n"
            "}                                                             \r\n"
            "firstValue/=(double)weightsum;                                \r\n"
            "ExtLineBuffer[limit-1]=firstValue;                            \r\n"
            "}                                                             \r\n"
            "else limit=prev_calculated-1;                                 \r\n"
            "for(i=limit;i<rates_total;i++)                                \r\n"
            "{                                                             \r\n"
            "sum=0;                                                        \r\n"
            "for(int j=0;j<InpMAPeriod;j++) sum+=(InpMAPeriod-j)*price[i-j];\r\n"
            "ExtLineBuffer[i]=sum/weightsum;                               \r\n"
            "}                                                             \r\n"
        "}                

请告诉我什么可以替代OpenCL内核程序函数中的
静态

我刚刚仔细查看了您的代码,注意到您使用的静态变量无论如何都是不正确的,因为
weightsum+=k
不是原子的或同步的,即使您有障碍,将有来自不同工作项的并发读写。在OpenCL中不能这样做。全局变量必须是常量

另一件事是,您没有接到任何调用
get\u global\u id(0)
或类似的调用,这是一个很大的危险信号-您的所有工作项都将彼此运行完全相同的代码,或者您只有一个工作项。前者意味着您正在大规模地丢弃计算结果,后者意味着您实际上没有使用任何并行性。在这两种情况下,您的代码都会很慢。它确实意味着
weightsum
实际上不需要是静态的


如果您想要可变全局内存,就必须将其作为缓冲区传入。请注意,如果多个工作项需要写入该缓冲区中的同一个内存字(或者一个工作项写入,另一个工作项读取),则需要使用全局屏障或原子。一个更好的方法是使用归约-搜索例如“OpenCL并行求和归约”会让你有所收获。从本质上讲,每个工作项都将在隔离状态下尽可能多地执行工作,然后这些工作项将协作以分层方式组合它们的结果。这通常具有O(log(N))的复杂性,而不是原子学的天真使用的O(N)。

您是否尝试过在内核函数之前将其声明为全局函数?我没有。。让我试一试。@pmdj您能告诉我如何声明全局。。。。我尝试了
global int weightsum。偶数
\u全局整数加权和,
。不起作用:(这是insights的答案。)我感谢你的时间和宝贵的建议。我对OpenCL有点陌生。因此,试图了解它是如何并行工作的。我试图为此设置worker,但我猜它们没有用。能否帮我根据多个工作项修改上述代码,使我的代码运行得更快?帮助我优化它。
error: variables in function scope cannot be declared static