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
|
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2021 Yermalayeu Ihar.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __SimdPerformance_h__
#define __SimdPerformance_h__
#include "Simd/SimdDefs.h"
#include <string>
#include <sstream>
namespace Simd
{
typedef std::string String;
template <class T> SIMD_INLINE String ToStr(const T & value)
{
std::stringstream ss;
ss << value;
return ss.str();
}
}
#if defined(SIMD_PERFORMANCE_STATISTIC) && (defined(NDEBUG) || defined(SIMD_PERF_STAT_IN_DEBUG))
#include "Simd/SimdTime.h"
#include <limits>
#include <iostream>
#include <iomanip>
#include <memory>
#include <map>
#include <thread>
#include <mutex>
#include <algorithm>
namespace Simd
{
namespace Base
{
class PerformanceMeasurer
{
String _name;
int64_t _start, _current, _total, _min, _max;
int64_t _count, _flop;
bool _entered, _paused;
public:
PerformanceMeasurer(const String& name = "Unknown", int64_t flop = 0);
PerformanceMeasurer(const PerformanceMeasurer& pm);
void Enter();
void Leave(bool pause = false);
String Statistic() const;
void Combine(const PerformanceMeasurer& other);
private:
double Average() const;
double GFlops() const;
};
class PerformanceMeasurerHolder
{
PerformanceMeasurer * _pm;
public:
SIMD_INLINE PerformanceMeasurerHolder(PerformanceMeasurer * pm, bool enter = true)
: _pm(pm)
{
if (_pm && enter)
_pm->Enter();
}
SIMD_INLINE void Enter()
{
if (_pm)
_pm->Enter();
}
SIMD_INLINE void Leave(bool pause)
{
if (_pm)
_pm->Leave(pause);
}
SIMD_INLINE ~PerformanceMeasurerHolder()
{
if (_pm)
_pm->Leave();
}
};
class PerformanceMeasurerStorage
{
typedef PerformanceMeasurer Pm;
typedef std::shared_ptr<Pm> PmPtr;
typedef std::map<String, PmPtr> FunctionMap;
typedef std::map<std::thread::id, FunctionMap> ThreadMap;
ThreadMap _map;
mutable std::mutex _mutex;
String _report;
SIMD_INLINE FunctionMap & ThisThread()
{
static thread_local FunctionMap * thread = NULL;
if (thread == NULL)
{
std::lock_guard<std::mutex> lock(_mutex);
thread = &_map[std::this_thread::get_id()];
}
return *thread;
}
public:
static PerformanceMeasurerStorage s_storage;
PerformanceMeasurerStorage()
{
}
SIMD_INLINE PerformanceMeasurer * Get(const String & name, int64_t flop = 0)
{
FunctionMap & thread = ThisThread();
PerformanceMeasurer * pm = NULL;
FunctionMap::iterator it = thread.find(name);
if (it == thread.end())
{
pm = new PerformanceMeasurer(name, flop);
thread[name].reset(pm);
}
else
pm = it->second.get();
return pm;
}
SIMD_INLINE PerformanceMeasurer * Get(const String func, const String & desc, int64_t flop = 0)
{
return Get(func + "{ " + desc + " }", flop);
}
const char* PerformanceStatistic();
};
}
}
#define SIMD_PERF_FUNCF(flop) Simd::Base::PerformanceMeasurerHolder SIMD_CAT(__pmh, __LINE__)(Simd::Base::PerformanceMeasurerStorage::s_storage.Get(SIMD_FUNCTION, (int64_t)(flop)))
#define SIMD_PERF_FUNC() SIMD_PERF_FUNCF(0)
#define SIMD_PERF_BEGF(desc, flop) Simd::Base::PerformanceMeasurerHolder SIMD_CAT(__pmh, __LINE__)(Simd::Base::PerformanceMeasurerStorage::s_storage.Get(SIMD_FUNCTION, desc, (int64_t)(flop)))
#define SIMD_PERF_BEG(desc) SIMD_PERF_BEGF(desc, 0)
#define SIMD_PERF_IFF(cond, desc, flop) Simd::Base::PerformanceMeasurerHolder SIMD_CAT(__pmh, __LINE__)((cond) ? Simd::Base::PerformanceMeasurerStorage::s_storage.Get(SIMD_FUNCTION, desc, (int64_t)(flop)) : NULL)
#define SIMD_PERF_IF(cond, desc) SIMD_PERF_IFF(cond, desc, 0)
#define SIMD_PERF_END(desc) Simd::Base::PerformanceMeasurerStorage::s_storage.Get(SIMD_FUNCTION, desc)->Leave();
#define SIMD_PERF_INITF(name, desc, flop) Simd::Base::PerformanceMeasurerHolder name(Simd::Base::PerformanceMeasurerStorage::s_storage.Get(SIMD_FUNCTION, desc, (int64_t)(flop)), false);
#define SIMD_PERF_INIT(name, desc) SIMD_PERF_INITF(name, desc, 0);
#define SIMD_PERF_START(name) name.Enter();
#define SIMD_PERF_PAUSE(name) name.Leave(true);
#define SIMD_PERF_EXT(ext) Simd::Base::PerformanceMeasurerHolder SIMD_CAT(__pmh, __LINE__)((ext)->Perf(SIMD_FUNCTION))
#else//SIMD_PERFORMANCE_STATISTIC
#define SIMD_PERF_FUNCF(flop)
#define SIMD_PERF_FUNC()
#define SIMD_PERF_BEGF(desc, flop)
#define SIMD_PERF_BEG(desc)
#define SIMD_PERF_IFF(cond, desc, flop)
#define SIMD_PERF_IF(cond, desc)
#define SIMD_PERF_END(desc)
#define SIMD_PERF_INITF(name, desc, flop)
#define SIMD_PERF_INIT(name, desc)
#define SIMD_PERF_START(name)
#define SIMD_PERF_PAUSE(name)
#define SIMD_PERF_EXT(ext)
#endif//SIMD_PERFORMANCE_STATISTIC
#endif//__SimdPerformance_h__
|