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

Implemented TensorOperation factory.

parent ff996614
......@@ -23,12 +23,7 @@ Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_basic.hpp"
#include "tensor_connected.hpp"
#include "tensor_operation.hpp"
#include "tensor_op_create.hpp"
#include "tensor_op_destroy.hpp"
#include "tensor_op_transform.hpp"
#include "tensor_op_add.hpp"
#include "tensor_op_contract.hpp"
#include "tensor_op_factory.hpp"
#include <unordered_map>
......
......@@ -21,6 +21,11 @@ bool TensorOpAdd::isSet() const
return (this->getNumOperandsSet() == this->getNumOperands() && this->getIndexPattern().length() > 0);
}
TensorOperation * TensorOpAdd::createNew()
{
return new TensorOpAdd();
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor operation: Adds a tensor to another tensor
REVISION: 2019/05/30
REVISION: 2019/05/31
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -32,6 +32,9 @@ public:
/** Returns TRUE iff the tensor operation is fully set. **/
virtual bool isSet() const override;
/** Create a new polymorphic instance of this subclass. **/
static TensorOperation * createNew();
private:
};
......
......@@ -22,6 +22,11 @@ bool TensorOpContract::isSet() const
return (this->getNumOperandsSet() == this->getNumOperands() && this->getIndexPattern().length() > 0);
}
TensorOperation * TensorOpContract::createNew()
{
return new TensorOpContract();
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor operation: Contracts two tensors and accumulates the result into another tensor
REVISION: 2019/05/30
REVISION: 2019/05/31
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -33,6 +33,9 @@ public:
/** Returns TRUE iff the tensor operation is fully set. **/
virtual bool isSet() const override;
/** Create a new polymorphic instance of this subclass. **/
static TensorOperation * createNew();
private:
};
......
......@@ -20,6 +20,11 @@ bool TensorOpCreate::isSet() const
return (this->getNumOperandsSet() == this->getNumOperands());
}
TensorOperation * TensorOpCreate::createNew()
{
return new TensorOpCreate();
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor operation: Creates a tensor
REVISION: 2019/05/30
REVISION: 2019/05/31
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -32,6 +32,9 @@ public:
/** Returns TRUE iff the tensor operation is fully set. **/
virtual bool isSet() const override;
/** Create a new polymorphic instance of this subclass. **/
static TensorOperation * createNew();
private:
};
......
......@@ -20,6 +20,11 @@ bool TensorOpDestroy::isSet() const
return (this->getNumOperandsSet() == this->getNumOperands());
}
TensorOperation * TensorOpDestroy::createNew()
{
return new TensorOpDestroy();
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor operation: Destroys a tensor
REVISION: 2019/05/30
REVISION: 2019/05/31
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -32,6 +32,9 @@ public:
/** Returns TRUE iff the tensor operation is fully set. **/
virtual bool isSet() const override;
/** Create a new polymorphic instance of this subclass. **/
static TensorOperation * createNew();
private:
};
......
/** ExaTN::Numerics: Tensor operation factory
REVISION: 2019/05/31
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
#include "tensor_op_factory.hpp"
namespace exatn{
namespace numerics{
TensorOpFactory::TensorOpFactory()
{
registerTensorOp(TensorOpCode::CREATE,&TensorOpCreate::createNew);
registerTensorOp(TensorOpCode::DESTROY,&TensorOpDestroy::createNew);
registerTensorOp(TensorOpCode::TRANSFORM,&TensorOpTransform::createNew);
registerTensorOp(TensorOpCode::ADD,&TensorOpAdd::createNew);
registerTensorOp(TensorOpCode::CONTRACT,&TensorOpContract::createNew);
}
void TensorOpFactory::registerTensorOp(TensorOpCode opcode, createTensorOpFn creator)
{
factory_map_[opcode] = creator;
return;
}
TensorOperation * TensorOpFactory::createTensorOp(TensorOpCode opcode)
{
auto it = factory_map_.find(opcode);
if(it != factory_map_.end()) return (it->second)();
return nullptr;
}
TensorOpFactory * TensorOpFactory::get()
{
static TensorOpFactory single_instance;
return &single_instance;
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor operation factory
REVISION: 2019/05/31
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
/** Rationale:
(a) Creates new tensor operations of desired kind.
**/
#ifndef EXATN_NUMERICS_TENSOR_OP_FACTORY_HPP_
#define EXATN_NUMERICS_TENSOR_OP_FACTORY_HPP_
#include "tensor_basic.hpp"
#include "tensor_operation.hpp"
#include "tensor_op_create.hpp"
#include "tensor_op_destroy.hpp"
#include "tensor_op_transform.hpp"
#include "tensor_op_add.hpp"
#include "tensor_op_contract.hpp"
#include <memory>
#include <map>
namespace exatn{
namespace numerics{
class TensorOpFactory{
public:
TensorOpFactory(const TensorOpFactory &) = delete;
TensorOpFactory & operator=(const TensorOpFactory &) = delete;
TensorOpFactory(TensorOpFactory &&) noexcept = default;
TensorOpFactory & operator=(TensorOpFactory &&) noexcept = default;
virtual ~TensorOpFactory() = default;
/** Registers a new tensor operation subtype to produce instances of. **/
void registerTensorOp(TensorOpCode opcode, createTensorOpFn creator);
/** Creates a new instance of a desired subtype. **/
TensorOperation * createTensorOp(TensorOpCode opcode);
/** Returns a pointer to the TensorOpFactory singleton. **/
static TensorOpFactory * get();
private:
TensorOpFactory(); //private ctor
std::map<TensorOpCode,createTensorOpFn> factory_map_;
};
} //namespace numerics
} //namespace exatn
#endif //EXATN_NUMERICS_TENSOR_OP_FACTORY_HPP_
......@@ -21,6 +21,11 @@ bool TensorOpTransform::isSet() const
return (this->getNumOperandsSet() == this->getNumOperands());
}
TensorOperation * TensorOpTransform::createNew()
{
return new TensorOpTransform();
}
} //namespace numerics
} //namespace exatn
/** ExaTN::Numerics: Tensor operation: Transforms/initializes a tensor
REVISION: 2019/05/30
REVISION: 2019/05/31
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
......@@ -32,6 +32,9 @@ public:
/** Returns TRUE iff the tensor operation is fully set. **/
virtual bool isSet() const override;
/** Create a new polymorphic instance of this subclass. **/
static TensorOperation * createNew();
private:
};
......
......@@ -84,6 +84,8 @@ protected:
};
using createTensorOpFn = TensorOperation* (*)(void);
} //namespace numerics
} //namespace exatn
......
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