Commit 60f04e4e authored by Dmitry I. Lyakh's avatar Dmitry I. Lyakh

Added empty heuristic tensor network contraction sequence optimizer.

Added constant iterator to TensorNetwork.
parent e8bdb1f9
......@@ -22,6 +22,7 @@ add_library(${LIBRARY_NAME}
network_builder_mps.cpp
network_build_factory.cpp
contraction_seq_optimizer_dummy.cpp
contraction_seq_optimizer_heuro.cpp
contraction_seq_optimizer_factory.cpp
tensor_network.cpp)
......
/** ExaTN::Numerics: Tensor contraction sequence optimizer factory
REVISION: 2019/09/05
REVISION: 2019/09/09
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -13,6 +13,7 @@ namespace numerics{
ContractionSeqOptimizerFactory::ContractionSeqOptimizerFactory()
{
registerContractionSeqOptimizer("dummy",&ContractionSeqOptimizerDummy::createNew);
registerContractionSeqOptimizer("heuro",&ContractionSeqOptimizerHeuro::createNew);
}
void ContractionSeqOptimizerFactory::registerContractionSeqOptimizer(const std::string & name,
......
/** ExaTN::Numerics: Tensor contraction sequence optimizer factory
REVISION: 2019/09/05
REVISION: 2019/09/09
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -14,6 +14,7 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_basic.hpp"
#include "contraction_seq_optimizer.hpp"
#include "contraction_seq_optimizer_dummy.hpp"
#include "contraction_seq_optimizer_heuro.hpp"
#include <string>
#include <memory>
......
/** ExaTN::Numerics: Tensor contraction sequence optimizer: Heuristics
REVISION: 2019/09/09
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "contraction_seq_optimizer_heuro.hpp"
#include "tensor_network.hpp"
namespace exatn{
namespace numerics{
static constexpr unsigned int NUM_WALKERS = 1024; //default number of walkers for tensor contraction sequence optimization
double ContractionSeqOptimizerHeuro::determineContractionSequence(const TensorNetwork & network,
std::list<ContrTriple> & contr_seq,
unsigned int intermediate_num_begin)
{
contr_seq.clear();
double flops = 0.0;
//`Finish
return flops;
}
std::unique_ptr<ContractionSeqOptimizer> ContractionSeqOptimizerHeuro::createNew()
{
return std::unique_ptr<ContractionSeqOptimizer>(new ContractionSeqOptimizerHeuro());
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor contraction sequence optimizer: Heuristics
REVISION: 2019/09/09
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
/** Rationale:
**/
#ifndef EXATN_NUMERICS_CONTRACTION_SEQ_OPTIMIZER_HEURO_HPP_
#define EXATN_NUMERICS_CONTRACTION_SEQ_OPTIMIZER_HEURO_HPP_
#include "contraction_seq_optimizer.hpp"
namespace exatn{
namespace numerics{
class ContractionSeqOptimizerHeuro: public ContractionSeqOptimizer{
public:
virtual double determineContractionSequence(const TensorNetwork & network,
std::list<ContrTriple> & contr_seq,
unsigned int intermediate_num_begin) override;
static std::unique_ptr<ContractionSeqOptimizer> createNew();
};
} //namespace numerics
} //namespace exatn
#endif //EXATN_NUMERICS_CONTRACTION_SEQ_OPTIMIZER_HEURO_HPP_
/** ExaTN::Numerics: Tensor network
REVISION: 2019/09/08
REVISION: 2019/09/09
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -481,6 +481,37 @@ bool TensorNetwork::appendTensor(unsigned int tensor_id,
}
bool TensorNetwork::appendTensorGate(unsigned int tensor_id,
std::shared_ptr<Tensor> tensor,
const std::vector<unsigned int> & pairing)
{
if(finalized_ == 0){
std::cout << "#ERROR(TensorNetwork::appendTensorGate): Invalid request: " <<
"Appending a tensor gate to an unfinalized tensor network is forbidden!" << std::endl;
return false;
}
if(tensor_id == 0){
std::cout << "#ERROR(TensorNetwork::appendTensorGate): Invalid request: " <<
"Tensor 0 (output tensor) must already be present in the tensor network!" << std::endl;
return false;
}
//Check validity of leg pairing:
auto tensor_rank = tensor->getRank();
if((tensor_rank % 2) != 0){
std::cout << "#ERROR(TensorNetwork::appendTensorGate): Invalid argument: Odd-rank tensors are not allowed as gates!" << std::endl;
return false;
}
if(tensor_rank != (pairing.size() * 2)){
std::cout << "#ERROR(TensorNetwork::appendTensorGate): Invalid argument: Wrong size of the leg pairing vector!" << std::endl;
return false;
}
//`Finish
invalidateContractionSequence(); //invalidate previously cached tensor contraction sequence
finalized_ = 1; //implicit leg pairing always keeps the tensor network in a finalized state
return true;
}
bool TensorNetwork::appendTensorNetwork(TensorNetwork && network, //in: appended tensor network
const std::vector<std::pair<unsigned int, unsigned int>> & pairing) //in: leg pairing: output tensor mode (primary) -> output tensor mode (appended)
{
......@@ -752,7 +783,7 @@ bool TensorNetwork::mergeTensors(unsigned int left_id, unsigned int right_id, un
bool TensorNetwork::splitTensor(unsigned int tensor_id,
const TensorShape & contracted_dims,
const std::vector<bool> & left_dims)
const std::vector<int> & right_dims)
{
if(tensor_id == 0){
std::cout << "#ERROR(TensorNetwork::splitTensor): Invalid request: " <<
......
/** ExaTN::Numerics: Tensor network
REVISION: 2019/09/08
REVISION: 2019/09/09
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -72,10 +72,9 @@ namespace numerics{
class TensorNetwork{
public:
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
using Iterator = typename std::unordered_map<unsigned int,TensorConn>::iterator; //iterator
using Iterator = typename std::unordered_map<unsigned int, TensorConn>::iterator; //iterator
using ConstIterator = typename std::unordered_map<unsigned int, TensorConn>::const_iterator; //constant iterator
/** Creates an unnamed empty tensor network with a single scalar output tensor named "_SMOKY_TENSOR_" **/
TensorNetwork();
......@@ -127,6 +126,10 @@ public:
inline Iterator begin() {return tensors_.begin();}
/** End iterator **/
inline Iterator end() {return tensors_.end();}
/** Begin constant iterator **/
inline ConstIterator cbegin() {return tensors_.cbegin();}
/** End constant iterator **/
inline ConstIterator cend() {return tensors_.cend();}
/** Finalizes the explicit construction of the tensor network (construction with advance knowledge).
The tensor network cannot be empty. **/
......@@ -154,6 +157,13 @@ public:
const std::vector<std::pair<unsigned int, unsigned int>> & pairing, //in: leg pairing: output tensor mode -> appended tensor mode
const std::vector<LegDirection> & leg_dir = std::vector<LegDirection>{}); //in: optional leg directions (for all tensor modes)
/** Appends a new even-rank tensor to the tensor network by matching the first half
of the tensor legs with network's output legs provided in "pairing".
The second half of the tensor legs will then replace the matched output legs. **/
bool appendTensorGate(unsigned int tensor_id, //in: appended tensor id (unique within the tensor network)
std::shared_ptr<Tensor> tensor, //in: appended tensor
const std::vector<unsigned int> & pairing); //in: leg pairing: output tensor modes (half-rank)
/** Appends a tensor network to the current tensor network by matching the modes
of the output tensors of both tensor networks. The unmatched modes of the
output tensor of the appended tensor network will be appended to the updated output
......@@ -189,7 +199,7 @@ public:
right tensor. The new dimensions are then appended at the end. **/
bool splitTensor(unsigned int tensor_id, //in: id of the tensor to be split into two tensors
const TensorShape & contracted_dims, //in: dimension extents of the contracted (new) dimensions connecting two tensors after splitting
const std::vector<bool> & left_dims); //in: assignment of original tensor dimensions to new tensors (true: belongs to left, false: belongs to right tensor)
const std::vector<int> & right_dims); //in: assignment of original tensor dimensions to new tensors (0: left, 1: right tensor)
/** Returns the FMA flop count for a given contraction of two tensors identified by their ids
in the tensor network. Optionally returns the arithmetic intensity of the tensor contraction as well.
......
Markdown is supported
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