Geogram  Version 1.9.1
A programming library of geometric algorithms
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 
43 #include <geogram/basic/common.h>
45 #include <geogram/basic/counted.h>
47 #include <functional>
48 
54 namespace GEO {
55 
66  class GEOGRAM_API Thread : public Counted {
67  public:
68 
72  Thread() : id_(0) {
73  }
74 
78  virtual void run() = 0;
79 
87  index_t id() const {
88  return id_;
89  }
90 
97  static Thread* current();
98 
107  static index_t current_id() {
108  return current() == nullptr ? 0 : current()->id();
109  }
110 
111  protected:
113  ~Thread() override;
114 
115 
116  private:
123  void set_id(index_t id_in) {
124  id_ = id_in;
125  }
126 
135  static void set_current(Thread* thread);
136 
137  index_t id_;
138 
139  // ThreadManager needs to access set_current() and
140  // set_id().
141  friend class ThreadManager;
142  };
143 
146 
155  typedef std::vector<Thread_var> ThreadGroup;
156 
164  template <class THREAD>
165  class TypedThreadGroup : public ThreadGroup {
166  public:
173  }
174 
181  THREAD* operator[] (index_t i) {
182  geo_debug_assert(i < size());
183  Thread* result = ThreadGroup::operator[] (i);
184  return static_cast<THREAD*>(result);
185  }
186  };
187 
213  class GEOGRAM_API ThreadManager : public Counted {
214  public:
233  virtual void run_threads(ThreadGroup& threads);
234 
244 
245  protected:
258  ) = 0;
259 
260 
269  static void set_thread_id(Thread* thread, index_t id) {
270  thread->set_id(id);
271  }
272 
281  static void set_current_thread(Thread* thread) {
282  Thread::set_current(thread);
283  }
284 
286  ~ThreadManager() override;
287  };
288 
291 
297  class GEOGRAM_API MonoThreadingThreadManager : public ThreadManager {
298  public:
304 
305  protected:
308 
315  ) override;
316  };
317 
321  namespace Process {
322 
329  void GEOGRAM_API initialize(int flags);
330 
336  void GEOGRAM_API terminate();
337 
338 
344  void GEOGRAM_API sleep(index_t microseconds);
345 
350  void GEOGRAM_API show_stats();
351 
355  void GEOGRAM_API brute_force_kill();
356 
364 
370  void GEOGRAM_API run_threads(ThreadGroup& threads);
371 
377  index_t GEOGRAM_API number_of_cores();
378 
386  void GEOGRAM_API set_thread_manager(ThreadManager* thread_manager);
387 
395  bool GEOGRAM_API is_running_threads();
396 
406  void GEOGRAM_API enable_FPE(bool flag);
407 
414  bool GEOGRAM_API FPE_enabled();
415 
424  void GEOGRAM_API enable_multithreading(bool flag);
425 
432  bool GEOGRAM_API multithreading_enabled();
433 
442  void GEOGRAM_API set_max_threads(index_t num_threads);
443 
448  index_t GEOGRAM_API max_threads();
449 
463  void GEOGRAM_API enable_cancel(bool flag);
464 
471  bool GEOGRAM_API cancel_enabled();
472 
477  size_t GEOGRAM_API used_memory();
478 
483  size_t GEOGRAM_API max_used_memory();
484 
489  std::string GEOGRAM_API executable_filename();
490 
495  }
496 
527  void GEOGRAM_API parallel_for(
528  index_t from, index_t to, std::function<void(index_t)> func,
529  index_t threads_per_core = 1,
530  bool interleaved = false
531  );
532 
559  void GEOGRAM_API parallel_for_slice(
560  index_t from, index_t to, std::function<void(index_t, index_t)> func,
561  index_t threads_per_core = 1
562  );
563 
570  void GEOGRAM_API parallel(
571  std::function<void()> f1,
572  std::function<void()> f2
573  );
574 
581  void GEOGRAM_API parallel(
582  std::function<void()> f1,
583  std::function<void()> f2,
584  std::function<void()> f3,
585  std::function<void()> f4
586  );
587 
595  void GEOGRAM_API parallel(
596  std::function<void()> f1,
597  std::function<void()> f2,
598  std::function<void()> f3,
599  std::function<void()> f4,
600  std::function<void()> f5,
601  std::function<void()> f6,
602  std::function<void()> f7,
603  std::function<void()> f8
604  );
605 
606 }
607 
608 #endif
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition: assert.h:196
Base class for reference-counted objects.
Definition: counted.h:71
Single thread ThreadManager.
Definition: process.h:297
index_t maximum_concurrent_threads() override
Gets the maximum number of possible concurrent threads.
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.
Definition: smart_pointer.h:76
Platform-independent base class for running concurrent threads.
Definition: process.h:213
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:281
~ThreadManager() override
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:269
Platform-independent base class for running threads.
Definition: process.h:66
static index_t current_id()
Gets the identifier of the current thread.
Definition: process.h:107
~Thread() override
index_t id() const
Gets the identifier of this thread.
Definition: process.h:87
virtual void run()=0
Starts the thread execution.
Thread()
Thread constructor.
Definition: process.h:72
static Thread * current()
Gets the current thread.
Typed collection of Threads.
Definition: process.h:165
THREAD * operator[](index_t i)
Gets a thread element by index.
Definition: process.h:181
TypedThreadGroup()
Creates an empty group of Threads.
Definition: process.h:172
Base class of reference-counted objects, to be used with smart pointers.
Common include file, providing basic definitions. Should be included before anything else by all head...
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 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.
bool multithreading_enabled()
Gets the status of multi-threading.
index_t number_of_cores()
Gets the number of available cores.
void print_stack_trace()
Prints a stack trace to the standard error.
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: basic.h:55
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:155
SmartPointer< Thread > Thread_var
Definition: process.h:145
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:329
SmartPointer< ThreadManager > ThreadManager_var
Definition: process.h:290
Pointers with automatic reference counting.
Functions and classes for process manipulation.