Pteros  2.0
Molecular modeling library for human beings!
task_base.h
1 /*
2  * This file is a part of
3  *
4  * ============================================
5  * ### Pteros molecular modeling library ###
6  * ============================================
7  *
8  * https://github.com/yesint/pteros
9  *
10  * (C) 2009-2021, Semen Yesylevskyy
11  *
12  * All works, which use Pteros, should cite the following papers:
13  *
14  * 1. Semen O. Yesylevskyy, "Pteros 2.0: Evolution of the fast parallel
15  * molecular analysis library for C++ and python",
16  * Journal of Computational Chemistry, 2015, 36(19), 1480–1488.
17  * doi: 10.1002/jcc.23943.
18  *
19  * 2. Semen O. Yesylevskyy, "Pteros: Fast and easy to use open-source C++
20  * library for molecular analysis",
21  * Journal of Computational Chemistry, 2012, 33(19), 1632–1636.
22  * doi: 10.1002/jcc.22989.
23  *
24  * This is free software distributed under Artistic License:
25  * http://www.opensource.org/licenses/artistic-license-2.0.php
26  *
27 */
28 
29 
30 #pragma once
31 
32 #include "pteros/core/system.h"
33 #include "pteros/analysis/frame_info.h"
34 #include <spdlog/spdlog.h>
35 
36 // Forward declaration of the message channel
37 template<class T> class MessageChannel;
38 
39 namespace pteros {
40 
41 // Forward declarations
42 class DataContainer;
43 class TaskDriver;
44 
45 
46 class TaskBase {
47  friend class TaskDriver;
48  friend class TrajectoryReader;
49 
50 public:
51  TaskBase();
52  TaskBase(const TaskBase& other);
53  virtual ~TaskBase(){}
54  virtual TaskBase* clone() const = 0;
55 
56  int get_id(){ return task_id; }
57 
58  System system;
59 
60  std::shared_ptr<spdlog::logger> log;
61 
62  virtual void pre_process() = 0;
63  virtual void process_frame(const FrameInfo& info) = 0;
64  virtual void post_process(const FrameInfo& info) = 0;
65 
66  // Default implementation of collector for parallel tasks
67  virtual void collect_data(const std::vector<std::shared_ptr<TaskBase>>& tasks, int n_frames){}
68 
69  // Default implementation of global preprocess for parallel tasks
70  virtual void before_spawn(){}
71 
72 protected:
73  virtual void set_id(int _id){ task_id = _id; }
74 
75  virtual bool is_parallel() = 0;
76 
77  // Handlers, which call actual functions
78  // Could be overriden in subclasses
79  virtual void before_spawn_handler(){
80  before_spawn();
81  }
82 
83  virtual void pre_process_handler(){
84  pre_process();
85  }
86 
87  virtual void process_frame_handler(const FrameInfo& info){
88  process_frame(info);
89  }
90 
91  virtual void post_process_handler(const FrameInfo& info){
92  post_process(info);
93  }
94 
95  int task_id;
96  int n_consumed;
97 
98 private:
99 
100  void put_frame(const Frame& frame);
101  void put_system(const System& sys);
102 
103  std::shared_ptr<TaskDriver> driver;
104 };
105 
106 }