[英]two-dimensional arrays in CUDA
I'm practicing this simple code which takes a two-dimensional array and sums them up with CUDA.我正在练习这个简单的代码,它采用二维数组并将它们与 CUDA 相加。 In the end, the result of C is not what I accepting.最后,C的结果不是我接受的。 Also, I was wondering whether I can use vector
instead of c-style arrays.另外,我想知道是否可以使用vector
而不是 c 样式的 arrays。
#include <iostream>
using namespace std;
#define N 2
__global__ void MatAdd(double** a, double** b,
double** c)
{
int i = threadIdx.x;
int j = threadIdx.y;
c[i][j] = a[i][j] + b[i][j];
}
int main()
{
double a[2][2]= {{1.0,2.0},{3.0,4.0}};
double b[2][2]= {{1.0,2.0},{3.0,4.0}};
double c[2][2]; // it will be the result!
double** a_d;
double** b_d;
double** c_d;
int d_size = N * N * sizeof(double);
int numBlocks = 1;
dim3 threadsPerBlock(N, N);
cudaMalloc(&a_d, d_size);
cudaMalloc(&b_d, d_size);
cudaMalloc(&c_d, d_size);
cudaMemcpy(a_d, a, d_size, cudaMemcpyHostToDevice);
cudaMemcpy(b_d, b, d_size, cudaMemcpyHostToDevice);
cudaMemcpy(c_d, c, d_size, cudaMemcpyHostToDevice);
MatAdd<<<numBlocks, threadsPerBlock>>>(a_d, b_d, c_d);
//cudaDeviceSynchronize();
cudaMemcpy(c, c_d, d_size, cudaMemcpyDeviceToHost);
for (int i=0; i<N; i++){
for(int j=0; j<N; j++){
cout<<c[i][j]<<endl;
}
}
return 0;
}
You must not use the double**
type in this case.在这种情况下,您不能使用double**
类型。 Alternatively, you should use a flatten array that contains all the values of a given matrix in a double*
-type variable .或者,您应该使用一个扁平数组,该数组在double*
类型变量中包含给定矩阵的所有值。
The heart of the problem is located in the following line (and the similar next ones):问题的核心位于以下行(以及类似的下一行):
cudaMemcpy(a_d, a, d_size, cudaMemcpyHostToDevice);
Here you assume that a
and a_d
are compatible types, but they are not.在这里,您假设a
和a_d
是兼容的类型,但它们不是。 A double**
-typed variable is a pointer that refer to one or more pointers in memory (typically an array of pointer referencing many different double
-typed arrays), while a double*
-typed variable or a static 2D C array refer to a contiguous location in memory. A double**
-typed variable is a pointer that refer to one or more pointers in memory (typically an array of pointer referencing many different double
-typed arrays), while a double*
-typed variable or a static 2D C array refer to a memory 中的连续位置。
Note that you can access to a given (i,j)
cell of a matrix using matrix[N*i+j]
, where N
is the number of column, assuming matrix is a flatten matrix of type double*
and use a row-major ordering .请注意,您可以使用matrix[N*i+j]
访问矩阵的给定(i,j)
单元格,其中N
是列数,假设 matrix 是double*
类型的展平矩阵并使用row-主要订购。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.