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
30#include "atomicBlock/dataProcessingFunctional3D.hh"
31#include "core/globalDefs.h"
32#include "multiBlock/multiDataProcessorWrapper3D.hh"
33
34namespace hemo {
35
36class HemoCellFunctional : public plb::BoxProcessingFunctional3D {
37public:
39 void getModificationPattern(std::vector<bool>& isWritten) const {
40 for (pluint i = 0; i < isWritten.size(); i++) {
41 isWritten[i] = false;
42 }
43 }
44 plb::BlockDomain::DomainT appliesTo() const { return plb::BlockDomain::bulk; }
45 void getTypeOfModification(std::vector<plb::modif::ModifT>& modified) const {
46 for (pluint i = 0; i < modified.size(); i++) {
47 modified[i] = plb::modif::nothing;
48 }
49
50 }
51};
52
53template<class GatherType>
54class HemoCellGatheringFunctional : public plb::BoxProcessingFunctional3D {
55 //Make the structures byte accessible for easy mpi serialization
56 union byteint {
57 unsigned char b[sizeof(int)];
58 int i;
59 };
61 int ID;
62 GatherType g;
63 };
64public:
65 //Numblocks should be larger than the maximum number of blocks per process
66 //The total can be retrieved from Multiblock.getManagment.getsparseblock.getnumblocks
67 HemoCellGatheringFunctional(std::map<int,GatherType> & gatherValues_) :
68 gatherValues(gatherValues_) {}
69 void getModificationPattern(std::vector<bool>& isWritten) const {
70 for (pluint i = 0; i < isWritten.size(); i++) {
71 isWritten[i] = false;
72 }
73 }
74 plb::BlockDomain::DomainT appliesTo() const { return plb::BlockDomain::bulk; }
75 void getTypeOfModification(std::vector<plb::modif::ModifT>& modified) const {
76 for (pluint i = 0; i < modified.size(); i++) {
77 modified[i] = plb::modif::nothing;
78 }
79
80 }
81 static void gather(std::map<int,GatherType> & gatherValues) {
82 int be = 0;
83 int sendsize = sizeof(int) + sizeof(IDandGatherType)*gatherValues.size();
84 std::vector<unsigned char> sendbuffer(sendsize);
85
86 byteint local_number;
87 local_number.i = gatherValues.size();
88 for (unsigned int i = 0; i < sizeof(int) ; i++) {
89 sendbuffer[be] = local_number.b[i];
90 be++;
91 }
92 for (auto const & entry : gatherValues) {
94 bg.ID = entry.first;
95 bg.g = entry.second;
96 *((IDandGatherType*)&sendbuffer[be]) = bg;
97 be+=sizeof(IDandGatherType);
98 }
99
100 std::vector<int> sendcounts(plb::global::mpi().getSize());
101 MPI_Allgather(&sendsize,sizeof(int),MPI_BYTE,&sendcounts[0],sizeof(int),MPI_BYTE,MPI_COMM_WORLD);
102
103 int receivesize = 0;
104 std::vector<int> displacements(1,0);
105 for (int & size : sendcounts) {
106 receivesize += size;
107 displacements.push_back(displacements.back() + size);
108 }
109 displacements.pop_back();
110 std::vector<unsigned char> receivebuffer(receivesize);
111
112 MPI_Allgatherv(&sendbuffer[0],sendsize,MPI_BYTE,&receivebuffer[0],&sendcounts[0],&displacements[0],MPI_BYTE,MPI_COMM_WORLD);
113 be = 0;
114
115 for (int j = 0 ; j < plb::global::mpi().getSize() ; j++) {
116 be = displacements[j];
117 for (unsigned int i = 0; i < sizeof(int) ; i++) {
118 local_number.b[i] = receivebuffer[be];
119 be++;
120 }
121 for (int i = 0 ; i < local_number.i ; i++) {
123 bg = *((IDandGatherType*)&receivebuffer[be]);
124 be += sizeof(IDandGatherType);
125 gatherValues[bg.ID] = bg.g;
126 }
127 }
128
129 }
130
131 //This map should be set in the processingGenericBlocks function and is local to the mpi processor;
132 std::map<int,GatherType> & gatherValues;
133};
134
135}
136#endif /* HEMOCELLFUNCTIONAL_H */
137
Definition hemoCellFunctional.h:36
void getModificationPattern(std::vector< bool > &isWritten) const
Definition hemoCellFunctional.h:39
HemoCellFunctional()
Definition hemoCellFunctional.h:38
void getTypeOfModification(std::vector< plb::modif::ModifT > &modified) const
Definition hemoCellFunctional.h:45
plb::BlockDomain::DomainT appliesTo() const
Definition hemoCellFunctional.h:44
Definition hemoCellFunctional.h:54
void getTypeOfModification(std::vector< plb::modif::ModifT > &modified) const
Definition hemoCellFunctional.h:75
void getModificationPattern(std::vector< bool > &isWritten) const
Definition hemoCellFunctional.h:69
std::map< int, GatherType > & gatherValues
Definition hemoCellFunctional.h:132
static void gather(std::map< int, GatherType > &gatherValues)
Definition hemoCellFunctional.h:81
plb::BlockDomain::DomainT appliesTo() const
Definition hemoCellFunctional.h:74
HemoCellGatheringFunctional(std::map< int, GatherType > &gatherValues_)
Definition hemoCellFunctional.h:67
long unsigned int pluint
Definition constant_defaults.h:130
Definition config.cpp:34
Definition hemoCellFunctional.h:60
GatherType g
Definition hemoCellFunctional.h:62
int ID
Definition hemoCellFunctional.h:61
Definition hemoCellFunctional.h:56
unsigned char b[sizeof(int)]
Definition hemoCellFunctional.h:57
int i
Definition hemoCellFunctional.h:58