hemocell
Loading...
Searching...
No Matches
hemoCellFunctional.h
Go to the documentation of this file.
1/*
2This file is part of the HemoCell library
3
4HemoCell is developed and maintained by the Computational Science Lab
5in the University of Amsterdam. Any questions or remarks regarding this library
6can be sent to: info@hemocell.eu
7
8When using the HemoCell library in scientific work please cite the
9corresponding paper: https://doi.org/10.3389/fphys.2017.00563
10
11The HemoCell library is free software: you can redistribute it and/or
12modify it under the terms of the GNU Affero General Public License as
13published by the Free Software Foundation, either version 3 of the
14License, or (at your option) any later version.
15
16The library is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU Affero General Public License for more details.
20
21You should have received a copy of the GNU Affero General Public License
22along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24#ifndef HEMOCELLFUNCTIONAL_H
25#define HEMOCELLFUNCTIONAL_H
26
27#include "constant_defaults.h"
28#include "mpi.h"
29#include <cstring>
30
31#include "atomicBlock/dataProcessingFunctional3D.hh"
32#include "core/globalDefs.h"
33#include "multiBlock/multiDataProcessorWrapper3D.hh"
34
35namespace hemo {
36
37class HemoCellFunctional : public plb::BoxProcessingFunctional3D {
38public:
40 void getModificationPattern(std::vector<bool> & isWritten) const {
41 for (pluint i = 0; i < isWritten.size(); i++) { isWritten[i] = false; }
42 }
43 plb::BlockDomain::DomainT appliesTo() const { return plb::BlockDomain::bulk; }
44 void getTypeOfModification(std::vector<plb::modif::ModifT> & modified) const {
45 for (pluint i = 0; i < modified.size(); i++) { modified[i] = plb::modif::nothing; }
46 }
47};
48
49template <class GatherType>
50class HemoCellGatheringFunctional : public plb::BoxProcessingFunctional3D {
51 //Make the structures byte accessible for easy mpi serialization
52 union byteint {
53 unsigned char b[sizeof(int)];
54 int i;
55 };
56 // One map entry; Key is int (rank, block) or plint (cell ID).
57 template <typename Key>
59 Key ID;
60 GatherType g;
61 };
62
63public:
64 //Numblocks should be larger than the maximum number of blocks per process
65 //The total can be retrieved from Multiblock.getManagment.getsparseblock.getnumblocks
66 HemoCellGatheringFunctional(std::map<int, GatherType> & gatherValues_) : gatherValues(gatherValues_) {}
67 void getModificationPattern(std::vector<bool> & isWritten) const {
68 for (pluint i = 0; i < isWritten.size(); i++) { isWritten[i] = false; }
69 }
70 plb::BlockDomain::DomainT appliesTo() const { return plb::BlockDomain::bulk; }
71 void getTypeOfModification(std::vector<plb::modif::ModifT> & modified) const {
72 for (pluint i = 0; i < modified.size(); i++) { modified[i] = plb::modif::nothing; }
73 }
82 template <typename Key>
83 static void gather(std::map<Key, GatherType> & gatherValues) {
85 }
86
93 template <typename Key>
94 static void gatherToRoot(std::map<Key, GatherType> & gatherValues, int root = 0) {
96 }
97
98private:
99 // root < 0: all ranks receive (Allgatherv); otherwise only `root` (Gatherv).
100 template <typename Key>
101 static void exchange(std::map<Key, GatherType> & gatherValues, int root) {
102 typedef IDandGatherType<Key> Record;
103 int be = 0;
104 int sendsize = sizeof(int) + sizeof(Record) * gatherValues.size();
105 std::vector<unsigned char> sendbuffer(sendsize);
106
107 byteint local_number;
108 local_number.i = gatherValues.size();
109 for (unsigned int i = 0; i < sizeof(int); i++) {
110 sendbuffer[be] = local_number.b[i];
111 be++;
112 }
113 for (auto const & entry : gatherValues) {
114 Record bg;
115 bg.ID = entry.first;
116 bg.g = entry.second;
117 // The records follow a 4-byte count, so they are not aligned:
118 // copy bytes instead of dereferencing a cast pointer.
119 std::memcpy(&sendbuffer[be], &bg, sizeof(Record));
120 be += sizeof(Record);
121 }
122
123 const int nRanks = plb::global::mpi().getSize();
124 const bool allReceive = root < 0;
125 const bool receiving = allReceive || plb::global::mpi().getRank() == root;
126 std::vector<int> sendcounts(nRanks);
127 if (allReceive) {
128 MPI_Allgather(&sendsize, 1, MPI_INT, sendcounts.data(), 1, MPI_INT, MPI_COMM_WORLD);
129 } else {
130 MPI_Gather(&sendsize, 1, MPI_INT, sendcounts.data(), 1, MPI_INT, root, MPI_COMM_WORLD);
131 }
132
133 int receivesize = 0;
134 std::vector<int> displacements(nRanks, 0);
135 if (receiving) {
136 for (int j = 0; j < nRanks; j++) {
137 displacements[j] = receivesize;
138 receivesize += sendcounts[j];
139 }
140 }
141 std::vector<unsigned char> receivebuffer(receivesize);
142
143 if (allReceive) {
144 MPI_Allgatherv(sendbuffer.data(), sendsize, MPI_BYTE, receivebuffer.data(), sendcounts.data(),
145 displacements.data(), MPI_BYTE, MPI_COMM_WORLD);
146 } else {
147 MPI_Gatherv(sendbuffer.data(), sendsize, MPI_BYTE, receivebuffer.data(), sendcounts.data(), displacements.data(),
148 MPI_BYTE, root, MPI_COMM_WORLD);
149 }
150 if (!receiving) { return; }
151
152 for (int j = 0; j < nRanks; j++) {
153 be = displacements[j];
154 for (unsigned int i = 0; i < sizeof(int); i++) {
155 local_number.b[i] = receivebuffer[be];
156 be++;
157 }
158 for (int i = 0; i < local_number.i; i++) {
159 Record bg;
160 std::memcpy(&bg, &receivebuffer[be], sizeof(Record));
161 be += sizeof(Record);
162 gatherValues[bg.ID] = bg.g;
163 }
164 }
165 }
166
167public:
168 //This map should be set in the processingGenericBlocks function and is local to the mpi processor;
169 std::map<int, GatherType> & gatherValues;
170};
171
172}
173#endif /* HEMOCELLFUNCTIONAL_H */
Definition hemoCellFunctional.h:37
void getModificationPattern(std::vector< bool > &isWritten) const
Definition hemoCellFunctional.h:40
HemoCellFunctional()
Definition hemoCellFunctional.h:39
void getTypeOfModification(std::vector< plb::modif::ModifT > &modified) const
Definition hemoCellFunctional.h:44
plb::BlockDomain::DomainT appliesTo() const
Definition hemoCellFunctional.h:43
Definition hemoCellFunctional.h:50
static void gatherToRoot(std::map< Key, GatherType > &gatherValues, int root=0)
Combine the maps of all ranks on rank root only. Collective.
Definition hemoCellFunctional.h:94
static void exchange(std::map< Key, GatherType > &gatherValues, int root)
Definition hemoCellFunctional.h:101
void getTypeOfModification(std::vector< plb::modif::ModifT > &modified) const
Definition hemoCellFunctional.h:71
void getModificationPattern(std::vector< bool > &isWritten) const
Definition hemoCellFunctional.h:67
static void gather(std::map< Key, GatherType > &gatherValues)
Combine the maps of all ranks on every rank. Collective.
Definition hemoCellFunctional.h:83
std::map< int, GatherType > & gatherValues
Definition hemoCellFunctional.h:169
plb::BlockDomain::DomainT appliesTo() const
Definition hemoCellFunctional.h:70
HemoCellGatheringFunctional(std::map< int, GatherType > &gatherValues_)
Definition hemoCellFunctional.h:66
long unsigned int pluint
Definition constant_defaults.h:152
Definition config.cpp:37
Definition hemoCellFunctional.h:58
GatherType g
Definition hemoCellFunctional.h:60
Key ID
Definition hemoCellFunctional.h:59
Definition hemoCellFunctional.h:52
unsigned char b[sizeof(int)]
Definition hemoCellFunctional.h:53
int i
Definition hemoCellFunctional.h:54