vs2012中将数组视图作为容器 [英] Array view as a container in vs2012
问题描述
大家好!我正在VS 2012上使用C ++ AMP,并尝试在没有数据源(仅传递大小)的情况下创建array_view.使用VS 2013,我们可以使用新添加的构造函数来实现.有什么解决方案/补丁要制作 可以在VS 2012上使用.
Hi guys! I am working with C++ AMP on VS 2012 and trying to create array_view without data source(passing just size). With VS 2013 we can make it with newly added constructor. Is there any solution / patch to make it working with VS 2012.
我正在使用类对象,并且在我的代码中有一种情况,如果我使用new()创建concurrency :: array,则需要在不使用源数组的情况下在设备上创建内存动态分配),然后在构造函数中释放 它不能作为指针(我需要保留为类的数据成员)在parallel_for_each内部.我有一个以array_view作为数据成员的类,整个类都在parallel_for_each内部.
I am working with class objects and have a situation in my code that I need to create memory on device without using source array, if I create concurrency::array with new(dynamic allocation) inside the constructor an then free it which is not possible as pointer(which I need to keep as data member of class) are not allowed inside parallel_for_each. I have class which has array_view as data member and entire class is going inside parallel_for_each.
谢谢
推荐答案
嗨_jay,
感谢您积极使用C ++ AMP.不带源的Array_view是VS 2013中添加的功能.目前,我们尚无计划将此功能提供给早期版本.
Thank you for actively using C++ AMP. Array_view without source is a feature added in VS 2013. At this point of time, we don't have plans to make this feature available for earlier versions.
使用'concurrency :: array',您的问题仍然可以解决.与其在p_f_e中捕获数据成员",不如创建对该数据成员的本地引用,然后在p_f_e内部捕获本地引用.
Using 'concurrency::array', your problem can still be solved. Instead of capturing the 'data member' inside p_f_e, create a local reference to that data member and capture the local reference inside your p_f_e.
下面是它的简单示例:
A级
{
array< int> * a;
class A
{
array<int> *a;
public:
A()
{
&stb :: cout<< "创建并发::数组对象<< std :: endl;
a =新数组< int>(256);
}
public:
A()
{
std::cout << " Creating concurrency::array Object " << std::endl;
a = new array<int>(256);
}
void p_f_e_function()
{
array< int> & temp_a = * a;
parallel_for_each(temp_a.extent,[& temp_a](index< 1>& idx)限制(amp)
{
temp_a [idx] = 255;
});
void p_f_e_function()
{
array<int> &temp_a = *a;
parallel_for_each(temp_a.extent,[&temp_a](index<1>& idx) restrict(amp)
{
temp_a[idx] = 255;
});
std :: vector< int>输出(256);
copy(* a,output.begin());//我正在使用* a 来访问GPU内存中的内容.
for(int i = 0; i< output.size(); i ++)
{
if(output [i]!= 255)
{
std :: cout<< "失败于"<<我<< " ...预期255:实际" <<输出[i]<< std :: endl;
}
}
}
〜A()
{
&stb :: cout<< "释放并发::数组对象<< std :: endl;
删除a;
}
};
std::vector<int> output(256);
copy(*a,output.begin()); // I am using *a to access the content in GPU memory.
for(int i = 0; i < output.size() ; i++)
{
if(output[i] != 255)
{
std::cout << " Failed at " << i << " ... Expected 255 : Actual " << output[i] << std::endl;
}
}
}
~A()
{
std::cout << " Freeing concurrency::array Object " << std::endl;
delete a;
}
};
让我们知道这是否可以解决您的问题.
Let us know if this solves your problem.
这篇关于vs2012中将数组视图作为容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!