-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encapsulate Storage_base class #528
base: dev-master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,15 +26,7 @@ | |
///@cond | ||
class Storage_base : public intrusive_ptr_base<Storage_base> { | ||
public: | ||
void *Mem; | ||
// std::vector<unsigned int> shape; | ||
|
||
unsigned long long len; // default 0 | ||
unsigned long long cap; // default 0 | ||
unsigned int dtype; // default 0, Void | ||
int device; // default -1, on cpu | ||
|
||
Storage_base() : cap(0), len(0), Mem(NULL), dtype(0), device(-1){}; | ||
Storage_base() = default; | ||
// Storage_base(const std::initializer_list<unsigned int> &init_shape); | ||
// Storage_base(const std::vector<unsigned int> &init_shape); | ||
Storage_base(const unsigned long long &len_in, const int &device, const bool &init_zero = true); | ||
|
@@ -46,9 +38,13 @@ | |
// void Init(const std::initializer_list<unsigned int> &init_shape); | ||
std::string dtype_str() const; | ||
std::string device_str() const; | ||
const unsigned long long &capacity() const { return this->cap; } | ||
const unsigned long long &size() const { return this->len; } | ||
~Storage_base(); | ||
virtual const unsigned long long capacity() const { | ||
cytnx_error_msg(true, "Not implemented.%s", ""); | ||
} | ||
virtual const unsigned long long size() const { | ||
cytnx_error_msg(true, "Not implemented.%s", ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to return a const reference for the ScalarType. |
||
} | ||
virtual ~Storage_base(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Base classes must have a virtual deconstructor. Without |
||
|
||
template <class T> | ||
T &at(const cytnx_uint64 &idx) const; | ||
|
@@ -59,7 +55,11 @@ | |
template <class T> | ||
T *data() const; | ||
|
||
void *data() const { return this->Mem; } | ||
// `Storage_base` can be instanitiated directly. It's deconstructor calls `data()`, so we cannot | ||
// throw a runtime error here. | ||
virtual void *data() const { return nullptr; } | ||
virtual int dtype() const { return Type.Void; } | ||
virtual int device() const { return Device.cpu; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New getters due to the variables of |
||
|
||
void _cpy_bool(void *ptr, const std::vector<cytnx_bool> &vin); | ||
|
||
|
@@ -87,37 +87,37 @@ | |
template <class T> | ||
void _Init_byptr_safe(T *rawptr, const unsigned long long &len_in) { | ||
// check: | ||
if (this->dtype == Type.Float) { | ||
if (this->dtype() == Type.Float) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_float), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Double) { | ||
} else if (this->dtype() == Type.Double) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_double), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Uint64) { | ||
} else if (this->dtype() == Type.Uint64) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_uint64), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Uint32) { | ||
} else if (this->dtype() == Type.Uint32) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_uint32), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Int64) { | ||
} else if (this->dtype() == Type.Int64) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_int64), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Int32) { | ||
} else if (this->dtype() == Type.Int32) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_int32), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.ComplexDouble) { | ||
} else if (this->dtype() == Type.ComplexDouble) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_complex128), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.ComplexFloat) { | ||
} else if (this->dtype() == Type.ComplexFloat) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_complex64), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Int16) { | ||
} else if (this->dtype() == Type.Int16) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_int16), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Uint16) { | ||
} else if (this->dtype() == Type.Uint16) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_uint16), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else if (this->dtype == Type.Bool) { | ||
} else if (this->dtype() == Type.Bool) { | ||
cytnx_error_msg(typeid(T) != typeid(cytnx_bool), "%s", | ||
"[ERROR _Init_byptr_safe type not match]"); | ||
} else { | ||
|
@@ -146,6 +146,18 @@ | |
const std::vector<cytnx_uint64> &shape, | ||
const std::vector<std::vector<cytnx_uint64>> &locators, | ||
const cytnx_uint64 &Nunit, const bool &is_scalar); | ||
|
||
/** | ||
* @brief Drop the ownership of the underlying contiguous memory. | ||
* | ||
* The caller MUST take the ownership before calling this method. Any operation following this | ||
* method causes undefined behavior. | ||
* | ||
* @return The pointer referencing the underlying storage. | ||
* @deprecated This method may be removed without any notification. | ||
*/ | ||
virtual void *release() noexcept { return nullptr; } | ||
|
||
// these is the one that do the work, and customize with Storage_base | ||
// virtual void Init(const std::vector<unsigned int> &init_shape); | ||
virtual void Init(const unsigned long long &len_in, const int &device = -1, | ||
|
@@ -219,17 +231,15 @@ | |
virtual void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val); | ||
virtual void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val); | ||
virtual void set_item(const cytnx_uint64 &idx, const cytnx_bool &val); | ||
|
||
// virtual bool approx_eq(const boost::intrusive_ptr<Storage_base> &rhs, | ||
// const cytnx_double tol = 1e-8); | ||
}; | ||
///@endcond | ||
|
||
///@cond | ||
template <typename DType> | ||
class StorageImplementation : public Storage_base { | ||
public: | ||
StorageImplementation() { this->dtype = Type.cy_typeid(DType()); }; | ||
StorageImplementation() | ||
: capacity_(0), size_(0), start_(nullptr), dtype_(Type.cy_typeid(DType())), device_(-1){}; | ||
void Init(const unsigned long long &len_in, const int &device = -1, | ||
const bool &init_zero = true); | ||
void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1, | ||
|
@@ -251,6 +261,29 @@ | |
boost::intrusive_ptr<Storage_base> real(); | ||
boost::intrusive_ptr<Storage_base> imag(); | ||
|
||
const unsigned long long capacity() const override { return capacity_; } | ||
const unsigned long long size() const override { return size_; } | ||
void *data() const override { return start_; } | ||
int dtype() const override { return dtype_; } | ||
int device() const override { return device_; } | ||
|
||
/** | ||
* @brief Drop the ownership of the underlying contiguous memory. | ||
* | ||
* The caller MUST take the ownership before calling this method. Any operation following this | ||
* method causes undefined behavior. | ||
* | ||
* @return The pointer referencing the underlying storage. | ||
* @deprecated This method may be removed without any notification. | ||
*/ | ||
void *release() noexcept override { | ||
void *original_start = start_; | ||
start_ = nullptr; | ||
size_ = 0; | ||
capacity_ = 0; | ||
return original_start; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should only be used in the functions converting |
||
}; | ||
|
||
// generators: | ||
void fill(const cytnx_complex128 &val); | ||
void fill(const cytnx_complex64 &val); | ||
|
@@ -304,6 +337,12 @@ | |
template <typename OtherDType> | ||
void SetItem(cytnx_uint64 index, const OtherDType &value); | ||
void SetItem(cytnx_uint64 index, const Scalar &value); | ||
|
||
void *start_; | ||
unsigned long long size_; | ||
unsigned long long capacity_; | ||
unsigned int dtype_; | ||
int device_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variables moved from |
||
}; | ||
///@endcond | ||
|
||
|
@@ -583,7 +622,7 @@ | |
@brief the dtype-id of current Storage, see cytnx::Type for more details. | ||
@return [cytnx_uint64] the dtype-id. | ||
*/ | ||
const unsigned int &dtype() const { return this->_impl->dtype; } | ||
unsigned int dtype() const { return this->_impl->dtype(); } | ||
|
||
/** | ||
@brief the dtype (std::string) of current Storage, see cytnx::Type for more details. | ||
|
@@ -597,7 +636,7 @@ | |
@brief the device-id of current Storage, see cytnx::Device for more details. | ||
@return [cytnx_int64] the device-id. | ||
*/ | ||
const int &device() const { return this->_impl->device; } | ||
int device() const { return this->_impl->device(); } | ||
|
||
/** | ||
@brief the device (std::string) of current Storage, see cytnx::Device for more details. | ||
|
@@ -699,7 +738,7 @@ | |
@return [cytnx_uint64] | ||
|
||
*/ | ||
const unsigned long long &size() const { return this->_impl->len; } | ||
unsigned long long size() const { return this->_impl->size(); } | ||
|
||
/** | ||
@brief the capacity in the Storage. | ||
|
@@ -708,7 +747,18 @@ | |
@return [cytnx_uint64] | ||
|
||
*/ | ||
const unsigned long long &capacity() const { return this->_impl->cap; } | ||
unsigned long long capacity() const { return this->_impl->capacity(); } | ||
|
||
/** | ||
* @brief Drop the ownership of the underlying contiguous memory. | ||
* | ||
* The caller MUST take the ownership before calling this method. Any operation following this | ||
* method causes undefined behavior. | ||
* | ||
* @return The pointer referencing the underlying storage. | ||
* @deprecated This method may be removed without any notification. | ||
*/ | ||
void *release() noexcept { return this->_impl->release(); } | ||
|
||
/** | ||
@brief print the info of the Storage, including the device, dtype and size. | ||
|
@@ -818,64 +868,64 @@ | |
// check: | ||
cytnx_error_msg(1, "[FATAL] ERROR unsupport type%s", "\n"); | ||
// this->_impl->Init(vin.size(),device); | ||
// memcpy(this->_impl->Mem,&vin[0],sizeof(T)*vin.size()); | ||
// memcpy(this->_impl->data(),&vin[0],sizeof(T)*vin.size()); | ||
} | ||
|
||
void _from_vector(const std::vector<cytnx_complex128> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.ComplexDouble](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_complex128) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_complex128) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_complex64> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.ComplexFloat](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_complex64) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_complex64) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_double> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Double](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_double) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_double) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_float> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Float](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_float) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_float) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_uint64> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Uint64](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint64) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_uint64) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_int64> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Int64](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int64) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_int64) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_uint32> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Uint32](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint32) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_uint32) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_int32> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Int32](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int32) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_int32) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_uint16> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Uint16](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint16) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_uint16) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_int16> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Int16](); | ||
this->_impl->Init(vin.size(), device); | ||
memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int16) * vin.size()); | ||
memcpy(this->_impl->data(), &vin[0], sizeof(cytnx_int16) * vin.size()); | ||
} | ||
void _from_vector(const std::vector<cytnx_bool> &vin, const int device = -1) { | ||
this->_impl = __SII.USIInit[Type.Bool](); | ||
this->_impl->Init(vin.size(), device); | ||
this->_impl->_cpy_bool(this->_impl->Mem, vin); | ||
// memcpy(this->_impl->Mem,vin.data(),sizeof(cytnx_bool)*vin.size()); | ||
this->_impl->_cpy_bool(this->_impl->data(), vin); | ||
// memcpy(this->_impl->data(),vin.data(),sizeof(cytnx_bool)*vin.size()); | ||
} | ||
/// @endcond | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#include <vector> | ||
#include <map> | ||
#include <random> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
@@ -34,9 +35,9 @@ void storage_binding(py::module &m) { | |
} | ||
|
||
// calculate stride: | ||
std::vector<ssize_t> stride(1, Type.typeSize(tmpIN.dtype())); | ||
size_t type_size = Type.typeSize(tmpIN.dtype()); | ||
std::vector<ssize_t> stride(1, type_size); | ||
std::vector<ssize_t> shape(1, tmpIN.size()); | ||
// ssize_t accu = tmpIN.size(); | ||
|
||
py::buffer_info npbuf; | ||
std::string chr_dtype; | ||
|
@@ -63,19 +64,11 @@ void storage_binding(py::module &m) { | |
"\n"); | ||
} | ||
|
||
npbuf = py::buffer_info(tmpIN._impl->Mem, // ptr | ||
Type.typeSize(tmpIN.dtype()), // size of elem | ||
// Call `.release()` to avoid the memory passed to numpy being freed. | ||
npbuf = py::buffer_info(tmpIN.release(), type_size, | ||
chr_dtype, // pss format | ||
1, // rank | ||
shape, // shape | ||
stride // stride | ||
); | ||
py::array out(npbuf); | ||
// delegate numpy array with it's ptr, and swap a auxiliary ptr for intrusive_ptr to | ||
// free. | ||
void *pswap = malloc(sizeof(bool)); | ||
tmpIN._impl->Mem = pswap; | ||
return out; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variables should be private. |
||
/* rank= */ 1, shape, stride); | ||
return py::array(npbuf); | ||
}) | ||
|
||
// construction | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mark this function as deprecated. It may be deleted or renamed to
data()
likestd
convention.