CUDA:统一内存和指针地址的更改

CUDA: Unified Memory and change of pointer address?

本文关键字:地址 指针 内存 CUDA      更新时间:2023-10-16

我正在使用cuBlas为一些矩阵运算创建一个库。我首先实现了矩阵多重

库头类(.h文件(的代码段


#include "cusolverDn.h"  // NOLINT
#include "cuda_runtime.h"  // NOLINT
#include "device_launch_parameters.h"  // NOLINT
namespace perception_core {
namespace matrix_transform {
class CudaMatrixTransformations {
public:
CudaMatrixTransformations();
~CudaMatrixTransformations();
void MatrixMultiplicationDouble(double *A, double *B, double *C, const int m, const int k, const int n);
private:
// Cublas stuff
cudaError_t cudaStat1;
cudaError_t cudaStat2;
cublasHandle_t cublasH;
cublasStatus_t cublas_status;

};
}  // namespace matrix_transform
}  // namespace perception_core
#endif  // LIB_CUDA_ROUTINES_INCLUDE_MATRIX_TRANSFORMS_H_

乘法(.cu文件(的库类实现片段

// This calculates the matrix mult C(m,n) = A(m,k) * B(k,n)
void CudaMatrixTransformations::MatrixMultiplicationDouble(
double *A, double *B, double *C, int m, int k, const int n) {
// Calculate size of each array
size_t s_A = m * k;
size_t s_B = k * n;
size_t s_C = m * n;
// Create the arrays to use in the GPU
double *d_A = NULL;
double *d_B = NULL;
double *d_C = NULL;

// Allocate memory
cudaStat1 = cudaMallocManaged(&d_A, s_A * sizeof(double));
cudaStat2 = cudaMallocManaged(&d_B, s_B * sizeof(double));
assert(cudaSuccess == cudaStat1);
assert(cudaSuccess == cudaStat2);
cudaStat1 = cudaMallocManaged(&d_C, s_C * sizeof(double));
assert(cudaSuccess == cudaStat1);
// Copy the data to the device data
memcpy(d_A, A, s_A * sizeof(double));
memcpy(d_B, B, s_B * sizeof(double));
// Set up stuff for using CUDA
int lda = m;
int ldb = k;
int ldc = m;
const double alf = 1;
const double bet = 0;
const double *alpha = &alf;
const double *beta = &bet;
cublas_status = cublasCreate(&cublasH);
assert(cublas_status == CUBLAS_STATUS_SUCCESS);
// Perform multiplication
cublas_status = cublasDgemm(cublasH, // CUDA handle
CUBLAS_OP_N, CUBLAS_OP_N, // no operation on matrices
m, n, k, // dimensions in the matrices
alpha, // scalar for multiplication
d_A, lda, // matrix d_A and its leading dim 
d_B, ldb, // matrix d_B and its leading dim 
beta, // scalar for multiplication
d_C, ldc // matrix d_C and its leading dim 
);
cudaStat1 = cudaDeviceSynchronize();
assert(cublas_status == CUBLAS_STATUS_SUCCESS);
assert(cudaSuccess == cudaStat1);
// Destroy the handle
cublasDestroy(cublasH);
C = (double*)malloc(s_C * sizeof(double));
memcpy(C, d_C, s_C * sizeof(double));
// Make sure to free resources
if (d_A) cudaFree(d_A);
if (d_B) cudaFree(d_B);
if (d_C) cudaFree(d_C);
return;
}
CudaMatrixTransformations::CudaMatrixTransformations() {
cublas_status = CUBLAS_STATUS_SUCCESS;
cudaStat1 = cudaSuccess;
cudaStat2 = cudaSuccess;
}

然后我创建了一个gtest程序来测试我的功能。其中,我在MatrixMultiplicationDouble函数中传递了一个double *result = NULL;作为C参数。

gtest程序(.cc文件(的代码段

TEST_F(MatrixTransformsTest, MatrixMultiplication) {
double loc_q[] = {3, 4, 5, 6, 7 ,8};
double *q = loc_q;
double loc_w[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
double *w = loc_w;
double *result = NULL;
double loc_result[M_ROWS * M_COLS] = {14, 50, 86, 122, 23, 86, 149, 212};
matrix_result = loc_result;
size_t m = 4;
size_t k = 3;
size_t n = 2;
perception_core::matrix_transform::CudaMatrixTransformations transforms;
transforms.MatrixMultiplicationDouble(w, q, result, m, k, n);
auto rr = std::addressof(result);
printf("nC addr: %pn", rr); 
std::cout << "result:n";
print_matrix(result, m, n);
EXPECT_TRUE(compare<double>(result, matrix_result, m * n));
}

cuBlas中的例程运行良好,因为当我在.cu文件中打印矩阵时,我可以看到结果。但是,当我尝试访问gtest文件中的result时,我会遇到seg错误。经过进一步检查,我注意到result指针的地址在.cu内部和.cpp中是不同的。举个例子:

C addr: 0x7ffc5749db08 (inside .cu)
C addr: 0x7ffc5749dba0 (inside .cpp)

我认为通过使用统一内存,我可以从主机或设备访问该指针。我似乎找不到为什么这个地址会更改并修复seg故障问题的答案。使用统一内存有什么不足之处吗?非常感谢。

这行没有做你需要的:

cudaStat1 = cudaMallocManaged(&C, s_C * sizeof(double));

当您修改C指针的数值时,该修改将不会显示在调用环境中。这就是按值传递的性质,当您调用CudaMatrixTransformations::MatrixMultiplicationDouble时,C指针的数值按值传递

因此,这一行将在函数内部工作(也许(,但结果不会以这种方式传递回调用环境。

我建议重新编写代码,以便以类似于处理AB的方式处理C。创建一个额外的指针d_C,在上面执行cudaMallocManaged,然后在返回memcpy之前,将结果从d_C返回到C。这假设您在调用此函数之前为C指针进行了正确的分配。

还要注意,最后您将释放AB——我认为这不是您想要的。返回前应释放d_Ad_Bd_C

您的代码还有其他问题。例如,您提到返回一个result指针,但我看不到任何证据。实际上,我没有看到任何名为result的指针。此外,函数原型(在类定义中(与您的实现不匹配。原型建议返回double*,而您的实现返回void

由于我列出了观察结果,我不认为addressof的使用会给你提供你认为的信息。如果你要比较数值指针值,你需要比较值本身,而不是这些值存储位置的地址。