-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpd_engine.hpp
263 lines (230 loc) · 8.17 KB
/
dpd_engine.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#ifndef dpd_engine_hpp
#define dpd_engine_hpp
#include "dpd_state.hpp"
#include <memory>
#include <map>
#include <functional>
class DPDEngine
{
protected:
virtual bool CanSupportHookeanBonds() const
{ return false; }
virtual bool CanSupportAngleBonds() const
{ return false; }
virtual bool CanSupportStationaryBeadTypes() const
{ return false; }
virtual bool CanSupportSpatialDPDParameters() const
{ return false; }
virtual bool CanSupportGeneralDPDParameters() const
{ return false; }
virtual bool CanSupportSpatialBondParameters() const
{ return false; }
virtual bool CanSupportGeneralBondParameters() const
{ return false; }
public:
virtual ~DPDEngine()
{}
struct timings_t
{
double compile = -1; // Everything todo with translating the in-memory WorldState into a loadable graph
double aquire = -1; // Any extra time taken to aquire resources
double configure = -1; // Time taken to move loadable graph into hardware
double execute_to_first_bead = -1; // Time taken from start of execution till first bead in first batch
double execute_to_last_bead = -1; // Time taken from start of execution till last bead in last batch
double perf_counters = -1; // Any extra time needed to extract performance counters
};
/* Check the given world and check it can be attached to this engine.
Return empty string if ok, otherwise return the reason it can't
be attached.
Engines are recommended to call this as the base and override
like CanSupportXXX(), so that they can reject things they didn't
know about at time of writing.
*/
virtual std::string CanSupport(const WorldState *s) const
{
std::unordered_set<std::string> space_params{"x","y","z", "ux", "uy", "uz"};
std::unordered_set<std::string> interaction_vars;
for(const auto &ii : s->interactions){
ii.conservative.collect_variables(interaction_vars);
ii.dissipative.collect_variables(interaction_vars);
}
if(!interaction_vars.empty()){
bool space_only=true;
for(auto k : interaction_vars){
if(space_params.find(k)==space_params.end()){
space_only=false;
break;
}
}
if(space_only){
if(!CanSupportSpatialDPDParameters()){
return "Engine does not support space varying DPD parameters.";
}
}else{
if(!CanSupportGeneralDPDParameters()){
return "Engine does not support aribtrary varying DPD parameters.";
}
}
}
for(auto &b : s->bead_types){
if(b.stationary && !CanSupportStationaryBeadTypes()){
return "Engine does not support stationary bead types.";
}
}
std::unordered_set<std::string> bond_vars;
for(const auto &p : s->polymer_types){
if(!p.bonds.empty() && !CanSupportHookeanBonds()){
return "Engine does not support hookean bonds.";
}
if(!p.bond_pairs.empty() && !CanSupportAngleBonds()){
return "Engine does not support angle bonds.";
}
for(auto b : p.bonds){
b.kappa.collect_variables(bond_vars);
b.r0.collect_variables(bond_vars);
}
for(auto bp : p.bond_pairs){
bp.kappa.collect_variables(bond_vars);
bp.theta0.collect_variables(bond_vars);
}
}
if(!bond_vars.empty()){
bool space_only=true;
for(auto k : bond_vars){
if(space_params.find(k)==space_params.end()){
space_only=false;
break;
}
}
if(space_only){
if(!CanSupportSpatialBondParameters()){
return "Engine does not support space varying bond parameters.";
}
}else{
if(!CanSupportGeneralBondParameters()){
return "Engine does not support aribtrary varying bond parameters.";
}
}
}
return "";
}
virtual double GetMaxBondLength() const
{ return 1.0; }
/* If the engine needs to aquire hardware, this tells it to start
aquiring resources in the background (if it can).
*/
virtual void PrepareResources()
{}
/*! If the system supports timings, this will export them and return true. Otherwise it returns false.
Timings are really only intended to support bench-mark style executions, so
something like:
Attach();
Run();
*/
virtual bool GetTimings(timings_t &timings)
{
return false;
}
struct engine_config_entry_t
{
std::string name;
std::string description;
std::string value;
};
/*
Gets a copy of all config values and their current values.
*/
virtual void GetConfigEntries(
std::vector<engine_config_entry_t> &config
) {
config.clear();
}
virtual void SetConfigEntry(const std::string &key, const std::string &value)
{
throw std::runtime_error("Unknown engine config name '"+key+"'");
}
// Attach the given world-state to this engine.
// Any future methods are relative to this state.
// Attach(nullptr) will detach the engine.
// While attached, the engine can assume that all pointers
// are stable (e.g. no vectors moved), the types will all be
// constant, and the bead states will not be changed.
virtual void Attach(WorldState *state) =0;
// Run the worldstate forwards for the given number
// of steps. While executing the engine has exclusive
// read-write access to the beads array mutable properties,
// but it must not re-order or otherwise change the vector.
// An engine can reasonably assume that nSteps will be large
// enough to ammortise any setup costs.
virtual void Run(unsigned nSteps) =0;
// Run the world state for a number of intervals of given
// size, with the callback called at the end of each interval.
// When the callback is invoked the world-state will be valid,
// though execution may still be proceeding in parallel.
// The callback should return true to continue execution, or
// false to terminate early
// Returns the number of steps actually completed.
// If interval_count is -1 then run for ever.
virtual unsigned Run(
int interval_count,
unsigned interval_size,
std::function<bool()> interval_callback
) {
unsigned done=0;
while(1){
if(interval_count==0){
break;
}
Run(interval_size);
done += interval_size;
if(!interval_callback()){
break;
}
if(interval_count>0){
--interval_count;
}
}
return done;
}
};
class DPDEngineFactory
{
public:
using creator_func_t = std::function<std::shared_ptr<DPDEngine>()>;
static std::vector<std::string> ListFactories()
{
auto &m=get_map();
std::vector<std::string> res;
for(const auto &e : m){
res.push_back(e.first);
}
return res;
}
static bool RegisterFactory(const std::string &name, creator_func_t func)
{
auto &m=get_map();
auto it=m.find(name);
if(it!=m.end()){
throw std::runtime_error("Duplicate DPD engines registered as '"+name+"'");
}
m.insert({name,func});
return true;
}
static std::shared_ptr<DPDEngine> create(const std::string &name)
{
auto &m=get_map();
auto it=m.find(name);
if(it==m.end()){
throw std::runtime_error("No DPD engine registered called '"+name+"'");
}
return it->second();
}
private:
using factory_map=std::map<std::string,creator_func_t>;
static factory_map &get_map()
{
static factory_map the_map;
return the_map;
}
};
#endif