我正在编写的程序(Accelerator.cu)不会在NVCC8.0.61下用nvcc -std=c++11 -o accelerator accelerator.cu编译。对于__device__、__global__和__shared__失败的原因,还有其他答案,但没有一个在自定义代码中揭示了此错误的原因。我正试图跟随导游https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#um-global-scope。但是,在尝试以下代码时:
#include <cuda_runtime_api.h>
#include <cuda.h>
// CUDA Acceleration Adapter.
class Accelerator {
public:
__device__ __managed__ float** A;
__device__ __managed__ float* B;
__device__ __managed__ int N;
__device__ __managed__ int C;
Accelerator () {}
Accelerator (int N, int C) {
// initialize variables (unified memory).
N = records;
// are "inputs"
this->C = C;
}
void setData (vector<vector<float>>& A, vector<float>& B) {
// convert vectors to arrays that the GPU can address.
}
void accelerate (vector<float>& results) {
// run kernel.
// convert results back to vector.
}
__global__ void evaluate (float **A, float *B, int N, int C) {
// do stuff.
}
};
void main () {
Accelerator testAcc();
}但是,我收到了所有A的错误
accelerator.cu(8): error: attribute "device" does not apply here
accelerator.cu(8): error: attribute "managed" does not apply here以及其他3个成员变量的类似错误。
这是我第一次尝试编写我自己的GPU加速程序.如果有人知道出了什么问题,我们会非常感谢你的帮助。
发布于 2018-04-17 02:12:02
你可能会遇到很多问题。我将主要关注一些尝试获得您展示的编译内容的内容,而不是深入了解您可能在这里讨论的CUDA编程的每一个方面。
您引用的问题(例如在类成员变量上使用__device__ )是明令禁止。
同样,也不允许使用__managed__ (因为它是作用域静态分配)。在这样的场景中,您应该使用普通的类成员变量,并根据需要动态分配它们,比如在构造函数中,或者使用动态托管分配器(cudaMallocManaged)。使用指针变量作为类成员变量肯定表明了这种方法。
你所概述的可能还有其他的挑战。例如,__global__函数可能不是类成员函数。。
在CUDA编程中,您可能需要进行大量的学习,但以下是您所展示的未涉及任何明显问题的被黑客攻击的版本:
#include <vector>
using namespace std;
// CUDA Acceleration Adapter.
__global__ void evaluate (float *a, float *b, int n, int c) {
for (int i = 0; i < n; i++) a[i]++;
for (int i = 0; i < c; i++) b[i]++;
}
class Accelerator {
public:
float* A;
float* B;
int N;
int C;
Accelerator () {}
Accelerator (int N, int C) {
// initialize variables (unified memory).
this->N = N;
// are "inputs"
this->C = C;
cudaMallocManaged(&A, N*sizeof(float));
cudaMallocManaged(&B, C*sizeof(float));
}
void setData (vector<float>& A, vector<float>& B) {
for (int i=0; i < N; i++) (this->A)[i] = A[i];
for (int i=0; i < C; i++) (this->B)[i] = B[i];
}
void accelerate (vector<float>& results) {
evaluate<<<1,1>>>(A, B, N, C);
cudaDeviceSynchronize();
for (int i = 0; i<N; i++) results[i] = A[i];
}
};
int main () {
Accelerator testAcc(5,3);
}https://stackoverflow.com/questions/49868392
复制相似问题