-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatefulConfig.h
128 lines (102 loc) · 3.81 KB
/
statefulConfig.h
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
#ifndef __STATEFUL_CONFIG__H__
#define __STATEFUL_CONFIG__H__
#include <spdlog/spdlog.h>
#include <stdio.h>
#include <thallium.hpp>
#include <time.h>
#include <unistd.h>
#include <unordered_map>
#define BILLION 1000000000L
namespace tl = thallium;
struct statefulConfig
{
statefulConfig(){
// this->initADIOS();
};
/*
adios does not work well with the multi thread case
void initADIOS()
{ // TODO, update this part
// the adios object is deleted after this function
if (this->Adios == nullptr)
{
this->Adios.reset(new adios2::ADIOS);
}
this->m_io = this->Adios->DeclareIO("gorilla_gs");
// this->m_io.SetEngine("BP4");
// define varaible, it doesn't matter about the start and count since it will be udpated by
// setselection
const std::string variableName = "data";
this->m_io.DefineVariable<double>(
variableName, adios2::Dims{ 512, 512, 512 }, adios2::Dims(), adios2::Dims());
this->m_io.DefineVariable<int>("step");
this->m_engine = m_io.Open("gorilla_gs.bp", adios2::Mode::Write);
// the close engine should be called when every thing (file writing) finish
// std::cout << "---debug adios io name in init: " << this->m_io.Name() << std::endl;
// std::cout << "---debug adios engine type in init: " << this->m_engine.Type() << std::endl;
}
*/
void startTimer(std::string timerName)
{
std::lock_guard<tl::mutex> lck(this->m_timerLock);
if (this->m_timer_map.count(timerName) != 0)
{
throw std::runtime_error("the timer exist with name: " + timerName);
}
struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
this->m_timer_map[timerName] = start;
}
double endTimer(std::string timerName)
{
std::lock_guard<tl::mutex> lck(this->m_timerLock);
if (this->m_timer_map.count(timerName) == 0)
{
throw std::runtime_error("the timer is not initilized with name: " + timerName);
}
struct timespec end;
clock_gettime(CLOCK_REALTIME, &end);
double timespan = (end.tv_sec - this->m_timer_map[timerName].tv_sec) * 1.0 +
(end.tv_nsec - this->m_timer_map[timerName].tv_nsec) * 1.0 / BILLION;
spdlog::debug("timerName {} time span: {}", timerName, timespan);
// delete the timer
this->m_timer_map.erase(timerName);
// return the caculated time
return timespan;
}
// just show the current time - start time
// without delete the timer
double tickTimer(std::string timerName)
{
std::lock_guard<tl::mutex> lck(this->m_timerLock);
if (this->m_timer_map.count(timerName) == 0)
{
throw std::runtime_error("the timer is not initilized with name: " + timerName);
}
struct timespec current;
clock_gettime(CLOCK_REALTIME, ¤t);
double timespan = (current.tv_sec - this->m_timer_map[timerName].tv_sec) * 1.0 +
(current.tv_nsec - this->m_timer_map[timerName].tv_nsec) * 1.0 / BILLION;
std::cout << timerName << " time tick: " << timespan << std::endl;
// return the tick time
return timespan;
}
~statefulConfig(){};
// adios info
// refer to the implementation for this
// https://gitlab.kitware.com/vtk/adis/-/blob/master/adis/DataSource.h
// std::unique_ptr<adios2::ADIOS> Adios = nullptr;
// adios2::IO m_io;
// adios2::Engine m_engine;
// the variable is supposed to defined once per process
// std::unique_ptr<adios2::Variable<double> > var_u = nullptr;
// adios2::Variable<int> var_step;
// this lock is used to avoid the race condition for data write between different thread in one
// process refer to https://github.com/ornladios/ADIOS2/issues/2076#issuecomment-617925292 for
// detials, adios is not fully supported by the multi thread version
// tl::mutex m_adiosLock;
// timer info
tl::mutex m_timerLock;
std::unordered_map<std::string, struct timespec> m_timer_map;
};
#endif