forked from sanshar/Dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHCIshm.h
157 lines (124 loc) · 4.46 KB
/
SHCIshm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Developed by Sandeep Sharma with contributions from James E. T. Smith and Adam A. Holmes, 2017
Copyright (c) 2017, Sandeep Sharma
This file is part of DICE.
This program is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHCI_SHM_H
#define SHCI_SHM_H
#include <vector>
#include <string>
#include <Eigen/Dense>
#include <Eigen/Core>
#include "global.h"
using namespace Eigen;
void initSHM();
template <typename T>
void SHMVecFromVecs(std::vector<T>& vec, T* &SHMvec, std::string& SHMname,
boost::interprocess::shared_memory_object& SHMsegment,
boost::interprocess::mapped_region& SHMregion) {
size_t totalMemory = 0;
int comm_rank=0, comm_size=1;
#ifndef SERIAL
MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
#endif
if (comm_rank == 0)
totalMemory = vec.size()*sizeof(T);
#ifndef SERIAL
MPI_Bcast(&totalMemory, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
SHMsegment.truncate(totalMemory);
SHMregion = boost::interprocess::mapped_region{SHMsegment, boost::interprocess::read_write};
if (localrank == 0)
memset(SHMregion.get_address(), 0., totalMemory);
SHMvec = (T*)(SHMregion.get_address());
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
if (comm_rank == 0) {
for (size_t i=0; i<vec.size(); i++)
SHMvec[i] = vec[i];
}
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
if (localrank == 0) {
long intdim = totalMemory;
long maxint = 26843540; //mpi cannot transfer more than these number of doubles
long maxIter = intdim/maxint;
#ifndef SERIAL
MPI_Barrier(shmcomm);
char* shrdMem = static_cast<char*>(SHMregion.get_address());
for (int i=0; i<maxIter; i++) {
MPI_Bcast ( shrdMem+i*maxint, maxint, MPI_CHAR, 0, shmcomm);
MPI_Barrier(shmcomm);
}
MPI_Bcast ( shrdMem+(maxIter)*maxint, totalMemory - maxIter*maxint, MPI_CHAR, 0, shmcomm);
#endif
}
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
boost::interprocess::shared_memory_object::remove(SHMname.c_str());
}
template <typename T>
void SHMVecFromVecs(T *vec, int vecsize, T* &SHMvec, std::string& SHMname,
boost::interprocess::shared_memory_object& SHMsegment,
boost::interprocess::mapped_region& SHMregion) {
boost::interprocess::shared_memory_object::remove(SHMname.c_str());
size_t totalMemory = 0;
int comm_rank=0, comm_size=1;
#ifndef SERIAL
MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
#endif
if (comm_rank == 0)
totalMemory = vecsize*sizeof(T);
#ifndef SERIAL
MPI_Bcast(&totalMemory, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
SHMsegment.truncate(totalMemory);
SHMregion = boost::interprocess::mapped_region{SHMsegment, boost::interprocess::read_write};
if (localrank == 0)
memset(SHMregion.get_address(), 0., totalMemory);
SHMvec = (T*)(SHMregion.get_address());
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
if (comm_rank == 0) {
for (size_t i=0; i<vecsize; i++)
SHMvec[i] = vec[i];
}
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
if (localrank == 0) {
long intdim = totalMemory;
long maxint = 26843540; //mpi cannot transfer more than these number of doubles
long maxIter = intdim/maxint;
#ifndef SERIAL
MPI_Barrier(shmcomm);
char* shrdMem = static_cast<char*>(SHMregion.get_address());
for (int i=0; i<maxIter; i++) {
MPI_Bcast ( shrdMem+i*maxint, maxint, MPI_CHAR, 0, shmcomm);
MPI_Barrier(shmcomm);
}
MPI_Bcast ( shrdMem+(maxIter)*maxint, totalMemory - maxIter*maxint, MPI_CHAR, 0, shmcomm);
#endif
}
#ifndef SERIAL
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void SHMVecFromMatrix(MatrixXx& vec, CItype* &SHMvec, std::string& SHMname,
boost::interprocess::shared_memory_object& SHMsegment,
boost::interprocess::mapped_region& SHMregion);
#endif