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

Class DeviceSpMatDoubleCSR

XM/include/Utils/memory.h:1212–1285  ·  view source on GitHub ↗

sparse vector wrapper on device: double type + CSR format

Source from the content-addressed store, hash-verified

1210
1211// sparse vector wrapper on device: double type + CSR format
1212class DeviceSpMatDoubleCSR {
1213 public:
1214 int gpu_id;
1215 int64_t row_size;
1216 int64_t col_size;
1217 int64_t nnz;
1218 int* row_ptrs;
1219 int* col_ids;
1220 double* vals;
1221 cusparseSpMatDescr_t cusparse_descr;
1222
1223 DeviceSpMatDoubleCSR(): gpu_id(0), row_size(0), col_size(0), nnz(0),
1224 row_ptrs(nullptr), col_ids(nullptr), vals(nullptr), cusparse_descr(NULL) {}
1225 DeviceSpMatDoubleCSR(const int gpu_id, const int row_size, const int col_size, const int nnz):
1226 gpu_id(gpu_id), row_size(row_size), col_size(col_size), nnz(nnz),
1227 row_ptrs(nullptr), col_ids(nullptr), vals(nullptr), cusparse_descr(NULL) {
1228 this->allocate(gpu_id, row_size, col_size, nnz);
1229 }
1230
1231 inline void allocate(const int gpu_id, const int row_size, const int col_size, const int nnz) {
1232 if (this->vals == nullptr) {
1233 this->gpu_id = gpu_id;
1234 this->row_size = row_size;
1235 this->col_size = col_size;
1236 this->nnz = nnz;
1237 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1238 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1239 CHECK_CUDA( cudaMalloc((void**) &this->row_ptrs, sizeof(int) * (this->row_size + 1)) );
1240 if (nnz > 0) {
1241 CHECK_CUDA( cudaMalloc((void**) &this->col_ids, sizeof(int) * this->nnz) );
1242 CHECK_CUDA( cudaMalloc((void**) &this->vals, sizeof(double) * this->nnz) );
1243 } else {
1244 this->col_ids = nullptr;
1245 this->vals = nullptr;
1246 }
1247 CHECK_CUSPARSE( cusparseCreateCsr(
1248 &this->cusparse_descr, this->row_size, this->col_size, this->nnz,
1249 this->row_ptrs, this->col_ids, this->vals,
1250 CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I, CUSPARSE_INDEX_BASE_ZERO, CUDA_R_64F
1251 ) );
1252 }
1253 return;
1254 }
1255
1256 inline double get_norm(const DeviceBlasHandle& cublas_H) {
1257 double norm;
1258 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1259 CHECK_CUBLAS( cublasDnrm2_v2(
1260 cublas_H.cublas_handle, this->nnz, this->vals, 1, &norm
1261 ) );
1262 return norm;
1263 }
1264
1265 ~DeviceSpMatDoubleCSR() {
1266 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1267 if (this->row_ptrs != nullptr) {
1268 CHECK_CUDA( cudaFree(this->row_ptrs) );
1269 this->row_ptrs = nullptr;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected