现在正在看CUBLAS_Library这个手册,看到cublasIsamax()的时候,自己写了一段小程序,为什么结果不稳定,一次运行一个结果,是不是我理解错了cublasStatus_t cublasIsamax(cublasHandle_t handle,int n,const floatx,int incx,intresult)
中的result这个变量?下面这个例子我将result定义为int result=(int)malloc(sizeof(int));我不知道该将result定义多大才好,我第一次是这样定义result的:int result=(int)malloc(length*sizeof(int));.
我的问题是result输出的到底是什么值?它该定义为多大?我自己的理解是result输出一个向量中最大元素那个值的下标,如果两个值一样大,就输出那个最小的下标。
#include “cuda_runtime.h”
#include “device_launch_parameters.h”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cublas_v2.h>
const int length=10;
int main(void)
{
cudaError_t cudaStat;
cublasStatus_t stat;
cublasHandle_t handle;
int i,j;
int result=(int)malloc(sizeof(int));
float* devPtrA;
float* a=0;
a=(float*)malloc(length*sizeof(*a));
if(!a)
{
printf(“host memory allocation failed”);
return EXIT_FAILURE;
}
for(i=0;i<length;i++)
{
a[i]=(float)(i5+2);
}
cudaStat=cudaMalloc((void**)&devPtrA,lengthsizeof(*a));
if(cudaStat!=cudaSuccess)
{
printf(“device memory allocation failed”);
return EXIT_FAILURE;
}
stat=cublasCreate(&handle);
if(stat!=CUBLAS_STATUS_SUCCESS)
{
printf(“CUBLAS initialization failed\n”);
return EXIT_FAILURE;
}
stat=cublasIsamax(handle,length,devPtrA,1,result);
if(stat!=CUBLAS_STATUS_SUCCESS)
{
printf(“data download failed”);
cudaFree(devPtrA);
cublasDestroy(handle);
return EXIT_FAILURE;
}
cudaFree(devPtrA);
cublasDestroy(handle);
printf("%d ",result[0]);
printf(“\n”);
free(a);
free(result);
return EXIT_SUCCESS;
}