CXX0030:错误:表达不能评估,visual studio& CUDA [英] CXX0030:error:expression could not be evaluated, visual studio && CUDA

查看:206
本文介绍了CXX0030:错误:表达不能评估,visual studio& CUDA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Whenever I try to run the code in the debugger that I posted below it always show an error message on the device pointer such as in variable check_d,check_re_h...saying


无法评估CXX0030:error:expression。

CXX0030:error:expression could not be evaluated.

我对CUDA和visual studio非常新,所以任何帮助将非常感谢。

I am very new to CUDA and visual studio so any help would be greatly appreciated.

#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;
}


推荐答案

代码,从设备内存分配(也是间距分配)开始到2D mem副本。下面是一个固定的代码。请注意,我已澄清在注释中的修改

There were some issues with your code, beginning with device memory allocations (also pitched allocations) to 2D mem copies. Below is a "fixed" code. Please, notice that I have clarified the modifications in the comments

#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;
}

这篇关于CXX0030:错误:表达不能评估,visual studio&amp; CUDA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆