0

每当我尝试在下面发布的调试器中运行代码时,它总是在设备指针上显示一条错误消息,例如变量 check_d,check_re_h...说

CXX0030:错误:无法评估表达式。

我对 CUDA 和 Visual Studio 非常陌生,因此将不胜感激任何帮助。

#include<stdio.h>
//#include<stddef.h>
//#include"sourceannotations.h"
#include<cuda.h>
//#include<cutil.h>
#include<cuda_runtime_api.h>
#include<cuda_runtime.h>
#include<iostream>
#include<device_launch_parameters.h>
#include <cmath>
#include<cstdlib>
#include<time.h>
#include<string>
#include<vector>
#define PI 3.141592654
using namespace std;
// #define checkCudaErrors(err)  __checkCudaErrors (err, __FILE__, __LINE__)
struct vertex
{
    float x,y,z,h;
    // struct triangle* _triangle ;
    // struct octree* tree ;
    vertex():x(0),y(0),z(0),h(1){};//,tree(NULL)/*, _triangle(NULL)*/{}
    vertex(float x, float y, float z, float h=0):x(x),y(y),z(z),h(h){};//,/*_triangle(NULL),*/tree(NULL){}


};
struct triangle
{
    vertex v1,v2,v3;
    triangle(){
        //v1._triangle = v2._triangle = v3._triangle = this;
    }
};
//if the function is decleared as global then it is run by multiple threads parallelly

__global__ void VecAdd(/*int *A, int *B, int *C,*/ int *check)
{
    //int count =0;
    int idx = blockIdx.x+ threadIdx.x;
    //  int count=0;
    //int tx = threadIdx.x;
    //this is for checking the value of idx 
    check[idx] = idx;
    //C[idx] = A[idx] + B[idx];


}
__global__ void check(float mat[][4],vertex *a,float *re,int *index)
{
    // float re[4];
    float sum =0;  
    int idx = blockIdx.x+ threadIdx.x;
    //   index[idx] = idx;
    //int count=0;
    //int tx = threadIdx.x;

    /*for (int i=0; i<4; i++)
      {*/

    sum +=  mat[idx][0]* a->x;
    sum +=  mat[idx][1]* a->y;
    sum +=  mat[idx][2]* a->z;
    sum +=  mat[idx][3];
    /*sum +=  *((float*)mat+idx+4*0)* a->x;
      sum +=  *((float*)mat+idx+4*1)* a->y;
      sum +=  *((float*)mat+idx+4*2)* a->z;
      sum +=  *((float*)mattr+idx+4*3);*/    


    /*}*/
    re[idx] = sum;

}
int main()
{
    //float res[4][4];

    triangle t1;

    t1.v1.x = 2;
    t1.v1.y = 1.33512;
    t1.v1.z = 5.849567;

    t1.v2.x = 2;
    t1.v2.y = -1.33512;
    t1.v2.z = 5.849567;

    t1.v3.x = 2;
    t1.v3.y = 0;
    t1.v3.z = 5;

    vertex *check_d;
    vertex *check_h;
    float *check_re_d;
    float *check_re_h;

    float translation_check_d[4][4];
    float translation_check_h[4][4] = {{1, 0, 0, -t1.v1.x},
        {0, 1, 0, -t1.v1.y},
        {0, 0, 1, -t1.v1.z},
        {0 ,0 ,0, 1}};

    check_h = new vertex(1,-4,3);
    check_re_h = new float[4];
    cudaMalloc((void**)&check_d,sizeof(vertex));
    cudaMalloc((void**)&check_re_d,4*sizeof(float));
    cudaMemcpy(check_d,check_h,sizeof(vertex),cudaMemcpyHostToDevice);
    cudaMemcpy(check_re_d,check_re_h,4*sizeof(float),cudaMemcpyHostToDevice);

    size_t dPitch;

    cudaMallocPitch((void**)translation_check_d,&dPitch,4*sizeof(float),4);

    cudaMemcpy2D(translation_check_d,dPitch,translation_check_h,dPitch,4*sizeof(float),4,cudaMemcpyHostToDevice);
    int *index_h = NULL;
    int *index_d = NULL;
    index_h = new int[4];
    cudaMalloc((void**)&index_d,4*sizeof(int));

    cudaMemcpy(index_d,index_h,sizeof(int),cudaMemcpyHostToDevice);

    check<<<1,4>>>(translation_check_d,check_d,check_re_d,index_d);
    //VecAdd<<<10,1>>>(index_d);
    cudaMemcpy(check_re_h,check_re_d,4*sizeof(float),cudaMemcpyDeviceToHost);
    cudaMemcpy(index_h,index_d,4*sizeof(int),cudaMemcpyDeviceToHost);
    std::cout<<"These are the value"<<"INDEX: "<<index_h[0]<<" x: "<<check_re_h[0]<<"\n";
    std::cout<<"These are the value"<<"INDEX: "<<index_h[1]<<" x: "<<check_re_h[1]<<"\n";
    std::cout<<"These are the value"<<"INDEX: "<<index_h[2]<<" x: "<<check_re_h[2]<<"\n";
    cudaFree(check_d);
    cudaFree(check_re_d);
    cudaFree(index_d);
    cudaFree(check_h);
    cudaFree(check_re_h);
    cudaFree(index_h);
    int a;
    cin>>a;
    return 0;
}
4

1 回答 1

1

您的代码存在一些问题,首先是设备内存分配(也是倾斜分配)到 2D 内存副本。下面是一个“固定”代码。请注意,我已经澄清了评论中的修改

#include<stdio.h>  
#include<cuda.h>
#include<cuda_runtime_api.h>
#include<cuda_runtime.h>
#include<iostream>
#include<device_launch_parameters.h>
#include <cmath>
#include<cstdlib>
#include<time.h>
#include<string>
#include<vector>
#include<conio.h>

#define PI 3.141592654
using namespace std;

/*****************/
/* VERTEX STRUCT */
/*****************/
struct vertex
{
    float x,y,z,h;
    vertex():x(0),y(0),z(0),h(1){};//,tree(NULL)/*, _triangle(NULL)*/{}
    vertex(float x, float y, float z, float h=0):x(x),y(y),z(z),h(h {};//,/*_triangle(NULL),*/tree(NULL){}
};

/*******************/
/* TRIANGLE STRUCT */
/*******************/
struct triangle
{
    vertex v1,v2,v3;
    triangle(){ }
};

// The kernel function interface should contain also the pitch value. I have removed the int* index parameter (not needed now).
// I have also updated the mat parameter.
//__global__ void check(float mat[][4],vertex *a,float *re,int *index)
__global__ void check(float** mat,vertex *a,float *re,size_t pitch)
{
    float sum = 0;  
    int idx = threadIdx.x;

    float* row = (float*)((char*)mat + idx*pitch);

    printf("row %i column 0 value %f \n",idx,row[0]);
    printf("row %i column 1 value %f \n",idx,row[1]);
    printf("row %i column 2 value %f \n",idx,row[2]);
    printf("row %i column 3 value %f \n",idx,row[3]);

    sum +=  mat[idx][0]* a->x;
    sum +=  mat[idx][1]* a->y;
    sum +=  mat[idx][2]* a->z;
    sum +=  mat[idx][3];

    re[idx] = sum;
 }

 /********/
 /* MAIN */
 /********/

 int main()
 {
     triangle t1;

     t1.v1.x = 2;
     t1.v1.y = 1.33512;
     t1.v1.z = 5.849567;

     t1.v2.x = 2;
     t1.v2.y = -1.33512;
     t1.v2.z = 5.849567;

     t1.v3.x = 2;
     t1.v3.y = 0;
     t1.v3.z = 5;

     vertex* check_h = new vertex(1,-4,3);
     vertex* check_d;   cudaMalloc((void**)&check_d,sizeof(vertex));
     float* check_re_h = new float[4];
     float* check_re_d; cudaMalloc((void**)&check_re_d,4*sizeof(float));

     cudaMemcpy(check_d,check_h,sizeof(vertex),cudaMemcpyHostToDevice);
     cudaMemcpy(check_re_d,check_re_h,4*sizeof(float),cudaMemcpyHostToDevice);

     float translation_check_h[4][4] = {{1, 0, 0, -t1.v1.x},{0, 1, 0, -t1.v1.y},{0, 0, 1, -t1.v1.z},{0 ,0 ,0, 1}};
     //This is a host-side static array definition. 
     //float translation_check_d[4][4];
     float** translation_check_d;

     // This is a wrong usage of cudaMallocPitch. The correct syntax is cudaMallocPitch(void** devPtr, size_t* pitch, size_t widthInBytes, size_t height).   
     // size_t dPitch; cudaMallocPitch((void**)translation_check_d,&dPitch,4*sizeof(float),4);
     size_t dPitch; cudaMallocPitch(&translation_check_d,&dPitch,4*sizeof(float),4);
     // I have fixed also the cudaMemcpy2D call, see below.

      //cudaMemcpy2D(translation_check_d,dPitch,translation_check_h,dPitch,4*sizeof(float),4,cudaMemcpyHostToDevice);
     cudaMemcpy2D(translation_check_d, dPitch, translation_check_h, 4*sizeof(float), 4*sizeof(float), 4, cudaMemcpyHostToDevice);

     // Useless
     //int *index_h = new int[4];
     //int *index_d = NULL; cudaMalloc((void**)&index_d,4*sizeof(int));
     //cudaMemcpy(index_d,index_h,sizeof(int),cudaMemcpyHostToDevice);

     //check<<<1,4>>>(translation_check_d,check_d,check_re_d,index_d);
     check<<<1,4>>>(translation_check_d,check_d,check_re_d,dPitch);

     // I haven't checked the rest, being it straightforward.
     getch();
     return 0;
}
于 2013-10-29T11:28:19.173 回答