Could I use double precision with `cusparse` or `cublas` function in mexcuda.cu file?

2 views (last 30 days)
Hi,
I am trying to compile my .cu file with cudamex. In this file, I would like to write some functions in cusparse or cublas file. Because I don't know much of setting the number of block or threads. But I find that all the cublas or cusparse functions are in float format, which is equal to single class in Matlab, imo. Could I have some way to input double precision numbers? Besides, must I include the cublas and cusparse library? Are there any version of them built in Matlab? The following is my example of copying float vector. It runs well. If it has some reundancy or irregular part, please let me know. Thank you.
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cuda_runtime.h>
#include <cusparse.h>
#include <cublas_v2.h>
#include <helper_functions.h>
#include <helper_cuda.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray const *prhs[])
{
// =========================================================================
// input : x
// =========================================================================
mxGPUArray* x = mxGPUCopyFromMxArray(prhs[0]);
int N = (int)(mxGPUGetNumberOfElements(x));
// =========================================================================
// create other variable: y
// =========================================================================
auto dn = mxGPUGetNumberOfDimensions(x);
auto d = mxGPUGetDimensions(x);
mxGPUArray* y = mxGPUCreateGPUAraray(dn,d,mxSINGLE_CLASS,mxREAL,MX_GPU_INITIALIZE_VALUES);
// =========================================================================
// set device pointers: d_x, d_y
// =========================================================================
float *d_x = (float*)mxGPUGetData(x);
float *d_y = (float*)mxGPUGetData(y);
// =========================================================================
// gpu computing: initial
// =========================================================================
/* Get handle to the CUBLAS context */
cublasHandle_t cublasHandle = 0;
cublasStatus_t cublasStatus;
cublasStatus = cublasCreate(&cublasHandle);
checkCudaErrors(cublasStatus);
// =========================================================================
// step1
// =========================================================================
cublasScopy(cublasHandle, N, d_x,1,d_y,1);
// =========================================================================
// output
// =========================================================================
plhs[0] = mxGPUCreateMxArrayOnCPU(y);
// =========================================================================
// clear
// =========================================================================
mxGPUDestroyGPUArray(x);
mxGPUDestroyGPUArray(y);
}

Answers (1)

Sanjana
Sanjana on 28 Sep 2024
Hi,
I understand that you are using 'cuSPARSE' and 'cuBLAS', and since they utilize int32 and single precision to save memory, you are seeking a method to input double precision values.
MATLAB does not have built-in libraries specifically for 'cuSPARSE' and 'cuBLAS'. However, it provides GPU-accelerated functionalities to create sparse gpuArray objects. The sparse function in MATLAB can be used with gpuArray inputs to work with sparse matrices, which use double precision.You can refer to the following example for more details:
Additionally, you can refer to the following list of functions that support sparse GPU arrays:
Regards,
Sanjana

Tags

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!