cuda - `cublasIsamin` 返回不正确的值
问题描述
数组存储为xyzxyz...
,我想获取某个方向(x 或 y 或 z)的最大值和最小值,这是测试程序:
#include <cuda_runtime.h>
#include <cuda_runtime_api.h> // cudaMalloc, cudaMemcpy, etc.
#include <cublas_v2.h>
#include <helper_functions.h> // shared functions common to CUDA Samples
#include <helper_cuda.h> // CUDA error checking
#include <stdio.h> // printf
#include <iostream>
template <typename T>
void print_arr(T *arr, int L)
{
for (int i = 0; i < L; i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main()
{
float hV[10] = {3, 0, 7, 1, 2, 8, 6, 7, 6, 4};
print_arr(hV, 10);
float *dV;
cudaMalloc(&dV, sizeof(float) * 10);
cudaMemcpy(dV, hV, sizeof(float) * 10, cudaMemcpyHostToDevice);
cublasHandle_t cublasHandle = NULL;
checkCudaErrors(cublasCreate(&cublasHandle));
int hResult[2] = {0};
checkCudaErrors(cublasIsamax(cublasHandle, 10, dV, 3, hResult + 0));
checkCudaErrors(cublasIsamin(cublasHandle, 10, dV, 3, hResult + 1));
print_arr(hResult, 2);
return 0;
}
预期结果:
3 0 7 1 2 8 6 7 6 4
3 2
结果:
3 0 7 1 2 8 6 7 6 4
3 5
这个结果有问题吗?还是我理解错了?
链接到cublasIsamin
.
解决方案
cublasIsamin
找到最小值的索引。该索引不是在原始数组上计算的,但也会考虑incx
参数。此外,它将搜索n
元素(第一个参数),而不考虑其他参数,例如incx
.
你有一个这样的数组:
index: 0 1 2 3 4 5 6 7 8 9
x/y/z: x y z x y z x y z x
value: 3 0 7 1 2 8 6 7 6 4
x index: 1 2 3 4
因此最小值x
在索引 3 处,搜索总共n=4
(不是 10 个)元素。对于 x 值,我们必须从dV
偏移量 0 开始搜索,增量为 3,以获取最大n=4
元素。
考虑到所有这些,正确的调用是:
cublasIsamax(cublasHandle, 4, dV, 3, hResult + 0));
cublasIsamin(cublasHandle, 4, dV, 3, hResult + 1));
预期的结果是:
3 2
推荐阅读
- python - 在列表中搜索数据并将其导出到 .csv 文件中
- bash - 运行编译程序 20 次并总结结果
- android - Android - 即时应用程序中的 Firebase 初始化 - 获取异常
- javascript - GoogleAuthProvider 在网络应用程序中给出错误“this.ta is not a function”
- python - 无法将对象更改为在熊猫数据框中浮动
- javascript - React 中的对象
- javascript - 固定精度始终在 js 0.5 -> 0.5000
- django - Django 中的 APIView 链接
- python - 为什么 python tesseract 为字母提供错误的绑定框?
- android - 我试图查询在 firebase 中使用 push() 创建的 id