ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
watch.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// Created by Eduard Valeyev on 2019-04-05.
4//
5
6#ifndef TTG_WATCH_H
7#define TTG_WATCH_H
8
9#include "ttg/impl_selector.h"
10#include "ttg/util/bug.h"
11
12namespace ttg_madness {
13 // clang-format off
14/*
15 * This allows programmatic control of watchpoints. Requires MADWorld using legacy ThreadPool and macOS. Example:
16 * @code
17 * double x = 0.0;
18 * ::ttg_madness::initialize_watchpoints();
19 * ::ttg_madness::watchpoint_set(&x, ttg::detail::MemoryWatchpoint_x86_64::kWord,
20 * ttg::detail::MemoryWatchpoint_x86_64::kWhenWritten);
21 * x = 1.0; // this will generate SIGTRAP ...
22 * ::ttg_madness::ttg_default_execution_context().taskq.add([&x](){ x = 1.0; }); // and so will this ...
23 * ::ttg_madness::watchpoint_set(&x, ttg::detail::MemoryWatchpoint_x86_64::kWord,
24 * ttg::detail::MemoryWatchpoint_x86_64::kWhenWrittenOrRead);
25 * ::ttg_madness::ttg_default_execution_context().taskq.add([&x](){
26 * std::cout << x << std::endl; }); // and even this!
27 *
28 * @endcode
29 */
30 // clang-format on
31
32 namespace detail {
33 inline const std::vector<const pthread_t *> &watchpoints_threads() {
34 static std::vector<const pthread_t *> threads;
35 // can set watchpoints only with the legacy MADNESS threadpool
36 // TODO improve this when shortsighted MADNESS macro names are strengthened, i.e. HAVE_INTEL_TBB ->
37 // MADNESS_HAS_INTEL_TBB
38 // TODO also exclude the case of a PARSEC-based backend
39#ifndef HAVE_INTEL_TBB
40 if (threads.empty()) {
41 static pthread_t main_thread_id = pthread_self();
42 threads.push_back(&main_thread_id);
43 for (auto t = 0ul; t != ::madness::ThreadPool::size(); ++t) {
44 threads.push_back(&(::madness::ThreadPool::get_threads()[t].get_id()));
45 }
46 }
47#endif
48 return threads;
49 }
50 } // namespace detail
51
53 inline void initialize_watchpoints() {
54#if defined(HAVE_INTEL_TBB)
56 "WARNING: watchpoints are only supported with MADWorld using the legacy threadpool");
57#endif
58#if !defined(__APPLE__)
59 ttg::print_error(ttg::default_execution_context().rank(), "WARNING: watchpoints are only supported on macOS");
60#endif
62 }
63
65 template <typename T>
68 const auto &threads = detail::watchpoints_threads();
69 for (auto t : threads) ttg::detail::MemoryWatchpoint_x86_64::Pool::instance()->set(addr, size, cond, t);
70 }
71
73 template <typename T>
74 inline void watchpoint_clear(T *addr) {
75 const auto &threads = detail::watchpoints_threads();
76 for (auto t : threads) ttg::detail::MemoryWatchpoint_x86_64::Pool::instance()->clear(addr, t);
77 }
78
79} // namespace ttg_madness
80#endif // TTG_WATCH_H
static std::shared_ptr< Pool > instance()
accesses the unique pool; asserts that the default instance has been initialized by calling initializ...
Definition bug.h:95
static void initialize_instance(const std::vector< const pthread_t * > &threads)
Definition bug.h:90
const std::vector< const pthread_t * > & watchpoints_threads()
Definition watch.h:33
this contains MADNESS-based TTG functionality
Definition fwd.h:17
void initialize_watchpoints()
must be called from main thread before setting watchpoints
Definition watch.h:53
void watchpoint_clear(T *addr)
clears the hardware watchpoint for window [addr,addr+size) previously created with watchpoint_set<T>
Definition watch.h:74
void watchpoint_set(T *addr, ttg::detail::MemoryWatchpoint_x86_64::Size size, ttg::detail::MemoryWatchpoint_x86_64::Condition cond)
sets a hardware watchpoint for window [addr,addr+size) and condition cond
Definition watch.h:66
World default_execution_context()
Accesses the default backend's default execution context.
Definition run.h:110
void print_error(const T &t, const Ts &... ts)
atomically prints to std::cerr a sequence of items (separated by ttg::print_separator) followed by st...
Definition print.h:139