source: ThirdParty/mpqc_open/src/lib/util/group/memory.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: 9.1 KB
Line 
1//
2// memory.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#ifdef __GNUC__
29#pragma implementation
30#endif
31
32#ifdef HAVE_CONFIG_H
33#include <scconfig.h>
34#endif
35
36#include <scconfig.h>
37#include <util/misc/formio.h>
38#include <util/group/memory.h>
39
40#include <util/group/memproc.h>
41
42#ifdef HAVE_SYSV_IPC
43# include <util/group/messshm.h>
44# include <util/group/memshm.h>
45#endif
46
47#if defined(HAVE_MPI)
48# include <util/group/messmpi.h>
49# include <util/group/memmtmpi.h>
50#endif
51
52#if defined(HAVE_ARMCI)
53# include <util/group/memarmci.h>
54#endif
55
56using namespace std;
57using namespace sc;
58
59//////////////////////////////////////////////////////////////////////
60// MemoryGrpBuf template instantiations
61
62#ifdef EXPLICIT_TEMPLATE_INSTANTIATION
63template class MemoryGrpBuf<double>;
64template class MemoryGrpBuf<int>;
65template class MemoryGrpBuf<char>;
66template class MemoryGrpBuf<unsigned char>;
67#endif
68
69//////////////////////////////////////////////////////////////////////
70// MemoryGrp members
71
72static ClassDesc MemoryGrp_cd(
73 typeid(MemoryGrp),"MemoryGrp",1,"public DescribedClass",
74 0, 0, 0);
75
76MemoryGrp::MemoryGrp()
77{
78 debug_ = 0;
79
80 offsets_ = 0;
81
82 init_locks();
83}
84
85MemoryGrp::MemoryGrp(const Ref<KeyVal>& keyval)
86{
87 debug_ = keyval->intvalue("debug");
88
89 offsets_ = 0;
90
91 init_locks();
92}
93
94MemoryGrp::~MemoryGrp()
95{
96 delete[] offsets_;
97 delete[] locks_;
98}
99
100void
101MemoryGrp::init_locks()
102{
103 Ref<ThreadGrp> thgrp = ThreadGrp::get_default_threadgrp();
104 nlock_ = 2 * thgrp->nthread();
105 locks_ = new Ref<ThreadLock>[nlock_];
106 for (int i=0; i<nlock_; i++) locks_[i] = thgrp->new_lock();
107}
108
109MemoryGrp *
110MemoryGrp::initial_memorygrp()
111{
112 int argc = 0;
113 return initial_memorygrp(argc,0);
114}
115
116MemoryGrp *
117MemoryGrp::initial_memorygrp(int &argc, char *argv[])
118{
119 MemoryGrp *grp = 0;
120
121 char *keyval_string = 0;
122
123 // see if a memory group is given on the command line
124 if (argc && argv) {
125 for (int i=0; i<argc; i++) {
126 if (argv[i] && !strcmp(argv[i], "-memorygrp")) {
127 char *memorygrp_string = argv[i];
128 i++;
129 if (i >= argc) {
130 ExEnv::errn() << "-memorygrp must be following by an argument"
131 << endl;
132 abort();
133 }
134 keyval_string = argv[i];
135 // move the memorygrp arguments to the end of argv
136 int j;
137 for (j=i+1; j<argc; j++) {
138 argv[j-2] = argv[j];
139 }
140 argv[j++] = memorygrp_string;
141 argv[j++] = keyval_string;
142 // decrement argc to hide the last two arguments
143 argc -= 2;
144 break;
145 }
146 }
147 }
148
149 if (!keyval_string) {
150 // find out if the environment gives the containing memory group
151 keyval_string = getenv("MEMORYGRP");
152 if (keyval_string) {
153 if (!strncmp("MEMORYGRP=", keyval_string, 11)) {
154 keyval_string = strchr(keyval_string, '=');
155 }
156 if (*keyval_string == '=') keyval_string++;
157 }
158 }
159
160 // if keyval input for a memory group was found, then
161 // create it.
162 if (keyval_string) {
163 //ExEnv::outn() << "Creating MemoryGrp from \"" << keyval_string << "\"" << endl;
164 Ref<ParsedKeyVal> strkv = new ParsedKeyVal();
165 strkv->parse_string(keyval_string);
166 Ref<DescribedClass> dc = strkv->describedclassvalue();
167 grp = dynamic_cast<MemoryGrp*>(dc.pointer());
168 if (dc.null()) {
169 ExEnv::errn() << "initial_memorygrp: couldn't find a MemoryGrp in "
170 << keyval_string << endl;
171 abort();
172 }
173 else if (!grp) {
174 ExEnv::errn() << "initial_memorygrp: wanted MemoryGrp but got "
175 << dc->class_name() << endl;
176 abort();
177 }
178 // prevent an accidental delete
179 grp->reference();
180 strkv = 0;
181 dc = 0;
182 // accidental delete not a problem anymore since all smart pointers
183 // to grp are dead
184 grp->dereference();
185 return grp;
186 }
187
188 return grp;
189}
190
191void
192MemoryGrp::activate()
193{
194}
195
196void
197MemoryGrp::deactivate()
198{
199}
200
201void
202MemoryGrp::print(ostream&o) const
203{
204 o << scprintf("MemoryGrp (node %d):\n", me());
205 o << scprintf("%d: n = %d\n", me(), n());
206 for (int i=0; i<=n_; i++) {
207 o << scprintf("%d: offset[%d] = %5d\n", me(), i, offsets_[i]);
208 }
209}
210
211void
212MemoryGrp::sum_reduction(double *data, distsize_t doffset, int dlength)
213{
214 distsize_t offset = doffset * sizeof(double);
215 int length = dlength * sizeof(double);
216
217 if (offset + length > totalsize()) {
218 ExEnv::errn() << "MemoryGrp::sum_reduction: arg out of range:"
219 << " offset = " << double(offset)
220 << " length = " << length
221 << " totalsize() = " << double(totalsize())
222 << endl;
223 abort();
224 }
225
226 double *source_data = (double*) obtain_readwrite(offset, length);
227
228 for (int i=0; i<dlength; i++) {
229 source_data[i] += data[i];
230 }
231
232 release_readwrite((void*) source_data, offset, length);
233}
234
235void
236MemoryGrp::sum_reduction_on_node(double *data, size_t doffset, int dlength,
237 int node)
238{
239 if (node == -1) node = me();
240
241 sum_reduction(data, doffset + offset(node)/sizeof(double),
242 dlength);
243}
244
245void*
246MemoryGrp::malloc_local(size_t nbyte)
247{
248 return new char[nbyte];
249}
250
251void
252MemoryGrp::free_local(void *data)
253{
254 delete[] reinterpret_cast<char*>(data);
255}
256
257double*
258MemoryGrp::malloc_local_double(size_t ndouble)
259{
260 return reinterpret_cast<double*>(malloc_local(ndouble*sizeof(double)));
261}
262
263void
264MemoryGrp::free_local_double(double *data)
265{
266 free_local(data);
267}
268
269void
270MemoryGrp::catchup()
271{
272 return;
273}
274
275void
276MemoryGrp::obtain_local_lock(size_t start, size_t fence)
277{
278 distsize_t locked_region_size = 1 + localsize()/nlock_;
279 int lstart = start/locked_region_size;
280 int llast = fence/locked_region_size;
281 for (int i=lstart; i<=llast; i++) {
282 locks_[i]->lock();
283 }
284}
285
286void
287MemoryGrp::release_local_lock(size_t start, size_t fence)
288{
289 distsize_t locked_region_size = 1 + localsize()/nlock_;
290 int lstart = start/locked_region_size;
291 int llast = fence/locked_region_size;
292 for (int i=lstart; i<=llast; i++) {
293 locks_[i]->unlock();
294 }
295}
296
297static Ref<MemoryGrp> default_memorygrp;
298
299void
300MemoryGrp::set_default_memorygrp(const Ref<MemoryGrp>& grp)
301{
302 default_memorygrp = grp;
303}
304
305MemoryGrp*
306MemoryGrp::get_default_memorygrp()
307{
308 if (default_memorygrp.nonnull()) return default_memorygrp.pointer();
309
310 Ref<MessageGrp> msg = MessageGrp::get_default_messagegrp();
311
312#if defined(HAVE_MPI) && defined(DEFAULT_MTMPI)
313 Ref<ThreadGrp> thr = ThreadGrp::get_default_threadgrp();
314 default_memorygrp = new MTMPIMemoryGrp(msg,thr);
315 return default_memorygrp.pointer();
316#endif
317
318#if defined(DEFAULT_ARMCI)
319 default_memorygrp = new ARMCIMemoryGrp(msg);
320 return default_memorygrp.pointer();
321#endif
322
323 if (msg.null()) {
324 ExEnv::errn() << scprintf("MemoryGrp::get_default_memorygrp: requires default MessageGrp if default behavior not configured\n");
325 abort();
326 }
327#if defined(HAVE_MPI)
328 else if (msg->class_desc() == ::class_desc<MPIMessageGrp>()) {
329 Ref<ThreadGrp> thr = ThreadGrp::get_default_threadgrp();
330 default_memorygrp = new MTMPIMemoryGrp(msg,thr);
331 return default_memorygrp.pointer();
332 }
333#endif
334#if defined(HAVE_ARMCI)
335 else if (msg->class_desc() == ::class_desc<MPIMessageGrp>()) {
336 default_memorygrp = new ARMCIMemoryGrp(msg);
337 return default_memorygrp.pointer();
338 }
339#endif
340#ifdef HAVE_SYSV_IPC
341 else if (msg->class_desc() == ::class_desc<ShmMessageGrp>()) {
342 default_memorygrp = new ShmMemoryGrp(msg);
343 return default_memorygrp.pointer();
344 }
345#endif
346 else if (msg->n() == 1) {
347 default_memorygrp = new ProcMemoryGrp();
348 return default_memorygrp.pointer();
349 }
350 else {
351 ExEnv::errn() << scprintf("MemoryGrp::get_default_memorygrp: cannot create "
352 "default for \"%s\"\n.", msg->class_name());
353 abort();
354 }
355
356 if (default_memorygrp.null()) {
357 ExEnv::err0() << scprintf("WARNING: MemoryGrp::get_default_memorygrp(): failed\n");
358 default_memorygrp = new ProcMemoryGrp;
359 }
360 return default_memorygrp.pointer();
361}
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.