CUDA:统一内存和指针地址的更改
CUDA: Unified Memory and change of pointer address?
我正在使用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
指针的数值按值传递
因此,这一行将在函数内部工作(也许(,但结果不会以这种方式传递回调用环境。
我建议重新编写代码,以便以类似于处理A
和B
的方式处理C
。创建一个额外的指针d_C
,在上面执行cudaMallocManaged
,然后在返回memcpy
之前,将结果从d_C
返回到C
。这假设您在调用此函数之前为C
指针进行了正确的分配。
还要注意,最后您将释放A
和B
——我认为这不是您想要的。返回前应释放d_A
、d_B
和d_C
。
您的代码还有其他问题。例如,您提到返回一个result
指针,但我看不到任何证据。实际上,我没有看到任何名为result
的指针。此外,函数原型(在类定义中(与您的实现不匹配。原型建议返回double*
,而您的实现返回void
。
由于我列出了观察结果,我不认为addressof
的使用会给你提供你认为的信息。如果你要比较数值指针值,你需要比较值本身,而不是这些值存储位置的地址。
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么指针不写入类的地址?
- CUDA:统一内存和指针地址的更改
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 如何从绝对地址的 C 样式指针创建对C++对象的引用
- 当我在 C++ 中将派生类的指针分配给指针时,地址会更改
- 是否可以仅通过将分配的指针地址存储在C++中来分析内存?
- 如何在程序集函数中将元素数组作为参数传递时转发 ARM 寄存器的地址指针
- 如何使用构造函数初始化内存地址(指针变量)?
- 数组基础地址指针
- 如何在不使用返回的情况下从函数获取变量的地址(指针)
- 保存QWidgets的地址/指针
- C++ 读取内存地址/指针和偏移量
- 从存储在 CTreeCtrl 的 LPARAM 中的结构中检索地址/指针 (IXMLDOMNode*) - 不起作用
- 简单的C++地址/指针澄清
- 如何获取成员函数的地址指针
- 当“按引用传递”成为强制传递而不是按地址/指针传递时
- C++ 为什么不是同一个地址(指针)