Commit 1f79491f authored by Dmitry I. Lyakh's avatar Dmitry I. Lyakh

Made TensorShape, TensorSignature, and Tensor byte-packable.

parent e6fef3be
/** ExaTN: Packable interface
REVISION: 2020/06/25
Copyright (C) 2018-2020 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
#ifndef EXATN_PACKABLE_HPP_
#define EXATN_PACKABLE_HPP_
#include "byte_packet.h"
namespace exatn {
class Packable {
public:
/** Packs the object into a plain byte packet. **/
virtual void pack(BytePacket & byte_packet) const = 0;
/** Unpacks the object from a plain byte packet. **/
virtual void unpack(BytePacket & byte_packet) = 0;
};
} //namespace exatn
#endif //EXATN_PACKABLE_HPP_
/** ExaTN::Numerics: Tensor
REVISION: 2020/05/22
REVISION: 2020/06/25
Copyright (C) 2018-2020 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -112,6 +112,46 @@ Tensor::Tensor(const Tensor & another,
}
}
void Tensor::pack(BytePacket & byte_packet) const
{
const std::size_t name_len = name_.length();
appendToBytePacket(&byte_packet,name_len);
for(std::size_t i = 0; i < name_len; ++i) appendToBytePacket(&byte_packet,name_[i]);
shape_.pack(byte_packet);
signature_.pack(byte_packet);
appendToBytePacket(&byte_packet,element_type_);
const std::size_t num_isometries = isometries_.size();
appendToBytePacket(&byte_packet,num_isometries);
for(const auto & isometry: isometries_){
const std::size_t num_vertices = isometry.size();
appendToBytePacket(&byte_packet,num_vertices);
for(const auto & vertex_id: isometry) appendToBytePacket(&byte_packet,vertex_id);
}
return;
}
void Tensor::unpack(BytePacket & byte_packet)
{
std::size_t name_len = 0;
extractFromBytePacket(&byte_packet,name_len);
name_.resize(name_len);
for(std::size_t i = 0; i < name_len; ++i) extractFromBytePacket(&byte_packet,name_[i]);
shape_.unpack(byte_packet);
signature_.unpack(byte_packet);
extractFromBytePacket(&byte_packet,element_type_);
isometries_.clear();
std::size_t num_isometries = 0;
extractFromBytePacket(&byte_packet,num_isometries);
isometries_.resize(num_isometries);
for(auto & isometry: isometries_){
std::size_t num_vertices = 0;
extractFromBytePacket(&byte_packet,num_vertices);
isometry.resize(num_vertices);
for(auto & vertex_id: isometry) extractFromBytePacket(&byte_packet,vertex_id);
}
return;
}
void Tensor::printIt(bool with_hash) const
{
if(!with_hash){
......
/** ExaTN::Numerics: Abstract Tensor
REVISION: 2020/05/22
REVISION: 2020/06/25
Copyright (C) 2018-2020 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -40,6 +40,7 @@ Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
#define EXATN_NUMERICS_TENSOR_HPP_
#include "tensor_basic.hpp"
#include "packable.hpp"
#include "tensor_shape.hpp"
#include "tensor_signature.hpp"
#include "tensor_leg.hpp"
......@@ -61,7 +62,7 @@ namespace numerics{
using TensorHashType = std::size_t;
class Tensor{
class Tensor: public Packable {
public:
/** Create a tensor by providing its name, shape and signature. **/
......@@ -114,6 +115,9 @@ public:
Tensor & operator=(Tensor && tensor) noexcept = default;
virtual ~Tensor() = default;
virtual void pack(BytePacket & byte_packet) const override;
virtual void unpack(BytePacket & byte_packet) override;
/** Print. **/
void printIt(bool with_hash = false) const;
void printItFile(std::ofstream & output_file,
......
/** ExaTN::Numerics: Tensor shape
REVISION: 2019/12/08
REVISION: 2020/06/25
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
Copyright (C) 2018-2020 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_shape.hpp"
......@@ -27,6 +27,23 @@ TensorShape::TensorShape(const TensorShape & another,
for(unsigned int new_pos = 0; new_pos < rank; ++new_pos) extents_[new_pos] = orig[order[new_pos]];
}
void TensorShape::pack(BytePacket & byte_packet) const
{
const std::size_t tensor_rank = extents_.size();
appendToBytePacket(&byte_packet,tensor_rank);
for(const auto & extent: extents_) appendToBytePacket(&byte_packet,extent);
return;
}
void TensorShape::unpack(BytePacket & byte_packet)
{
std::size_t tensor_rank = 0;
extractFromBytePacket(&byte_packet,tensor_rank);
extents_.resize(tensor_rank);
for(auto & extent: extents_) extractFromBytePacket(&byte_packet,extent);
return;
}
void TensorShape::printIt() const
{
std::cout << "{";
......
/** ExaTN::Numerics: Tensor shape
REVISION: 2019/12/08
REVISION: 2020/06/25
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
Copyright (C) 2018-2020 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
/** Rationale:
(a) Tensor shape is an ordered set of tensor dimension extents.
......@@ -13,6 +13,7 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#define EXATN_NUMERICS_TENSOR_SHAPE_HPP_
#include "tensor_basic.hpp"
#include "packable.hpp"
#include <iostream>
#include <fstream>
......@@ -26,7 +27,7 @@ namespace exatn{
namespace numerics{
class TensorShape{
class TensorShape: public Packable {
public:
/** Create an empty tensor shape. **/
......@@ -48,6 +49,9 @@ public:
TensorShape & operator=(TensorShape &&) noexcept = default;
virtual ~TensorShape() = default;
virtual void pack(BytePacket & byte_packet) const override;
virtual void unpack(BytePacket & byte_packet) override;
/** Print. **/
void printIt() const;
void printItFile(std::ofstream & output_file) const;
......
/** ExaTN::Numerics: Tensor signature
REVISION: 2019/11/12
REVISION: 2020/06/25
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
Copyright (C) 2018-2020 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_signature.hpp"
......@@ -42,6 +42,23 @@ TensorSignature::TensorSignature(const TensorSignature & another,
for(unsigned int new_pos = 0; new_pos < rank; ++new_pos) subspaces_[new_pos] = orig[order[new_pos]];
}
void TensorSignature::pack(BytePacket & byte_packet) const
{
const std::size_t tensor_rank = subspaces_.size();
appendToBytePacket(&byte_packet,tensor_rank);
for(const auto & subspace: subspaces_) appendToBytePacket(&byte_packet,subspace);
return;
}
void TensorSignature::unpack(BytePacket & byte_packet)
{
std::size_t tensor_rank = 0;
extractFromBytePacket(&byte_packet,tensor_rank);
subspaces_.resize(tensor_rank);
for(auto & subspace: subspaces_) extractFromBytePacket(&byte_packet,subspace);
return;
}
void TensorSignature::printIt() const
{
std::cout << "{";
......
/** ExaTN::Numerics: Tensor signature
REVISION: 2019/11/12
REVISION: 2020/06/25
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
Copyright (C) 2018-2020 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2020 Oak Ridge National Laboratory (UT-Battelle) **/
/** Rationale:
(a) Tensor signature is an ordered set of tensor dimension specifiers,
......@@ -19,6 +19,7 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#define EXATN_NUMERICS_TENSOR_SIGNATURE_HPP_
#include "tensor_basic.hpp"
#include "packable.hpp"
#include "spaces.hpp"
#include <utility>
......@@ -32,7 +33,7 @@ namespace exatn{
namespace numerics{
class TensorSignature{
class TensorSignature: public Packable {
public:
/** Create an empty tensor signature. **/
......@@ -57,6 +58,9 @@ public:
TensorSignature & operator=(TensorSignature &&) noexcept = default;
virtual ~TensorSignature() = default;
virtual void pack(BytePacket & byte_packet) const override;
virtual void unpack(BytePacket & byte_packet) override;
/** Print. **/
void printIt() const;
void printItFile(std::ofstream & output_file) const;
......
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