SyTen
syten::STensorImpl::STensor Class Reference

Implementation for syten::STensor. More...

#include <stensor_base.h>

+ Collaboration diagram for syten::STensorImpl::STensor:

Classes

struct  AutodiffRenewReturn
 Simple helper struct returned by STensor::get_and_renew_autodiff() and STensorProxy::get_and_renew_autodiff(). More...
 

Public Member Functions

MemoryUsage::MemorySize allocSize () const
 allocSize()/memory_size.h support. More...
 
void clean_up_sbases ()
 Compares the SBases to the underlying Tensor and clears potentially empty elements. More...
 
void clear ()
 Deletes the STensor. More...
 
bool dense_is_standard () const
 Returns true if the tensor is a rank-0 tensor or all blocks are in standard form. More...
 
void make_dense_cuda ()
 Brings all dense tensors into CUDA form if the tensor rank is larger than 0. More...
 
void make_dense_standard ()
 Brings all dense tensors into standard form if the tensor rank is larger than 0. More...
 
void maybe_make_dense_cuda ()
 Conditionally transforms all stored dense tensors into CUDA dense tensors if CUDA support is compiled in, at least one CUDA device is enabled and the individual blocks are larger than the CUDA threshold. More...
 
Constructors
 STensor ()
 Default ctor, initialises a rank-0 tensor. More...
 
 STensor (STensor &&)
 Move ctor, defaulted. More...
 
STensoroperator= (STensor &&)=default
 Move assignment op, defaulted. More...
 
 STensor (STensor const &, SourceLocation const &=SourceLocation::current())
 Copy ctor, logs all calls. More...
 
STensoroperator= (STensor const &)
 Copy assignment op, logs all calls (but cannot attribute them properly). More...
 
 STensor (Vec< SBasis > bases, Vec< Index > fermionic_order={})
 Ctor initialising an empty tensor of the same rank as bases has entries and, if specified, the given fermionic order (ascending otherwise). More...
 
 STensor (SDef const &v)
 Ctor initialising a rank-0 tensor with the specified scalar value. More...
 
 ~STensor ()
 Dtor. More...
 
template<Rank rank>
 STensor (Tensor< rank > other_, Vec< SBasis > bases_={}, Vec< Index > fermionic_order_={})
 Ctor initialising a tensor of the specified rank, bases and fermionic order. More...
 
STensor copy () const
 Function to explicitly copy a syten::STensor. More...
 
STensor move ()
 Function to move from a syten::STensor, for symmetry with syten::STensorImpl::STensor::copy. More...
 
Conversion operators
 operator SDef () const
 Explicit conversion to a SDef. More...
 
 operator SRDef () const
 Explicit conversion to a SRDef. More...
 
Visitors
template<typename F >
auto visit (F func)
 Visit-function, applies the supplied lambda func to the currently active tensor and returns the result. More...
 
template<typename F >
auto visit (F func) const
 Visit-function, applies the supplied lambda func to the currently active tensor and returns the result. More...
 
template<typename F >
void visnz (F func) const
 Visit function, applies the supplied lambda func to the currently active tensor if it has non-zero rank. More...
 
template<typename F >
void visnz (F func)
 Visit function, applies the supplied lambda func to the currently active tensor if it has non-zero rank. More...
 
Direct access to the stored tensor
template<Rank r>
Tensor< r > const & get () const
 Retrieves a constant reference to the tensor of the supplied rank, throws if the stored tensor has a different rank. More...
 
template<Rank r>
Tensor< r > & get () &
 Retrieves a reference to the tensor of the supplied rank, throws if the stored tensor has a different rank. More...
 
template<Rank r>
Tensor< r > get () &&
 Moves the tensor of the supplied rank out of the object, throws if the stored tensor has a different rank. More...
 
Information about the stored tensor.
Index rank () const
 Returns the rank of the currently-stored tensor. More...
 
Index num_blocks () const
 Returns the number of blocks of the stored tensor. More...
 
Renaming and priming any particular basis.
STensorrename (std::string_view from, std::string_view to) &
 Renames the basis wich nickname from to have nickname to. More...
 
STensor rename (std::string_view from, std::string_view to) &&
 Renames the basis wich nickname from to have nickname to. More...
 
STensorrename (SBasisId const &which, std::string_view to) &
 Rename the specified basis to have nickname to. More...
 
STensor rename (SBasisId const &which, std::string_view to) &&
 Rename the specified basis to have nickname to. More...
 
STensorprime (SBasisId const &which, std::int8_t value=1) &
 Increases the prime-level of the specified basis. More...
 
STensor prime (SBasisId const &which, std::int8_t value=1) &&
 Increases the prime-level of the specified basis. More...
 
STensorprime_all (std::int8_t value=1) &
 Increases the prime-level of all bases by one. More...
 
STensor prime_all (std::int8_t value=1) &&
 Increases the prime-level of all bases by one. More...
 
STensorunprime (SBasisId const &which, std::int8_t value=1) &
 Decreases the prime-level of the specified basis. More...
 
STensor unprime (SBasisId const &which, std::int8_t value=1) &&
 Decreases the prime-level of the specified basis. More...
 
STensorunprime_all (std::int8_t value=1) &
 Decreases the prime-level of all bases by one. More...
 
STensor unprime_all (std::int8_t value=1) &&
 Decreases the prime-level of all bases by one. More...
 
STensorp_r (SBasisId const &which, std::string_view to) &
 Renames the specified basis to which and also primes it. More...
 
STensor p_r (SBasisId const &which, std::string_view to) &&
 Renames the specified basis to which and also primes it. More...
 
Rank verification
STensor const & e (Index i) const &
 Asserts that this has the (expected) rank i and returns a const reference to this. More...
 
STensore (Index i) &
 Asserts that this has the (expected) rank i and returns a reference to this. More...
 
STensor e (Index i) &&
 Asserts that this has the (expected) rank i and returns an expiring this by value. More...
 
R/W access to the stored basis
SBasis const & b (Index which) const
 Returns a const reference to the SBasis corresponding to the which-th tensor leg. More...
 
SBasis const & b (SBasisId const &which) const
 Returns a const reference to the SBasis which is identical to the supplied one. More...
 
SBasisb (Index which)
 Returns a reference to the SBasis corresponding to the which-th tensor leg. More...
 
SBasisb (SBasisId const &which)
 Returns a reference to the SBasis which is identical to the supplied one. More...
 
Index bi (SBasisId const &which) const
 Returns the index of the supplied SBasis. More...
 
Vec< SBasis > const & get_bases () const
 Returns a const reference to the vector of bases. More...
 
void set_bases (Vec< SBasis > bases)
 Sets the bases of the tensors to the supplied vector. More...
 
bool has_basis (SBasisId const &b) const
 Returns true if this tensor has a leg with Id b. More...
 
R/W access to fermionic order.
Vec< Index > const & get_fermionic_order () const
 Returns a const reference to the vector of fermionic ordering. More...
 
void set_fermionic_order (Vec< Index > fermionic)
 Sets the vector of fermionic ordering to the supplied vector. More...
 
Special handling of rump data during tensor storage.
STensor initialise_rump () const
 Called after *this was saved to disk to intialise an in-memory rump object. More...
 
void restore_from_rump (STensor &&rump)
 Called after *this was restored from disk to restore from an in-memory rump object. More...
 
Autodifferentiation support.
bool autodiff_enabled () const
 Returns true if autodifferentiation is enabled for this tensor, causing it to also be enabled for all downstream tensors. More...
 
Autodiff::STensorId enable_autodiff (Autodiff::STensorId id=0)
 Enables autodifferentation tracing for this STensor, returns the autodiff ID of the tensor. More...
 
void disable_autodiff ()
 Disables autodifferentiation tracing for this STensor. More...
 
Autodiff::STensorId restart_autodiff (Autodiff::STensorId id=0)
 Toggles autodifferentiation tracing for this STensor by disabling and enabling it, returns the new autodiff ID of the tensor. More...
 
Autodiff::STensorId get_autodiff_id () const
 Returns the autodifferentation ID of this tensor if enabled, 0 if disabled. More...
 
Autodiff::ComputeNodePtr get_autodiff_node () const
 Returns a shared pointer to the autodifferentiation compute node which created this tensor if enabled, nullptr otherwise. More...
 
std::tuple< Autodiff::STensorId, Autodiff::ComputeNodePtrget_autodiff () const
 Returns a tuple of autodifferentiation ID and node. More...
 
Autodiff::STensorId new_autodiff_id ()
 Generates a new autodiff ID for the STensor. More...
 
void set_autodiff_node (std::string &&opname_, Vec< Pair< Autodiff::ComputeNodePtr, Size > > &&input_nodes_, Vec< Autodiff::STensorId > &&output_ids, Autodiff::AdjointEvaluator &&func_, Vec< STensor > &&output_shapes_, Vec< AsyncCached< STensor > > &&cached_tensors_={})
 Constructs a new ComputeNode from the supplied arguments and sets this tensors shared pointer to point at the newly-constructed node. More...
 
void set_autodiff_node (Autodiff::ComputeNodePtr ptr)
 Sets the autodiff node of this tensor to the supplied pointer. More...
 
AutodiffRenewReturn get_and_renew_autodiff ()
 Returns a struct AutodiffRenewReturn as if by executing. More...
 
STensor autodiff (Autodiff::STensorId const input_id, AutodiffRescale do_rescale=AutodiffRescale::n())
 Returns the total derivative of this tensor with respect to the tensor with the supplied tensor ID. More...
 

Private Member Functions

template<Rank... Ranks>
void init (Rank rank, std::index_sequence< Ranks... >={})
 Initialisation helper to set rank of the stored tensor to the specified value. More...
 
void sanity_check_bases () const
 Asserts that basis and fermionic order length and basis directions match the underlying stored tensor. More...
 

Private Attributes

Autodiff::ComputeNodePtr ad_compute_node = {}
 If autodifferentiation is enabled: pointer to compute node which created this tensor. More...
 
Autodiff::STensorId ad_id = 0
 If autodifferentiation is enabled: ID of this tensor in the autodiff framework. More...
 
Vec< SBasisbases = {}
 List of names bases, one per leg of the underlying tensor. More...
 
Vec< Indexfermionic_order = {}
 Fermionic order of the underlying tensor legs. More...
 
STensorStorage< SYTEN_STENSOR_RANKStensor = Tensor<0>()
 Storage for the underlying tensor. More...
 

Boost serialisation

static constexpr unsigned int version = 1
 Boost serialisation: version. More...
 
template<typename Archive >
void save (Archive &ar, const unsigned int) const
 Boost serialisation: saving to an archive. More...
 
template<typename Archive >
void load (Archive &ar, const unsigned int in_version)
 Boost serialisation: Loading from an archive. More...
 

Detailed Description

Implementation for syten::STensor.

This class uses std::variant to hide the underlying tensor rank. As an effect, various bits are slightly less straightforward than they could be, as we have to handle the compile-time fixed rank together with a run-time variable rank.

There are two main idioms to do this:

  • First, the visit(Lambda λ) and visnz(Lambda λ) member functions take the supplied templated lambda λ and apply it to the currently-stored tensor. visit() returns the return value of this lambda, visnz() returns void but also does not apply the lambda if the current tensor has rank zero ('Non-Zero'). See e.g. inc/stensor/stensor_transpose.cpp for simple examples of visnz() and STensor::rank() for a simple example of the visit() function. Note that within the supplied lambda, the tensor rank is again a compile-time constant.
  • Second, and more elaborate, one may use pack expansion and short-cuirciting of the && operator to match a run-time variable against a list of compile-time allowed tensor ranks. See STensor::init() and how it is called in STensor::STensor() for a simple example.

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