Cuda 在结构中包装推力装置矢量?

Cuda 在结构中包装推力装置矢量?,cuda,thrust,Cuda,Thrust,我正在实现一个数学算法,需要保持大约15个不同的浮点向量。我想把所有这些设备向量包装成一个结构 typedef struct { thrust::device_vector<float> ... } data; 我收到这个错误 错误:typedef“data”不能用于详细的类型说明符中 此外,我怀疑当内存块大小的数据中没有任何内容驻留在主机内存中时,是否会对其进行mallock操作。正如注释中所述,标题中描述的原始错误与推力无关,而是由于使用了struct data,而data

我正在实现一个数学算法,需要保持大约15个不同的浮点向量。我想把所有这些设备向量包装成一个结构

typedef struct {
  thrust::device_vector<float> ...
} data;
我收到这个错误

错误:typedef“data”不能用于详细的类型说明符中


此外,我怀疑当内存块大小的数据中没有任何内容驻留在主机内存中时,是否会对其进行mallock操作。正如注释中所述,标题中描述的原始错误与推力无关,而是由于使用了
struct data
,而
data
已经进行了类型定义

在回答评论中提出的附加问题时,我只是想说,我没有充分考虑在结构中使用
推力::设备_向量
的后果。当我说也许考虑使用<代码>推力::DeVice TPTR <代码>时,我想到了这样的东西,这似乎对我有用:

#include <stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
#include <thrust/functional.h>

#define N 10

typedef struct {
  thrust::device_ptr<float> dp1;
  thrust::device_ptr<float> dp2;
  int i;
} data;


int main(){

  thrust::negate<int> op;

  data *mydata = (data *)malloc(sizeof(data));
  thrust::host_vector<float> h1(N);
  thrust::sequence(h1.begin(), h1.end());
  thrust::device_vector<float> d1=h1;
  mydata->dp1=d1.data();
  mydata->dp1[0]=mydata->dp1[N-1];

  thrust::transform(mydata->dp1, mydata->dp1 + N, mydata->dp1, op); // in-place transformation

  thrust::copy(d1.begin(), d1.end(), h1.begin());
  for (int i=0; i<N; i++)
    printf("h1[%d] = %f\n", i, h1[i]);

  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义n10
类型定义结构{
推力:装置ptr dp1;
推力:装置ptr dp2;
int i;
}数据;
int main(){
推力:否定op;
data*mydata=(data*)malloc(sizeof(data));
推力:主_矢量h1(N);
推力::序列(h1.begin(),h1.end());
推力:装置_矢量d1=h1;
mydata->dp1=d1.data();
mydata->dp1[0]=mydata->dp1[N-1];
转换(mydata->dp1,mydata->dp1+N,mydata->dp1,op);//就地转换
复制(d1.begin(),d1.end(),h1.begin());

对于(int i=0;i您之所以会收到该错误,是因为您正在执行
sizeof(struct data)
data
已定义了类型。您应该只执行
sizeof(data)
我没有直接回应这种方法的可用性。您可能希望在结构外部创建设备向量,然后使用
推力::设备\u ptr
结构。谢谢,这修复了typedef错误。当您说拥有推力::设备\u ptr结构时,您的意思是:1)创建foo=推力::设备向量,用数据填充它2)malloc struct data,它是设备ptr的结构,而不是设备向量3)分配数据->bar=foo?
#include <stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
#include <thrust/functional.h>

#define N 10

typedef struct {
  thrust::device_ptr<float> dp1;
  thrust::device_ptr<float> dp2;
  int i;
} data;


int main(){

  thrust::negate<int> op;

  data *mydata = (data *)malloc(sizeof(data));
  thrust::host_vector<float> h1(N);
  thrust::sequence(h1.begin(), h1.end());
  thrust::device_vector<float> d1=h1;
  mydata->dp1=d1.data();
  mydata->dp1[0]=mydata->dp1[N-1];

  thrust::transform(mydata->dp1, mydata->dp1 + N, mydata->dp1, op); // in-place transformation

  thrust::copy(d1.begin(), d1.end(), h1.begin());
  for (int i=0; i<N; i++)
    printf("h1[%d] = %f\n", i, h1[i]);

  return 0;
}