Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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
C++ 访问由固定的\u分配器分配的主机\u向量项(推力库)_C++_Cuda_Thrust - Fatal编程技术网

C++ 访问由固定的\u分配器分配的主机\u向量项(推力库)

C++ 访问由固定的\u分配器分配的主机\u向量项(推力库),c++,cuda,thrust,C++,Cuda,Thrust,我有以下向量: thrust::host_vector< T , thrust::cuda::experimental::pinned_allocator< T > > h_vector 这导致了以下错误: ../src/gpu.cuh(134): error: a reference of type "float &" (not const-qualified) cannot be initialized with a value of type "thrus

我有以下向量:

thrust::host_vector< T , thrust::cuda::experimental::pinned_allocator< T > > h_vector
这导致了以下错误:

../src/gpu.cuh(134): error: a reference of type "float &" (not const-qualified) cannot be initialized with a value of type "thrust::host_vector<float, thrust::system::cuda::experimental::pinned_allocator<float>>"
有没有更好的方法来实现这一点

编辑:原型代码

#include <thrust/host_vector.h>
#include <thrust/system/cuda/experimental/pinned_allocator.h>

static const int DATA_SIZE = 1024;

int main()
{

    thrust::host_vector<float, thrust::cuda::experimental::pinned_allocator<float> > *hh = new thrust::host_vector<float, thrust::cuda::experimental::pinned_allocator<float> >(DATA_SIZE);
    float member, *fptr;
    int i;

//  member = hh[1]; //fails

    fptr = thrust::raw_pointer_cast(hh->data()); //works
    member = fptr[1];
    return 0;
}
#包括
#包括
静态常量int DATA_SIZE=1024;
int main()
{
推力::主机向量*hh=新推力::主机向量(数据大小);
浮动成员*fptr;
int i;
//member=hh[1];//失败
fptr=stress::raw_pointer_cast(hh->data());//有效
成员=fptr[1];
返回0;
}
编辑2: 我实际上使用了这个向量:

thrust::host_vector< T , thrust::cuda::experimental::pinned_allocator< T > > *h_vector
推力::主机\向量>*h \向量

使我的原始问题完全具有误导性。

我不知道为什么在代码中需要这种复杂程度。你看过我贴的例子了吗

无论如何,这行代码:

   thrust::host_vector<float, thrust::cuda::experimental::pinned_allocator<float> > *hh = new thrust::host_vector<float, thrust::cuda::experimental::pinned_allocator<float> >(DATA_SIZE);
hh
是指向向量的指针时,不是尝试访问向量中元素的有效方法。这将是索引到向量数组中的一种有效方法,而这并不是您想要做的

另一方面,如果您这样做:

member = (*hh)[1];
我相信你的编译错误会消失。对我来说是的


请注意,我不认为这是CUDA或推力问题。我在使用
std::vector
尝试您的方法时遇到了类似的问题。还要注意的是,在您最初的问题中,您没有指出
h_vector
是指向向量的指针,并且您显示的代码行没有以这种方式创建它。因此,您编辑的/原型代码与原始描述明显不同。

什么是
h_数组
?向量的名称是
h\u向量
。我认为问题并不像您在这里描述的那样,您应该提供一个完整、简单的复制器,以及您正在使用的推力、cuda和主机编译器的版本细节(这正是您所期望的)。我试着根据你所展示的内容制作一个简单的复制机,但没有任何问题。看一看。请按照我所展示的示例提供完整的复制器,包括完整的编译器错误输出。感谢您指出类型错误。我刚刚添加了一个小测试用例,它再现了我的错误。请注意,尽管在我提供的文章中声明为指向host_向量的指针并使用了new,但原始代码是一个模板类,应该将host_向量作为成员。你说得对!我完全错过了比赛。我从typedef和fogot中提取了第一个摘录,以准确地再现我使用的方式。我很抱歉误导你,非常感谢你的帮助!
   thrust::host_vector<float, thrust::cuda::experimental::pinned_allocator<float> > *hh = new thrust::host_vector<float, thrust::cuda::experimental::pinned_allocator<float> >(DATA_SIZE);
member = hh[1];
member = (*hh)[1];