如何将 thrust::device_vector<int> 强制转换为使用原始指针运行

how to cast thrust::device_vector<int> to function with raw pointer

 __global__ void HYPER (int tFast, int tFastLenth, int kilo, int lenPrzvFast, double eps, int AF,double *arrINTLighFast, int *arrPrzvFncFst, int dv_ptr) 
     {
  for(int j = 0;j<(tFast*tFastLenth);j++)
      {  arrINTLighFast[j]=0;
      } 
          for(int j = 0;j<(kilo);j++) arrPrzvFncFst[j]=0;
   for(int j = 1;j<(tFast*tFastLenth);j++)
       { arrINTLighFast[j]=  arrINTLighFast[j-1] + AF*exp(-j/(eps+tFast) ); }

   for(int j = 0;j<(tFast*tFastLenth-1);j++)
         {
            for(int i=(arrINTLighFast[j]);i< (arrINTLighFast[j+1]);i++)
                {arrPrzvFncFst[i]=j;}
         }
   for(int j = 0;j<lenPrzvFast;j++)
        { devvecPrzvFncFst61440Slow149998[j]= arrPrzvFncFst[j] ;}
 }


int main (void)
{
const int tFast = 9;
const int tFastLenth = 6;
double arrINTLighFast[tFast*tFastLenth];
int arrPrzvFncFst[61500];
int AF = 1000;
int kilo = 1024;
int kilo150 = 149998;
const double   eps=0.0000001;
const int lenPrzvFast=61500;

    thrust::host_vector<int> vecPrzvFncFst61440Slow149998;
    int Len_vecPrzv=( lenPrzvFast+kilo150);       
    for (int j=0;j<Len_vecPrzv;j++)   vecPrzvFncFst61440Slow149998.push_back(0);
    for (int j=0;j<Len_vecPrzv;j++)     vecPrzvFncFst61440Slow149998 [j] = 0;
    thrust::device_vector<int> devvecPrzvFncFst61440Slow149998 = vecPrzvFncFst61440Slow149998;

    int *dv_ptr = thrust::raw_pointer_cast(devvecPrzvFncFst61440Slow149998.data());

    HYPER <<<blocks, threads>>>(tFast, tFastLenth, kilo, lenPrzvFast, eps, AF, arrINTLighFast, arrPrzvFncFst, dv_ptr);

   thrust::host_vector<int> HostvecPrzvFncFst61440Slow149998 = devvecPrzvFncFst61440Slow149998;
    std::cout << "Device vector is: " << std::endl;
    for(int j = 0; j<vecPrzvFncFst61440Slow149998.size(); j++) 
            std::cout << "vecPrzvFncFst61440Slow149998[" << j << "] = " << HostvecPrzvFncFst61440Slow149998[j] << std::endl;
 return 0;
 }

有个问题我不能在函数中使用向量,所以我决定使用thrust::raw_pointer_cast。但是,我遇到了问题:在编译过程中出现 错误:标识符 "devvecPrzvFncFst61440Slow149998" 未定义 。第二个是我无法确定如何将 int *dv_ptr 传递给函数和原型,有一个 错误:"int *" 类型的参数与 "int *" 类型的参数不兼容 "int"。我在网上找了找也没有办法成功解决我上面提到的问题

感谢您的宝贵时间

您的内核函数 HYPER 没有定义参数 devvecPrzvFncFst61440Slow149998,因此当您尝试在此处使用它时:

for(int j = 0;j<lenPrzvFast;j++)
    { devvecPrzvFncFst61440Slow149998[j]= arrPrzvFncFst[j] ;}

您将收到未定义的标识符错误。这里没有魔法,您的 CUDA 内核主要必须遵守普通 C 函数的规则。如果要使用变量,最好在函数参数中列出(全局范围变量和内置变量除外,这不是)。

你提到的另一个问题是由于 dv_ptr 是一个 pointer 类型:

int *dv_ptr = thrust::raw_pointer_cast(devvecPrzvFncFst61440Slow149998.data());

但您正试图将其传递到内核参数位置:

HYPER <<<blocks, threads>>>(..., dv_ptr);
                                 ^^^^^^

期望 普通(非指针)类型:

__global__ void HYPER (..., int dv_ptr) 
                            ^^^^^^^^^^

以下代码修复了这些问题并为我编译干净:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>

#define blocks 1
#define threads 1

 __global__ void HYPER (int tFast, int tFastLenth, int kilo, int lenPrzvFast, double eps, int AF,double *arrINTLighFast, int *arrPrzvFncFst, int *dv_ptr)
     {
  for(int j = 0;j<(tFast*tFastLenth);j++)
      {  arrINTLighFast[j]=0;
      }
          for(int j = 0;j<(kilo);j++) arrPrzvFncFst[j]=0;
   for(int j = 1;j<(tFast*tFastLenth);j++)
       { arrINTLighFast[j]=  arrINTLighFast[j-1] + AF*exp(-j/(eps+tFast) ); }

   for(int j = 0;j<(tFast*tFastLenth-1);j++)
         {
            for(int i=(arrINTLighFast[j]);i< (arrINTLighFast[j+1]);i++)
                {arrPrzvFncFst[i]=j;}
         }
   for(int j = 0;j<lenPrzvFast;j++)
        { dv_ptr[j]= arrPrzvFncFst[j] ;}
 }


int main (void)
{
const int tFast = 9;
const int tFastLenth = 6;
double arrINTLighFast[tFast*tFastLenth];
int arrPrzvFncFst[61500];
int AF = 1000;
int kilo = 1024;
int kilo150 = 149998;
const double   eps=0.0000001;
const int lenPrzvFast=61500;

    thrust::host_vector<int> vecPrzvFncFst61440Slow149998;
    int Len_vecPrzv=( lenPrzvFast+kilo150);
    for (int j=0;j<Len_vecPrzv;j++)   vecPrzvFncFst61440Slow149998.push_back(0);
    for (int j=0;j<Len_vecPrzv;j++)     vecPrzvFncFst61440Slow149998 [j] = 0;
    thrust::device_vector<int> devvecPrzvFncFst61440Slow149998 = vecPrzvFncFst61440Slow149998;

    int *dv_ptr = thrust::raw_pointer_cast(devvecPrzvFncFst61440Slow149998.data());

    HYPER <<<blocks, threads>>>(tFast, tFastLenth, kilo, lenPrzvFast, eps, AF, arrINTLighFast, arrPrzvFncFst, dv_ptr);

   thrust::host_vector<int> HostvecPrzvFncFst61440Slow149998 = devvecPrzvFncFst61440Slow149998;
    std::cout << "Device vector is: " << std::endl;
    for(int j = 0; j<vecPrzvFncFst61440Slow149998.size(); j++)
            std::cout << "vecPrzvFncFst61440Slow149998[" << j << "] = " << HostvecPrzvFncFst61440Slow149998[j] << std::endl;
 return 0;
 }