source: ThirdParty/mpqc_open/src/lib/util/group/pooltest.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: 4.8 KB
Line 
1//
2// pooltest.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#include <stdlib.h>
29#include <math.h>
30
31#include <util/group/pool.h>
32#include <util/misc/formio.h>
33
34using namespace std;
35using namespace sc;
36
37class Double {
38 private:
39 static Pool* pool_;
40 static Double* list;
41 Double* next;
42 Double* prev;
43 double* d;
44 int size;
45 static void zaplist(Double*);
46 public:
47 Double(size_t size);
48 ~Double();
49 void zap();
50 static void zapall();
51 void clear();
52 static void pool(Pool*);
53};
54
55Double* Double::list = 0;
56Pool* Double::pool_ = 0;
57
58void
59Double::clear()
60{
61 if (d) pool_->release(d);
62 d = 0;
63 size = 0;
64}
65
66void
67Double::zapall()
68{
69 zaplist(list);
70}
71
72void
73Double::zaplist(Double*l)
74{
75 for (Double* i=l; i; i = i->next) {
76 i->zap();
77 }
78}
79
80Double::Double(size_t s):
81 size(s)
82{
83 if (!pool_) {
84 cerr << scprintf("Double::Double: Pool not initialized\n");
85 abort();
86 }
87 d = pool_->allocate_double(size);
88 if (!d) {
89 //cout << scprintf("\nDouble::Double allocation of size %d failed\n",
90 // size);
91 cout << "F" << endl;
92 size = 0;
93 }
94 next = list;
95 prev = 0;
96 list = this;
97 if (next) next->prev = this;
98}
99
100Double::~Double()
101{
102 clear();
103 if (next) next->prev = prev;
104 if (prev) prev->next = next;
105 else list = next;
106}
107
108void
109Double::zap()
110{
111 int i;
112 int* x = (int*)d;
113 for (i=0; i<size*2; i++) {
114 if (x[i] == PoolData::magic) {
115 cerr << scprintf("Double::zap: tried to zap a magic number\n");
116 abort();
117 }
118 }
119 for (i=0; i<size; i++) d[i] = 0.0;
120}
121
122void
123Double::pool(Pool*p)
124{
125 if (pool_ && list) {
126 cerr << scprintf("Double::pool: cannot reinitialize pool\n");
127 abort();
128 }
129 pool_ = p;
130}
131
132void test1(Pool*);
133void test2(Pool*);
134
135int
136main()
137{
138 const int poolsize = 4000000;
139 Pool *pool = new(malloc(poolsize)) Pool(poolsize);
140
141 Double::pool(pool);
142
143 srand48(100);
144
145 cout << "test1:" << endl;
146 test1(pool);
147 cout << "test2:" << endl;
148 test2(pool);
149
150 return 0;
151}
152
153
154void
155test1(Pool*pool)
156{
157 pool->check();
158
159 pool->print();
160 cout << endl;
161
162 Double t1(10);
163
164 Double::zapall();
165
166 pool->check();
167
168 pool->print();
169 cout << endl;
170
171 Double t2(10000);
172
173 Double::zapall();
174
175 pool->check();
176
177 Double t3(100);
178
179 Double::zapall();
180
181 pool->check();
182
183 pool->print();
184 cout << endl;
185
186 Double::zapall();
187
188 pool->check();
189
190 pool->print();
191 cout << endl;
192
193 t2.clear();
194
195 pool->check();
196
197 pool->print();
198 cout << endl;
199
200 Double t4(100);
201
202 pool->check();
203
204 pool->print();
205 cout << endl;
206
207 t1.clear();
208 t4.clear();
209 t3.clear();
210
211 pool->check();
212
213 pool->print();
214 cout << endl;
215
216}
217
218void
219test2(Pool*pool)
220{
221 int i, ii;
222
223 const int npass = 10;
224 const int nd = 512;
225 Double* d[nd];
226
227 for (i=0; i<nd; i++) d[i] = 0;
228
229 for (ii=0; ii<npass; ii++) {
230 for (i=0; i<nd;) {
231 if (mrand48() > 0) {
232 // allocate data
233 size_t size = lrand48() & 0x03ff;
234 d[i] = new Double(size);
235 cerr.flush();
236 cout << "a" << flush;
237 i++;
238 }
239 else {
240 // deallocate data
241 int loc = (int) (drand48()*i);
242 if (loc >= nd) loc = nd-1;
243 if (loc < 0) loc = 0;
244 if (d[loc]) {
245 cerr.flush();
246 cout << "d" << flush;
247 delete d[loc];
248 d[loc] = 0;
249 }
250 }
251 //pool->print();
252 //pool->check();
253 //Double::zapall();
254 }
255 for (i=0; i<nd; i++) {
256 if (d[i]) {
257 cerr.flush();
258 cout << "d" << flush;
259 delete d[i];
260 d[i] = 0;
261 }
262 }
263 cout << endl;
264 pool->print();
265 //pool->check();
266 //Double::zapall();
267 }
268}
269
270/////////////////////////////////////////////////////////////////////////////
271
272// Local Variables:
273// mode: c++
274// c-file-style: "CLJ"
275// End:
Note: See TracBrowser for help on using the repository browser.