source: ThirdParty/mpqc_open/src/lib/util/group/memamsg.cc@ 398fcd

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 398fcd was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 10.2 KB
Line 
1//
2// memamsg.cc
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifndef _util_group_memamsg_cc
29#define _util_group_memamsg_cc
30
31#ifdef __GNUC__
32#pragma implementation
33#endif
34
35#include <util/misc/formio.h>
36#include <util/group/pool.h>
37#include <util/group/memamsg.h>
38#include <util/group/memiter.h>
39
40using namespace std;
41using namespace sc;
42
43#ifdef HAVE_HRECV
44# define DISABLE do { masktrap(1); ExEnv::outn().flush(); } while(0)
45# define ENABLE do { ExEnv::outn().flush(); masktrap(0); } while(0)
46 extern "C" {
47 long masktrap(long state);
48 }
49#else
50# define DISABLE ExEnv::outn().flush()
51# define ENABLE ExEnv::outn().flush()
52#endif
53
54#define PRINTF(args) do { DISABLE; \
55 ExEnv::outn() << scprintf args ; \
56 ExEnv::outn().flush(); \
57 ENABLE; \
58 } while(0)
59
60#undef PRINTF
61#define PRINTF(args)
62
63///////////////////////////////////////////////////////////////////////
64// The MemoryDataRequest class
65
66MemoryDataRequest::MemoryDataRequest(Request r, int node,
67 int offset, int size, int lock,
68 int serial_number)
69{
70 assign(r, node, offset, size, lock, serial_number);
71}
72
73void
74MemoryDataRequest::assign(Request r, int node,
75 int offset, int size, int lock,
76 int serial_number)
77{
78 data_[0] = (int) r;
79 data_[1] = node;
80 data_[2] = offset;
81 data_[3] = size;
82 data_[4] = serial_number;
83 data_[5] = lock;
84}
85
86const char *
87MemoryDataRequest::request_string() const
88{
89 switch (request()) {
90 case MemoryDataRequest::Deactivate:
91 return "Deactivate";
92 case MemoryDataRequest::Retrieve:
93 return "Retrieve";
94 case MemoryDataRequest::Replace:
95 return "Replace";
96 case MemoryDataRequest::DoubleSum:
97 return "DoubleSum";
98 case MemoryDataRequest::Sync:
99 return "Sync";
100 default:
101 return "BadRequest";
102 }
103}
104
105void
106MemoryDataRequest::print(const char *msg, ostream & o) const
107{
108 if (msg == 0) msg = "";
109
110 o.flush();
111 if (request() == Sync) {
112 o << scprintf("%s \"%s\" %d-%d reactivate = %d\n",
113 msg, request_string(), node(), serial_number(), reactivate());
114 }
115 else {
116 o << scprintf("%s \"%s\" offset = %5d, %5d bytes, %d-%d, %s\n",
117 msg, request_string(),
118 offset(), size(), node(), serial_number(),
119 (lock()?"lock":"nolock"));
120 }
121 o.flush();
122}
123
124void
125MemoryDataRequest::operator =(const MemoryDataRequest &r)
126{
127 for (int i=0; i<NData; i++) {
128 data_[i] = r.data_[i];
129 }
130}
131
132///////////////////////////////////////////////////////////////////////
133// The MemoryDataRequestQueue class
134
135void
136MemoryDataRequestQueue::push(MemoryDataRequest&r)
137{
138 if (n_ == MaxDepth) {
139 ExEnv::errn() << scprintf("MemoryDataRequestQueue: MaxDepth exceeded\n");
140 abort();
141 }
142 q_[n_] = r;
143 n_++;
144}
145
146void
147MemoryDataRequestQueue::pop(MemoryDataRequest&r)
148{
149 if (n_ == 0) {
150 ExEnv::errn() << scprintf("MemoryDataRequestQueue: nothing to pop\n");
151 abort();
152 }
153 n_--;
154 r = q_[n_];
155}
156
157///////////////////////////////////////////////////////////////////////
158// Members for ActiveMsgMemoryGrp
159
160static ClassDesc ActiveMsgMemoryGrp_cd(
161 typeid(ActiveMsgMemoryGrp),"ActiveMsgMemoryGrp",1,"public MsgMemoryGrp",
162 0, 0, 0);
163
164ActiveMsgMemoryGrp::ActiveMsgMemoryGrp(const Ref<MessageGrp>& msg):
165 MsgMemoryGrp(msg)
166{
167 data_ = 0;
168}
169
170ActiveMsgMemoryGrp::ActiveMsgMemoryGrp(const Ref<KeyVal>& keyval):
171 MsgMemoryGrp(keyval)
172{
173 data_ = 0;
174}
175
176void
177ActiveMsgMemoryGrp::set_localsize(size_t localsize)
178{
179 if (debug_) {
180 ExEnv::out0() << "ActiveMsgMemoryGrp::set_localsize(" << localsize << ")" << endl;
181 }
182 deactivate();
183 MsgMemoryGrp::set_localsize(localsize);
184 delete[] data_;
185 data_ = new char[localsize];
186 activate();
187 if (debug_) {
188 ExEnv::out0() << "ActiveMsgMemoryGrp::set_localsize done: offsets:";
189 for (int i=0; i<=n(); i++) {
190 ExEnv::out0() << " " << double(offset(i));
191 }
192 ExEnv::out0() << endl;
193 }
194}
195
196void *
197ActiveMsgMemoryGrp::localdata()
198{
199 return data_;
200}
201
202ActiveMsgMemoryGrp::~ActiveMsgMemoryGrp()
203{
204 deactivate();
205 delete[] data_;
206}
207
208void *
209ActiveMsgMemoryGrp::obtain_writeonly(distsize_t offset, int size)
210{
211 void *data = (void *) new char[size];
212 return data;
213}
214
215void *
216ActiveMsgMemoryGrp::obtain_readwrite(distsize_t offset, int size)
217{
218 PRINTF(("ActiveMsgMemoryGrp::obtain_readwrite entered\n"));
219 void *data = (void *) new char[size];
220 MemoryIter i(data, offsets_, n());
221 for (i.begin(offset, size); i.ready(); i.next()) {
222 if (i.node() == me()) {
223 PRINTF(("ActiveMsgMemoryGrp::obtain_readwrite: local copy\n"));
224 obtain_local_lock(i.offset(), i.offset()+i.size());
225 memcpy(i.data(), &data_[i.offset()], i.size());
226 }
227 else {
228 PRINTF(("ActiveMsgMemoryGrp::obtain_readwrite: node = %d, "
229 "int offset = %d, int size = %d\n",
230 i.node(), i.offset()/sizeof(int), i.size()/sizeof(int)));
231 retrieve_data(i.data(), i.node(), i.offset(), i.size(), 1);
232 }
233 }
234 PRINTF(("ActiveMsgMemoryGrp::obtain_readwrite exiting\n"));
235 return data;
236}
237
238void *
239ActiveMsgMemoryGrp::obtain_readonly(distsize_t offset, int size)
240{
241 void *data = (void *) new char[size];
242 PRINTF(("%d: ActiveMsgMemoryGrp::obtain_readonly:"
243 "overall: offset = %d size = %d\n",
244 me(), offset, size));
245 MemoryIter i(data, offsets_, n());
246 for (i.begin(offset, size); i.ready(); i.next()) {
247 PRINTF(("%d: ActiveMsgMemoryGrp::obtain_readonly:working on:"
248 "node = %d offset = %d size = %d\n",
249 me(), i.node(), i.offset(), i.size()));
250 if (i.node() == me()) {
251 PRINTF(("%d: ActiveMsgMemoryGrp::obtain_readonly: local: "
252 "offset = %d size = %d\n", me(), i.offset(), i.size()));
253 memcpy(i.data(), &data_[i.offset()], i.size());
254 }
255 else {
256 PRINTF(("ActiveMsgMemoryGrp::obtain_readonly: node = %d, "
257 "int offset = %d, int size = %d\n",
258 i.node(), i.offset()/sizeof(int), i.size()/sizeof(int)));
259 retrieve_data(i.data(), i.node(), i.offset(), i.size(), 0);
260 }
261 }
262 return data;
263}
264
265void
266ActiveMsgMemoryGrp::sum_reduction(double *data, distsize_t doffset, int dsize)
267{
268 distsize_t offset = doffset * sizeof(double);
269 int size = dsize * sizeof(double);
270 MemoryIter i(data, offsets_, n());
271 for (i.begin(offset, size); i.ready(); i.next()) {
272 if (i.node() == me()) {
273 int chunkdsize = i.size()/sizeof(double);
274 double *chunkdata = (double*) &data_[i.offset()];
275 double *tmp = (double*) i.data();
276 PRINTF(("%d: summing %d doubles from 0x%x to 0x%x\n",
277 me(), chunkdsize, tmp, chunkdata));
278 obtain_local_lock(i.offset(), i.offset()+i.size());
279 for (int j=0; j<chunkdsize; j++) {
280 *chunkdata++ += *tmp++;
281 }
282 release_local_lock(i.offset(), i.offset()+i.size());
283 }
284 else {
285 sum_data((double*)i.data(), i.node(), i.offset(), i.size());
286 }
287 }
288}
289
290void
291ActiveMsgMemoryGrp::sum_reduction_on_node(double *data, size_t doffset,
292 int dlength, int node)
293{
294 if (node == -1) node = me();
295
296 if (node == me()) {
297 double *localdata = (double*) &data_[sizeof(double)*doffset];
298 obtain_local_lock(sizeof(double)*doffset,
299 sizeof(double)*(doffset+dlength));
300 for (int j=0; j<dlength; j++) {
301 *localdata++ += *data++;
302 }
303 release_local_lock(sizeof(double)*doffset,
304 sizeof(double)*(doffset+dlength));
305 }
306 else {
307 sum_data(data, node, sizeof(double)*doffset, sizeof(double)*dlength);
308 }
309}
310
311void
312ActiveMsgMemoryGrp::release_readonly(void *data, distsize_t offset, int size)
313{
314 delete[] (char*) data;
315}
316
317void
318ActiveMsgMemoryGrp::release_writeonly(void *data, distsize_t offset, int size)
319{
320 MemoryIter i(data, offsets_, n());
321 for (i.begin(offset, size); i.ready(); i.next()) {
322 if (i.node() == me()) {
323 PRINTF(("ActiveMsgMemoryGrp::release_write: local\n"));
324 PRINTF((" i.offset() = %d i.data() = 0x%x i.size() = %d\n",
325 i.offset(), i.data(), i.size()));
326 PRINTF((" &data_[i.offset()] = 0x%x\n", &data_[i.offset()]));
327 memcpy(&data_[i.offset()], i.data(), i.size());
328 }
329 else {
330 PRINTF(("ActiveMsgMemoryGrp::release_write: node = %d, "
331 "int offset = %d, int size = %d\n",
332 i.node(), i.offset()/sizeof(int), i.size()/sizeof(int)));
333 replace_data(i.data(), i.node(), i.offset(), i.size(), 0);
334 }
335 }
336 delete[] (char*) data;
337}
338
339void
340ActiveMsgMemoryGrp::release_readwrite(void *data, distsize_t offset, int size)
341{
342 MemoryIter i(data, offsets_, n());
343 for (i.begin(offset, size); i.ready(); i.next()) {
344 if (i.node() == me()) {
345 memcpy(&data_[i.offset()], i.data(), i.size());
346 release_local_lock(i.offset(), i.offset()+i.size());
347 }
348 else {
349 replace_data(i.data(), i.node(), i.offset(), i.size(), 1);
350 }
351 }
352 delete[] (char*) data;
353}
354
355void
356ActiveMsgMemoryGrp::print(ostream &o) const
357{
358 MemoryGrp::print(o);
359}
360
361#endif
362
363/////////////////////////////////////////////////////////////////////////////
364
365// Local Variables:
366// mode: c++
367// c-file-style: "CLJ"
368// End:
Note: See TracBrowser for help on using the repository browser.