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
|
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#include <thread>
#include <atomic>
#include "app.h"
#include "thread.h"
#include "file/config.h"
#include "thread_queue.h"
namespace MR
{
namespace Thread
{
namespace {
size_t __number_of_threads = 0;
nthreads_t __nthreads_type = nthreads_t::UNINITIALISED;
}
size_t number_of_threads ()
{
if (__nthreads_type != nthreads_t::UNINITIALISED)
return __number_of_threads;
auto opt = App::get_options ("nthreads");
if (opt.size()) {
__number_of_threads = opt[0][0];
__nthreads_type = nthreads_t::EXPLICIT;
return __number_of_threads;
}
//ENVVAR name: MRTRIX_NTHREADS
//ENVVAR set the number of threads that MRtrix3 applications should use.
//ENVVAR This overrides the automatically determined number, or the
//ENVVAR :option:`NumberOfThreads` setting in the configuration file, but
//ENVVAR will be overridden by the ENVVAR ``-nthreads`` command-line option.
const char* from_env = getenv ("MRTRIX_NTHREADS");
if (from_env) {
__number_of_threads = to<size_t> (from_env);
__nthreads_type = nthreads_t::EXPLICIT;
return __number_of_threads;
}
//CONF option: NumberOfThreads
//CONF default: number of threads provided by hardware
//CONF Set the default number of CPU threads to use for multi-threading.
if (File::Config::get ("NumberOfThreads").size()) {
const int i = File::Config::get_int ("NumberOfThreads", -1);
if (i >= 0) {
__number_of_threads = i;
__nthreads_type = nthreads_t::EXPLICIT;
return __number_of_threads;
}
}
__number_of_threads = std::thread::hardware_concurrency();
__nthreads_type = nthreads_t::IMPLICIT;
return __number_of_threads;
}
nthreads_t type_nthreads()
{
return __nthreads_type;
}
size_t threads_to_execute()
{
return (__Backend::valid() ? 0 : number_of_threads());
}
void (*__Backend::previous_print_func) (const std::string& msg) = nullptr;
void (*__Backend::previous_report_to_user_func) (const std::string& msg, int type) = nullptr;
__Backend::__Backend () :
refcount (0) {
DEBUG ("initialising threads...");
std::atomic<uint8_t> a;
if (!a.is_lock_free())
WARN ("atomic operations on uint8_t are not lock-free - this may introduce instabilities in operation!");
if (sizeof (a) != sizeof (uint8_t))
WARN ("size of atomic<uint8_t> differs from that of uint8_t - this may introduce instabilities in operation!");
previous_print_func = print;
previous_report_to_user_func = report_to_user_func;
print = thread_print_func;
report_to_user_func = thread_report_to_user_func;
}
__Backend::~__Backend ()
{
print = previous_print_func;
report_to_user_func = previous_report_to_user_func;
}
void __Backend::thread_print_func (const std::string& msg)
{
std::lock_guard<std::mutex> lock (mutex);
previous_print_func (msg);
}
void __Backend::thread_report_to_user_func (const std::string& msg, int type)
{
std::lock_guard<std::mutex> lock (mutex);
previous_report_to_user_func (msg, type);
}
__Backend* __Backend::backend = nullptr;
std::mutex __Backend::mutex;
}
}
|