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

Class DeviceSpMatDoubleCSC

XM/include/Utils/memory.h:1142–1209  ·  view source on GitHub ↗

sparse vector wrapper on device: double type + CSC format

Source from the content-addressed store, hash-verified

1140
1141// sparse vector wrapper on device: double type + CSC format
1142class DeviceSpMatDoubleCSC {
1143 public:
1144 int gpu_id;
1145 int row_size;
1146 int col_size;
1147 int nnz;
1148 int* col_ptrs;
1149 int* row_ids;
1150 double* vals;
1151 cusparseSpMatDescr_t cusparse_descr;
1152
1153 DeviceSpMatDoubleCSC(): gpu_id(0), row_size(0), col_size(0), nnz(0),
1154 col_ptrs(nullptr), row_ids(nullptr), vals(nullptr), cusparse_descr(NULL) {}
1155 DeviceSpMatDoubleCSC(const int gpu_id, const int row_size, const int col_size, const int nnz):
1156 gpu_id(gpu_id), row_size(row_size), col_size(col_size), nnz(nnz),
1157 col_ptrs(nullptr), row_ids(nullptr), vals(nullptr), cusparse_descr(NULL) {
1158 this->allocate(gpu_id, row_size, col_size, nnz);
1159 }
1160
1161 inline void allocate(const int gpu_id, const int row_size, const int col_size, const int nnz) {
1162 if (this->vals == nullptr) {
1163 this->gpu_id = gpu_id;
1164 this->row_size = row_size;
1165 this->col_size = col_size;
1166 this->nnz = nnz;
1167 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1168 CHECK_CUDA( cudaMalloc((void**) &this->col_ptrs, sizeof(int) * (this->col_size + 1)) );
1169 CHECK_CUDA( cudaMalloc((void**) &this->row_ids, sizeof(int) * this->nnz) );
1170 CHECK_CUDA( cudaMalloc((void**) &this->vals, sizeof(double) * this->nnz) );
1171 CHECK_CUSPARSE(cusparseCreateCsc(
1172 &this->cusparse_descr, this->row_size, this->col_size, this->nnz,
1173 this->col_ptrs, this->row_ids, this->vals,
1174 CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I, CUSPARSE_INDEX_BASE_ZERO, CUDA_R_64F
1175 ) );
1176 }
1177 return;
1178 }
1179
1180 inline double get_norm(const DeviceBlasHandle& cublas_H) {
1181 double norm;
1182 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1183 CHECK_CUBLAS( cublasDnrm2_v2(
1184 cublas_H.cublas_handle, this->nnz, this->vals, 1, &norm
1185 ) );
1186 return norm;
1187 }
1188
1189 ~DeviceSpMatDoubleCSC() {
1190 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1191 if (this->col_ptrs != nullptr) {
1192 CHECK_CUDA( cudaFree(this->col_ptrs) );
1193 this->col_ptrs = nullptr;
1194 }
1195 if (this->row_ids != nullptr) {
1196 CHECK_CUDA( cudaFree(this->row_ids) );
1197 this->row_ids = nullptr;
1198 }
1199 if (this->vals != nullptr) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected