C ++ AMP中的全局变量 [英] Global variables in C++ AMP
问题描述
我正在尝试创建一个可用于C#的C ++ AMP dll. 相同的,相当大的矩阵用于所有计算,我希望仅将其复制到GPU一次.在CUDA中,我只能使用将设备指针保存为全局变量,但是由于不允许并行使用全局变量 因为我有点迷路.
I’m trying to create a C++ AMP dll to be used from C#. The same, rather large, matrix is used for all calculation, and I would prefer to copy it to the GPU only once. In CUDA I can just use save a device pointer as a global variable, but since I’m not allowed to use global variables in parallel for I’m a bit lost.
任何建议将不胜感激.
推荐答案
在调用p_f_e之前,请创建对该全局变量的本地引用,并捕获对p_f_e的引用以进行计算.
Before the call to the p_f_e, create a local reference to that global variable and capture that reference to the p_f_e for your computations.
下面的简单代码可以解决您的问题:
Following a simple code that solves your problem :
extern array_view<int,1> *arr_view;
extern array<int, 1> *arr;
int test()
{
arr_view = new array_view<int,1>(16); // Creating a global instance of Array_view
arr = new array<int,1>(16); // Creating a global instance of Array
array_view<int,1> &arrview_ref = *arr_view; // Creating Local reference to Array_view object
array<int,1> &arr_ref = *arr; // Creating Local reference to Array object
parallel_for_each(arr_view->extent,[=,&arr_ref](index<1> idx) restrict(amp)
{
arrview_ref[idx] = idx[0];
arr_ref[idx]= 16 - idx[0];
});
std::vector<int> output = *arr; // copy out;
for(int i=0; i < 16; i++)
{
std::cout << (*arr_view)[i] << " " << output[i] << std::endl;
}
}
这篇关于C ++ AMP中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!