Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:07:23

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 #include "PTL/VUserTaskQueue.hh"
0030 
0031 #include <cstddef>
0032 #include <cstdint>
0033 #include <thread>
0034 
0035 namespace PTL
0036 {
0037 class TaskManager;
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     int          GetVerbose() const { return m_verbose; }
0072     void         SetVerbose(int val) { m_verbose = val; }
0073 
0074 public:  // with description
0075     // Singleton implementing master thread behavior
0076     static TaskRunManager* GetInstance(bool useTBB = false);
0077     static TaskRunManager* GetMasterRunManager(bool useTBB = false);
0078 
0079 private:
0080     static pointer& GetPrivateMasterRunManager();
0081     static pointer& GetPrivateMasterRunManager(bool init, bool useTBB = false);
0082 
0083 protected:
0084     // Barriers: synch points between master and workers
0085     bool            m_is_initialized = false;
0086     int             m_verbose        = 0;
0087     uint64_t        m_workers        = 0;
0088     VUserTaskQueue* m_task_queue     = nullptr;
0089     ThreadPool*     m_thread_pool    = nullptr;
0090     TaskManager*    m_task_manager   = nullptr;
0091 };
0092 
0093 }  // namespace PTL