ug4
pcl_util_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 
35 #include "common/serialization.h"
36 
37 namespace pcl {
38 
39 template <typename TKey, typename TValue, typename Compare>
40 void MinimalKeyValuePairAcrossAllProcs(TKey& keyInOut, TValue& valInOut, const Compare& cmp)
41 {
42  // in the serial case, input key and value are already output key and value
43 #ifndef UG_PARALLEL
44  return;
45 #endif
46  size_t nProc = NumProcs();
47  if (nProc == 1)
48  return;
49 
50  // write key/value pair into binary buffer
51  ug::BinaryBuffer buf;
52  ug::Serialize(buf, keyInOut);
53  ug::Serialize(buf, valInOut);
54 
55  // gather buffers on root
57  pc.gather(buf, 0);
58 
59  // find minimum on root
60  size_t rk = ProcRank();
61  if (rk == 0)
62  {
63  TKey key;
64  TValue val;
65 
66  for (size_t i = 0; i < nProc; ++i)
67  {
68  ug::Deserialize(buf, key);
69  ug::Deserialize(buf, val);
70 
71  if (cmp(key, keyInOut))
72  {
73  keyInOut = key;
74  valInOut = val;
75  }
76  }
77 
78  buf.clear();
79  ug::Serialize(buf, keyInOut);
80  ug::Serialize(buf, valInOut);
81  }
82 
83  // communicate minimum to all procs
84  pc.broadcast(buf, 0);
85 
86  // copy to return values
87  ug::Deserialize(buf, keyInOut);
88  ug::Deserialize(buf, valInOut);
89 }
90 
91 
92 } // end of namespace pcl
Definition: pcl_process_communicator.h:70
void gather(const void *sendBuf, int sendCount, DataType sendType, void *recBuf, int recCount, DataType recType, int root) const
performs MPI_Gather on the processes of the communicator.
Definition: pcl_process_communicator.cpp:339
void broadcast(void *v, size_t size, DataType type, int root=0) const
Definition: pcl_process_communicator.cpp:685
A Buffer for binary data.
Definition: binary_buffer.h:56
void clear()
clears the buffer
Definition: binary_buffer.cpp:48
void MinimalKeyValuePairAcrossAllProcs(TKey &keyInOut, TValue &valInOut, const Compare &cmp=Compare())
Find minimal key/value pair across processes This function will receive one key/value pair from each ...
Definition: pcl_util_impl.h:40
int ProcRank()
returns the rank of the process
Definition: pcl_base.cpp:83
int NumProcs()
returns the number of processes
Definition: pcl_base.cpp:91
Definition: parallel_grid_layout.h:46
void Deserialize(TIStream &buf, ParallelVector< T > &v)
Deerialize for ParallelVector<T>
Definition: restart_bridge.cpp:112
void Serialize(TOStream &buf, const ParallelVector< T > &v)
Serialize for ParallelVector<T>
Definition: restart_bridge.cpp:103