ExaTN
tensor_graph_executor.hpp
1 
16 #ifndef EXATN_RUNTIME_TENSOR_GRAPH_EXECUTOR_HPP_
17 #define EXATN_RUNTIME_TENSOR_GRAPH_EXECUTOR_HPP_
18 
19 #include "Identifiable.hpp"
20 
21 #include "tensor_graph.hpp"
22 #include "tensor_node_executor.hpp"
23 #include "tensor_operation.hpp"
24 
25 #include <memory>
26 #include <atomic>
27 
28 #include <iostream>
29 #include <fstream>
30 
31 namespace exatn {
32 namespace runtime {
33 
34 class TensorGraphExecutor : public Identifiable, public Cloneable<TensorGraphExecutor> {
35 
36 public:
37 
39  node_executor_(nullptr), logging_(0), stopping_(false), active_(false) {}
40 
41  TensorGraphExecutor(const TensorGraphExecutor &) = delete;
42  TensorGraphExecutor & operator=(const TensorGraphExecutor &) = delete;
43  TensorGraphExecutor(TensorGraphExecutor &&) noexcept = delete;
44  TensorGraphExecutor & operator=(TensorGraphExecutor &&) noexcept = delete;
45 
46  virtual ~TensorGraphExecutor(){
48  }
49 
51  void resetNodeExecutor(std::shared_ptr<TensorNodeExecutor> node_executor) {
52  node_executor_ = node_executor;
53  if(node_executor_) node_executor_->initialize();
54  return;
55  }
56 
58  void resetLoggingLevel(int level = 0) {
59  if(logging_.load() == 0){
60  if(level != 0) logfile_.open("exatn_exec_thread.log", std::ios::out | std::ios::trunc);
61  }else{
62  if(level == 0) logfile_.close();
63  }
64  logging_.store(level);
65  return;
66  }
67 
70  virtual void execute(TensorGraph & dag) = 0;
71 
73  virtual std::shared_ptr<TensorGraphExecutor> clone() = 0;
74 
76  std::shared_ptr<talsh::Tensor> getLocalTensor(const numerics::Tensor & tensor,
77  const std::vector<std::pair<DimOffset,DimExtent>> & slice_spec) {
78  assert(node_executor_);
79  return node_executor_->getLocalTensor(tensor,slice_spec);
80  }
81 
85  void stopExecution() {
86  stopping_.store(true); //this signal will be picked by the execution thread
87  while(active_.load()){}; //once the DAG execution is stopped the execution thread will set active_ to FALSE
88  return;
89  }
90 
91 protected:
92  std::shared_ptr<TensorNodeExecutor> node_executor_; //tensor operation executor
93  std::ofstream logfile_; //logging file stream (output)
94  std::atomic<int> logging_; //logging level (0:none)
95  std::atomic<bool> stopping_; //signal to pause the execution thread
96  std::atomic<bool> active_; //TRUE while the execution thread is executing DAG operations
97 };
98 
99 } //namespace runtime
100 } //namespace exatn
101 
102 #endif //EXATN_RUNTIME_TENSOR_GRAPH_EXECUTOR_HPP_
exatn::numerics::Tensor
Definition: tensor.hpp:63
exatn::runtime::TensorGraphExecutor::clone
virtual std::shared_ptr< TensorGraphExecutor > clone()=0
exatn::runtime::TensorGraphExecutor::resetNodeExecutor
void resetNodeExecutor(std::shared_ptr< TensorNodeExecutor > node_executor)
Definition: tensor_graph_executor.hpp:51
exatn::runtime::TensorGraphExecutor::resetLoggingLevel
void resetLoggingLevel(int level=0)
Definition: tensor_graph_executor.hpp:58
exatn
Definition: DriverClient.hpp:10
exatn::Cloneable
Definition: Identifiable.hpp:18
exatn::runtime::TensorGraphExecutor::getLocalTensor
std::shared_ptr< talsh::Tensor > getLocalTensor(const numerics::Tensor &tensor, const std::vector< std::pair< DimOffset, DimExtent >> &slice_spec)
Definition: tensor_graph_executor.hpp:76
exatn::Identifiable
Definition: Identifiable.hpp:9
exatn::runtime::TensorGraphExecutor::execute
virtual void execute(TensorGraph &dag)=0
exatn::runtime::TensorGraphExecutor
Definition: tensor_graph_executor.hpp:34
exatn::runtime::TensorGraphExecutor::stopExecution
void stopExecution()
Definition: tensor_graph_executor.hpp:85
exatn::runtime::TensorGraph
Definition: tensor_graph.hpp:139