Geogram Version 1.8.5
A programming library of geometric algorithms
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GEOGRAM_BASIC_PROCESS
41#define GEOGRAM_BASIC_PROCESS
42
47#include <functional>
48
54namespace GEO {
55
56// thread_local is supposed to be supported by c++0x,
57// but some old MSVC compilers do not have it.
58#if defined(GEO_COMPILER_MSVC) && !defined(thread_local)
59# define thread_local __declspec(thread)
60#endif
61
62// Older MAC OS X do not have thread_local
63#ifdef GEO_OS_APPLE
64# if defined(TARGET_OS_OSX) && MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_9
65# define thread_local
66# define GEO_NO_THREAD_LOCAL
67# endif
68#endif
69
80 class GEOGRAM_API Thread : public Counted {
81 public:
82
86 Thread() : id_(0) {
87 }
88
92 virtual void run() = 0;
93
101 index_t id() const {
102 return id_;
103 }
104
110 static Thread* current();
111
112
113 protected:
115 ~Thread() override;
116
117
118 private:
125 void set_id(index_t id_in) {
126 id_ = id_in;
127 }
128
137 static void set_current(Thread* thread);
138
139 index_t id_;
140
141 // ThreadManager needs to access set_current() and
142 // set_id().
143 friend class ThreadManager;
144 };
145
148
157 typedef std::vector<Thread_var> ThreadGroup;
158
166 template <class THREAD>
168 public:
176
183 THREAD* operator[] (index_t i) {
184 geo_debug_assert(i < size());
185 Thread* result = ThreadGroup::operator[] (i);
186 return static_cast<THREAD*>(result);
187 }
188 };
189
215 class GEOGRAM_API ThreadManager : public Counted {
216 public:
235 virtual void run_threads(ThreadGroup& threads);
236
246
255 virtual void enter_critical_section() = 0;
256
263 virtual void leave_critical_section() = 0;
264
265 protected:
277 ThreadGroup& threads, index_t max_threads
278 ) = 0;
279
280
289 static void set_thread_id(Thread* thread, index_t id) {
290 thread->set_id(id);
291 }
292
301 static void set_current_thread(Thread* thread) {
302 Thread::set_current(thread);
303 }
304
306 ~ThreadManager() override;
307 };
308
311
317 class GEOGRAM_API MonoThreadingThreadManager : public ThreadManager {
318 public:
324
329 void enter_critical_section() override;
330
335 void leave_critical_section() override;
336
337 protected:
340
346 ThreadGroup& threads, index_t max_threads
347 ) override;
348 };
349
353 namespace Process {
354
361 void GEOGRAM_API initialize(int flags);
362
368 void GEOGRAM_API terminate();
369
370
376 void GEOGRAM_API sleep(index_t microseconds);
377
382 void GEOGRAM_API show_stats();
383
387 void GEOGRAM_API brute_force_kill();
388
396
402 void GEOGRAM_API run_threads(ThreadGroup& threads);
403
412 void GEOGRAM_API enter_critical_section();
413
421 void GEOGRAM_API leave_critical_section();
422
429
437 void GEOGRAM_API set_thread_manager(ThreadManager* thread_manager);
438
446 bool GEOGRAM_API is_running_threads();
447
457 void GEOGRAM_API enable_FPE(bool flag);
458
465 bool GEOGRAM_API FPE_enabled();
466
475 void GEOGRAM_API enable_multithreading(bool flag);
476
483 bool GEOGRAM_API multithreading_enabled();
484
493 void GEOGRAM_API set_max_threads(index_t num_threads);
494
499 index_t GEOGRAM_API max_threads();
500
514 void GEOGRAM_API enable_cancel(bool flag);
515
522 bool GEOGRAM_API cancel_enabled();
523
528 size_t GEOGRAM_API used_memory();
529
534 size_t GEOGRAM_API max_used_memory();
535
536
541 std::string GEOGRAM_API executable_filename();
542 }
543
574 void GEOGRAM_API parallel_for(
575 index_t from, index_t to, std::function<void(index_t)> func,
576 index_t threads_per_core = 1,
577 bool interleaved = false
578 );
579
606 void GEOGRAM_API parallel_for_slice(
607 index_t from, index_t to, std::function<void(index_t, index_t)> func,
608 index_t threads_per_core = 1
609 );
610
617 void GEOGRAM_API parallel(
618 std::function<void()> f1,
619 std::function<void()> f2
620 );
621
628 void GEOGRAM_API parallel(
629 std::function<void()> f1,
630 std::function<void()> f2,
631 std::function<void()> f3,
632 std::function<void()> f4
633 );
634
642 void GEOGRAM_API parallel(
643 std::function<void()> f1,
644 std::function<void()> f2,
645 std::function<void()> f3,
646 std::function<void()> f4,
647 std::function<void()> f5,
648 std::function<void()> f6,
649 std::function<void()> f7,
650 std::function<void()> f8
651 );
652
653}
654
655#endif
656
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition assert.h:195
Common include file, providing basic definitions. Should be included before anything else by all head...
Base class for reference-counted objects.
Definition counted.h:71
Single thread ThreadManager.
Definition process.h:317
index_t maximum_concurrent_threads() override
Gets the maximum number of possible concurrent threads.
void leave_critical_section() override
Leaves a critical section.
void enter_critical_section() override
Enters a critical section.
void run_concurrent_threads(ThreadGroup &threads, index_t max_threads) override
Runs a group of Threads concurrently.
A smart pointer with reference-counted copy semantics.
Platform-independent base class for running concurrent threads.
Definition process.h:215
virtual void run_concurrent_threads(ThreadGroup &threads, index_t max_threads)=0
Runs a group of Threads concurrently.
static void set_current_thread(Thread *thread)
Specifies the current instance, used by current().
Definition process.h:301
virtual void enter_critical_section()=0
Enters a critical section.
~ThreadManager() override
virtual void leave_critical_section()=0
Leaves a critical section.
virtual void run_threads(ThreadGroup &threads)
Runs a group of Threads.
virtual index_t maximum_concurrent_threads()=0
Gets the maximum number of possible concurrent threads.
static void set_thread_id(Thread *thread, index_t id)
Sets the id of a thread.
Definition process.h:289
Platform-independent base class for running threads.
Definition process.h:80
~Thread() override
index_t id() const
Gets the identifier of this thread.
Definition process.h:101
static Thread * current()
Gets the current thread.
virtual void run()=0
Starts the thread execution.
Thread()
Thread constructor.
Definition process.h:86
Typed collection of Threads.
Definition process.h:167
THREAD * operator[](index_t i)
Gets a thread element by index.
Definition process.h:183
TypedThreadGroup()
Creates an empty group of Threads.
Definition process.h:174
Base class of reference-counted objects, to be used with smart pointers.
void run_threads(ThreadGroup &threads)
Runs a set of threads simultaneously.
void enable_cancel(bool flag)
Enables interruption of cancelable tasks.
void initialize(int flags)
Initializes GeogramLib.
void set_thread_manager(ThreadManager *thread_manager)
Sets the thread manager (internal use).
std::string executable_filename()
Gets the full path to the currently running program.
void set_max_threads(index_t num_threads)
Limits the number of concurrent threads to use.
void sleep(index_t microseconds)
Sleeps for a period of time.
void leave_critical_section()
Leaves a critical section.
void brute_force_kill()
Terminates the current process.
index_t maximum_concurrent_threads()
Returns the maximum number of threads that can be running simultaneously.
void enable_FPE(bool flag)
Enables/disables floating point exceptions.
void enter_critical_section()
Enters a critical section.
bool multithreading_enabled()
Gets the status of multi-threading.
index_t number_of_cores()
Gets the number of available cores.
bool FPE_enabled()
Gets the status of floating point exceptions.
bool is_running_threads()
Checks whether threads are running.
size_t max_used_memory()
Gets the maximum used memory.
size_t used_memory()
Gets the currently used memory.
bool cancel_enabled()
Gets the status of the cancel mode.
void enable_multithreading(bool flag)
Enables/disables multi-threaded computations Multi-threading can also be configured by setting the va...
void terminate()
Terminates GeogramLib.
index_t max_threads()
Gets the number of allowed concurrent threads.
void show_stats()
Displays statistics about the current process.
Global Vorpaline namespace.
Definition algorithm.h:64
void parallel(std::function< void()> f1, std::function< void()> f2)
Calls functions in parallel.
std::vector< Thread_var > ThreadGroup
Collection of Threads.
Definition process.h:157
SmartPointer< Thread > Thread_var
Definition process.h:147
void parallel_for_slice(index_t from, index_t to, std::function< void(index_t, index_t)> func, index_t threads_per_core=1)
Executes a loop with concurrent threads.
void parallel_for(index_t from, index_t to, std::function< void(index_t)> func, index_t threads_per_core=1, bool interleaved=false)
Executes a loop with concurrent threads.
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:287
SmartPointer< ThreadManager > ThreadManager_var
Definition process.h:310
Pointers with automatic reference counting.
Functions and classes for process manipulation.