dense vector wrapper on device: double type
| 909 | |
| 910 | // dense vector wrapper on device: double type |
| 911 | class DeviceDnVecDouble { |
| 912 | public: |
| 913 | int gpu_id; |
| 914 | int size; |
| 915 | double* vals; |
| 916 | cusparseDnVecDescr_t cusparse_descr; |
| 917 | |
| 918 | DeviceDnVecDouble(): gpu_id(0), size(0), vals(nullptr), cusparse_descr(NULL) {} |
| 919 | DeviceDnVecDouble(const int gpu_id, const int size, bool as_byte = false): |
| 920 | gpu_id(gpu_id), size(size), vals(nullptr), cusparse_descr(NULL) { |
| 921 | this->allocate(this->gpu_id, this->size, as_byte); |
| 922 | } |
| 923 | |
| 924 | inline void allocate(const int gpu_id, const int size, bool as_byte = false) { |
| 925 | if (this->vals == nullptr) { |
| 926 | this->gpu_id = gpu_id; |
| 927 | this->size = size; |
| 928 | CHECK_CUDA( cudaSetDevice(this->gpu_id) ); |
| 929 | // as_byte is used to allocate buffer size, which is usually given in terms of bytes |
| 930 | if (as_byte) { |
| 931 | this->size = (size + sizeof(double) - 1) / sizeof(double); |
| 932 | } else { |
| 933 | this->size = size; |
| 934 | } |
| 935 | CHECK_CUDA( cudaMalloc((void**) &this->vals, sizeof(double) * this->size) ); |
| 936 | CHECK_CUSPARSE( cusparseCreateDnVec(&this->cusparse_descr, this->size, this->vals, CUDA_R_64F) ); |
| 937 | } |
| 938 | return; |
| 939 | } |
| 940 | inline double get_norm(const DeviceBlasHandle& cublas_H) { |
| 941 | double norm; |
| 942 | CHECK_CUDA( cudaSetDevice(this->gpu_id) ); |
| 943 | CHECK_CUBLAS( cublasDnrm2_v2( |
| 944 | cublas_H.cublas_handle, this->size, this->vals, 1, &norm |
| 945 | ) ); |
| 946 | return norm; |
| 947 | } |
| 948 | |
| 949 | ~DeviceDnVecDouble() { |
| 950 | CHECK_CUDA( cudaSetDevice(this->gpu_id) ); |
| 951 | if (this->vals != nullptr) { |
| 952 | CHECK_CUDA( cudaFree(this->vals) ); |
| 953 | this->vals = nullptr; |
| 954 | } |
| 955 | if (this->cusparse_descr != NULL) { |
| 956 | CHECK_CUSPARSE( cusparseDestroyDnVec(this->cusparse_descr) ); |
| 957 | this->cusparse_descr = NULL; |
| 958 | } |
| 959 | // std::cout << "DeviceDnVecDouble destructor called!" << std::endl; |
| 960 | } |
| 961 | }; |
| 962 | |
| 963 | // dense vector wrapper on device: float type |
| 964 | class DeviceDnVecFloat { |
nothing calls this directly
no outgoing calls
no test coverage detected