Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Cuda/Morpheus_Workspace.hpp
1
24#ifndef MORPHEUS_DENSEVECTOR_CUDA_WORKSPACE_IMPL_HPP
25#define MORPHEUS_DENSEVECTOR_CUDA_WORKSPACE_IMPL_HPP
26
27#include <Morpheus_Macros.hpp>
28#if defined(MORPHEUS_ENABLE_CUDA)
29
30#include <impl/Morpheus_CudaUtils.hpp>
31
32#ifdef MORPHEUS_ENABLE_TPL_CUBLAS
33#include <cublas_v2.h>
34#endif // MORPHEUS_ENABLE_TPL_CUBLAS
35
36namespace Morpheus {
37namespace Impl {
38
40 public:
41 CudaWorkspace() : _workspace(nullptr), _nbytes(0) {}
42
43 ~CudaWorkspace() { _free(); }
44
45 template <typename ValueType>
46 void allocate(size_t N) {
47 size_t bytes = N * sizeof(ValueType);
48 if ((bytes > _nbytes) && (_nbytes > 0)) {
49 _free();
50 }
51 _malloc(bytes);
52 }
53
54 template <typename ValueType>
55 ValueType* data() {
56 return reinterpret_cast<ValueType*>(_workspace);
57 }
58
59 private:
60 void* _workspace;
61 size_t _nbytes;
62
63 void _malloc(size_t bytes) {
64 if (_workspace == nullptr) {
65 _nbytes = bytes;
66 if (cudaSuccess != cudaMalloc(&_workspace, bytes)) {
67 std::cout << "Malloc Failed on Device" << std::endl;
68 exit(EXIT_FAILURE);
69 }
70 }
71 }
72
73 void _free() {
74 if (_workspace != nullptr) {
75 if (cudaSuccess == cudaFree(_workspace)) {
76 _workspace = nullptr;
77 _nbytes = 0;
78 }
79 }
80 }
81};
82
83#ifdef MORPHEUS_ENABLE_TPL_CUBLAS
84
86 public:
87 CublasWorkspace() : _handle(nullptr) {}
88
90 if (_handle != nullptr) {
91 if (cublasDestroy(_handle) != CUBLAS_STATUS_SUCCESS) {
92 _handle = nullptr;
93 }
94 }
95 }
96
97 void init() {
98 if (_handle == nullptr) {
99 if (cublasCreate(&_handle) != CUBLAS_STATUS_SUCCESS) {
100 printf("CUBLAS initialization failed\n");
101 exit(EXIT_FAILURE);
102 }
103 cublasSetPointerMode(_handle, CUBLAS_POINTER_MODE_DEVICE);
104 }
105 }
106
107 cublasHandle_t handle() { return _handle; }
108
109 private:
110 cublasHandle_t _handle;
111};
112
113extern CublasWorkspace cublasdotspace;
114
115#endif // MORPHEUS_ENABLE_TPL_CUBLAS
116
117extern CudaWorkspace cudotspace;
118
119} // namespace Impl
120} // namespace Morpheus
121
122#endif // MORPHEUS_ENABLE_CUDA
123#endif // MORPHEUS_DENSEVECTOR_CUDA_WORKSPACE_IMPL_HPP
Definition: Cuda/Morpheus_Workspace.hpp:85
Definition: Cuda/Morpheus_Workspace.hpp:39
Generic Morpheus interfaces.
Definition: dummy.cpp:24
A wrapper that checks if the provided type is a scalar type.
Definition: Morpheus_TypeTraits.hpp:85