1 // $Id: HOPSPACK_ParameterList.cpp 203 2012-05-14 22:27:30Z tplante $
2 // $URL: https://software.sandia.gov/svn/hopspack/trunk/src/src-shared/HOPSPACK_ParameterList.cpp $
3
4 //@HEADER
5 // ************************************************************************
6 //
7 // HOPSPACK: Hybrid Optimization Parallel Search Package
8 // Copyright 2009 Sandia Corporation
9 //
10 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
11 // the U.S. Government retains certain rights in this software.
12 //
13 // This file is part of HOPSPACK.
14 //
15 // HOPSPACK is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU Lesser General Public License as
17 // published by the Free Software Foundation; either version 2.1 of the
18 // License, or (at your option) any later version.
19 //
20 // This library is distributed in the hope that it will be useful, but
21 // WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 // Lesser General Public License for more details.
24 //
25 // You should have received a copy of the GNU Lesser General Public
26 // License along with this library. If not, see http://www.gnu.org/licenses/.
27 //
28 // Questions? Contact Tammy Kolda (tgkolda@sandia.gov)
29 // or Todd Plantenga (tplante@sandia.gov)
30 //
31 // ************************************************************************
32 //@HEADER
33
34 /*!
35 \file HOPSPACK_ParameterList.cpp
36 \brief Implement HOPSPACK::ParameterList.
37 */
38
39 #include "HOPSPACK_ParameterEntry.hpp"
40 #include "HOPSPACK_ParameterList.hpp"
41
42
43 //----------------------------------------------------------------------
44 // Static local data
45 //----------------------------------------------------------------------
46 static HOPSPACK::ParameterList cEMPTY_SUBLIST;
47
48
49 //----------------------------------------------------------------------
50 // Constructors and Destructor
51 //----------------------------------------------------------------------
52
ParameterList()53 HOPSPACK::ParameterList::ParameterList()
54 {}
55
ParameterList(const ParameterList & source)56 HOPSPACK::ParameterList::ParameterList(const ParameterList& source)
57 {
58 params = source.params;
59 }
60
operator =(const ParameterList & source)61 HOPSPACK::ParameterList& HOPSPACK::ParameterList::operator=(const ParameterList& source)
62 {
63 if (&source == this)
64 return *this;
65
66 params = source.params;
67 return *this;
68 }
69
~ParameterList()70 HOPSPACK::ParameterList::~ParameterList()
71 {
72 }
73
74
75 //----------------------------------------------------------------------
76 // Sublist methods
77 //----------------------------------------------------------------------
78
sublist(const string & name) const79 const HOPSPACK::ParameterList& HOPSPACK::ParameterList::sublist(const string& name) const
80 {
81 // Find name in list, if it exists.
82 ConstIterator i = params.find(name);
83
84 // If it does not exist, return an empty list.
85 if (i == params.end())
86 {
87 return( cEMPTY_SUBLIST );
88 }
89
90 // If it does exist and is a list, return the list value.
91 if (entry(i).isList())
92 return (entry(i).getListValue());
93
94 // Otherwise, the parameter exists but is not a list. Throw an error.
95 cerr << "ERROR: Parameter " << name << " is not a list." << endl;
96 throw INTERNAL_ERROR;
97 }
98
getOrSetSublist(const string & name)99 HOPSPACK::ParameterList& HOPSPACK::ParameterList::getOrSetSublist(const string& name)
100 {
101 // Find name in list, if it exists.
102 Iterator i = params.find(name);
103
104 // If it does exist and is a list, return the list value.
105 // Otherwise, throw an error.
106 if (i != params.end()) {
107 if (entry(i).isList())
108 return (entry(i).getListValue());
109 else
110 {
111 cerr << "ERROR: Parameter " << name << " is not a list." << endl;
112 throw INTERNAL_ERROR;
113 }
114 }
115
116 // If it does not exist, create a new empty list and return a reference
117 return params[name].setList(true);
118 }
119
120
121 //----------------------------------------------------------------------
122 // Set parameter methods
123 //----------------------------------------------------------------------
124
setParameter(const string & name,bool value)125 void HOPSPACK::ParameterList::setParameter(const string& name, bool value)
126 {
127 ConstIterator i = params.find(name);
128 if ((i != params.end()) && (entry(i).isBool() == false))
129 return;
130
131 params[name].setValue(value);
132 return;
133 }
134
setParameter(const string & name,int value)135 void HOPSPACK::ParameterList::setParameter(const string& name, int value)
136 {
137 ConstIterator i = params.find(name);
138 if ((i != params.end()) && (entry(i).isInt() == false))
139 return;
140
141 params[name].setValue(value);
142 return;
143 }
144
setParameter(const string & name,double value)145 void HOPSPACK::ParameterList::setParameter(const string& name, double value)
146 {
147 ConstIterator i = params.find(name);
148 if ((i != params.end()) && (entry(i).isDouble() == false))
149 return;
150
151 params[name].setValue(value);
152 return;
153 }
154
setParameter(const string & name,const char * value)155 void HOPSPACK::ParameterList::setParameter(const string& name, const char* value)
156 {
157 ConstIterator i = params.find(name);
158 if ((i != params.end()) && (entry(i).isString() == false))
159 return;
160
161 params[name].setValue(value);
162 return;
163 }
164
setParameter(const string & name,const string & value)165 void HOPSPACK::ParameterList::setParameter(const string& name, const string& value)
166 {
167 ConstIterator i = params.find(name);
168 if ((i != params.end()) && (entry(i).isString() == false))
169 return;
170
171 params[name].setValue(value);
172 return;
173 }
174
setParameter(const string & name,const vector<char> value)175 void HOPSPACK::ParameterList::setParameter(const string& name, const vector< char > value)
176 {
177 ConstIterator i = params.find(name);
178 if ((i != params.end()) && (entry(i).isVector() == false))
179 return;
180
181 params[name].setValue(value);
182 return;
183 }
184
setParameter(const string & name,const Vector & value)185 void HOPSPACK::ParameterList::setParameter(const string& name, const Vector& value)
186 {
187 ConstIterator i = params.find(name);
188 if ((i != params.end()) && (entry(i).isVector() == false))
189 return;
190
191 params[name].setValue(value);
192 return;
193 }
194
setParameter(const string & name,const Matrix & value)195 void HOPSPACK::ParameterList::setParameter(const string& name, const Matrix& value)
196 {
197 ConstIterator i = params.find(name);
198 if ((i != params.end()) && (entry(i).isMatrix() == false))
199 return;
200
201 params[name].setValue(value);
202 return;
203 }
204
205
206 //----------------------------------------------------------------------
207 // Get parameter methods
208 //----------------------------------------------------------------------
209
getParameter(const string & name,bool nominal) const210 bool HOPSPACK::ParameterList::getParameter(const string& name, bool nominal) const
211 {
212 ConstIterator i = params.find(name);
213 if ((i != params.end()) && (entry(i).isBool()))
214 return entry(i).getBoolValue();
215 return nominal;
216 }
217
getParameter(const string & name,int nominal) const218 int HOPSPACK::ParameterList::getParameter(const string& name, int nominal) const
219 {
220 ConstIterator i = params.find(name);
221 if ((i != params.end()) && (entry(i).isInt()))
222 return entry(i).getIntValue();
223 return nominal;
224 }
225
getParameter(const string & name,double nominal) const226 double HOPSPACK::ParameterList::getParameter(const string& name, double nominal) const
227 {
228 ConstIterator i = params.find(name);
229 if ((i != params.end()) && (entry(i).isDouble()))
230 return entry(i).getDoubleValue();
231 return nominal;
232 }
233
getParameter(const string & name,const char * nominal) const234 const string& HOPSPACK::ParameterList::getParameter(const string& name, const char* nominal) const
235 {
236 ConstIterator i = params.find(name);
237 if ((i != params.end()) && (entry(i).isString()))
238 return entry(i).getStringValue();
239
240 // Save nominal char* value as a string, and return the string value.
241 tmpstrings.push_back(nominal);
242 return tmpstrings.back();
243 }
244
getParameter(const string & name,const string & nominal) const245 const string& HOPSPACK::ParameterList::getParameter(const string& name, const string& nominal) const
246 {
247 ConstIterator i = params.find(name);
248 if ((i != params.end()) && (entry(i).isString()))
249 return entry(i).getStringValue();
250 return nominal;
251 }
252
getParameter(const string & name,const Vector & nominal) const253 const HOPSPACK::Vector& HOPSPACK::ParameterList::getParameter(const string& name, const Vector& nominal) const
254 {
255 ConstIterator i = params.find(name);
256 if ((i != params.end()) && (entry(i).isVector()))
257 return entry(i).getVectorValue();
258 return nominal;
259 }
260
261
getDoubleParameter(const string & name) const262 double HOPSPACK::ParameterList::getDoubleParameter(const string& name) const
263 {
264 ConstIterator i = params.find(name);
265
266 if ((i != params.end()) && (entry(i).isDouble()))
267 return entry(i).getDoubleValue();
268
269 cerr << "HOPSPACK::ParameterList::getValueParameter - no such parameter (" << name << ")"<< endl;
270 throw INTERNAL_ERROR;
271 }
272
273
274 //----------------------------------------------------------------------
275 // GetOrSet parameter methods
276 //----------------------------------------------------------------------
277
getOrSetParameter(const string & name,bool nominal)278 bool HOPSPACK::ParameterList::getOrSetParameter(const string& name,
279 bool nominal)
280 {
281 ConstIterator i = params.find(name);
282
283 if (i == params.end()) {
284 params[name].setValue(nominal, true);
285 i = params.find(name);
286 }
287
288 if ((i != params.end()) && (entry(i).isBool()))
289 return entry(i).getBoolValue();
290
291 cerr << "HOPSPACK::ParameterList::getParameter - get error for bool" << endl;
292 throw INTERNAL_ERROR;
293 }
294
getOrSetParameter(const string & name,int nominal)295 int HOPSPACK::ParameterList::getOrSetParameter(const string& name,
296 int nominal)
297 {
298 ConstIterator i = params.find(name);
299
300 if (i == params.end()) {
301 params[name].setValue(nominal, true);
302 i = params.find(name);
303 }
304
305 if ((i != params.end()) && (entry(i).isInt()))
306 return entry(i).getIntValue();
307
308 cerr << "HOPSPACK::ParameterList::getParameter - get error for int" << endl;
309 throw INTERNAL_ERROR;
310 }
311
getOrSetParameter(const string & name,double nominal)312 double HOPSPACK::ParameterList::getOrSetParameter(const string& name,
313 double nominal)
314 {
315 ConstIterator i = params.find(name);
316
317 if (i == params.end()) {
318 params[name].setValue(nominal, true);
319 i = params.find(name);
320 }
321
322 if ((i != params.end()) && (entry(i).isDouble()))
323 return entry(i).getDoubleValue();
324
325 cerr << "HOPSPACK::ParameterList::getParameter - get error for double" << endl;
326 throw INTERNAL_ERROR;
327 }
328
getOrSetParameter(const string & name,const char * nominal)329 const string& HOPSPACK::ParameterList::getOrSetParameter(const string& name,
330 const char* nominal)
331 {
332 ConstIterator i = params.find(name);
333
334 if (i == params.end()) {
335 params[name].setValue(nominal, true);
336 i = params.find(name);
337 }
338
339 if ((i != params.end()) && (entry(i).isString()))
340 return entry(i).getStringValue();
341
342 cerr << "HOPSPACK::ParameterList::getParameter - get error for string" << endl;
343 throw INTERNAL_ERROR;
344 }
345
getOrSetParameter(const string & name,const string & nominal)346 const string& HOPSPACK::ParameterList::getOrSetParameter(const string& name,
347 const string& nominal)
348 {
349 ConstIterator i = params.find(name);
350
351 if (i == params.end()) {
352 params[name].setValue(nominal, true);
353 i = params.find(name);
354 }
355
356 if ((i != params.end()) && (entry(i).isString()))
357 return entry(i).getStringValue();
358
359 cerr << "HOPSPACK::ParameterList::getParameter - get error for string" << endl;
360 throw INTERNAL_ERROR;
361 }
362
363
364 //----------------------------------------------------------------------
365 // Get parameter methods without a nominal value
366 //----------------------------------------------------------------------
367
getCharVecParameter(const string & name) const368 const vector< char > & HOPSPACK::ParameterList::getCharVecParameter(const string& name) const
369 {
370 ConstIterator i = params.find(name);
371
372 if ((i != params.end()) && (entry(i).isCharVec()))
373 return entry(i).getCharVecValue();
374
375 cerr << "HOPSPACK::ParameterList::getCharVecParameter - no such parameter (" << name << ")" << endl;
376 throw INTERNAL_ERROR;
377 }
378
getVectorParameter(const string & name) const379 const HOPSPACK::Vector& HOPSPACK::ParameterList::getVectorParameter(const string& name) const
380 {
381 ConstIterator i = params.find(name);
382
383 if ((i != params.end()) && (entry(i).isVector()))
384 return entry(i).getVectorValue();
385
386 cerr << "HOPSPACK::ParameterList::getVectorParameter - no such parameter (" << name << ")" << endl;
387 throw INTERNAL_ERROR;
388 }
389
getMatrixParameter(const string & name) const390 const HOPSPACK::Matrix& HOPSPACK::ParameterList::getMatrixParameter(const string& name) const
391 {
392 ConstIterator i = params.find(name);
393
394 if ((i != params.end()) && (entry(i).isMatrix()))
395 return entry(i).getMatrixValue();
396
397 cerr << "HOPSPACK::ParameterList::getMatrixParameter - no such parameter (" << name << ")" << endl;
398 throw INTERNAL_ERROR;
399 }
400
401
402 //----------------------------------------------------------------------
403 // Parameter existence methods
404 //----------------------------------------------------------------------
405
isParameterBool(const string & name) const406 bool HOPSPACK::ParameterList::isParameterBool(const string& name) const
407 {
408 ConstIterator i = params.find(name);
409
410 if (i != params.end())
411 return (entry(i).isBool());
412
413 return false;
414 }
415
isParameterInt(const string & name) const416 bool HOPSPACK::ParameterList::isParameterInt(const string& name) const
417 {
418 ConstIterator i = params.find(name);
419
420 if (i != params.end())
421 return (entry(i).isInt());
422
423 return false;
424 }
425
isParameterDouble(const string & name) const426 bool HOPSPACK::ParameterList::isParameterDouble(const string& name) const
427 {
428 ConstIterator i = params.find(name);
429
430 if (i != params.end())
431 return (entry(i).isDouble());
432
433 return false;
434 }
435
isParameterString(const string & name) const436 bool HOPSPACK::ParameterList::isParameterString(const string& name) const
437 {
438 ConstIterator i = params.find(name);
439
440 if (i != params.end())
441 return (entry(i).isString());
442
443 return false;
444 }
445
isParameterCharVec(const string & name) const446 bool HOPSPACK::ParameterList::isParameterCharVec(const string& name) const
447 {
448 ConstIterator i = params.find(name);
449
450 if (i != params.end())
451 return (entry(i).isCharVec());
452
453 return false;
454 }
455
isParameterSublist(const string & name) const456 bool HOPSPACK::ParameterList::isParameterSublist(const string& name) const
457 {
458 ConstIterator i = params.find(name);
459
460 if (i != params.end())
461 return (entry(i).isList());
462
463 return false;
464 }
465
isParameterVector(const string & name) const466 bool HOPSPACK::ParameterList::isParameterVector(const string& name) const
467 {
468 ConstIterator i = params.find(name);
469
470 if (i != params.end())
471 return (entry(i).isVector());
472
473 return false;
474 }
475
isParameterMatrix(const string & name) const476 bool HOPSPACK::ParameterList::isParameterMatrix(const string& name) const
477 {
478 ConstIterator i = params.find(name);
479
480 if (i != params.end())
481 return (entry(i).isMatrix());
482
483 return false;
484 }
485
isParameter(const string & name) const486 bool HOPSPACK::ParameterList::isParameter(const string& name) const
487 {
488 return (params.find(name) != params.end());
489 }
490
491
492 //----------------------------------------------------------------------
493 // Method deleteParameter
494 //----------------------------------------------------------------------
deleteParameter(const string & name)495 void HOPSPACK::ParameterList::deleteParameter(const string& name)
496 {
497 Iterator it = params.find (name);
498 if (it != params.end())
499 params.erase (it);
500
501 return;
502 }
503
504
505 //----------------------------------------------------------------------
506 // Method print
507 //----------------------------------------------------------------------
508
print(ostream & stream,int indent) const509 ostream& HOPSPACK::ParameterList::print(ostream& stream, int indent) const
510 {
511 if (params.begin() == params.end())
512 {
513 for (int j = 0; j < indent; j ++)
514 stream << ' ';
515 stream << "[empty list]" << endl;
516 }
517 else
518 for (ConstIterator i = params.begin(); i != params.end(); ++i)
519 {
520 for (int j = 0; j < indent; j ++)
521 stream << ' ';
522 if (entry(i).isList())
523 {
524 stream << name(i) << " -> " << endl;
525 entry(i).getListValue().print(stream, indent + 2);
526 }
527 else
528 stream << name(i) << " = " << entry(i) << endl;
529 }
530 return stream;
531 }
532
533
534 //----------------------------------------------------------------------
535 // MPI pack and unpack methods
536 //----------------------------------------------------------------------
537
538 #if defined(HAVE_MPI)
539
540 //---- DEFINE MESSAGE IDS FOR MPI/PVM BUFFERS.
541 static const int nMPIMSG_NEW_ENTRY = 1;
542 static const int nMPIMSG_END_OF_LIST = 2;
543
pack(GenProcComm & cGPC) const544 void HOPSPACK::ParameterList::pack (GenProcComm & cGPC) const
545 {
546 for (ConstIterator i = params.begin(); i != params.end(); ++i)
547 {
548 cGPC.pack (nMPIMSG_NEW_ENTRY);
549 cGPC.pack (name(i));
550 entry(i).pack (cGPC);
551 }
552 cGPC.pack (nMPIMSG_END_OF_LIST);
553 return;
554 }
555
unpack(GenProcComm & cGPC)556 void HOPSPACK::ParameterList::unpack (GenProcComm & cGPC)
557 {
558 int code;
559 string name;
560 cGPC.unpack (code);
561
562 while (code != nMPIMSG_END_OF_LIST)
563 {
564 cGPC.unpack (name);
565 params[name].unpack (cGPC);
566 cGPC.unpack (code);
567 }
568 return;
569 }
570
571 #endif //-- HAVE_MPI
572
573
574 //----------------------------------------------------------------------
575 // Private methods
576 //----------------------------------------------------------------------
577
578 #ifdef SNL_TFLOPS_ENV
579
name(ConstIterator i) const580 const string& HOPSPACK::ParameterList::name(ConstIterator i) const
581 {
582 return ((*i).first);
583 }
584
entry(Iterator i)585 HOPSPACK::ParameterEntry& HOPSPACK::ParameterList::entry(Iterator i)
586 {
587 return ((*i).second);
588 }
589
entry(ConstIterator i) const590 const HOPSPACK::ParameterEntry& HOPSPACK::ParameterList::entry(ConstIterator i) const
591 {
592 return ((*i).second);
593 }
594
595 #else
596
name(ConstIterator i) const597 const string& HOPSPACK::ParameterList::name(ConstIterator i) const
598 {
599 return (i->first);
600 }
601
entry(Iterator i)602 HOPSPACK::ParameterEntry& HOPSPACK::ParameterList::entry(Iterator i)
603 {
604 return (i->second);
605 }
606
entry(ConstIterator i) const607 const HOPSPACK::ParameterEntry& HOPSPACK::ParameterList::entry(ConstIterator i) const
608 {
609 return (i->second);
610 }
611
612 #endif
613