NimbleSM
NimbleSM is a solid mechanics simulation code for dynamic systems
Loading...
Searching...
No Matches
nimble.mpi.reduction_utils.h
Go to the documentation of this file.
1/*
2//@HEADER
3// ************************************************************************
4//
5// NimbleSM
6// Copyright 2018
7// National Technology & Engineering Solutions of Sandia, LLC (NTESS)
8//
9// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10// retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY EXPRESS OR IMPLIED
28// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
30// NO EVENT SHALL NTESS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact David Littlewood (djlittl@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42*/
43
44#ifndef NIMBLE_MPI_REDUCTION_UTILS
45#define NIMBLE_MPI_REDUCTION_UTILS
46
47#include <mpi.h>
48
49#include <algorithm>
50#include <array>
51#include <cstring>
52#include <exception>
53#include <iostream>
54#include <memory>
55#include <numeric>
56#include <string>
57#include <tuple>
58#include <type_traits>
59#include <vector>
60
63
64namespace nimble {
65
66std::vector<int>
67PackIDSpace(const std::vector<int>& raw_node_ids, int max_nodes_assigned_to_a_rank, const mpicontext& context);
68
69int
70GetMaximumNodeId(const std::vector<int>& global_node_ids, const mpicontext& context);
71
72std::unique_ptr<int[]>
73GetNumberOfNodesAssignedToEachRank(const std::vector<int>& global_node_ids, const mpicontext& context);
74
75void
76EnsureCheckpoint(const mpicontext& context, const std::string& message);
77
78template <class Key, class Val>
79std::vector<std::pair<Key, std::vector<Val>>>
80GroupConsecutive(const std::vector<std::pair<Key, Val>>& lst)
81{
82 if (lst.size() == 0) return {};
83 std::vector<int> key_ids;
84 key_ids.reserve(lst.size() + 1);
85 const Key* group_key = &lst[0].first;
86 int max_key_id = 0;
87 int lst_size = lst.size();
88 for (const std::pair<Key, Val>& p : lst) {
89 if (p.first != *group_key) {
90 ++max_key_id;
91 group_key = &p.first;
92 }
93 key_ids.push_back(max_key_id);
94 }
95 key_ids.push_back(max_key_id + 1);
96 std::vector<std::pair<Key, std::vector<Val>>> groups;
97 groups.reserve(max_key_id + 1);
98 {
99 int prev_key_id = 0;
100 int group_size = 1;
101 for (int i = 1; i <= lst_size; ++i) {
102 if (key_ids[i] != prev_key_id) {
103 groups.emplace_back(lst[i - 1].first, std::vector<Val>(group_size));
104 auto& new_elem = groups.back();
105 const std::pair<Key, Val>* src_ptr = &lst[i - group_size];
106 for (Val& q : new_elem.second) {
107 q = src_ptr->second;
108 ++src_ptr;
109 }
110 group_size = 1;
111 prev_key_id = key_ids[i];
112 } else {
113 ++group_size;
114 }
115 }
116 }
117 return groups;
118}
119
120} // namespace nimble
121
122#endif // NIMBLE_MPI_REDUCTION_UTILS
Definition nimble.mpi.mpicontext.h:63
Definition kokkos_contact_manager.h:49
std::unique_ptr< int[]> GetNumberOfNodesAssignedToEachRank(const std::vector< int > &global_node_ids, const mpicontext &context)
int GetMaximumNodeId(const std::vector< int > &global_node_ids, const mpicontext &context)
std::vector< int > PackIDSpace(const std::vector< int > &raw_node_ids, int max_nodes_assigned_to_a_rank, const mpicontext &context)
std::vector< std::pair< Key, std::vector< Val > > > GroupConsecutive(const std::vector< std::pair< Key, Val > > &lst)
Definition nimble.mpi.reduction_utils.h:80
void EnsureCheckpoint(const mpicontext &context, const std::string &message)