Cytnx v0.7.4
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
cytnx::Storage Class Reference

an memeory storage with multi-type/multi-device support More...

#include <Storage.hpp>

Public Member Functions

void Init (const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1)
 initialize a Storage
 
 Storage (const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1)
 
 Storage ()
 
void Save (const std::string &fname) const
 Save current Storage to file.
 
void Save (const char *fname) const
 
void Tofile (const std::string &fname) const
 
void Tofile (const char *fname) const
 
void Tofile (std::fstream &f) const
 
Storage astype (const unsigned int &new_type) const
 cast the type of current Storage
 
const unsigned intdtype () const
 the dtype-id of current Storage
 
const std::string dtype_str () const
 the dtype (std::string) of current Storage
 
const int & device () const
 the device-id of current Storage
 
const std::string device_str () const
 the device (std::string) of current Storage
 
template<class T >
void append (const T &val)
 append a value
 
void resize (const cytnx_uint64 &newsize)
 resize the current Storage.
 
void to_ (const int &device)
 move the current Storage to different deivce.
 
Storage to (const int &device)
 move a new Storage with same content as current Storage on different deivce.
 
Storage clone () const
 return a copy of current storage.
 
const unsigned long longsize () const
 the size ( no. of elements ) in the Storage
 
const unsigned long longcapacity () const
 the capacity ( no. of real elements in memory) in the Storage
 
void print_info () const
 print the info of the Storage, including the device, dtype and size.
 
void set_zeros ()
 set all the elements to zero.
 
bool operator== (const Storage &rhs)
 compare two Storage
 
bool operator!= (const Storage &rhs)
 
template<class T >
void fill (const T &val)
 set all the elements to the assigned value val
 
Storage real () const
 Get the real part form a Complex type Storage.
 
Storage imag () const
 Get the imaginary part form a Complex type Storage.
 
Scalar get_item (const cytnx_uint64 &idx) const
 
template<class T >
void set_item (const cytnx_uint64 &idx, const T &elem)
 
Scalar::Sproxy operator() (const cytnx_uint64 &idx)
 

Static Public Member Functions

static Storage Load (const std::string &fname)
 Load current Storage from file.
 
static Storage Load (const char *fname)
 
static Storage Fromfile (const std::string &fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
 
static Storage Fromfile (const char *fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
 
template<class T >
static Storage from_vector (const std::vector< T > &vin, const int device=-1)
 

Detailed Description

an memeory storage with multi-type/multi-device support

Constructor & Destructor Documentation

◆ Storage() [1/2]

cytnx::Storage::Storage ( const unsigned long long size,
const unsigned int dtype = Type.Double,
int  device = -1 
)
inline

◆ Storage() [2/2]

cytnx::Storage::Storage ( )
inline

Member Function Documentation

◆ append()

template<class T >
void cytnx::Storage::append ( const T val)
inline

append a value

Parameters
valthe value to append. it can be any type defined in cytnx::Type

[Note]

  1. cannot append a complex value into a real Storage.

◆ astype()

Storage cytnx::Storage::astype ( const unsigned int new_type) const
inline

cast the type of current Storage

Parameters
new_typethe new type of the Storage instance. This can be any of type defined in cytnx::Type

description:

  1. if the new_type is the same as the dtype of current Storage, then return self; otherwise, return a new instance that has the same content as current Storage with dtype=new_type
  2. the return Stoarge will be on the same device as the current Storage.

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main(){
/*
1. Create a Storage with
dtype =Type.Double [default],
*/
Storage A(10);
cout << A.dtype_str() << endl;
// the type is the same as A's type, so B shares the same object with A
Storage B = A.astype(Type.Double);
cout << B.dtype_str() << endl;
cout << is(B,A) << endl; // true
// cast A from Type.Double to Type.Float
Storage C = A.astype(Type.Float);
cout << C.dtype_str() << endl;
cout << is(C, A) << endl; // false
Storage D(10,Type.Double,Device.cuda+0);
// D is on GPU, so E is also on GPU
Storage E = D.astype(Type.Float);
cout << E.device_str() << endl;
return 0;
}
an memeory storage with multi-type/multi-device support
Definition Storage.hpp:934
const int & device() const
the device-id of current Storage
Definition Storage.hpp:1079
Storage astype(const unsigned int &new_type) const
cast the type of current Storage
Definition Storage.hpp:1052
const std::string dtype_str() const
the dtype (std::string) of current Storage
Definition Storage.hpp:1070
const std::string device_str() const
the device (std::string) of current Storage
Definition Storage.hpp:1088
Definition Accessor.hpp:12
Device_class Device
Definition Device.cpp:105
Type_class Type
Definition Type.cpp:137

output>

Double (Float64)
Double (Float64)
1
Float (Float32)
0
cytnx device: CUDA/GPU-id:0

python API:

from cytnx import *
A = Storage(10)
print(A.dtype_str())
B = A.astype(Type.Double)
print(B.dtype_str())
print(B is A)
C = A.astype(Type.Float)
print(C.dtype_str())
print(C is A)
D = Storage(10,device=Device.cuda+0);
E = D.astype(Type.Float)
print(E.device_str())


output>

Double (Float64)
Double (Float64)
True
Float (Float32)
False
cytnx device: CUDA/GPU-id:0

◆ capacity()

const unsigned long long & cytnx::Storage::capacity ( ) const
inline

the capacity ( no. of real elements in memory) in the Storage

Returns
[cytnx_uint64]

◆ clone()

Storage cytnx::Storage::clone ( ) const
inline

return a copy of current storage.

Returns
[Storage]

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main(){
Storage A(15);
Storage B = A; // B shares same object with A
Storage C = A.clone(); // C is a copy of A
// use is() to check if two variable shares same object
cout << is(B,A) << endl;
cout << is(C,A) << endl;
return 0;
}
Storage clone() const
return a copy of current storage.
Definition Storage.hpp:1192

output>

1
0

python API:

from cytnx import *
A = Storage(15)
B = A
C = A.clone()
print(B is A)
print(C is A)


output>

True
False

◆ device()

const int & cytnx::Storage::device ( ) const
inline

the device-id of current Storage

Returns
[cytnx_int64] the device-id.

◆ device_str()

const std::string cytnx::Storage::device_str ( ) const
inline

the device (std::string) of current Storage

Returns
[std::string] device name

◆ dtype()

const unsigned int & cytnx::Storage::dtype ( ) const
inline

the dtype-id of current Storage

Returns
[cytnx_uint64] the dtype-id.

◆ dtype_str()

const std::string cytnx::Storage::dtype_str ( ) const
inline

the dtype (std::string) of current Storage

Returns
[std::string] dtype name

◆ fill()

template<class T >
void cytnx::Storage::fill ( const T val)
inline

set all the elements to the assigned value val

Parameters
valthe value to set on all the elements. it can be any type defined in cytnx::Type

[Note]

  1. cannot assign a complex value into a real Storage.

◆ from_vector()

template<class T >
static Storage cytnx::Storage::from_vector ( const std::vector< T > &  vin,
const int  device = -1 
)
inlinestatic

◆ Fromfile() [1/2]

Storage cytnx::Storage::Fromfile ( const char fname,
const unsigned int dtype,
const cytnx_int64 count = -1 
)
static

◆ Fromfile() [2/2]

Storage cytnx::Storage::Fromfile ( const std::string &  fname,
const unsigned int dtype,
const cytnx_int64 count = -1 
)
static

◆ get_item()

Scalar cytnx::Storage::get_item ( const cytnx_uint64 idx) const
inline

◆ imag()

Storage cytnx::Storage::imag ( ) const
inline

Get the imaginary part form a Complex type Storage.

[Note] Cannot be called from a real type Storage.

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main(){
/*
Get the imag part from a complex128 (ComplexDouble) Storage
*/
Storage S1(10,Type.ComplexDouble);
for(unsigned int i=0;i<S1.size();i++){
}
cout << S1 << endl;
cout << S1r << endl;
/*
Get the imag part from a complex64 (ComplexFloat) Storage
*/
Storage S2(10,Type.ComplexFloat);
for(unsigned int i=0;i<S1.size();i++){
}
cout << S2 << endl;
cout << S2r << endl;
return 0;
}
Storage imag() const
Get the imaginary part form a Complex type Storage.
Definition Storage.hpp:1390
std::complex< double > cytnx_complex128
Definition Type.hpp:30
std::complex< float > cytnx_complex64
Definition Type.hpp:29

output>

dtype : Complex Double (Complex Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+1.00000e+00j 1.00000e+00+2.00000e+00j 2.00000e+00+3.00000e+00j 3.00000e+00+4.00000e+00j 4.00000e+00+5.00000e+00j 5.00000e+00+6.00000e+00j 6.00000e+00+7.00000e+00j 7.00000e+00+8.00000e+00j 8.00000e+00+9.00000e+00j 9.00000e+00+1.00000e+01j  ]

dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 1.00000e+01 ]

dtype : Complex Float (Complex Float32)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+2.00000e+00j 1.00000e+00+3.00000e+00j 2.00000e+00+4.00000e+00j 3.00000e+00+5.00000e+00j 4.00000e+00+6.00000e+00j 5.00000e+00+7.00000e+00j 6.00000e+00+8.00000e+00j 7.00000e+00+9.00000e+00j 8.00000e+00+1.00000e+01j 9.00000e+00+1.10000e+01j  ]

dtype : Float (Float32)
device: cytnx device: CPU
size  : 10
[ 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]

python API:

from cytnx import *
S1 = Storage(10,Type.ComplexDouble)
for i in range(10):
S1[i] = i + 1j*(i+1)
S1r = S1.imag()
print(S1)
print(S1r)
S2 = Storage(10,Type.ComplexFloat)
for i in range(10):
S2[i] = i + 1j*(i+1)
S2r = S2.imag()
print(S2)
print(S2r)


output>

dtype : Complex Double (Complex Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+1.00000e+00j 1.00000e+00+2.00000e+00j 2.00000e+00+3.00000e+00j 3.00000e+00+4.00000e+00j 4.00000e+00+5.00000e+00j 5.00000e+00+6.00000e+00j 6.00000e+00+7.00000e+00j 7.00000e+00+8.00000e+00j 8.00000e+00+9.00000e+00j 9.00000e+00+1.00000e+01j  ]


dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 1.00000e+01 ]


dtype : Complex Float (Complex Float32)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+1.00000e+00j 1.00000e+00+2.00000e+00j 2.00000e+00+3.00000e+00j 3.00000e+00+4.00000e+00j 4.00000e+00+5.00000e+00j 5.00000e+00+6.00000e+00j 6.00000e+00+7.00000e+00j 7.00000e+00+8.00000e+00j 8.00000e+00+9.00000e+00j 9.00000e+00+1.00000e+01j  ]


dtype : Float (Float32)
device: cytnx device: CPU
size  : 10
[ 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 1.00000e+01 ]


◆ Init()

void cytnx::Storage::Init ( const unsigned long long size,
const unsigned int dtype = Type.Double,
int  device = -1 
)
inline

initialize a Storage

Parameters
sizethe number of elements for the Storage
dtypethe dtype of the Storage instance. This can be any of type defined in cytnx::Type
devicethe device of the Storage instance. This can be cytnx::Device.cpu or cytnx::Device.cuda+<gpuid>

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main(){
/*
1. Create a Storage with
10 elements,
dtype =Type.Double [default],
device=Device.cpu [default]
*/
Storage A(10);
cout << A << endl;
/*
2. Create a Storage with
10 elements,
dtype =Type.Uint64,
device=Device.cpu [default],
[Note] the dtype can be any one of the supported type.
*/
Storage B(10,Type.Uint64);
cout << B << endl;
/*
3. Initialize a Storage with
10 elements,
dtype =Type.Double,
device=Device.cuda+0, (on gpu with gpu-id=0)
[Note] the gpu device can be set with Device.cuda+<gpu-id>
*/
Storage C(10,Type.Double,Device.cuda+0);
cout << C << endl;
//4. Create an empty Storage, and init later
D.Init(10,Type.Double,Device.cpu);
return 0;
}
void Init(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1)
initialize a Storage
Definition Storage.hpp:962

output>

dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 ]

dtype : Uint64
device: cytnx device: CPU
size  : 10
[                   0                   0                   0                   0                   0                   0                   0                   0                   0                   0  ]

dtype : Double (Float64)
device: cytnx device: CUDA/GPU-id:0
size  : 10
[ 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 ]

python API:

from cytnx import *
A = Storage(10);
print(A)
B = Storage(10,dtype=Type.Uint64)
print(B)
C = Storage(10,device=Device.cuda+0)
print(C)
D = Storage()
D.Init(10,dtype=Type.Double,device=Device.cpu)


output>

dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 ]


dtype : Uint64
device: cytnx device: CPU
size  : 10
[                   0                   0                   0                   0                   0                   0                   0                   0                   0                   0  ]


dtype : Double (Float64)
device: cytnx device: CUDA/GPU-id:0
size  : 10
[ 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 ]


◆ Load() [1/2]

Storage cytnx::Storage::Load ( const char fname)
static

◆ Load() [2/2]

Storage cytnx::Storage::Load ( const std::string &  fname)
static

Load current Storage from file.

Parameters
fnamefile name

description: load the Storage from file with file path specify with input param 'fname'.

◆ operator!=()

◆ operator()()

Scalar::Sproxy cytnx::Storage::operator() ( const cytnx_uint64 idx)

◆ operator==()

compare two Storage

Parameters
Storageanother Storage to compare to

[Note] the == operator will compare the content between two storages. use cytnx::is() for checking two variables share the same instance.

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main(){
/*
1. Create a Storage with
dtype =Type.Double [default],
*/
Storage A(10);
cout << A.dtype_str() << endl;
Storage B = A;
cout << (B == A) << endl; // true (share same instance)
cout << is(B,A) << endl; // true (share same instance)
cout << (C == A) << endl; // true (the same content.)
cout << is(C,A) << endl; // false (not share same instance)
return 0;
}

output>

Double (Float64)
1
1
1
0

python API:

from cytnx import *
A = Storage(10)
print(A.dtype_str())
B = A
C = A.clone()
print(B == A) # true (share same instance)
print(B is A) # true (share same instance)
print(C == A) # true (the same content.)
print(C is A) # false (not share same instance)


output>

Double (Float64)
True
True
True
False

◆ print_info()

void cytnx::Storage::print_info ( ) const
inline

print the info of the Storage, including the device, dtype and size.

◆ real()

Storage cytnx::Storage::real ( ) const
inline

Get the real part form a Complex type Storage.

[Note] Cannot be called from a real type Storage.

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main(){
/*
Get the real part from a complex128 (ComplexDouble) Storage
*/
Storage S1(10,Type.ComplexDouble);
for(unsigned int i=0;i<S1.size();i++){
}
cout << S1 << endl;
cout << S1r << endl;
/*
Get the real part from a complex64 (ComplexFloat) Storage
*/
Storage S2(10,Type.ComplexFloat);
for(unsigned int i=0;i<S1.size();i++){
}
cout << S2 << endl;
cout << S2r << endl;
return 0;
}
Storage real() const
Get the real part form a Complex type Storage.
Definition Storage.hpp:1372

output>

dtype : Complex Double (Complex Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+1.00000e+00j 1.00000e+00+2.00000e+00j 2.00000e+00+3.00000e+00j 3.00000e+00+4.00000e+00j 4.00000e+00+5.00000e+00j 5.00000e+00+6.00000e+00j 6.00000e+00+7.00000e+00j 7.00000e+00+8.00000e+00j 8.00000e+00+9.00000e+00j 9.00000e+00+1.00000e+01j  ]

dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 ]

dtype : Complex Float (Complex Float32)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+2.00000e+00j 1.00000e+00+3.00000e+00j 2.00000e+00+4.00000e+00j 3.00000e+00+5.00000e+00j 4.00000e+00+6.00000e+00j 5.00000e+00+7.00000e+00j 6.00000e+00+8.00000e+00j 7.00000e+00+9.00000e+00j 8.00000e+00+1.00000e+01j 9.00000e+00+1.10000e+01j  ]

dtype : Float (Float32)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 ]

python API:

from cytnx import *
S1 = Storage(10,Type.ComplexDouble)
for i in range(10):
S1[i] = i + 1j*(i+1)
S1r = S1.real()
print(S1)
print(S1r)
S2 = Storage(10,Type.ComplexFloat)
for i in range(10):
S2[i] = i + 1j*(i+1)
S2r = S2.real()
print(S2)
print(S2r)


output>

dtype : Complex Double (Complex Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+1.00000e+00j 1.00000e+00+2.00000e+00j 2.00000e+00+3.00000e+00j 3.00000e+00+4.00000e+00j 4.00000e+00+5.00000e+00j 5.00000e+00+6.00000e+00j 6.00000e+00+7.00000e+00j 7.00000e+00+8.00000e+00j 8.00000e+00+9.00000e+00j 9.00000e+00+1.00000e+01j  ]


dtype : Double (Float64)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 ]


dtype : Complex Float (Complex Float32)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00+1.00000e+00j 1.00000e+00+2.00000e+00j 2.00000e+00+3.00000e+00j 3.00000e+00+4.00000e+00j 4.00000e+00+5.00000e+00j 5.00000e+00+6.00000e+00j 6.00000e+00+7.00000e+00j 7.00000e+00+8.00000e+00j 8.00000e+00+9.00000e+00j 9.00000e+00+1.00000e+01j  ]


dtype : Float (Float32)
device: cytnx device: CPU
size  : 10
[ 0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 8.00000e+00 9.00000e+00 ]


◆ resize()

void cytnx::Storage::resize ( const cytnx_uint64 newsize)
inline

resize the current Storage.

Parameters
newsize.

◆ Save() [1/2]

void cytnx::Storage::Save ( const char fname) const

◆ Save() [2/2]

void cytnx::Storage::Save ( const std::string &  fname) const

Save current Storage to file.

Parameters
fnamefile name

description: save the Storage to file with file path specify with input param 'fname' with postfix ".cyst"

◆ set_item()

template<class T >
void cytnx::Storage::set_item ( const cytnx_uint64 idx,
const T elem 
)
inline

◆ set_zeros()

void cytnx::Storage::set_zeros ( )
inline

set all the elements to zero.

[Note] although it is also possible to use Storage.fill(0) to set all the elements to zero, using set_zeros will have significant faster performance.

◆ size()

const unsigned long long & cytnx::Storage::size ( ) const
inline

the size ( no. of elements ) in the Storage

Returns
[cytnx_uint64]

◆ to()

Storage cytnx::Storage::to ( const int device)
inline

move a new Storage with same content as current Storage on different deivce.

Parameters
devicethe device-id. It can be any device defined in cytnx::Device

[Note] if the

Parameters
deviceis the same as the current Storage's device, return self.

see also Storage.to_()

◆ to_()

void cytnx::Storage::to_ ( const int device)
inline

move the current Storage to different deivce.

Parameters
devicethe device-id. It can be any device defined in cytnx::Device

see also Storage.to()

◆ Tofile() [1/3]

void cytnx::Storage::Tofile ( const char fname) const

◆ Tofile() [2/3]

void cytnx::Storage::Tofile ( const std::string &  fname) const

◆ Tofile() [3/3]

void cytnx::Storage::Tofile ( std::fstream &  f) const

The documentation for this class was generated from the following files: