Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:19:36

0001 // Copyright (c) 2026 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _BRepGraph_ParallelPolicy_HeaderFile
0015 #define _BRepGraph_ParallelPolicy_HeaderFile
0016 
0017 #include <OSD_Parallel.hxx>
0018 
0019 #include <algorithm>
0020 #include <cstdint>
0021 
0022 //! Lightweight workload-aware policy for deciding whether an internal phase
0023 //! should actually launch parallel work when parallel mode is allowed.
0024 //!
0025 //! The goal is to avoid forcing every short-lived loop onto the thread pool.
0026 //! Decisions are based on available worker capacity and on the amount of work
0027 //! already visible to the phase, instead of on buried per-loop split sizes.
0028 class BRepGraph_ParallelPolicy
0029 {
0030 public:
0031   //! Simple workload estimate for an execution phase.
0032   struct Workload
0033   {
0034     uint32_t PrimaryItems     = 0; //!< Main loop range.
0035     uint32_t AuxiliaryItems   = 0; //!< Additional independent items participating in the phase.
0036     uint32_t InteractionCount = 0; //!< Pairwise or adjacency work discovered for the phase.
0037   };
0038 
0039   //! Return the effective logical worker count reported by OSD_Parallel.
0040   [[nodiscard]] static int WorkerCount()
0041   {
0042     static const int THE_WORKER_COUNT = std::max(1, OSD_Parallel::NbLogicalProcessors());
0043     return THE_WORKER_COUNT;
0044   }
0045 
0046   //! Check whether parallel execution is allowed and meaningful at all.
0047   [[nodiscard]] static bool IsParallelAllowed(const bool theAllowParallel)
0048   {
0049     return theAllowParallel && WorkerCount() > 1;
0050   }
0051 
0052   //! Decide whether the estimated workload is large enough to amortize
0053   //! thread-pool launch and synchronization overhead.
0054   //! @param[in] theAllowParallel  whether parallel mode is allowed by the caller
0055   //! @param[in] theWorkers        effective logical worker count
0056   //! @param[in] theWorkload       estimated workload for the phase
0057   //! @return true if parallel execution should be used
0058   [[nodiscard]] Standard_EXPORT static bool ShouldRun(const bool      theAllowParallel,
0059                                                       const int       theWorkers,
0060                                                       const Workload& theWorkload);
0061 
0062   //! Overload that queries the active worker count lazily.
0063   //! @param[in] theAllowParallel  whether parallel mode is allowed by the caller
0064   //! @param[in] theWorkload       estimated workload for the phase
0065   //! @return true if parallel execution should be used
0066   [[nodiscard]] static bool ShouldRun(const bool theAllowParallel, const Workload& theWorkload)
0067   {
0068     return ShouldRun(theAllowParallel, WorkerCount(), theWorkload);
0069   }
0070 };
0071 
0072 #endif // _BRepGraph_ParallelPolicy_HeaderFile