/usr/include/mia-2.4/mia/core/parallelcxx11.hh is in libmia-2.4-dev 2.4.3-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2016 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_core_parallelcxx11_hh
#define mia_core_parallelcxx11_hh
#include <mia/core/defines.hh>
#include <thread>
#include <atomic>
#include <mutex>
#include <cassert>
#include <vector>
NS_MIA_BEGIN
typedef std::mutex CMutex;
typedef std::recursive_mutex CRecursiveMutex;
class EXPORT_CORE CMaxTasks {
public:
static int get_max_tasks();
static void set_max_tasks(int mt);
private:
static int max_tasks;
};
#define ATOMIC std::atomic
template <typename Mutex>
class TScopedLock {
public:
TScopedLock(Mutex& m): m_mutex(m){
m_mutex.lock();
own_lock = true;
};
TScopedLock(const TScopedLock<Mutex>& other) = delete;
TScopedLock& operator = (const TScopedLock<Mutex>& other) = delete;
~TScopedLock(){
if (own_lock)
m_mutex.unlock();
};
void release() {
if (own_lock) {
own_lock = false;
m_mutex.unlock();
}
}
private:
Mutex& m_mutex;
bool own_lock;
};
typedef TScopedLock<CMutex> CScopedLock;
typedef TScopedLock<CRecursiveMutex> CRecursiveScopedLock;
class EXPORT_CORE C1DParallelRange {
public:
C1DParallelRange(int begin, int end, int block = 1):
m_begin(begin),
m_end(end),
m_block(block),
m_current_wp(0)
{
assert(begin <= end);
}
C1DParallelRange(const C1DParallelRange& orig):
m_begin(orig.m_begin),
m_end(orig.m_end),
m_block(orig.m_block)
{
m_current_wp = orig.m_current_wp.load();
}
C1DParallelRange get_next_workpackage() {
int wp = m_current_wp++;
int begin = m_begin + wp * m_block;
int end = begin + m_block;
if (begin > m_end) {
return C1DParallelRange(m_end,m_end,0);
}
if (end > m_end) {
return C1DParallelRange(begin, m_end, 1);
}
return C1DParallelRange(begin, end, 1);
}
bool empty() const {
return m_begin >= m_end;
}
int begin() const {
return m_begin;
}
int end() const {
return m_end;
}
private:
int m_begin;
int m_end;
int m_block;
std::atomic<int> m_current_wp;
};
template <typename Range, typename Func>
void pfor_callback(Range& range, Func f)
{
while (true) {
Range wp = range.get_next_workpackage();
if (!wp.empty())
f(wp);
else
break;
}
}
template <typename Range, typename Func>
void pfor(Range range, Func f) {
int max_treads = CMaxTasks::get_max_tasks();
std::thread::hardware_concurrency();
std::vector<std::thread> threads;
for (int i = 0; i < max_treads; ++i) {
threads.push_back(std::thread(pfor_callback<Range, Func>, std::ref(range), f));
}
for (int i = 0; i < max_treads; ++i) {
threads[i].join();
}
};
template <typename V>
class ReduceValue {
public:
typedef V Value;
ReduceValue(const Value& i):identity(i), value(i) {
}
template <typename Reduce>
void reduce(const Value& v, Reduce r)
{
CScopedLock sl(mutex);
value = r(v, value);
}
const Value& get_identity() const {
return identity;
}
const Value& get_reduced() const {
return value;
}
private:
mutable CMutex mutex;
Value identity;
Value value;
};
template <typename Range, typename Value, typename Func, typename Reduce>
void preduce_callback(Range& range, ReduceValue<Value>& v, Func f, Reduce r)
{
Value value = v.get_identity();
while (true) {
Range wp = range.get_next_workpackage();
if (!wp.empty())
value = f(wp, value);
else
break;
}
v.reduce(value, r);
}
template <typename Range, typename Value, typename Func, typename Reduce>
Value preduce(Range range, Value identity, Func f, Reduce r)
{
int max_treads = CMaxTasks::get_max_tasks();
ReduceValue<Value> value(identity);
std::vector<std::thread> threads;
for (int i = 0; i < max_treads; ++i) {
threads.push_back(std::thread(preduce_callback<Range, Value, Func, Reduce>,
std::ref(range), std::ref(value), f, r));
}
for (int i = 0; i < max_treads; ++i) {
threads[i].join();
}
return value.get_reduced();
};
NS_MIA_END
#endif
|