Could I use double precision with `cusparse` or `cublas` function in mexcuda.cu file?
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);
}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);
} 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);
} gpu, cublas MATLAB Answers — New Questions