每当我尝试在下面发布的调试器中运行代码时,它总是在设备指针上显示一条错误消息,例如变量 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;
}