NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
WorkScheduler.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include <mutex>
28#include <condition_variable>
29
30#include "neml2/base/NEML2Object.h"
31#include "neml2/misc/types.h"
32
33namespace neml2
34{
47{
48public:
51
57 WorkScheduler(const OptionSet & options);
58
64 void schedule_work(Device &, std::size_t &);
65
67 void dispatched_work(Device, std::size_t);
68
70 void completed_work(Device, std::size_t);
71
74
76 virtual std::vector<Device> devices() const { return std::vector<Device>(); }
77
78protected:
80 virtual bool schedule_work_impl(Device &, std::size_t &) const = 0;
81
83 virtual void dispatched_work_impl(Device, std::size_t) = 0;
84
86 virtual void completed_work_impl(Device, std::size_t) = 0;
87
89 virtual bool all_work_completed() const = 0;
90
92 std::condition_variable _condition;
93
94private:
96 std::mutex _mutex;
97};
98} // namespace neml2
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:52
virtual bool all_work_completed() const =0
Check if all work has been completed.
void wait_for_completion()
Wait for all work to complete.
Definition WorkScheduler.cxx:70
void dispatched_work(Device, std::size_t)
Update the scheduler with the dispatch of the last batch.
Definition WorkScheduler.cxx:55
virtual bool schedule_work_impl(Device &, std::size_t &) const =0
Implementation of the work scheduling.
void completed_work(Device, std::size_t)
Update the scheduler with the completion of the last batch.
Definition WorkScheduler.cxx:62
virtual void dispatched_work_impl(Device, std::size_t)=0
Update the scheduler with the dispatch of the last batch.
WorkScheduler(const OptionSet &options)
Construct a new WorkScheduler object.
Definition WorkScheduler.cxx:39
static OptionSet expected_options()
Options for the scheduler.
Definition WorkScheduler.cxx:31
virtual void completed_work_impl(Device, std::size_t)=0
Update the scheduler with the completion of the last batch.
std::condition_variable _condition
Condition variable for the scheduling thread.
Definition WorkScheduler.h:92
virtual std::vector< Device > devices() const
Device options.
Definition WorkScheduler.h:76
void schedule_work(Device &, std::size_t &)
Determine the device and batch size for the next dispatch.
Definition WorkScheduler.cxx:45
Definition DiagnosticsInterface.cxx:30
c10::Device Device
Definition types.h:66