performanceData.h 3.46 KB
Newer Older
Ben Huber's avatar
Ben Huber committed
1
// Xerus - A General Purpose Tensor Library
Ben Huber's avatar
Ben Huber committed
2
// Copyright (C) 2014-2019 Benjamin Huber and Sebastian Wolf. 
Ben Huber's avatar
Ben Huber committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 
// Xerus is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// 
// Xerus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with Xerus. If not, see <http://www.gnu.org/licenses/>.
//
// For further information on Xerus visit https://libXerus.org 
// or contact us at contact@libXerus.org.

/**
21 22 23
* @file
* @brief Header file for the PerformanceData class.
*/
Ben Huber's avatar
Ben Huber committed
24 25

#pragma once
26

Ben Huber's avatar
Ben Huber committed
27
#include <string>
28 29
#include <vector>

Ben Huber's avatar
Ben Huber committed
30
#include "misc/timeMeasure.h"
31
#include "misc/histogram.h"
Ben Huber's avatar
Ben Huber committed
32

33 34
#include "basic.h"
#include "tensorNetwork.h"
Ben Huber's avatar
Ben Huber committed
35

36 37 38 39 40
namespace xerus {
	template<bool isOperator> class TTNetwork;
	typedef TTNetwork<false> TTTensor;
	typedef TTNetwork<true> TTOperator;
	
41 42 43 44
	/// @brief Storage class for the performance data collected during an algorithm (typically iteration count, time and residual)
	class PerformanceData {
	public:
		struct DataPoint {
45
			size_t iteration;
46
			size_t elapsedTime;
47
			std::vector<double> residuals;
48
			double error;
49
			size_t dofs;
50
			size_t flags;
51
			
52 53
			DataPoint(const size_t _itrCount, const size_t _time, const std::vector<double>& _residual, const value_t _error, const size_t _dofs, const size_t _flags) 
				: iteration(_itrCount), elapsedTime(_time), residuals(_residual), error(_error), dofs(_dofs), flags(_flags) {}
54
		};
55
		
56 57 58 59
		const bool active;
		
		bool printProgress;
		
60 61
		using ErrorFunction = std::function<double(const TTTensor&)>;
		ErrorFunction errorFunction;
62
		
63 64
		size_t startTime;
		size_t stopTime;
65 66 67 68
		std::vector<DataPoint> data;
		
		std::string additionalInformation;
		
69
		
70
		explicit PerformanceData(const bool _printProgress = false, const bool _active = true);
71
		
72
		explicit PerformanceData(const ErrorFunction& _errorFunction, const bool _printProgress = false, const bool _active = true);
73
		
74
		void start();
75
		
76
		void stop_timer();
77
		
78
		void continue_timer();
79
		
80
		void reset();
81
		
82
		size_t get_elapsed_time() const;
83
		
84
		size_t get_runtime() const;
85
		
86
		void add(const double _residual, const TTTensor& _x, const size_t _flags = 0);
87
		
88
		void add(const std::vector<double>& _residuals, const TTTensor& _x, const size_t _flags = 0);
89
		
90
		void add(const size_t _itrCount, const double _residual, const TTTensor& _x, const size_t _flags = 0);
91
		
92
		void add(const size_t _itrCount, const std::vector<double>& _residuals, const TTTensor& _x, const size_t _flags);
93
		
94
		operator bool() const { return active; }
95 96 97
		
		/// @brief The pipe operator allows to add everything that can be converted to string to the additional information in the header. 
		template<class T>
98
		PerformanceData& operator<<(const T &_info) {
99
			using ::xerus::misc::operator<<;
100
			if (active) {
Ben Huber's avatar
Ben Huber committed
101
				additionalInformation += misc::to_string(_info);
Sebastian Wolf's avatar
Sebastian Wolf committed
102
				if(printProgress) {
103
					XERUS_LOG_SHORT(PerformanceData, _info);
Sebastian Wolf's avatar
Sebastian Wolf committed
104
				}
105 106 107 108 109 110
			}
			return *this;
		}
		
		void dump_to_file(const std::string &_fileName) const;
		
Ben Huber's avatar
Ben Huber committed
111
		misc::LogHistogram get_histogram(const value_t _base, bool _assumeConvergence = false) const;
112
	};
Ben Huber's avatar
Ben Huber committed
113

114
	extern PerformanceData NoPerfData;
115
}