我在掌握为什么我的函数在使用CUBLAS的双精度范围内找到最大值和最小值时无法正常工作时遇到问题。
代码如下:
void findMaxAndMinGPU(double* values, int* max_idx, int* min_idx, int n) { double* d_values; cublasHandle_t handle; cublasStatus_t stat; safecall( cudaMalloc((void**) &d_values, sizeof(double) * n), "cudaMalloc (d_values) in findMaxAndMinGPU"); safecall( cudaMemcpy(d_values, values, sizeof(double) * n, cudaMemcpyHostToDevice), "cudaMemcpy (h_values > d_values) in findMaxAndMinGPU"); cublasCreate(&handle); stat = cublasIdamax(handle, n, d_values, sizeof(double), max_idx); if (stat != CUBLAS_STATUS_SUCCESS) printf("Max failedn"); stat = cublasIdamin(handle, n, d_values, sizeof(double), min_idx); if (stat != CUBLAS_STATUS_SUCCESS) printf("min failedn"); cudaFree(d_values); cublasDestroy(handle); }
其中值是要在其中搜索的值。 max_idx和min_idx是值中找到的数字的索引。 来自CUBLAS调用的结果似乎相当随机并输出错误的索引。
谁对我的问题有很好的答案? 我此刻有点伤心:(
你对cublasIdamax
和cublasIdamin
调用的一个论点是错误的。 BLAS 1级调用中的incx
参数应始终是单词输入的步幅,而不是字节。 所以我怀疑你想要更像的东西:
stat = cublasIdamax(handle, n, d_values, 1, max_idx); if (stat != CUBLAS_STATUS_SUCCESS) printf("Max failedn"); stat = cublasIdamin(handle, n, d_values, 1, min_idx); if (stat != CUBLAS_STATUS_SUCCESS) printf("min failedn");
通过使用sizeof(double)
您可以告诉例程使用8的步长,这将使调用超出输入数组的已分配存储并进入未初始化的内存。 我认为你实际上在d_values
有一个1的d_values
。
编辑:这是一个完整的可运行示例,它可以正常工作。 注意我将代码切换到单精度,因为我目前无法访问具有双精度function的硬件:
#include #include #include #include #include typedef float Real; void findMaxAndMinGPU(Real* values, int* max_idx, int* min_idx, int n) { Real* d_values; cublasHandle_t handle; cublasStatus_t stat; cudaMalloc((void**) &d_values, sizeof(Real) * n); cudaMemcpy(d_values, values, sizeof(Real) * n, cudaMemcpyHostToDevice); cublasCreate(&handle); stat = cublasIsamax(handle, n, d_values, 1, max_idx); if (stat != CUBLAS_STATUS_SUCCESS) printf("Max failedn"); stat = cublasIsamin(handle, n, d_values, 1, min_idx); if (stat != CUBLAS_STATUS_SUCCESS) printf("min failedn"); cudaFree(d_values); cublasDestroy(handle); } int main(void) { const int vmax=1000, nvals=10000; float vals[nvals]; srand ( time(NULL) ); for(int j=0; j vals[cmax]) ? i : cmax; } fprintf(stdout, "%d %d %d %dn", minIdx, cmin, maxIdx, cmax); return 0; }
在编译和运行时给出:
$ g++ -I/usr/local/cuda/include -L/usr/local/cuda/lib cublastest.cc -lcudart -lcublas $ ./a.out 273 272 85 84
请注意,CUBLAS遵循FORTRAN约定并使用1索引,而不是零索引,这就是为什么CUBLAS和CPU版本之间存在1的差异。
来自描述:最大幅度的元素: http : //docs.nvidia.com/cuda/cublas/index.html#topic_6_1
if you have { 1, 2, 3, -33, 22, 11 }
结果将是4! 不是5
需要了解更多c/c++开发分享使用CUBLAS查找最大值和最小值,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!
abs(-33) > 22
以上就是c/c++开发分享使用CUBLAS查找最大值和最小值相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/979591.html