Commit 85686637 authored by Dmitry I. Lyakh's avatar Dmitry I. Lyakh
Browse files

Merge branch 'devel'

parents f18dcfaa 28d64605
/** ExaTN::Numerics: Tensor
REVISION: 2019/06/05
REVISION: 2019/07/02
Copyright (C) 2018-2018 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2018 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor.hpp"
#include <assert.h>
#include <iostream>
#include <assert.h>
namespace exatn{
......@@ -30,6 +29,11 @@ name_(name), shape_(shape), signature_(shape.getRank())
{
}
Tensor::Tensor(const std::string & name):
name_(name)
{
}
void Tensor::printIt() const
{
std::cout << name_; signature_.printIt(); shape_.printIt();
......@@ -76,6 +80,26 @@ std::pair<SpaceId,SubspaceId> Tensor::getDimSpaceAttr(unsigned int dim_id) const
return signature_.getDimSpaceAttr(dim_id);
}
void Tensor::deleteDimension(unsigned int dim_id)
{
signature_.deleteDimension(dim_id);
shape_.deleteDimension(dim_id);
return;
}
void Tensor::appendDimension(std::pair<SpaceId,SubspaceId> subspace, DimExtent dim_extent)
{
signature_.appendDimension(subspace);
shape_.appendDimension(dim_extent);
return;
}
void Tensor::appendDimension(DimExtent dim_extent)
{
this->appendDimension(std::pair<SpaceId,SubspaceId>{SOME_SPACE,0},dim_extent);
return;
}
std::size_t Tensor::getTensorId() const
{
return reinterpret_cast<std::size_t>(this);
......
/** ExaTN::Numerics: Abstract Tensor
REVISION: 2019/06/05
REVISION: 2019/07/02
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -75,6 +75,8 @@ public:
template<typename T>
Tensor(const std::string & name, //tensor name
const std::vector<T> & extents); //tensor dimension extents
/** Create a rank-0 tensor (scalar). **/
Tensor(const std::string & name); //tensor name
Tensor(const Tensor & tensor) = default;
Tensor & operator=(const Tensor & tensor) = default;
......@@ -102,6 +104,14 @@ public:
SubspaceId getDimSubspaceId(unsigned int dim_id) const;
std::pair<SpaceId,SubspaceId> getDimSpaceAttr(unsigned int dim_id) const;
/** Deletes a specific tensor dimension, reducing the tensor rank by one. **/
void deleteDimension(unsigned int dim_id);
/** Appends a new dimension to the tensor at the end, increasing the tensor rank by one. **/
void appendDimension(std::pair<SpaceId,SubspaceId> subspace,
DimExtent dim_extent);
void appendDimension(DimExtent dim_extent);
/** Get the unique integer tensor id. **/
std::size_t getTensorId() const;
......
/** ExaTN::Numerics: Tensor connected to other tensors inside a tensor network
REVISION: 2019/06/03
REVISION: 2019/07/02
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -7,6 +7,7 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_connected.hpp"
#include <iostream>
#include <assert.h>
namespace exatn{
......@@ -27,6 +28,11 @@ void TensorConn::printIt() const
return;
}
unsigned int TensorConn::getNumLegs() const
{
return tensor_->getRank();
}
std::shared_ptr<Tensor> TensorConn::getTensor()
{
return tensor_;
......@@ -37,7 +43,7 @@ unsigned int TensorConn::getTensorId() const
return id_;
}
TensorLeg TensorConn::getTensorLeg(unsigned int leg_id) const
TensorLeg & TensorConn::getTensorLeg(unsigned int leg_id)
{
assert(leg_id < legs_.size());
return legs_[leg_id];
......@@ -48,6 +54,41 @@ const std::vector<TensorLeg> & TensorConn::getTensorLegs() const
return legs_;
}
DimExtent TensorConn::getDimExtent(unsigned int dim_id) const
{
return tensor_->getDimExtent(dim_id);
}
void TensorConn::resetLeg(unsigned int leg_id, TensorLeg tensor_leg)
{
assert(leg_id < legs_.size());
legs_[leg_id].resetConnection(tensor_leg.getTensorId(),
tensor_leg.getDimensionId(),
tensor_leg.getDirection());
return;
}
void TensorConn::deleteLeg(unsigned int leg_id)
{
assert(leg_id < legs_.size());
legs_.erase(legs_.cbegin()+leg_id);
tensor_->deleteDimension(leg_id);
return;
}
void TensorConn::appendLeg(std::pair<SpaceId,SubspaceId> subspace, DimExtent dim_extent, TensorLeg tensor_leg)
{
tensor_->appendDimension(subspace,dim_extent);
legs_.emplace_back(tensor_leg);
return;
}
void TensorConn::appendLeg(DimExtent dim_extent, TensorLeg tensor_leg)
{
this->appendLeg(std::pair<SpaceId,SubspaceId>{SOME_SPACE,0},dim_extent,tensor_leg);
return;
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor connected to other tensors in a tensor network
REVISION: 2019/06/03
REVISION: 2019/07/02
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -47,6 +47,9 @@ public:
/** Prints. **/
void printIt() const;
/** Returns the total number of legs (tensor rank/order). **/
unsigned int getNumLegs() const;
/** Returns a co-owned pointer to the tensor. **/
std::shared_ptr<Tensor> getTensor();
......@@ -54,11 +57,28 @@ public:
unsigned int getTensorId() const;
/** Returns a specific tensor leg. **/
TensorLeg getTensorLeg(unsigned int leg_id) const;
TensorLeg & getTensorLeg(unsigned int leg_id);
/** Returns all tensor legs. **/
const std::vector<TensorLeg> & getTensorLegs() const;
/** Returns the dimension extent of a specific tensor leg. **/
DimExtent getDimExtent(unsigned int dim_id) const;
/** Resets an existing tensor leg (specific connection to another tensor). **/
void resetLeg(unsigned int leg_id, //in: leg id to reset
TensorLeg tensor_leg); //in: new leg configuration
/** Deletes an existing tensor leg, reducing the tensor rank by one. **/
void deleteLeg(unsigned int leg_id); //in: leg id to delete
/** Appends a new tensor leg as the last leg, increasing the tensor rank by one. **/
void appendLeg(std::pair<SpaceId,SubspaceId> subspace, //in: subspace defining the new leg
DimExtent dim_extent, //in: dimension extent of the new leg
TensorLeg tensor_leg); //in: new leg configuration
void appendLeg(DimExtent dim_extent,
TensorLeg tensor_leg);
private:
std::shared_ptr<Tensor> tensor_; //co-owned pointer to the tensor
......
/** ExaTN::Numerics: Tensor network
REVISION: 2019/05/31
REVISION: 2019/07/02
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_network.hpp"
#include <iostream>
#include <assert.h>
namespace exatn{
namespace numerics{
TensorNetwork::TensorNetwork(const std::string & name):
name_(name)
{
tensors_.emplace(
std::make_pair(
0U,
TensorConn(std::make_shared<Tensor>(name),0U,std::vector<TensorLeg>())
)
);
}
TensorNetwork::TensorNetwork()
{
tensors_.emplace(
std::make_pair(
0U,
TensorConn(std::make_shared<Tensor>("_SMOKY_TENSOR_"),0U,std::vector<TensorLeg>())
)
);
}
void TensorNetwork::printIt() const
{
std::cout << "TensorNetwork[" << name_ << "](" << this->getNumTensors() << "){" << std::endl;
for(const auto & kv: tensors_) kv.second.printIt();
std::cout << "}" << std::endl;
return;
}
unsigned int TensorNetwork::getNumTensors() const
{
return static_cast<unsigned int>(tensors_.size() - 1); //output tensor is not counted
}
} //namespace numerics
......
/** ExaTN::Numerics: Tensor network
REVISION: 2019/05/31
REVISION: 2019/07/02
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -13,9 +13,15 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
(d) A tensor leg can connect a given tensor with one or more
other tensors in the same tensor network. Thus, tensor
legs can be binary, ternary, etc.
(e) A tensor network is always closed, which in some
cases requires introducing an explicit output tensor
collecting all open ends of the original tensor network.
(e) A tensor network is always closed, which requires introducing
an explicit output tensor collecting all open legs of the original
tensor network. If the original tensor network does not have open
legs, the output tensor is simply a scalar which the original tensor
network evaluates to; otherwise, a tensor network evaluates to a tensor.
(f) Tensor enumeration:
0: Output tensor/scalar which the tensor network evaluates to;
1..N: Input tensors/scalars constituting the original tensor network;
N+1..M: Intermediate tensors obtained by contractions of input tensors.
**/
#ifndef EXATN_NUMERICS_TENSOR_NETWORK_HPP_
......@@ -26,6 +32,7 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_op_factory.hpp"
#include <unordered_map>
#include <string>
namespace exatn{
......@@ -34,7 +41,14 @@ namespace numerics{
class TensorNetwork{
public:
TensorNetwork() = default;
static constexpr unsigned int NUM_WALKERS = 1024; //default number of walkers for tensor contraction sequence optimization
using ContractionSequence = std::vector<std::pair<unsigned int, unsigned int>>; //pairs of contracted tensor id's
/** Creates an unnamed empty tensor network with a single scalar output tensor named "_SMOKY_TENSOR_" **/
TensorNetwork();
/** Creates a named empty tensor network with a single scalar output tensor named with the same name. **/
TensorNetwork(const std::string & name);
TensorNetwork(const TensorNetwork &) = default;
TensorNetwork & operator=(const TensorNetwork &) = default;
......@@ -42,11 +56,18 @@ public:
TensorNetwork & operator=(TensorNetwork &&) noexcept = default;
virtual ~TensorNetwork() = default;
/** Prints **/
void printIt() const;
/** Returns the number of input tensors in the tensor network.
Note that the output tensor (tensor #0) is not counted here. **/
unsigned int getNumTensors() const;
private:
std::string name_; //tensor network name
std::unordered_map<unsigned int, TensorConn> tensors_; //tensors connected to each other via legs (tensor connections)
//map: Nonnegative tensor id --> Connected tensor
//map: Non-negative tensor id --> Connected tensor
};
} //namespace numerics
......
/** ExaTN::Numerics: Tensor shape
REVISION: 2019/05/27
REVISION: 2019/07/02
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -8,11 +8,16 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include <iostream>
#include <iterator>
#include <assert.h>
namespace exatn{
namespace numerics{
TensorShape::TensorShape()
{
}
void TensorShape::printIt() const
{
std::cout << "{";
......@@ -43,6 +48,19 @@ const std::vector<DimExtent> & TensorShape::getDimExtents() const
return extents_;
}
void TensorShape::deleteDimension(unsigned int dim_id)
{
assert(dim_id < extents_.size());
extents_.erase(extents_.cbegin()+dim_id);
return;
}
void TensorShape::appendDimension(DimExtent dim_extent)
{
extents_.emplace_back(dim_extent);
return;
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor shape
REVISION: 2019/06/05
REVISION: 2019/07/02
Copyright (C) 2018-2018 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2018 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -33,6 +33,8 @@ public:
TensorShape(std::initializer_list<T> extents);
template<typename T>
TensorShape(const std::vector<T> & extents);
/** Create an empty tensor shape. **/
TensorShape();
TensorShape(const TensorShape & tens_shape) = default;
TensorShape & operator=(const TensorShape & tens_shape) = default;
......@@ -52,6 +54,12 @@ public:
/** Get the extents of all tensor dimensions. **/
const std::vector<DimExtent> & getDimExtents() const;
/** Deletes a specific dimension, reducing the shape rank by one. **/
void deleteDimension(unsigned int dim_id);
/** Appends a new dimension at the end, increasing the shape rank by one. **/
void appendDimension(DimExtent dim_extent);
private:
std::vector<DimExtent> extents_; //tensor dimension extents
......
/** ExaTN::Numerics: Tensor signature
REVISION: 2018/11/16
REVISION: 2019/07/02
Copyright (C) 2018-2018 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2018 Oak Ridge National Laboratory (UT-Battelle) **/
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_signature.hpp"
#include <assert.h>
#include <iostream>
#include <iterator>
#include <assert.h>
namespace exatn{
......@@ -30,6 +29,10 @@ subspaces_(rank,std::pair<SpaceId,SubspaceId>(SOME_SPACE,0))
{
}
TensorSignature::TensorSignature()
{
}
void TensorSignature::printIt() const
{
std::cout << "{";
......@@ -67,6 +70,19 @@ std::pair<SpaceId,SubspaceId> TensorSignature::getDimSpaceAttr(unsigned int dim_
return subspaces_[dim_id];
}
void TensorSignature::deleteDimension(unsigned int dim_id)
{
assert(dim_id < subspaces_.size());
subspaces_.erase(subspaces_.cbegin()+dim_id);
return;
}
void TensorSignature::appendDimension(std::pair<SpaceId,SubspaceId> subspace)
{
subspaces_.emplace_back(subspace);
return;
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor signature
REVISION: 2019/05/27
REVISION: 2019/07/02
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -37,9 +37,10 @@ public:
Case 2: space_id != SOME_SPACE: Then space is registered and subspace_id refers to its registered subspace. **/
TensorSignature(std::initializer_list<std::pair<SpaceId,SubspaceId>> subspaces);
TensorSignature(const std::vector<std::pair<SpaceId,SubspaceId>> & subspaces);
/** Create a default tensor signature of std::pair<SOME_SPACE,0> by providing the tensor rank only. **/
TensorSignature(unsigned int rank);
/** Create an empty tensor signature. **/
TensorSignature();
TensorSignature(const TensorSignature & tens_signature) = default;
TensorSignature & operator=(const TensorSignature & tens_signature) = default;
......@@ -58,6 +59,12 @@ public:
SubspaceId getDimSubspaceId(unsigned int dim_id) const;
std::pair<SpaceId,SubspaceId> getDimSpaceAttr(unsigned int dim_id) const;
/** Deletes a specific subspace, reducing the signature rank by one. **/
void deleteDimension(unsigned int dim_id);
/** Appends a new subspace at the end, increasing the signature rank by one. **/
void appendDimension(std::pair<SpaceId,SubspaceId> subspace = {SOME_SPACE,0});
private:
std::vector<std::pair<SpaceId,SubspaceId>> subspaces_; //tensor signature
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment