Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:13:51

0001 //
0002 // MIT License
0003 // Copyright (c) 2020 Jonathan R. Madsen
0004 // Permission is hereby granted, free of charge, to any person obtaining a copy
0005 // of this software and associated documentation files (the "Software"), to deal
0006 // in the Software without restriction, including without limitation the rights
0007 // to use, copy, modify, merge, publish, distribute, sublicense, and
0008 // copies of the Software, and to permit persons to whom the Software is
0009 // furnished to do so, subject to the following conditions:
0010 // The above copyright notice and this permission notice shall be included in
0011 // all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
0012 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
0013 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
0014 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0015 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0016 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0017 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0018 //
0019 // class description:
0020 //   This is a class for run control in Tasking for multi-threaded runs
0021 //   It extends RunManager re-implementing multi-threaded behavior in
0022 //   key methods. See documentation for RunManager
0023 //   Users initializes an instance of this class instead of RunManager
0024 //   to start a multi-threaded simulation.
0025 
0026 #pragma once
0027 
0028 #include "PTL/ThreadPool.hh"
0029 
0030 #include <cstddef>
0031 #include <cstdint>
0032 #include <thread>
0033 
0034 namespace PTL
0035 {
0036 class TaskManager;
0037 class VUserTaskQueue;
0038 
0039 //======================================================================================//
0040 
0041 class TaskRunManager
0042 {
0043 public:
0044     using pointer = TaskRunManager*;
0045 
0046 public:
0047     // Parameters:
0048     //      m_task_queue: provide a custom task queue
0049     //      useTBB: only relevant if PTL_USE_TBB defined
0050     //      grainsize:  0 = auto
0051     explicit TaskRunManager(bool useTBB = false);
0052     virtual ~TaskRunManager();
0053 
0054 public:
0055     virtual int GetNumberOfThreads() const
0056     {
0057         return (m_thread_pool) ? (int)m_thread_pool->size() : 0;
0058     }
0059     virtual size_t GetNumberActiveThreads() const
0060     {
0061         return (m_thread_pool) ? m_thread_pool->size() : 0;
0062     }
0063 
0064 public:
0065     // Inherited methods to re-implement for MT case
0066     virtual void Initialize(uint64_t n = std::thread::hardware_concurrency());
0067     virtual void Terminate();
0068     ThreadPool*  GetThreadPool() const { return m_thread_pool; }
0069     TaskManager* GetTaskManager() const { return m_task_manager; }
0070     bool         IsInitialized() const { return m_is_initialized; }
0071 
0072 public:  // with description
0073     // Singleton implementing master thread behavior
0074     static TaskRunManager* GetInstance(bool useTBB = false);
0075     static TaskRunManager* GetMasterRunManager(bool useTBB = false);
0076 
0077 private:
0078     static pointer& GetPrivateMasterRunManager();
0079     static pointer& GetPrivateMasterRunManager(bool init, bool useTBB = false);
0080 
0081 protected:
0082     // Barriers: synch points between master and workers
0083     bool            m_is_initialized = false;
0084     uint64_t        m_workers        = 0;
0085     bool            m_use_tbb        = false;
0086     VUserTaskQueue* m_task_queue     = nullptr;
0087     ThreadPool*     m_thread_pool    = nullptr;
0088     TaskManager*    m_task_manager   = nullptr;
0089 };
0090 
0091 }  // namespace PTL