MCPcopy Create free account
hub / github.com/ComputationalRobotics/XM-code / DeviceSpVecDouble

Class DeviceSpVecDouble

XM/include/Utils/memory.h:1080–1139  ·  view source on GitHub ↗

sparse vector wrapper on device: double type

Source from the content-addressed store, hash-verified

1078
1079// sparse vector wrapper on device: double type
1080class DeviceSpVecDouble {
1081 public:
1082 int gpu_id;
1083 int size;
1084 int nnz;
1085 int* indices;
1086 double* vals;
1087 cusparseSpVecDescr_t cusparse_descr;
1088
1089 DeviceSpVecDouble(): gpu_id(0), size(0), nnz(0),
1090 indices(nullptr), vals(nullptr), cusparse_descr(NULL) {}
1091 DeviceSpVecDouble(const int gpu_id, const int size, const int nnz):
1092 gpu_id(gpu_id), size(size), nnz(nnz),
1093 vals(nullptr), indices(nullptr), cusparse_descr(NULL) {
1094 this->allocate(gpu_id, size, nnz);
1095 }
1096
1097 inline void allocate(const int gpu_id, const int size, const int nnz) {
1098 if (this->vals == nullptr) {
1099 this->gpu_id = gpu_id;
1100 this->size = size;
1101 this->nnz = nnz;
1102 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1103 CHECK_CUDA( cudaMalloc((void**) &this->indices, sizeof(int) * this->nnz) );
1104 CHECK_CUDA( cudaMalloc((void**) &this->vals, sizeof(double) * this->nnz) );
1105 CHECK_CUSPARSE( cusparseCreateSpVec(
1106 &this->cusparse_descr, this->size, this->nnz,
1107 this->indices, this->vals,
1108 CUSPARSE_INDEX_32I, CUSPARSE_INDEX_BASE_ZERO, CUDA_R_64F
1109 ) );
1110 }
1111 return;
1112 }
1113
1114 inline double get_norm(const DeviceBlasHandle& cublas_H) {
1115 double norm;
1116 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1117 CHECK_CUBLAS( cublasDnrm2_v2(
1118 cublas_H.cublas_handle, this->nnz, this->vals, 1, &norm
1119 ) );
1120 return norm;
1121 }
1122
1123 ~DeviceSpVecDouble() {
1124 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1125 if (this->indices != nullptr) {
1126 CHECK_CUDA( cudaFree(this->indices) );
1127 this->indices = nullptr;
1128 }
1129 if (this->vals != nullptr) {
1130 CHECK_CUDA( cudaFree(this->vals) );
1131 this->vals = nullptr;
1132 }
1133 if (this->cusparse_descr != NULL) {
1134 CHECK_CUSPARSE( cusparseDestroySpVec(this->cusparse_descr) );
1135 this->cusparse_descr = NULL;
1136 }
1137 // std::cout << "DeviceSpVecDouble destructor called!" << std::endl;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected