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/derivmap_helpers.h"
26 : #include "neml2/misc/errors.h"
27 : #include "neml2/tensors/functions/cat.h"
28 :
29 : namespace neml2
30 : {
31 :
32 : DerivMap
33 1 : derivmap_cat_reduce(std::vector<DerivMap> && results, Size batch_dim)
34 : {
35 : // Re-bin the results
36 1 : std::map<VariableName, std::map<VariableName, std::vector<Tensor>>> vars;
37 3 : for (auto && result : std::move(results))
38 4 : for (auto && [name1, vmap] : result)
39 4 : for (auto && [name2, value] : vmap)
40 2 : vars[name1][name2].emplace_back(std::move(value));
41 :
42 : // Concatenate the tensors
43 1 : DerivMap ret;
44 2 : for (auto && [name1, vmap] : vars)
45 2 : for (auto && [name2, values] : vmap)
46 : {
47 1 : if (values.front().batch_dim() <= batch_dim)
48 : {
49 : #ifndef NDEBUG
50 0 : for (auto && value : values)
51 0 : if (value.batch_dim() != values.front().batch_dim())
52 0 : throw neml2::NEMLException("Some Jacobian entries for " + name1.str() + " and " +
53 0 : name2.str() + " have different batch dimensions");
54 : #endif
55 0 : ret[name1][name2] = values.front();
56 : }
57 : else
58 1 : ret[name1][name2] = batch_cat(values, batch_dim);
59 : }
60 :
61 2 : return ret;
62 1 : }
63 :
64 : DerivMap
65 0 : derivmap_move_device(DerivMap && x, Device device)
66 : {
67 : // Move the tensors to the device
68 0 : for (auto && [name, vmap] : std::move(x))
69 0 : for (auto && [name2, value] : vmap)
70 0 : x[name][name2] = value.to(device);
71 0 : return std::move(x);
72 : }
73 :
74 : DerivMap
75 1 : derivmap_no_operation(DerivMap && x)
76 : {
77 1 : return std::move(x);
78 : }
79 :
80 : }
|