23 #ifndef CPP_INCLUDE_GPU_SVD_SOLVER_H_ 24 #define CPP_INCLUDE_GPU_SVD_SOLVER_H_ 27 #include<cuda_runtime_api.h> 28 #include<cuda_runtime.h> 29 #include<device_launch_parameters.h> 30 #include<cusolverDn.h> 34 #include "Eigen/Dense" 56 T *d_A;
gpuErrchk(cudaMalloc(&d_A, M * N *
sizeof(T)));
57 gpuErrchk(cudaMemcpy(d_A, h_A, M * N *
sizeof(T), cudaMemcpyHostToDevice));
67 int *devInfo;
gpuErrchk(cudaMalloc(&devInfo,
sizeof(
int)));
68 T *d_U;
gpuErrchk(cudaMalloc(&d_U, M * M *
sizeof(T)));
69 T *d_V;
gpuErrchk(cudaMalloc(&d_V, N * N *
sizeof(T)));
70 T *d_S;
gpuErrchk(cudaMalloc(&d_S, N *
sizeof(T)));
71 cusolverStatus_t stat;
74 cusolverDnHandle_t solver_handle;
75 cusolverDnCreate(&solver_handle);
76 stat = cusolverDnSgesvd_bufferSize(solver_handle, M, N, &work_size);
77 if (stat != CUSOLVER_STATUS_SUCCESS) {
78 std::cout <<
"Initialization of cuSolver failed." << std::endl;
79 cudaFree(d_S); cudaFree(d_U); cudaFree(d_V);
80 cusolverDnDestroy(solver_handle);
83 T *work;
gpuErrchk(cudaMalloc(&work, work_size *
sizeof(T)));
86 stat =
GpuSvd(solver_handle, M, N,
88 work, work_size, devInfo);
92 sizeof(
int), cudaMemcpyDeviceToHost));
93 if (stat != CUSOLVER_STATUS_SUCCESS || devInfo_h != 0) {
94 std::cerr <<
"GPU SVD Solver Internal Failure" << std::endl;
95 cudaFree(d_S); cudaFree(d_U); cudaFree(d_V); cudaFree(work);
96 cusolverDnDestroy(solver_handle);
99 cudaDeviceSynchronize();
102 gpuErrchk(cudaMemcpy(&s_(0, 0), d_S, N*
sizeof(T),
103 cudaMemcpyDeviceToHost));
104 gpuErrchk(cudaMemcpy(&u_(0, 0), d_U, M*M*
sizeof(T),
105 cudaMemcpyDeviceToHost));
106 gpuErrchk(cudaMemcpy(&v_(0, 0), d_V, N*N*
sizeof(T),
107 cudaMemcpyDeviceToHost));
109 cudaFree(d_S); cudaFree(d_U); cudaFree(d_V); cudaFree(work);
110 cusolverDnDestroy(solver_handle);
123 #endif // CPP_INCLUDE_GPU_SVD_SOLVER_H_ Matrix< T > MatrixU() const
Definition: gpu_svd_solver.h:113
void gpuErrchk(cudaError_t)
GpuSvdSolver()
Definition: gpu_svd_solver.h:49
Definition: cpu_operations.h:36
Vector< T > SingularValues() const
Definition: gpu_svd_solver.h:117
Definition: gpu_svd_solver.h:42
Matrix< T > MatrixV() const
Definition: gpu_svd_solver.h:115
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
Definition: vector.h:31
cusolverStatus_t GpuSvd(cusolverDnHandle_t solver_handle, int M, int N, float *d_A, float *d_S, float *d_U, float *d_V, float *work, int work_size, int *devInfo)
void Compute(const Matrix< T > &A)
Definition: gpu_svd_solver.h:51
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
Definition: matrix.h:31