1 | //
|
---|
2 | // memrdma.cc
|
---|
3 | // Based on memamsg.cc
|
---|
4 | //
|
---|
5 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
6 | //
|
---|
7 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
8 | // Maintainer: LPS
|
---|
9 | //
|
---|
10 | // This file is part of the SC Toolkit.
|
---|
11 | //
|
---|
12 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
13 | // it under the terms of the GNU Library General Public License as published by
|
---|
14 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
15 | // any later version.
|
---|
16 | //
|
---|
17 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | // GNU Library General Public License for more details.
|
---|
21 | //
|
---|
22 | // You should have received a copy of the GNU Library General Public License
|
---|
23 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
24 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | //
|
---|
26 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
27 | //
|
---|
28 |
|
---|
29 | #ifndef _util_group_memrdma_cc
|
---|
30 | #define _util_group_memrdma_cc
|
---|
31 |
|
---|
32 | #ifdef __GNUC__
|
---|
33 | #pragma implementation
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <util/class/scexception.h>
|
---|
37 | #include <util/misc/formio.h>
|
---|
38 | #include <util/group/pool.h>
|
---|
39 | #include <util/group/memrdma.h>
|
---|
40 | #include <util/group/memiter.h>
|
---|
41 | #include <stdexcept>
|
---|
42 |
|
---|
43 | using namespace std;
|
---|
44 | using namespace sc;
|
---|
45 |
|
---|
46 | #ifdef HAVE_HRECV
|
---|
47 | # define DISABLE do { masktrap(1); ExEnv::outn().flush(); } while(0)
|
---|
48 | # define ENABLE do { ExEnv::outn().flush(); masktrap(0); } while(0)
|
---|
49 | extern "C" {
|
---|
50 | long masktrap(long state);
|
---|
51 | }
|
---|
52 | #else
|
---|
53 | # define DISABLE ExEnv::outn().flush()
|
---|
54 | # define ENABLE ExEnv::outn().flush()
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #define PRINTF(args) do { DISABLE; \
|
---|
58 | ExEnv::outn() << scprintf args ; \
|
---|
59 | ExEnv::outn().flush(); \
|
---|
60 | ENABLE; \
|
---|
61 | } while(0)
|
---|
62 |
|
---|
63 | #undef PRINTF
|
---|
64 | #define PRINTF(args)
|
---|
65 |
|
---|
66 | ///////////////////////////////////////////////////////////////////////
|
---|
67 | // Members for RDMAMemoryGrp
|
---|
68 |
|
---|
69 | static ClassDesc RDMAMemoryGrp_cd(
|
---|
70 | typeid(RDMAMemoryGrp),"RDMAMemoryGrp",1,"public MsgMemoryGrp",
|
---|
71 | 0, 0, 0);
|
---|
72 |
|
---|
73 | RDMAMemoryGrp::RDMAMemoryGrp(const Ref<MessageGrp>& msg):
|
---|
74 | MsgMemoryGrp(msg)
|
---|
75 | {
|
---|
76 | data_ = 0;
|
---|
77 | default_pool_size_ = 1000000;
|
---|
78 | }
|
---|
79 |
|
---|
80 | RDMAMemoryGrp::RDMAMemoryGrp(const Ref<KeyVal>& keyval):
|
---|
81 | MsgMemoryGrp(keyval)
|
---|
82 | {
|
---|
83 | data_ = 0;
|
---|
84 | default_pool_size_ = 1000000;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void*
|
---|
88 | RDMAMemoryGrp::malloc_region(size_t nbyte)
|
---|
89 | {
|
---|
90 | void *data = 0;
|
---|
91 | for (int i=0; data==0 && i<pools_.size(); i++) {
|
---|
92 | data = pools_[i]->allocate(nbyte);
|
---|
93 | }
|
---|
94 | if (data == 0) {
|
---|
95 | if (default_pool_size_ < nbyte) default_pool_size_ = nbyte * 2;
|
---|
96 | else if (pools_.size() > 4) default_pool_size_ *= 2;
|
---|
97 | void *pooldata = malloc_local(default_pool_size_);
|
---|
98 | Pool *pool = new(pooldata) Pool(default_pool_size_);
|
---|
99 | pools_.push_back(pool);
|
---|
100 | data = pool->allocate(nbyte);
|
---|
101 | }
|
---|
102 | return data;
|
---|
103 | }
|
---|
104 |
|
---|
105 | void
|
---|
106 | RDMAMemoryGrp::free_region(void*data)
|
---|
107 | {
|
---|
108 | char *cdata = reinterpret_cast<char*>(data);
|
---|
109 | for (int i=0; i<pools_.size(); i++) {
|
---|
110 | char *pstart = reinterpret_cast<char*>(pools_[i]);
|
---|
111 | if (cdata > pstart && cdata < &pstart[pools_[i]->size()]) {
|
---|
112 | pools_[i]->release(data);
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | throw ProgrammingError("could not find data to release in a Pool",
|
---|
117 | __FILE__, __LINE__, this->class_desc());
|
---|
118 | }
|
---|
119 |
|
---|
120 | void
|
---|
121 | RDMAMemoryGrp::set_localsize(size_t localsize)
|
---|
122 | {
|
---|
123 | for (int i=0; i<pools_.size(); i++) {
|
---|
124 | free_local(pools_[i]);
|
---|
125 | }
|
---|
126 | pools_.resize(0);
|
---|
127 |
|
---|
128 | MsgMemoryGrp::set_localsize(localsize);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void *
|
---|
132 | RDMAMemoryGrp::localdata()
|
---|
133 | {
|
---|
134 | return data_;
|
---|
135 | }
|
---|
136 |
|
---|
137 | RDMAMemoryGrp::~RDMAMemoryGrp()
|
---|
138 | {
|
---|
139 | deactivate();
|
---|
140 | delete[] data_;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void *
|
---|
144 | RDMAMemoryGrp::obtain_writeonly(distsize_t offset, int size)
|
---|
145 | {
|
---|
146 | void *data = malloc_region(size);
|
---|
147 | return data;
|
---|
148 | }
|
---|
149 |
|
---|
150 | void *
|
---|
151 | RDMAMemoryGrp::obtain_readwrite(distsize_t offset, int size)
|
---|
152 | {
|
---|
153 | PRINTF(("RDMAMemoryGrp::obtain_readwrite entered\n"));
|
---|
154 | void *data = malloc_region(size);
|
---|
155 | MemoryIter i(data, offsets_, n());
|
---|
156 | for (i.begin(offset, size); i.ready(); i.next()) {
|
---|
157 | PRINTF(("RDMAMemoryGrp::obtain_readwrite: node = %d, "
|
---|
158 | "int offset = %d, int size = %d\n",
|
---|
159 | i.node(), i.offset()/sizeof(int), i.size()/sizeof(int)));
|
---|
160 | retrieve_data(i.data(), i.node(), i.offset(), i.size(), 1);
|
---|
161 | }
|
---|
162 | PRINTF(("RDMAMemoryGrp::obtain_readwrite exiting\n"));
|
---|
163 | return data;
|
---|
164 | }
|
---|
165 |
|
---|
166 | void *
|
---|
167 | RDMAMemoryGrp::obtain_readonly(distsize_t offset, int size)
|
---|
168 | {
|
---|
169 | void *data = malloc_region(size);
|
---|
170 | PRINTF(("%d: RDMAMemoryGrp::obtain_readonly:"
|
---|
171 | "overall: offset = %d size = %d\n",
|
---|
172 | me(), offset, size));
|
---|
173 | MemoryIter i(data, offsets_, n());
|
---|
174 | for (i.begin(offset, size); i.ready(); i.next()) {
|
---|
175 | PRINTF(("%d: RDMAMemoryGrp::obtain_readonly:working on:"
|
---|
176 | "node = %d offset = %d size = %d\n",
|
---|
177 | me(), i.node(), i.offset(), i.size()));
|
---|
178 | PRINTF(("RDMAMemoryGrp::obtain_readonly: node = %d, "
|
---|
179 | "int offset = %d, int size = %d\n",
|
---|
180 | i.node(), i.offset()/sizeof(int), i.size()/sizeof(int)));
|
---|
181 | retrieve_data(i.data(), i.node(), i.offset(), i.size(), 0);
|
---|
182 | }
|
---|
183 | return data;
|
---|
184 | }
|
---|
185 |
|
---|
186 | void
|
---|
187 | RDMAMemoryGrp::sum_reduction(double *data, distsize_t doffset, int dsize)
|
---|
188 | {
|
---|
189 | distsize_t offset = doffset * sizeof(double);
|
---|
190 | int size = dsize * sizeof(double);
|
---|
191 | MemoryIter i(data, offsets_, n());
|
---|
192 | for (i.begin(offset, size); i.ready(); i.next()) {
|
---|
193 | sum_data((double*)i.data(), i.node(), i.offset(), i.size());
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | void
|
---|
198 | RDMAMemoryGrp::sum_reduction_on_node(double *data, size_t doffset,
|
---|
199 | int dlength, int node)
|
---|
200 | {
|
---|
201 | if (node == -1) node = me();
|
---|
202 |
|
---|
203 | sum_data(data, node, sizeof(double)*doffset, sizeof(double)*dlength);
|
---|
204 | }
|
---|
205 |
|
---|
206 | void
|
---|
207 | RDMAMemoryGrp::release_readonly(void *data, distsize_t offset, int size)
|
---|
208 | {
|
---|
209 | free_region(data);
|
---|
210 | }
|
---|
211 |
|
---|
212 | void
|
---|
213 | RDMAMemoryGrp::release_writeonly(void *data, distsize_t offset, int size)
|
---|
214 | {
|
---|
215 | MemoryIter i(data, offsets_, n());
|
---|
216 | for (i.begin(offset, size); i.ready(); i.next()) {
|
---|
217 | PRINTF(("RDMAMemoryGrp::release_write: node = %d, "
|
---|
218 | "int offset = %d, int size = %d\n",
|
---|
219 | i.node(), i.offset()/sizeof(int), i.size()/sizeof(int)));
|
---|
220 | replace_data(i.data(), i.node(), i.offset(), i.size(), 0);
|
---|
221 | }
|
---|
222 | free_region(data);
|
---|
223 | }
|
---|
224 |
|
---|
225 | void
|
---|
226 | RDMAMemoryGrp::release_readwrite(void *data, distsize_t offset, int size)
|
---|
227 | {
|
---|
228 | MemoryIter i(data, offsets_, n());
|
---|
229 | for (i.begin(offset, size); i.ready(); i.next()) {
|
---|
230 | replace_data(i.data(), i.node(), i.offset(), i.size(), 1);
|
---|
231 | }
|
---|
232 | free_region(data);
|
---|
233 | }
|
---|
234 |
|
---|
235 | void
|
---|
236 | RDMAMemoryGrp::print(ostream &o) const
|
---|
237 | {
|
---|
238 | MemoryGrp::print(o);
|
---|
239 | }
|
---|
240 |
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | /////////////////////////////////////////////////////////////////////////////
|
---|
244 |
|
---|
245 | // Local Variables:
|
---|
246 | // mode: c++
|
---|
247 | // c-file-style: "CLJ"
|
---|
248 | // End:
|
---|