mptensor
v0.3.0
Parallel Library for Tensor Network Methods
|
Tensor class. The main object of mptensor. More...
#include <tensor.hpp>
Public Types | |
typedef C | value_type |
double or complex More... | |
typedef Matrix< C > | matrix_type |
type of Matrix class More... | |
typedef Matrix< C >::comm_type | comm_type |
type of communicator. More... | |
Public Member Functions | |
const Shape & | shape () const |
Shape of tensor. More... | |
size_t | rank () const |
Rank of tensor. More... | |
size_t | ndim () const |
Rank of tensor. More... | |
size_t | local_size () const |
Size of local storage. More... | |
size_t | get_upper_rank () const |
Upper rank for matrix representation. More... | |
const Axes & | get_axes_map () const |
Map of axes for lazy evaluation of transpose. More... | |
const Matrix< C > & | get_matrix () const |
distributed Matrix More... | |
Matrix< C > & | get_matrix () |
distributed Matrix More... | |
const comm_type & | get_comm () const |
Communicator. More... | |
int | get_comm_size () const |
Size of communicator. More... | |
int | get_comm_rank () const |
Rank of process. More... | |
Index | global_index (size_t i) const |
Convert local index to global index. More... | |
void | global_index_fast (size_t i, Index &idx) const |
Convert local index to global index fast. More... | |
void | local_position (const Index &idx, int &comm_rank, size_t &local_idx) const |
Calculate rank which has the given global index and its local index. More... | |
const C & | operator[] (size_t local_idx) const |
Const array subscript operator. More... | |
C & | operator[] (size_t local_idx) |
Array subscript operator. More... | |
bool | get_value (const Index &idx, C &val) const |
Get an element. More... | |
void | set_value (const Index &idx, C val) |
Set an element. More... | |
void | print_info (std::ostream &out, const std::string &tag="") const |
Output information of tensor. More... | |
void | print_info_mpi (std::ostream &, const std::string &tag="") const |
Output information of tensor. More... | |
void | save (const std::string &filename) const |
Save a tensor to files. More... | |
void | load (const std::string &filename) |
Load a tensor from files. More... | |
Tensor< Matrix, C > & | transpose (const Axes &axes) |
Transposition of tensor with lazy evaluation. More... | |
template<typename D > | |
Tensor< Matrix, C > & | multiply_vector (const std::vector< D > &vec, size_t n_axes) |
Element-wise vector multiplication. More... | |
template<typename D0 , typename D1 > | |
Tensor< Matrix, C > & | multiply_vector (const std::vector< D0 > &vec0, size_t n_axes0, const std::vector< D1 > &vec1, size_t n_axes1) |
Element-wise vector multiplication. More... | |
template<typename D0 , typename D1 , typename D2 > | |
Tensor< Matrix, C > & | multiply_vector (const std::vector< D0 > &vec0, size_t n_axes0, const std::vector< D1 > &vec1, size_t n_axes1, const std::vector< D2 > &vec2, size_t n_axes2) |
Element-wise vector multiplication. More... | |
template<typename D0 , typename D1 , typename D2 , typename D3 > | |
Tensor< Matrix, C > & | multiply_vector (const std::vector< D0 > &vec0, size_t n_axes0, const std::vector< D1 > &vec1, size_t n_axes1, const std::vector< D2 > &vec2, size_t n_axes2, const std::vector< D3 > &vec3, size_t n_axes3) |
Element-wise vector multiplication. More... | |
Tensor< Matrix, C > & | set_slice (const Tensor &a, size_t n_axes, size_t i_begin, size_t i_end) |
Inverse of slice(). More... | |
Tensor< Matrix, C > & | set_slice (const Tensor &a, const Index &index_begin, const Index &index_end) |
Inverse of slice(). More... | |
Tensor< lapack::Matrix, C > | gather () |
Gather all elements into a non-distributed tensor. More... | |
std::vector< C > | flatten () |
Return a copy of the flattened vector. More... | |
Tensor< Matrix, C > & | operator+= (const Tensor &rhs) |
Addition assignment. More... | |
Tensor< Matrix, C > & | operator-= (const Tensor &rhs) |
Subtraction assignment. More... | |
Tensor< Matrix, C > & | operator*= (C rhs) |
Scalar-multiplication assignment. More... | |
Tensor< Matrix, C > & | operator/= (C rhs) |
Scalar-division assignment. More... | |
Tensor< Matrix, C > & | operator= (C rhs) |
Initialize all elements by rhs. More... | |
template<typename UnaryOperation > | |
Tensor< Matrix, C > & | map (UnaryOperation op) |
Apply a unary operation to each element. More... | |
void | prep_global_to_local () const |
Preprocess for fast conversion from global index to local one. More... | |
void | prep_local_to_global () const |
Preprocess for fast conversion from local index to global one. More... | |
void | make_l2g_map () const |
Preprocess for fast conversion from local index to global one. More... | |
void | global_index_l2g_map (size_t lindex, size_t gindex[]) const |
Convert local index to global index using l2g_map. More... | |
void | global_index_l2g_map_transpose (size_t lindex, const size_t axes_trans[], size_t index_new[]) const |
Convert local index to global index of transposed tensor using l2g_map. More... | |
void | local_position_fast (size_t g_row, size_t g_col, int &comm_rank, size_t &local_idx) const |
Tensor () | |
Default constructor of tensor. More... | |
Tensor (const Shape &) | |
Constructor of tensor. More... | |
Tensor (const comm_type &) | |
Constructor of tensor. More... | |
Tensor (const comm_type &, const Shape &) | |
Constructor of tensor. More... | |
Tensor (const comm_type &, const Shape &, size_t upper_rank) | |
Constructor of tensor. More... | |
Tensor (const comm_type &, const Tensor< lapack::Matrix, C > &) | |
Constructor of tensor from non-distributed tensor. More... | |
Tensor (const comm_type &, const std::vector< C > &) | |
Constructor of tensor from non-distributed vector. More... | |
Tensor class. The main object of mptensor.
typedef Matrix<C>::comm_type mptensor::Tensor< Matrix, C >::comm_type |
type of communicator.
MPI_Comm
or int
.
typedef Matrix<C> mptensor::Tensor< Matrix, C >::matrix_type |
type of Matrix class
typedef C mptensor::Tensor< Matrix, C >::value_type |
double
or complex
std::vector< C > mptensor::Tensor< Matrix, C >::flatten |
Return a copy of the flattened vector.
Tensor< lapack::Matrix, C > mptensor::Tensor< Matrix, C >::gather |
Gather all elements into a non-distributed tensor.
|
inline |
Map of axes for lazy evaluation of transpose.
axes_map
stores an index mapping from a before-transposed tensor to an after-transposed tensor.
Let V
a 3-leg tensor with shape=[10, 20, 30]
and axes_map=[0, 1, 2]
. When we transpose V
as T = V.transpose(Axes(1, 2, 0));
, T
has shape=[20, 30, 10]
and axes_map=[2, 0, 1]
. Thus, axes_map
satisfies axes_map[iV] = iT
.
|
inline |
Communicator.
|
inline |
Rank of process.
|
inline |
Size of communicator.
|
inline |
distributed Matrix
|
inline |
distributed Matrix
|
inline |
Upper rank for matrix representation.
bool mptensor::Tensor< Matrix, C >::get_value | ( | const Index & | idx, |
C & | val | ||
) | const |
Get an element.
[in] | idx | Global index. |
[out] | val | Value of the element. |
Index mptensor::Tensor< Matrix, C >::global_index | ( | size_t | lindex | ) | const |
Convert local index to global index.
[in] | lindex | Local index |
void mptensor::Tensor< Matrix, C >::global_index_fast | ( | size_t | lindex, |
Index & | gindex | ||
) | const |
Convert local index to global index fast.
[in] | lindex | Local index |
[out] | gindex | Global index. |
|
inline |
Convert local index to global index using l2g_map.
[in] | lindex | Local index |
[out] | gindex | Global index. |
|
inline |
Convert local index to global index of transposed tensor using l2g_map.
[in] | lindex | Local index. |
[in] | axes_trans | axes mapping (see below). |
[out] | index_new | Global index of transposed tensor. |
Here axes_inv is inverse of axes in transpose().
void mptensor::Tensor< Matrix, C >::load | ( | const std::string & | filename | ) |
Load a tensor from files.
Rank-0 process reads an ASCII file with shape information whose name is given by filename
. Every process restores tensor elements from a binary file filename
.[rank_no].bin and an ASCII file filename
.[rank_no].idx .
filename | Name of the base file. |
[rank_no] in the name of binary files has at least 4 digit, (ex. filename.0001.bin). void mptensor::Tensor< Matrix, C >::local_position | ( | const Index & | index, |
int & | comm_rank, | ||
size_t & | local_idx | ||
) | const |
Calculate rank which has the given global index and its local index.
[in] | index | Global index. |
[out] | comm_rank | Rank which has the element at the global index. |
[out] | local_idx | Local index in comm_rank. |
|
inline |
|
inline |
Size of local storage.
|
inline |
Preprocess for fast conversion from local index to global one.
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::map | ( | UnaryOperation | op | ) |
Apply a unary operation to each element.
op | Unary operation. |
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector | ( | const std::vector< D > & | vec, |
size_t | n_axes | ||
) |
Element-wise vector multiplication.
For example, T.multiply_vector(v,1)
is equivalent to \( T_{ijk} := v_j T_{ijk}. \)
vec | Vector |
n_axes | Axes to multiply the vector |
vec
should be larger than the bond dimension of n_axes
. Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector | ( | const std::vector< D0 > & | vec0, |
size_t | n_axes0, | ||
const std::vector< D1 > & | vec1, | ||
size_t | n_axes1 | ||
) |
Element-wise vector multiplication.
For example, T.multiply_vector(v,1,w,0)
is equivalent to \( T_{ijk} := v_j w_i T_{ijk}. \)
vec0 | Vector |
n_axes0 | Axes to multiply the vector vec0 . |
vec1 | Vector |
n_axes1 | Axes to multiply the vector vec1 . |
vecX
should be larger than the bond dimension of n_axesX
. Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector | ( | const std::vector< D0 > & | vec0, |
size_t | n_axes0, | ||
const std::vector< D1 > & | vec1, | ||
size_t | n_axes1, | ||
const std::vector< D2 > & | vec2, | ||
size_t | n_axes2 | ||
) |
Element-wise vector multiplication.
For example, T.multiply_vector(v,1,w,0,x,2)
is equivalent to \( T_{ijkl} := v_j w_i x_k T_{ijkl}. \)
vec0 | Vector |
n_axes0 | Axes to multiply the vector vec0 . |
vec1 | Vector |
n_axes1 | Axes to multiply the vector vec1 . |
vec2 | Vector |
n_axes2 | Axes to multiply the vector vec2 . |
vecX
should be larger than the bond dimension of n_axesX
. Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector | ( | const std::vector< D0 > & | vec0, |
size_t | n_axes0, | ||
const std::vector< D1 > & | vec1, | ||
size_t | n_axes1, | ||
const std::vector< D2 > & | vec2, | ||
size_t | n_axes2, | ||
const std::vector< D3 > & | vec3, | ||
size_t | n_axes3 | ||
) |
Element-wise vector multiplication.
For example, T.multiply_vector(v,1,w,0,x,2,y,4)
is equivalent to \( T_{ijklm} := v_j w_i x_k y_m T_{ijklm}. \)
vec0 | Vector |
n_axes0 | Axes to multiply the vector vec0 . |
vec1 | Vector |
n_axes1 | Axes to multiply the vector vec1 . |
vec2 | Vector |
n_axes2 | Axes to multiply the vector vec2 . |
vec3 | Vector |
n_axes3 | Axes to multiply the vector vec3 . |
vecX
should be larger than the bond dimension of n_axesX
.
|
inline |
Rank of tensor.
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator*= | ( | C | rhs | ) |
Scalar-multiplication assignment.
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator+= | ( | const Tensor< Matrix, C > & | rhs | ) |
Addition assignment.
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator-= | ( | const Tensor< Matrix, C > & | rhs | ) |
Subtraction assignment.
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator/= | ( | C | rhs | ) |
Scalar-division assignment.
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator= | ( | C | rhs | ) |
Initialize all elements by rhs.
|
inline |
Array subscript operator.
|
inline |
Const array subscript operator.
|
inline |
Preprocess for fast conversion from global index to local one.
|
inline |
Preprocess for fast conversion from local index to global one.
void mptensor::Tensor< Matrix, C >::print_info | ( | std::ostream & | out, |
const std::string & | tag = "" |
||
) | const |
Output information of tensor.
Every process outputs information to output stream.
out | Output stream. |
tag | (optional) A tag which is inserted at the head of the line. |
void mptensor::Tensor< Matrix, C >::print_info_mpi | ( | std::ostream & | out, |
const std::string & | tag = "" |
||
) | const |
Output information of tensor.
All processes output information of a tensor one by one.
out | Output stream. |
tag | (optional) A tag which is inserted at the head of each line. |
|
inline |
Rank of tensor.
void mptensor::Tensor< Matrix, C >::save | ( | const std::string & | filename | ) | const |
Save a tensor to files.
Rank-0 process creates an ASCII file with shape information whose name is given by filename
. Every process saves tensor elements to a binary file filename
.[rank_no].bin and an ASCII file filename
.[rank_no].idx .
filename | Name of the base file. |
rank_no
in the name of binary files has at least 4 digit, (ex. filename.0001.bin). Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::set_slice | ( | const Tensor< Matrix, C > & | a, |
const Index & | index_begin, | ||
const Index & | index_end | ||
) |
Inverse of slice().
If index_begin[r]==index_end[r]
, this rank is not sliced. T.set_slice( slice(T,i,j), i,j)
does nothing. For example, T.set_slice(A,Index(1,0,3),Index(4,0,6));
is equal to T[1:4,:,3:6]=A[:,:,:]
in Python.
[in] | a | A sliced tensor to be set. |
[in] | index_begin | Start indices. |
[in] | index_end | End indices. |
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::set_slice | ( | const Tensor< Matrix, C > & | a, |
size_t | n_axes, | ||
size_t | i_begin, | ||
size_t | i_end | ||
) |
Inverse of slice().
T.set_slice( slice(T,r,i,j), r,i,j)
does nothing. For example, T.set_slice(A,1,4,10);
is equal to T[:,4:10,:]=A[:,:,:]
in Python.
[in] | a | A sliced tensor to be set. |
[in] | n_axes | sliced Axes. |
[in] | i_begin | Start index of slice. |
[in] | i_end | End index of slice. |
void mptensor::Tensor< Matrix, C >::set_value | ( | const Index & | idx, |
C | val | ||
) |
Set an element.
If my process does not have the element, this function does nothing.
[in] | idx | Global index. |
[in] | val | Value of the element. |
|
inline |
Shape of tensor.
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::transpose | ( | const Axes & | axes | ) |
Transposition of tensor with lazy evaluation.
When axes=
[1,2,0], \( T_{ijk} \) is transformed into \( T_{jki} \).
[in] | axes | Order of axes. |