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

Class DeviceDnVecFloat

XM/include/Utils/memory.h:964–1013  ·  view source on GitHub ↗

dense vector wrapper on device: float type

Source from the content-addressed store, hash-verified

962
963// dense vector wrapper on device: float type
964class DeviceDnVecFloat {
965 public:
966 int gpu_id;
967 int size;
968 float* vals;
969 cusparseDnVecDescr_t cusparse_descr;
970
971 DeviceDnVecFloat(): gpu_id(0), size(0), vals(nullptr), cusparse_descr(NULL) {}
972 DeviceDnVecFloat(const int gpu_id, const int size, bool as_byte = false):
973 gpu_id(gpu_id), size(size), vals(nullptr), cusparse_descr(NULL) {
974 this->allocate(this->gpu_id, this->size, as_byte);
975 }
976
977 inline void allocate(const int gpu_id, const int size, bool as_byte = false) {
978 if (this->vals == nullptr) {
979 this->gpu_id = gpu_id;
980 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
981 // as_byte is used to allocate buffer size, which is usually given in terms of bytes
982 if (as_byte) {
983 this->size = (size + sizeof(float) - 1) / sizeof(float);
984 } else {
985 this->size = size;
986 }
987 CHECK_CUDA( cudaMalloc((void**) &this->vals, sizeof(float) * this->size) );
988 CHECK_CUSPARSE( cusparseCreateDnVec(&this->cusparse_descr, this->size, this->vals, CUDA_R_32F) );
989 }
990 return;
991 }
992 inline float get_norm(const DeviceBlasHandle& cublas_H) {
993 float norm;
994 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
995 CHECK_CUBLAS( cublasSnrm2_v2(
996 cublas_H.cublas_handle, this->size, this->vals, 1, &norm
997 ) );
998 return norm;
999 }
1000
1001 ~DeviceDnVecFloat() {
1002 CHECK_CUDA( cudaSetDevice(this->gpu_id) );
1003 if (this->vals != nullptr) {
1004 CHECK_CUDA( cudaFree(this->vals) );
1005 this->vals = nullptr;
1006 }
1007 if (this->cusparse_descr != NULL) {
1008 CHECK_CUSPARSE( cusparseDestroyDnVec(this->cusparse_descr) );
1009 this->cusparse_descr = NULL;
1010 }
1011 // std::cout << "DeviceDnVecFloat destructor called!" << std::endl;
1012 }
1013};
1014
1015// dense vector wrapper on device: int type
1016class DeviceDnVecInt {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected