【问题标题】:`cublasIsamin` returns an incorrect value`cublasIsamin` 返回不正确的值
【发布时间】:2021-02-01 12:15:49
【问题描述】:

数组存储为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

这个结果有问题吗?还是我理解错了?

linkcublasIsamin

【问题讨论】:

    标签: cuda cublas


    【解决方案1】:

    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 值,我们必须从偏移量 0 开始搜索 dV,增量为 3,最多可找到 n=4 个元素。

    考虑到所有这些,正确的调用是:

    cublasIsamax(cublasHandle, 4, dV, 3, hResult + 0));
    cublasIsamin(cublasHandle, 4, dV, 3, hResult + 1));
    

    而预期的结果是:

    3 2
    

    【讨论】:

      猜你喜欢
      • 2016-10-09
      • 2021-10-26
      • 2015-10-13
      • 2014-12-06
      • 2017-08-21
      • 2021-10-27
      • 2021-02-10
      • 2012-05-11
      • 1970-01-01
      相关资源
      最近更新 更多