meric.h 3.67 KB
Newer Older
Ondrej Meca's avatar
Ondrej Meca committed
1
2
3
4
5
6

#ifndef SRC_MERIC_SHARED_SHAREDMERIC_H_
#define SRC_MERIC_SHARED_SHAREDMERIC_H_

#include "../meric.h"

7

Ondrej Meca's avatar
Ondrej Meca committed
8
9
10
11
12
namespace meric {

class SharedMeric: public Meric {

public:
xvysoc01's avatar
xvysoc01 committed
13
14
15
	
	void start(const std::string &region)
	{
16
		timespec spectime1, spectime2;
17
		if (OMPbarriers)
18
		{
19
20
			#pragma omp barrier
		}
21
22
		
		#pragma omp single nowait
23
		{
24
			MERIC_INFO << "region '" << region << "' starting\n";
25
			clock_gettime(CLOCK_REALTIME, &spectime1);
Ondrej Vysocky's avatar
Ondrej Vysocky committed
26
27
			SocketsRegionParameters change = regionValue(currentState, region);
			setRegionEnvironment(change);
28
29
			clock_gettime(CLOCK_REALTIME, &spectime2);
			MERIC_INFO << "MERIC settings changed in " << mutils::timeToDouble(spectime2 - spectime1)*1000 << " ms\n";
Ondrej Vysocky's avatar
Ondrej Vysocky committed
30
31
32
		}
		if (Meric::IGNORE)
			return;
33
		#pragma omp single nowait
Ondrej Vysocky's avatar
Ondrej Vysocky committed
34
		{
35
36
37
			if (!ignoreRegion(region))
				Meric::start(region);
			_stack.push_back(getRegionID(region));
38
39
40
41
42
43
			clock_gettime(CLOCK_REALTIME, &spectime1);
			MERIC_INFO << "MERIC status recorded in " << mutils::timeToDouble(spectime1 - spectime2)*1000 << " ms\n";
		}
		if (OMPbarriers)
		{
			#pragma omp barrier
44
		}
xvysoc01's avatar
xvysoc01 committed
45
46
	}
	
47
	double stop()
xvysoc01's avatar
xvysoc01 committed
48
	{
49
		double regionRuntime = 0.0;
50
		if (OMPbarriers)
51
		{
52
53
			#pragma omp barrier
		}
54
55

		#pragma omp single nowait
56
		{
57
			MERIC_INFO << "region '" << getRegionName(_stack.size(), _stack.back()) << "' stopping\n";
58
			SocketsRegionParameters change = regionValue(currentState, getRegionName(_stack.size()-1, _stack.back()-1));
Ondrej Vysocky's avatar
Ondrej Vysocky committed
59
			setRegionEnvironment(change);
Ondrej Vysocky's avatar
Ondrej Vysocky committed
60
61
		}
		if (Meric::IGNORE)
62
			return 0.0;
63
		#pragma omp single nowait
Ondrej Vysocky's avatar
Ondrej Vysocky committed
64
		{
65
66
67
68
69
			if (!ignoreRegion(getRegionName(_stack.size(), _stack.back())))
			{
				if (!_stack.size())
				{
					std::cerr << "MERIC ERROR: Incorrect regions. There are more 'ends' than 'starts'!\n";
70
					regionRuntime = -1.0;
71
				}
Ondrej Vysocky's avatar
Ondrej Vysocky committed
72
73
				else
				{
74
					regionRuntime = Meric::stop();
Ondrej Vysocky's avatar
Ondrej Vysocky committed
75
76
					_stack.pop_back();
				}
77
			}
Ondrej Vysocky's avatar
Ondrej Vysocky committed
78
79
			else
				_stack.pop_back();
80
		}
81
82
83
84
		if (OMPbarriers)
		{
			#pragma omp barrier
		}
85
		return regionRuntime;
xvysoc01's avatar
xvysoc01 committed
86
87
	}
	
88
	double stopStart(const std::string &region)
89
	{
90
		double regionRuntime = 0.0;
91
92
93
94
		if (OMPbarriers)
		{
			#pragma omp barrier
		}
95
96
		
		#pragma omp single nowait
Ondrej Vysocky's avatar
Ondrej Vysocky committed
97
		{
98
			MERIC_INFO << "region '" << getRegionName(_stack.size(), _stack.back()) << "' stopping and staring region'"  << region << "'\n";
Ondrej Vysocky's avatar
Ondrej Vysocky committed
99
100
101
			SocketsRegionParameters change = regionValue(currentState, region);
			setRegionEnvironment(change);
		}
102
103
		
		if (Meric::IGNORE)
104
			return 0.0;
105
106
		if (!_stack.size())
		{
107
			#pragma omp single nowait
108
			std::cerr << "MERIC ERROR: Incorrect regions. There are more 'ends' than 'starts'!\n";
109
			return -1.0;
110
		}
111
		#pragma omp single nowait
112
113
		{
			if (!ignoreRegion(getRegionName(_stack.size(), _stack.back())))
114
				regionRuntime = Meric::stop();
115
116
117
118
119
120
			_stack.pop_back();

			if (!ignoreRegion(region))
				Meric::start(region);
			_stack.push_back(getRegionID(region));
		}
121
122
123
124
		if (OMPbarriers)
		{
			#pragma omp barrier
		}
125
		return regionRuntime;
126
127
	}
	
128
129
130
131
132
133
134
135
136
137
	void startIgnore()
	{
		Meric::startIgnore();
	}
	
	void stopIgnore()
	{
		Meric::stopIgnore();
	}
	
138
139
	void save()
	{
140
141
		stop(); //applicationMainRegion
		
142
143
144
145
		if (MODE == MODE_HDEEM || MODE == MODE_BOTH)
		{
			HDEEM::stop(_stopTime);
		}
146
147
		else if (Meric::MODE == MODE_DAVIDE && Meric::CONTINUAL)
		{
Lubomir Riha's avatar
Lubomir Riha committed
148
149
150
			DAVIDE::stop(_stopTime);
		}

151
		Meric::close();
152
		Environment::setDefaultFrequencies();
153

154
155
156
		if (_stack.size() != 0)
		{
			std::cerr << "MERIC ERROR: not all regions were stopped before the MERIC_Close()\n";
157
			exit(-1);
158
159
		}
		
160
		for(int i=0; i<_storeList.size(); i++)
161
		{
162
163
			_storeList[i]->initStore(Meric::ITERATION, Meric::OUTPUT_DIR, Meric::OUTPUT_FILENAME, Meric::outputFilenameInfo);
			_storeList[i]->save(Meric::OUTPUT_DIR, Meric::OUTPUT_FILENAME, _name);
164
		}
165
166
	}

Ondrej Meca's avatar
Ondrej Meca committed
167
	SharedMeric();
Ondrej Meca's avatar
Ondrej Meca committed
168
	~SharedMeric();
169
private:
Ondrej Vysocky's avatar
Ondrej Vysocky committed
170

Ondrej Meca's avatar
Ondrej Meca committed
171
172
173
174
175
};

}

#endif /* SRC_MERIC_SHARED_SHAREDMERIC_H_ */