mirror of
https://github.com/NGSolve/netgen.git
synced 2025-01-12 22:20:35 +05:00
Merge branch 'export_taskmanager' into 'master'
Export TaskManager See merge request jschoeberl/netgen!232
This commit is contained in:
commit
4c1f24d313
@ -15,12 +15,14 @@ namespace ngcore
|
|||||||
{
|
{
|
||||||
std::ostream* testout = new std::ostream(nullptr); // NOLINT
|
std::ostream* testout = new std::ostream(nullptr); // NOLINT
|
||||||
|
|
||||||
|
level::level_enum Logger::global_level;
|
||||||
|
|
||||||
void Logger::log(level::level_enum level, std::string && s)
|
void Logger::log(level::level_enum level, std::string && s)
|
||||||
{
|
{
|
||||||
#ifdef NETGEN_USE_SPDLOG
|
#ifdef NETGEN_USE_SPDLOG
|
||||||
logger->log(spdlog::level::level_enum(level), s);
|
logger->log(spdlog::level::level_enum(level), s);
|
||||||
#else // NETGEN_USE_SPDLOG
|
#else // NETGEN_USE_SPDLOG
|
||||||
if(level>level::debug)
|
if(level>=global_level)
|
||||||
std::clog << s << '\n';
|
std::clog << s << '\n';
|
||||||
#endif // NETGEN_USE_SPDLOG
|
#endif // NETGEN_USE_SPDLOG
|
||||||
}
|
}
|
||||||
@ -126,7 +128,11 @@ namespace ngcore
|
|||||||
return std::make_shared<Logger>(std::make_shared<spdlog::logger>());
|
return std::make_shared<Logger>(std::make_shared<spdlog::logger>());
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetLoggingLevel(level::level_enum /*unused*/, const std::string& /*unused*/) {}
|
void SetLoggingLevel(level::level_enum level, const std::string& /*unused*/)
|
||||||
|
{
|
||||||
|
Logger::SetGlobalLoggingLevel(level);
|
||||||
|
}
|
||||||
|
|
||||||
void AddFileSink(const std::string& /*unused*/, level::level_enum /*unused*/,
|
void AddFileSink(const std::string& /*unused*/, level::level_enum /*unused*/,
|
||||||
const std::string& /*unused*/)
|
const std::string& /*unused*/)
|
||||||
{}
|
{}
|
||||||
|
@ -49,7 +49,11 @@ namespace ngcore
|
|||||||
|
|
||||||
class Logger
|
class Logger
|
||||||
{
|
{
|
||||||
|
static NGCORE_API level::level_enum global_level;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static void SetGlobalLoggingLevel( level::level_enum level ) { global_level = level; }
|
||||||
|
|
||||||
std::shared_ptr<spdlog::logger> logger;
|
std::shared_ptr<spdlog::logger> logger;
|
||||||
|
|
||||||
Logger(std::shared_ptr<spdlog::logger> l) : logger(std::move(l)) {}
|
Logger(std::shared_ptr<spdlog::logger> l) : logger(std::move(l)) {}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#include "python_ngcore.hpp"
|
#include "python_ngcore.hpp"
|
||||||
#include "bitarray.hpp"
|
#include "bitarray.hpp"
|
||||||
|
#include "taskmanager.hpp"
|
||||||
|
|
||||||
|
|
||||||
using namespace ngcore;
|
using namespace ngcore;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace pybind11::literals;
|
||||||
|
|
||||||
PYBIND11_MODULE(pyngcore, m) // NOLINT
|
PYBIND11_MODULE(pyngcore, m) // NOLINT
|
||||||
{
|
{
|
||||||
@ -167,4 +169,52 @@ PYBIND11_MODULE(pyngcore, m) // NOLINT
|
|||||||
"Clear sinks of specific logger, or all if none given");
|
"Clear sinks of specific logger, or all if none given");
|
||||||
m.def("FlushOnLoggingLevel", &FlushOnLoggingLevel, py::arg("level"), py::arg("logger")="",
|
m.def("FlushOnLoggingLevel", &FlushOnLoggingLevel, py::arg("level"), py::arg("logger")="",
|
||||||
"Flush every message with level at least `level` for specific logger or all loggers if none given.");
|
"Flush every message with level at least `level` for specific logger or all loggers if none given.");
|
||||||
|
|
||||||
|
m.def("RunWithTaskManager",
|
||||||
|
[](py::object lam)
|
||||||
|
{
|
||||||
|
GetLogger("TaskManager")->info("running Python function with task-manager");
|
||||||
|
RunWithTaskManager ([&] () { lam(); });
|
||||||
|
}, py::arg("lam"), R"raw_string(
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
lam : object
|
||||||
|
input function
|
||||||
|
|
||||||
|
)raw_string")
|
||||||
|
;
|
||||||
|
|
||||||
|
m.def("SetNumThreads", &TaskManager::SetNumThreads, py::arg("threads"), R"raw_string(
|
||||||
|
Set number of threads
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
threads : int
|
||||||
|
input number of threads
|
||||||
|
|
||||||
|
)raw_string");
|
||||||
|
|
||||||
|
// local TaskManager class to be used as context manager in Python
|
||||||
|
class ParallelContextManager {
|
||||||
|
int num_threads;
|
||||||
|
public:
|
||||||
|
ParallelContextManager() : num_threads(0) {};
|
||||||
|
ParallelContextManager(size_t pajesize) : num_threads(0) {
|
||||||
|
TaskManager::SetPajeTrace(pajesize > 0);
|
||||||
|
PajeTrace::SetMaxTracefileSize(pajesize);
|
||||||
|
}
|
||||||
|
void Enter() {num_threads = EnterTaskManager(); }
|
||||||
|
void Exit(py::object exc_type, py::object exc_value, py::object traceback) {
|
||||||
|
ExitTaskManager(num_threads);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
py::class_<ParallelContextManager>(m, "TaskManager")
|
||||||
|
.def(py::init<>())
|
||||||
|
.def(py::init<size_t>(), "pajetrace"_a, "Run paje-tracer, specify buffersize in bytes")
|
||||||
|
.def("__enter__", &ParallelContextManager::Enter)
|
||||||
|
.def("__exit__", &ParallelContextManager::Exit)
|
||||||
|
.def("__timing__", &TaskManager::Timing)
|
||||||
|
;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -74,8 +74,7 @@ namespace ngcore
|
|||||||
|
|
||||||
task_manager = new TaskManager();
|
task_manager = new TaskManager();
|
||||||
|
|
||||||
// TODO: use logger for output
|
GetLogger("TaskManager")->info("task-based parallelization (C++11 threads) using {} threads", task_manager->GetNumThreads());
|
||||||
std::cout << "task-based parallelization (C++11 threads) using "<< task_manager->GetNumThreads() << " threads" << std::endl;
|
|
||||||
|
|
||||||
#ifdef USE_NUMA
|
#ifdef USE_NUMA
|
||||||
numa_run_on_node (0);
|
numa_run_on_node (0);
|
||||||
|
Loading…
Reference in New Issue
Block a user