Line data Source code
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 : #include "neml2/dispatchers/SimpleScheduler.h"
26 : #include "neml2/misc/assertions.h"
27 :
28 : namespace neml2
29 : {
30 :
31 : register_NEML2_object(SimpleScheduler);
32 :
33 : OptionSet
34 11 : SimpleScheduler::expected_options()
35 : {
36 11 : OptionSet options = WorkScheduler::expected_options();
37 11 : options.doc() = "Dispatch work to a single device in given batch sizes.";
38 :
39 22 : options.set<std::string>("device");
40 22 : options.set("device").doc() = "Torch device to run on";
41 :
42 22 : options.set<std::size_t>("batch_size");
43 22 : options.set("batch_size").doc() = "Batch size";
44 :
45 22 : options.set<std::size_t>("capacity");
46 22 : options.set("capacity").doc() =
47 11 : "Maximum number of work items that can be dispatched, default to batch_size";
48 :
49 11 : return options;
50 0 : }
51 :
52 9 : SimpleScheduler::SimpleScheduler(const OptionSet & options)
53 : : WorkScheduler(options),
54 18 : _device(Device(options.get<std::string>("device"))),
55 18 : _batch_size(options.get<std::size_t>("batch_size")),
56 9 : _capacity(options.get("capacity").user_specified() ? options.get<std::size_t>("capacity")
57 9 : : _batch_size)
58 : {
59 9 : }
60 :
61 : bool
62 84 : SimpleScheduler::schedule_work_impl(Device & device, std::size_t & batch_size) const
63 : {
64 84 : if (_load + _batch_size > _capacity)
65 38 : return false;
66 :
67 46 : device = _device;
68 46 : batch_size = _batch_size;
69 46 : return true;
70 : }
71 :
72 : void
73 23 : SimpleScheduler::dispatched_work_impl(Device, std::size_t n)
74 : {
75 23 : _load += n;
76 23 : }
77 :
78 : void
79 23 : SimpleScheduler::completed_work_impl(Device, std::size_t n)
80 : {
81 23 : neml_assert(_load >= n, "Load underflow");
82 23 : _load -= n;
83 23 : }
84 :
85 : bool
86 12 : SimpleScheduler::all_work_completed() const
87 : {
88 12 : return _load == 0;
89 : }
90 :
91 : } // namespace neml2
|