Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Roccom_base.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 // $Id: Roccom_base.C,v 1.93 2008/12/06 08:43:25 mtcampbe Exp $
24 
32 /* Author: Xiangmin Jiao */
33 
34 #include <iostream>
35 #include <sys/time.h>
36 #include <algorithm>
37 #ifndef STATIC_LINK
38 #include <dlfcn.h>
39 #endif
40 #include <cctype>
41 #include <cstring>
42 #include <cstdlib>
43 #include <cstdio>
44 #include <sstream>
45 #include "Roccom_base.h"
46 #include "commpi.h"
47 
48 COM_BEGIN_NAME_SPACE
49 
51 
52 #ifndef __CHARMC__
53 
57 
58 #else
59 bool use_tcharm_global = false;
60 
63  use_tcharm_global = true;
64  TCHARM_Set_global(COMGLOB_ROCCOM, r, NULL);
65  }
66  else
67  roccom_base = r;
68 }
69 
70 //}
71 
72 #endif // __CHARMC__
73 
74 
75 
76 #ifndef DOXYGEN_SHOULD_SKIP_THIS
77 inline static void remove_arg( int *argc, char ***argv, int i) {
79  for ( int j=i; j<*argc-1; ++j) (*argv)[j]=(*argv)[j+1];
80  --*argc;
81 }
82 #endif
83 
84 Roccom_base::Roccom_base( int *argc, char ***argv) throw( COM_exception, int)
85  : _depth(0), _verbose(0), _verb1(0), _comm(MPI_COMM_WORLD),
86  _mpi_initialized(false), _errorcode(0), _exception_on(true), _profile_on(0),
87  _debug(false)
88 {
89  _attr_map.add_object("",NULL);
90  _func_map.add_object("",NULL);
91  _errorcode = 0;
92 
93 #ifdef PREFIX
94  _libdir = std::string(PREFIX);
95 #endif
96 
97  const char *comhome = std::getenv( "ROCCOM_HOME");
98  if ( comhome) _libdir = comhome;
99  else {
100  comhome = std::getenv( "ROCSTAR_HOME");
101  if ( comhome) _libdir = comhome;
102  }
103 
104  if ( !_libdir.empty()) _libdir.append( "/lib/");
105 
106  // Parse command-line options
107  std::map<int,int> verb_maps; // Process-specific verbose level
108  int i=1;
109 
110  while ( argc && i<*argc) {
111  if ( std::strncmp((*argv)[i], "-com-v", 6) == 0) {
112  // Check whether a number follows -com-v
113  const char *s=(*argv)[i]+6;
114  int rank = -1; // Default are all processes
115  if ( s[0] != '\0') rank = std::atoi( s);
116 
117  int verb=1;
118  if ( *argc>i+1 && (*argv)[i+1][0]>='0' && (*argv)[i+1][0]<='9')
119  { verb = std::atoi( (*argv)[i+1]); remove_arg( argc, argv, i+1); }
120 
121  if ( rank == -1) set_verbose(verb);
122  else verb_maps[rank] = verb;
123 
124  remove_arg( argc, argv, i);
125  }
126  else if ( std::strcmp((*argv)[i], "-com-mpi") == 0) {
127  if ( !COMMPI_Initialized()) _mpi_initialized=true;
128 
129  remove_arg( argc, argv, i);
130  }
131  else if ( std::strcmp((*argv)[i], "-com-home") == 0) {
132  if ( *argc>i+1 && (*argv)[i+1][0]>='0' && (*argv)[i+1][0]<='9') {
133  _libdir = (*argv)[i+1]; _libdir.append( "/lib/");
134  remove_arg( argc, argv, i+1);
135  }
136  remove_arg( argc, argv, i);
137  }
138  else
139  ++i;
140  }
141 
142 
143  // Initialize MPI if requested and MPI is not yet initialized.
144  // if ( _mpi_initialized){
145  // std::cout << "INITIALIZING MPI!!!" << std::endl;
146  if(!COMMPI_Initialized())
147  MPI_Init( argc, argv);
148  // }
149  // Set process-specific verbose level
150  int rank = 0;
152  std::map<int,int>::const_iterator it=verb_maps.find( rank);
153  if ( it!=verb_maps.end()) set_verbose( it->second);
154 
155  // Determine the F90 pointer treatment mode
156  _f90_mangling = -1;
157  _f90ptr_treat = -1;
158 
159 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
160  // For GNU C++, we know to use the csated address.
161  _cppobj_casting = 1;
162 #elif defined(_POWER) || defined(__INTEL_COMPILER)
163  // For IBM and INTEL compilers, we know to use the object's address
164  _cppobj_casting = 0;
165 #else
166  _cppobj_casting = -1;
167 #endif
168 
169  if ( _debug && rank == 0) {
170  std::cerr << "Roccom Version 3.0. CVS $Id: Roccom_base.C,v 1.93 2008/12/06 08:43:25 mtcampbe Exp $" << std::endl;
171  std::cerr << "Roccom: Started with verbose level " << _verbose << std::endl;
172 #ifndef DUMMY_MPI
173  if ( !COMMPI_Initialized())
174  std::cerr << "Roccom: MPI not initialized. Running in serial mode\n";
175 #else
176  std::cerr << "Roccom: Running in serial mode with DUMMY_MPI\n";
177 #endif
178  }
179 }
180 
182  // If MPI was initialized by Roccom, then call MPI_Finalize.
183  if (_mpi_initialized) MPI_Finalize();
184 }
185 
186 void Roccom_base::init( int *argc, char ***argv ) throw( int) {
187  extern void printStackBacktrace();
188 
189  COM::Roccom_base *ptr = get_roccom();
190  if ( ptr) throw( COM_ERR_WAS_INITIALIZED);
191 
192  ptr = new COM::Roccom_base( argc, argv);
193 
194  set_roccom( ptr);
195 }
196 
197 void Roccom_base::finalize() throw( int) {
198  COM::Roccom_base *ptr = get_roccom();
199  if ( !ptr) throw( COM_ERR_WASNOT_INITIALIZED);
200 
201  delete ptr;
202  set_roccom( NULL);
203 }
204 
205 void Roccom_base::abort( int ierr) {
206  if ( COMMPI_Initialized() ) MPI_Abort( MPI_COMM_WORLD, ierr);
207  else exit( ierr);
208 }
209 
210 void
211 Roccom_base::load_module( const std::string &lname,
212  const std::string &wname) throw(int)
213 {
214 #ifndef STATIC_LINK
215  if ( _debug)
216  std::cerr << "Roccom: Loading module " << lname
217  << " with arguments " << wname << "..." << std::endl;
218 
219  void *handle;
220  std::string lname_found;
221 
222  // Obtain a reference to the library.
223  int index = _module_map.find(lname).first;
224  if ( index<0) { // Load the library
225  std::string lname_short = std::string("lib") + lname + ".so";
226  std::string lname_full = _libdir + lname_short;
227 
228  // Open the library
229  handle = dlopen( lname_full.c_str(), RTLD_LAZY);
230  if (handle == NULL)
231  std::cerr << "dlopen error: " << dlerror() << std::endl;
232 
233  if ( handle == NULL && !_libdir.empty()) {
234  handle = dlopen( lname_short.c_str(), RTLD_LAZY);
235  if (handle == NULL)
236  std::cerr << "dlopen error: " << dlerror() << std::endl;
237  lname_found = lname_short;
238  }
239  else
240  lname_found = lname_full;
241 
242 
243  if ( handle == NULL) {
244  std::string libs;
245  if ( _libdir.empty()) libs = lname_full;
246  else libs = lname_full+" or "+lname_short;
247 
248  proc_exception( COM_exception(COM_ERR_COULD_OPENLIB, libs),
249  "Roccom_base::load_module");
250  }
251 
252  // Insert a new entry into _module_map.
253  index = _module_map.add_object( lname, Module_map::value_type());
254  _module_map[index].first = handle;
255  }
256  else
257  handle = _module_map[index].first;
258 
259  // Insert the handle and the window name into _module_map.
260  _module_map[index].second.insert(wname);
261 
262  // Look for the symbol
263  std::string fname = lname + "_load_module";
264 
265  // Set the default communicator to MPI_COMM_SELF
266  MPI_Comm comm = MPI_COMM_SELF;
267  std::swap(_comm, comm);
268 
269  void *fptr = dlsym( handle, fname.c_str());
270  if ( fptr != NULL) {
271  // This is a C/C++ module
272  typedef void(*Func1)(const char*);
273  (*(Func1)fptr)( wname.c_str());
274  }
275  else {
276  // Try out different name mangling schemes to figure out automatically.
277  int ibegin= ( _f90_mangling == -1 ) ? 0 : _f90_mangling;
278  int iend = (_f90_mangling == -1) ? 4: _f90_mangling;
279 
280  // 0: lowercase
281  // 1: uppercase
282  // 2: _
283  // 3: uppercase _
284  // 4: lowercase __
285  for ( int i=ibegin; i<=iend; ++i) {
286  if ( i & 1 == 1)
287  std::transform( fname.begin(), fname.end(), fname.begin(), toupper);
288  else
289  std::transform( fname.begin(), fname.end(), fname.begin(), tolower);
290 
291  if ( i==2 || (i==3&&i==ibegin)) fname.append( "_");
292  if (i==4) { fname.append( "_"); if (ibegin == 4) fname.append( "_"); }
293 
294  fptr = dlsym( handle, fname.c_str());
295  if ( fptr) { _f90_mangling = i; break; }
296  }
297 
298  if ( fptr != NULL) {
299  // This is a Fortran module
300  typedef void(*Func2)(const char*,long int);
301  (*(Func2)fptr)( wname.c_str(), wname.size());
302  }
303  else {
304  std::string msg;
305  if ( ibegin == iend) msg = fname+" in "+lname_found;
306  else msg = fname + " or any its lowercase w/o underscore in " + lname;
307 
308  msg.append( "\nError message from libdl is: ");
309  msg.append( dlerror());
310  proc_exception( COM_exception(COM_ERR_COULD_FINDSYM, msg),
311  "Roccom_base::load_module");
312  }
313  }
314 
315  // Restore the default communicator.
316  std::swap(_comm, comm);
317 
318  if ( _debug)
319  std::cerr << "Roccom: Module " << lname << " loaded." << std::endl;
320 #endif
321 }
322 
323 void
324 Roccom_base::unload_module( const std::string &lname,
325  const std::string &wname, int dodl) throw(int) {
326 
327 #ifndef STATIC_LINK
328  if ( _debug)
329  std::cerr << "Roccom: Unloading module " << lname << "..." << std::endl;
330 
331  // Obtain the window name corresponding to the library
332  int index = _module_map.find(lname).first;
333  if ( index<0)
334  proc_exception( COM_exception( COM_ERR_MODULE_NOTLOADED, lname),
335  "Roccom_base::unload_module");
336 
337  std::set<std::string> &obj = _module_map[index].second;
338 
339  // If the module was loaded multiple times but window name is not
340  // specified, then throw exception.
341  if ( wname.empty() && obj.size()>1)
342  proc_exception( COM_exception( COM_ERR_MODULE_NOTLOADED, lname),
343  "Roccom_base::unload_module");
344 
345  std::set<std::string>::iterator it = wname.empty()?obj.begin():obj.find( wname);
346 
347  // If wname is not found, throw exception
348  if ( it==obj.end())
349  proc_exception( COM_exception( COM_ERR_MODULE_NOTLOADED, lname),
350  "Roccom_base::unload_module");
351 
352  // Look for the symbol
353  std::string fname = lname + "_unload_module";
354  std::string wname_str=wname.size()?wname:*obj.begin();
355 
356  void *handle = _module_map[index].first;
357  void *fptr = dlsym( handle, fname.c_str());
358  if ( fptr != NULL) {
359  // This is a C/C++ module
360  typedef void(*Func1)(const char*);
361  (*(Func1)fptr)( wname_str.c_str());
362  }
363  else {
364  if ( _f90_mangling != -1) {
365  if ( _f90_mangling & 1 == 1)
366  std::transform( fname.begin(), fname.end(), fname.begin(), toupper);
367  else
368  std::transform( fname.begin(), fname.end(), fname.begin(), tolower);
369 
370  if ( _f90_mangling ==2 || _f90_mangling == 3) fname.append( "_");
371 
372  fptr = dlsym( handle, fname.c_str());
373  }
374 
375  if ( fptr != NULL) {
376  // This is a Fortran module
377  typedef void(*Func2)(const char*,long int);
378  (*(Func2)fptr)( wname_str.c_str(), wname_str.size());
379  }
380  else {
381  proc_exception( COM_exception(COM_ERR_COULD_FINDSYM, fname+" in "+lname),
382  "Roccom_base::unload_module");
383  }
384  }
385 
386  obj.erase( wname);
387 
388  if ( dodl && obj.size()==0) { // Unload module if all instances removed.
389  dlclose( handle);
390  _module_map.remove_object( lname);
391  }
392 
393  if ( _debug)
394  std::cerr << "Rocccom: Module " << lname << " unloaded" << std::endl;
395 #endif
396 }
397 
398 // Create a window with given name
399 void Roccom_base::new_window( const std::string &name, MPI_Comm comm) throw(int)
400 {
401  // If MPI_COMM_NULL is used, then use the default communicator.
402  if ( comm == MPI_COMM_NULL) comm = _comm;
403 
404  try {
405  if ( _debug)
406  std::cerr << "Roccom: Creating window \"" << name << '"'
407  << " with communicator " << comm << std::endl;
408 
409  if ( _window_map.find( name).second)
412 
413  _window_map.add_object( name, new Window( name, comm));
414  _errorcode = 0;
415  }
416  catch ( COM_exception ex) {
418  std::string s("When processing "); s.append(name);
419  proc_exception( ex, s);
420  }
421 }
422 
423 void Roccom_base::
424 window_init_done( const std::string &wname, bool panechanged) throw(int)
425 {
426  try {
427  get_window( wname).init_done(panechanged);
428  _errorcode = 0;
429  }
430  catch ( COM_exception ex) {
432 
433  std::string msg = std::string("When completing the init of window ")+wname;
434  proc_exception( ex, msg);
435  }
436  if ( _debug)
437  std::cerr << "Roccom: Done creating window \"" << wname
438  << '"' << std::endl;
439 }
440 
441 void Roccom_base::delete_window( const std::string &name) throw(int)
442 {
443  try {
444  if ( _debug)
445  std::cerr << "Roccom: Deleting window \"" << name << '"' << std::endl;
446 
447  _window_map.remove_object( name);
448  _errorcode = 0;
449  }
450  catch ( COM_exception ex) {
453  std::string s;
454  s = s + "When processing window " + name;
455  proc_exception( ex, s);
456  }
457 }
458 
459 void Roccom_base::delete_pane( const std::string &wname,
460  const int pane_id) throw(int)
461 {
462  try {
463  if ( _debug)
464  std::cerr << "Roccom: Delete pane " << pane_id << " of window "
465  << std::endl;
466 
467  get_window( wname).delete_pane( pane_id);
468  _errorcode = 0;
469  }
470  catch ( COM_exception ex) {
472  char buf[10];
473  std::sprintf( buf, "%d", pane_id);
474  std::string msg = std::string( "When processing window ")
475  + wname + " for pane " + buf;
476  proc_exception( ex, msg);
477  }
478 }
479 
480 void print_type( std::ostream &os, COM_Type type) {
481  switch (type) {
482  case COM_STRING:
483  os << "STRING"; break;
484  case COM_METADATA:
485  os << "METADATA"; break;
486  case COM_VOID:
487  os << "VOID"; break;
488  case COM_OBJECT:
489  os << "COM_Object"; break;
490  case COM_F90POINTER:
491  os << "F90POINTER"; break;
492  case COM_CHAR: case COM_CHARACTER:
493  os << "CHAR"; break;
494  case COM_DOUBLE:
495  case COM_DOUBLE_PRECISION:
496  os << "double"; break;
497  case COM_INT:
498  case COM_LONG:
499  case COM_INTEGER:
500  os << "int"; break;
501  case COM_FLOAT:
502  case COM_REAL:
503  os << "float"; break;
504  case COM_LOGICAL:
505  os << "logical"; break;
506  case COM_MPI_COMMC:
507  case COM_MPI_COMMF:
508  os << "MPI_Comm"; break;
509  default:
510  os << "type(" << type << ")";
511  }
512 }
513 
514 // Register a new attribute with given name
515 void Roccom_base::
516 new_attribute( const std::string &wa,
517  const char loc,
518  const int type,
519  int size,
520  const std::string &unit) throw(int)
521 {
522  try {
523  if ( _debug) {
524  std::cerr << "Roccom: new attribute \"" << wa
525  << "\"\nRoccom:\tLocation: " << loc << "\nRoccom:\tType: ";
526  print_type( std::cerr, type);
527  std::cerr << "\nRoccom:\tSize: " << size << "\nRoccom:\tUnit: \"" << unit << "\""
528  << std::endl;
529  }
530 
531  // Invoke Window::new_attribute.
532  std::string wname, aname;
533  split_name( wa, wname, aname);
534 
535  get_window( wname).new_attribute( aname, loc, type, size, unit);
536  _errorcode = 0;
537  }
538  catch ( COM_exception ex) {
540  std::string s;
541  s = s + "When processing attribute " + wa;
542  proc_exception( ex, s);
543  }
544 }
545 
546 // Register a new attribute with given name
547 void Roccom_base::
548 delete_attribute( const std::string &wa) throw(int)
549 {
550  try {
551  if ( _debug)
552  std::cerr << "Roccom: delete attribute \"" << wa << std::endl;
553 
554  // Invoke Window::new_attribute.
555  std::string wname, aname;
556  split_name( wa, wname, aname);
557 
558  get_window( wname).delete_attribute( aname);
559  _errorcode = 0;
560  }
561  catch ( COM_exception ex) {
563  std::string s;
564  s = s + "When processing attribute " + wa;
565  proc_exception( ex, s);
566  }
567 }
568 
569 void Roccom_base::set_size( const std::string &wa, int pid,
570  int nitems, int ng) throw(int)
571 {
572  try {
573  if ( _debug) {
574  std::cerr << "Roccom: Set size for attribute \""
575  << wa << '"' << " on pane " << pid
576  << " to " << nitems << " items with " << ng
577  << " ghosts" << std::endl;
578  }
579 
580  // Invoke Window::set_size
581  std::string wname, aname;
582  split_name( wa, wname, aname);
583 
584  get_window( wname).set_size( aname, pid, nitems, ng);
585  _errorcode = 0;
586  }
587  catch ( COM_exception ex) {
589  std::string s;
590  s = s + "When processing attribute " + wa;
591  proc_exception( ex, s);
592  }
593 }
594 
595 // A helper class for detecting the scheme to use for set_object
596 class COM_Object_derived : public COM_Object {
597 public:
598  static bool cast_obj() {
599  typedef void(COM_Object::*Member_Func)( int *);
600  Member_Func f = (Member_Func)&COM_Object_derived::validate_object;
601 
602  COM_Object_derived obj;
603  int ierr; (reinterpret_cast<COM_Object*>((void *)&obj)->*f)( &ierr);
604 
605  return ierr != 0;
606  }
607 
608 protected:
610 
611  virtual ~COM_Object_derived() {}
612 
613  void validate_object( int *ierr) {
614  if ( _cookie != DERIVED_COOKIE) *ierr = -1;
615  else *ierr = COM_Object::validate_object();
616  }
617 
618 protected:
619  enum { DERIVED_COOKIE=3374833 };
620  int _cookie;
621 };
622 
623 
624 // Associates an object with a specific window.
625 void Roccom_base::set_object( const std::string &wa, const int pid,
626  void *obj_addr, void *casted_addr) throw(int)
627 {
628  COM_assertion_msg( obj_addr, "Caught NULL pointer");
629  int size;
630  get_attribute( wa, NULL, NULL, &size, NULL);
631  COM_assertion_msg( size==1, "Must register a scalar with set_object");
632 
633  if ( obj_addr == casted_addr) {
634  set_array( wa, pid, obj_addr);
635  return;
636  }
637 
638  if ( _cppobj_casting == -1)
639  _cppobj_casting = COM_Object_derived::cast_obj();
640 
641  if ( _cppobj_casting) // Use stride to store the offset
642  set_array( wa, pid, casted_addr, (char*)casted_addr-(char*)obj_addr);
643  else
644  set_array( wa, pid, obj_addr);
645 }
646 
647 // Associates an object with a specific window.
648 void Roccom_base::get_object( const std::string &wa, const int pid,
649  void **ptr) throw(int)
650 {
651  int strd;
652  get_array( wa, pid, ptr, &strd);
653 
654  if ( strd <= 1) return; else *(char**)ptr -= strd;
655 }
656 
657 // Register the address of an arrays for a data field.
658 void Roccom_base::set_array( const std::string &wa, const int pid,
659  void *addr, int strd, int cap,
660  bool is_const) throw(int)
661 {
662  try {
663  if ( _debug) {
664  std::cerr << "Roccom: Set array for \"" << wa << "\" on pane "
665  << pid << " to " << addr << " with stride ";
666  if ( strd) std::cerr << strd;
667  else std::cerr << "==#components";
668 
669  std::cerr << " and capacity ";
670 
671  if ( cap) std::cerr << cap;
672  else std::cerr << "==#items";
673 
674  std::cerr << std::endl;
675  }
676 
677  std::string wname, aname;
678  split_name( wa, wname, aname);
679 
680  get_window( wname).set_array( aname, pid, addr, strd, cap, is_const);
681  _errorcode = 0;
682  }
683  catch ( COM_exception ex) {
685  std::string s;
686  s = s + "When processing attribute " + wa;
687  proc_exception( ex, s);
688  }
689 }
690 
691 void Roccom_base::set_bounds( const std::string &wa, const int pane_id,
692  const void *lbnd, const void *ubnd) throw(int)
693 {}
694 
695 void Roccom_base::allocate_array( const std::string &wa, const int pid,
696  void **addr, int strd, int cap) throw(int)
697 {
698  try {
699  if ( _debug) {
700  std::cerr << "Roccom: Allocate array for \"" << wa << "\" on pane "
701  << pid << " with stride ";
702  if ( strd) std::cerr << strd;
703  else std::cerr << "==#components";
704 
705  std::cerr << " and capacity ";
706 
707  if ( cap) std::cerr << cap;
708  else std::cerr << "==#items";
709 
710  std::cerr << std::endl;
711  }
712 
713  std::string wname, aname;
714  split_name( wa, wname, aname);
715 
716  get_window( wname).alloc_array( aname, pid, addr, strd, cap);
717  _errorcode = 0;
718  }
719  catch ( COM_exception ex) {
721  std::string s;
722  s = s + "When processing attribute " + wa;
723  proc_exception( ex, s);
724  }
725 }
726 
727 void Roccom_base::resize_array( const std::string &wa, const int pid,
728  void **addr, int strd, int cap) throw(int) {
729  try {
730  if ( _debug) {
731  std::cerr << "Roccom: Resize array for \"" << wa << "\" on pane "
732  << pid << " with stride ";
733  if ( strd<0) std::cerr << "==current";
734  else if (strd) std::cerr << strd;
735  else std::cerr << "==#components";
736 
737  std::cerr << " and capacity ";
738 
739  if ( cap) std::cerr << cap;
740  else std::cerr << "==#items";
741 
742  std::cerr << std::endl;
743  }
744 
745  std::string wname, aname;
746  split_name( wa, wname, aname);
747 
748  get_window( wname).resize_array( aname, pid, addr, strd, cap);
749  _errorcode = 0;
750  }
751  catch ( COM_exception ex) {
753  std::string s;
754  s = s + "When processing attribute " + wa;
755  proc_exception( ex, s);
756  }
757 }
758 
759 void Roccom_base::append_array( const std::string &wa, const int pid,
760  const void *val, int v_strd, int v_size) throw(int)
761 {
762  try {
763  if ( _debug)
764  std::cerr << "Roccom: Appending array " << val << " for \"" << wa
765  << "\" on pane " << pid << " with stride " << v_strd
766  << " and size " << v_size << std::endl;
767 
768  std::string wname, aname;
769  split_name( wa, wname, aname);
770 
771  get_window( wname).append_array( aname, pid, val, v_strd, v_size);
772  _errorcode = 0;
773  }
774  catch ( COM_exception ex) {
776  std::string s("When processing attribute "); s.append(wa);
777  proc_exception( ex, s);
778  }
779 }
780 
781 void Roccom_base::
782 use_attribute( const std::string &waname, const std::string &pwaname,
783  int withghost,const char *cndname, int val) throw(int)
784 {
785  try {
786  if ( _debug) {
787  std::cerr << "Roccom: Using attribute \"" << pwaname
788  << "\" onto \"" << waname << '"' << std::endl;
789  }
790  std::string pawname, paaname;
791  split_name( pwaname, pawname, paaname);
792 
793  std::string awname, aaname;
794  std::string::size_type ni = waname.find(".");
795  if ( ni == std::string::npos) {
796  awname = waname; aaname = "";
797  }
798  else
799  split_name( waname, awname, aaname);
800 
801  std::string cawname, caaname;
802  if ( cndname && *cndname!=0)
803  split_name( cndname, cawname, caaname);
804 
805  Attribute *cnd=caaname.empty()?NULL:get_window(cawname).attribute(caaname);
806  if ( !caaname.empty() && cnd==NULL)
807  throw COM_exception( COM_ERR_ATTRIBUTE_NOTEXIST, cndname);
808 
809  get_window(awname).inherit( get_window( pawname).attribute(paaname),
810  aaname, Pane::INHERIT_USE, withghost, cnd, val);
811  _errorcode = 0;
812  }
813  catch ( COM_exception ex) {
815  std::string s;
816  s = s + "When window " + waname + " uses attribute " + pwaname;
817  proc_exception( ex, s);
818  }
819 }
820 
821 void Roccom_base::
822 clone_attribute( const std::string &waname, const std::string &pwaname,
823  int withghost, const char *cndname, int val) throw(int)
824 {
825  try {
826  if ( _debug) {
827  std::cerr << "Roccom: Cloning attribute \"" << pwaname
828  << "\" onto \"" << waname << '"' << std::endl;
829  }
830  std::string pawname, paaname;
831  split_name( pwaname, pawname, paaname);
832 
833  std::string awname, aaname;
834  std::string::size_type ni = waname.find(".");
835  if ( ni == std::string::npos) {
836  awname = waname; aaname = "";
837  }
838  else
839  split_name( waname, awname, aaname);
840 
841  std::string cawname, caaname;
842  if ( cndname && *cndname!=0)
843  split_name( cndname, cawname, caaname);
844 
845  Attribute *cnd=caaname.empty()?NULL:get_window(cawname).attribute(caaname);
846  if ( !caaname.empty() && cnd==NULL)
847  throw COM_exception( COM_ERR_ATTRIBUTE_NOTEXIST, cndname);
848 
849  get_window(awname).inherit( get_window( pawname).attribute(paaname),
850  aaname, Pane::INHERIT_CLONE, withghost, cnd, val);
851  _errorcode = 0;
852  }
853  catch ( COM_exception ex) {
855  std::string s;
856  s = s + "When window " + waname + " clones attribute " + pwaname;
857  proc_exception( ex, s);
858  }
859 }
860 
861 void Roccom_base::
862 copy_attribute( const std::string &waname, const std::string &pwaname,
863  int withghost, const char *cndname, int val) throw(int)
864 {
865  try {
866  if ( _debug) {
867  std::cerr << "Roccom: Copying attribute \"" << pwaname
868  << "\" onto \"" << waname << '"' << std::endl;
869  }
870  std::string pawname, paaname;
871  split_name( pwaname, pawname, paaname);
872 
873  std::string awname, aaname;
874  std::string::size_type ni = waname.find(".");
875  if ( ni == std::string::npos) {
876  awname = waname; aaname = "";
877  }
878  else
879  split_name( waname, awname, aaname);
880 
881  std::string cawname, caaname;
882  if ( cndname && *cndname!=0)
883  split_name( cndname, cawname, caaname);
884 
885  Attribute *cnd=caaname.empty()?NULL:get_window(cawname).attribute(caaname);
886  if ( !caaname.empty() && cnd==NULL)
887  throw COM_exception( COM_ERR_ATTRIBUTE_NOTEXIST, cndname);
888 
889  get_window(awname).inherit( get_window( pawname).attribute(paaname),
890  aaname, Pane::INHERIT_COPY, withghost, cnd, val);
891  _errorcode = 0;
892  }
893  catch ( COM_exception ex) {
895  std::string s;
896  s = s + "When window " + waname + " copies attribute " + pwaname;
897  proc_exception( ex, s);
898  }
899 }
900 
901 void Roccom_base::
902 copy_attribute( int trg_hdl, int src_hdl,
903  int withghost, int ptn_hdl, int val) throw(int)
904 {
905  try {
906  if ( _debug) {
907  std::cerr << "Roccom: Copying attribute with handle \"" << src_hdl
908  << "\" onto \"" << trg_hdl << '"' << std::endl;
909  }
910 
911  if ( trg_hdl <= 0 || src_hdl <= 0)
913 
914  if ( _attr_map.is_immutable( trg_hdl))
916 
917  Attribute *trg = &get_attribute(trg_hdl);
918  trg->window()->inherit( &get_attribute(src_hdl), trg->name(),
919  Pane::INHERIT_COPY, withghost,
920  ptn_hdl?&get_attribute(ptn_hdl):NULL, val);
921  _errorcode = 0;
922  }
923  catch ( COM_exception ex) {
925  std::ostringstream sout;
926 
927  sout << "When copying attribute " << src_hdl << " onto "
928  << trg_hdl << " with pattern attribute " << ptn_hdl;
929  proc_exception( ex, sout.str());
930  }
931 }
932 
933 
934 void Roccom_base::deallocate_array( const std::string &wa,
935  const int pid) throw(int)
936 {
937  try {
938  if ( _debug)
939  std::cerr << "Roccom: Deallocate array for \"" << wa << "\" on pane "
940  << pid << " to" << std::endl;
941 
942  std::string wname, aname;
943  split_name( wa, wname, aname);
944 
945  get_window( wname).dealloc_array( aname, pid);
946  _errorcode = 0;
947  }
948  catch ( COM_exception ex) {
950  std::string s;
951  s = s + "When processing attribute " + wa;
952  proc_exception( ex, s);
953  }
954 }
955 
956 // Register a new attribute with given name
957 void Roccom_base::
958 get_attribute( const std::string &wa, char *loc,
959  int *type, int *size, std::string *unit) throw(int)
960 {
961  try {
962  if ( _debug) // Print debugging info
963  std::cerr << "Roccom: get attribute \"" << wa << "\"" << std::endl;
964 
965  // Invoke Window::get_attribute.
966  std::string wname, aname;
967  split_name( wa, wname, aname);
968 
969  get_window( wname).get_attribute( aname, loc, type, size, unit);
970  _errorcode = 0;
971 
972  if ( _debug) { // Print debugging info
973  if (loc) std::cerr << "Roccom:\tLocation: " << *loc
974  << "\nRoccom:\tType: ";
975  if (type) print_type( std::cerr, *type);
976  if ( size) std::cerr << "Roccom:\tSize: " << *size << std::endl;
977  if ( unit) std::cerr << "Roccom:\tUnit: \"" << *unit << "\"" << std::endl;
978  }
979  }
980  catch ( COM_exception ex) {
982  std::string s;
983  s = s + "When processing attribute " + wa;
984  proc_exception( ex, s);
985  }
986 }
987 
988 void Roccom_base::get_size( const std::string &wa, int pid,
989  int *nitems, int *ng) throw(int)
990 {
991  try {
992  if ( _debug) {
993  std::cerr << "Roccom: Get size for attribute \""
994  << wa << '"' << " for pane " << pid << std::endl;
995  }
996 
997  // Invoke Window::get_size
998  std::string wname, aname;
999  split_name( wa, wname, aname);
1000 
1001  get_window( wname).get_size( aname, pid, nitems, ng);
1002  _errorcode = 0;
1003  }
1004  catch ( COM_exception ex) {
1006  std::string s;
1007  s = s + "When processing attribute " + wa;
1008  proc_exception( ex, s);
1009  }
1010 }
1011 
1012 void Roccom_base::get_array( const std::string &wa, const int pane_id,
1013  void **addr, int *strd, int *cap,
1014  bool is_const) throw(int)
1015 {
1016  Pointer_descriptor ptr(NULL);
1017  get_array( wa, pane_id, ptr, strd, cap, is_const);
1018  if (addr) *addr = ptr.ptr;
1019 }
1020 
1021 // Get the address for an attribute on a specific pane.
1022 void Roccom_base::get_array( const std::string &wa, const int pid,
1023  Pointer_descriptor &addr,
1024  int *strd, int *cap, bool is_const) throw(int) {
1025  try {
1026  if ( _debug) {
1027  std::cerr << "Roccom: Get array for attribute \""
1028  << wa << '"' << " on pane " << pid << std::endl;
1029  }
1030 
1031  // Invoke Window::get_size
1032  std::string wname, aname;
1033  split_name( wa, wname, aname);
1034 
1035  get_window( wname).get_array( aname, pid, addr, strd, cap, is_const);
1036 
1037  if ( _debug) {
1038  std::cerr << "Roccom: ";
1039  if ( strd) std::cerr << " stride is " << *strd;
1040  if ( cap) std::cerr << " capacity is " << *cap;
1041  std::cerr << std::endl;
1042  }
1043 
1044  _errorcode = 0;
1045  }
1046  catch ( COM_exception ex) {
1048  std::string s;
1049  s = s + "When processing attribute " + wa;
1050  proc_exception( ex, s);
1051  }
1052 }
1053 
1054 // Get the status of an attribute.
1055 int Roccom_base::get_status( const std::string &wa, int pid) throw(int)
1056 {
1057  try {
1058  if ( _debug) {
1059  std::cerr << "Roccom: Get status for attribute \""
1060  << wa << '"' << " on pane " << pid << std::endl;
1061  }
1062 
1063  std::string wname, aname;
1064  split_name( wa, wname, aname, false);
1065 
1066  Window **w = _window_map.find( wname).second;
1067 
1068  if ( aname.empty() && pid == 0) {
1069  if ( w == NULL) return -1;
1070  else return 0;
1071  }
1072 
1073  int status = (**w).get_status( aname, pid);
1074 
1075  if ( _debug) {
1076  std::cerr << "Roccom: ";
1077  std::cerr << " status is " << status << std::endl;
1078  }
1079 
1080  _errorcode = 0;
1081  return status;
1082  }
1083  catch ( COM_exception ex) {
1085  std::string s;
1086  s = s + "When processing attribute " + wa;
1087  proc_exception( ex, s);
1088  }
1089 
1090  return 0;
1091 }
1092 
1093 void Roccom_base::copy_array( const std::string &wa, const int pid,
1094  void *val, int v_strd, int v_size,
1095  int offset) throw(int)
1096 {
1097  try {
1098  if ( _debug) {
1099  std::cerr << "Roccom: Copy array for attribute \""
1100  << wa << '"' << " on pane " << pid << std::endl;
1101  }
1102 
1103  // Invoke Window::get_size
1104  std::string wname, aname;
1105  split_name( wa, wname, aname);
1106 
1107  get_window( wname).copy_array( aname, pid, val, v_strd, v_size, offset);
1108 
1109  if ( _debug) {
1110  std::cerr << "Roccom: ";
1111  std::cerr << " stride is " << v_strd;
1112  std::cerr << " size is " << v_size;
1113  std::cerr << std::endl;
1114  }
1115 
1116  _errorcode = 0;
1117  }
1118  catch ( COM_exception ex) {
1120  std::string s;
1121  s = s + "When processing attribute " + wa;
1122  proc_exception( ex, s);
1123  }
1124 }
1125 
1126 void Roccom_base::get_bounds( const std::string &wa, const int pane_id,
1127  void *lbnd, void *ubnd) throw(int)
1128 {}
1129 
1130 int Roccom_base::check_bounds( const std::string &wa,
1131  int pane_id) throw(int)
1132 { return 0; }
1133 
1134 std::pair<int,int> // The pair of offset1 and offset2
1136 
1138 
1139  COM_assertion_msg( _f90ptr_treat>=0, "No F90 pointer initalized");
1140  if ( _f90ptr_treat == FPTR_INSERT)
1141  return std::pair<int,int>(n/2-sizeof(void*), n-sizeof(void*));
1142  else
1143  return std::pair<int,int>(0,n-sizeof(void*));
1144 }
1145 
1146 void Roccom_base::
1147 set_f90pointer( const std::string &waname, void *ptr,
1148  Func_ptr f, long int len) throw(int) {
1149  try {
1150  if ( _debug)
1151  std::cerr << "Roccom: set pointer \"" << waname
1152  << " to " << ptr << std::endl;
1153 
1154  std::string wname, aname;
1155  split_name( waname, wname, aname);
1156 
1157  Attribute *a = get_window( wname).attribute( aname);
1158  if ( a->location() != 'w')
1161  if ( a->data_type() == COM_F90POINTER) {
1163 
1164  std::pair<int,int> offs = get_f90pntoffsets( a);
1165  std::memcpy( a->pointer(), (char*)ptr-offs.first, offs.second);
1166 
1167  if ( offs.second<n) {
1168  *(void**)((char*)a->pointer()+offs.second) =
1169  (char*)a->pointer()+offs.first+((char*)len-(char*)ptr);
1170  }
1171 
1172  COM_assertion_msg( _f90ptr_treat>=0, "No F90 pointer initalized");
1173  if ( _f90ptr_treat == FPTR_INSERT || _f90ptr_treat == FPTR_APPEND) {
1174  typedef void(*Func2)(void*,void*, long int, long int);
1175  *(void**)((char*)a->pointer()+offs.first)=NULL;
1176  (*(Func2)f)( ptr, (char*)a->pointer()+offs.first,
1177  len, *(long int*)((char*)a->pointer()+offs.second));
1178  }
1179  else {
1180  typedef void(*Func2)(void*,void*);
1181  (*(Func2)f)( (char*)ptr+offs.first, a->pointer());
1182  }
1183 
1184  if ( _debug) {
1185  std::cerr << "Roccom: copied values ";
1186  for ( int i=0, ni=n/sizeof(void*); i<ni; ++i)
1187  std::cerr << ((void**)a->pointer())[i] << ' ';
1188  std::cerr << std::endl;
1189  }
1190  }
1191  else
1194  }
1195  catch ( COM_exception ex) {
1197  proc_exception( ex, "");
1198  }
1199 }
1200 
1201 void Roccom_base::
1202 get_f90pointer( const std::string &waname, void *ptr,
1203  Func_ptr f, long int len) throw(int) {
1204  try {
1205  if ( _debug)
1206  std::cerr << "Roccom: get pointer \"" << waname
1207  << " into " << ptr << std::endl;
1208 
1209  std::string wname, aname;
1210  split_name( waname, wname, aname);
1211 
1212  Attribute *a = get_window( wname).attribute( aname);
1213  if ( a->location() != 'w')
1216  if ( a->data_type() == COM_F90POINTER) {
1217  std::pair<int,int> offs = get_f90pntoffsets( a);
1218 
1219  COM_assertion_msg( _f90ptr_treat>=0, "No F90 pointer initalized");
1220  if ( _f90ptr_treat == FPTR_INSERT || _f90ptr_treat == FPTR_APPEND) {
1221  typedef void(*Func2)(void*,void*, long int, long int);
1222  (*(Func2)f)( (char*)a->pointer()+offs.first, ptr,
1223  *(long int*)((char*)a->pointer()+offs.second), len);
1224  }
1225  else {
1226  typedef void(*Func2)(void*,void*);
1227  (*(Func2)f)( a->pointer(), (char*)ptr+offs.first);
1228  }
1229  }
1230  else
1233  }
1234  catch ( COM_exception ex) {
1236  proc_exception( ex, "");
1237  }
1238 }
1239 
1240 MPI_Comm Roccom_base::
1241 get_communicator( const std::string &wname) throw(int) {
1242  try {
1243  if ( _debug)
1244  std::cerr << "Roccom: get the communicator of window \""
1245  << wname << std::endl;
1246  if ( COMMPI_Initialized()) {
1247  Window &w = get_window(wname);
1248  _errorcode = 0;
1249  return w.get_communicator();
1250  }
1251  else
1252  return MPI_COMM_NULL;
1253  }
1254  catch ( COM_exception ex) {
1255  ex.msg = append_frame( ex.msg, Roccom_base::get_window_npanes);
1256  std::string s;
1257  s = s + "When processing window " + wname;
1258  proc_exception( ex, s);
1259  }
1260  return 0;
1261 }
1262 
1263 void Roccom_base::
1264 get_panes( const std::string &wname, std::vector<int> &paneids_vec,
1265  int rank, int **pane_ids) throw(int){
1266  try {
1267  if ( _debug) {
1268  std::cerr << "Roccom: get pane ids of window \""
1269  << wname << "\" on ";
1270  if ( rank==-2) std::cerr << " this process";
1271  else if ( rank==-1) std::cerr << " all processes";
1272  else std::cerr << " process " << rank;
1273 
1274  std::cerr << std::endl;
1275  }
1276 
1277  get_window(wname).panes( paneids_vec, rank);
1278 
1279  if ( pane_ids) {
1280  *pane_ids = new int[paneids_vec.size()];
1281  std::copy( paneids_vec.begin(), paneids_vec.end(), *pane_ids);
1282  }
1283  _errorcode = 0;
1284  }
1285  catch ( COM_exception ex) {
1287  std::string s;
1288  s = s + "When processing window " + wname;
1289  proc_exception( ex, s);
1290  }
1291 }
1292 
1293 void Roccom_base::
1294 get_windows(std::vector<std::string> &names) throw(int){
1295  try {
1296  if ( _debug) {
1297  std::cerr << "Roccom: get windows";
1298  // if ( rank==-2) std::cerr << " this process";
1299  // else if ( rank==-1) std::cerr << " all processes";
1300  // else std::cerr << " process " << rank;
1301 
1302  std::cerr << std::endl;
1303  }
1304  names = _window_map.get_names();
1305  // get_window(wname).panes( paneids_vec, rank);
1306 
1307  // if ( pane_ids) {
1308  // *pane_ids = new int[paneids_vec.size()];
1309  // std::copy( paneids_vec.begin(), paneids_vec.end(), *pane_ids);
1310  // }
1311  _errorcode = 0;
1312  }
1313  catch ( COM_exception ex) {
1315  std::string s;
1316  s = s + "When processing get_windows.";
1317  proc_exception( ex, s);
1318  }
1319 }
1320 
1321 void Roccom_base::
1322 get_modules(std::vector<std::string> &names) throw(int){
1323  try {
1324  if ( _debug) {
1325  std::cerr << "Roccom: get modules";
1326  // if ( rank==-2) std::cerr << " this process";
1327  // else if ( rank==-1) std::cerr << " all processes";
1328  // else std::cerr << " process " << rank;
1329 
1330  std::cerr << std::endl;
1331  }
1332  names = _module_map.get_names();
1333  // get_window(wname).panes( paneids_vec, rank);
1334 
1335  // if ( pane_ids) {
1336  // *pane_ids = new int[paneids_vec.size()];
1337  // std::copy( paneids_vec.begin(), paneids_vec.end(), *pane_ids);
1338  // }
1339  _errorcode = 0;
1340  }
1341  catch ( COM_exception ex) {
1343  std::string s;
1344  s = s + "When processing get_modules.";
1345  proc_exception( ex, s);
1346  }
1347 }
1348 
1349 void Roccom_base::get_attributes( const std::string &wname, int *natts,
1350  std::string &str, char **names) throw(int)
1351 {
1352  try {
1353  if ( _debug)
1354  std::cerr << "Roccom: get attributes of window \""
1355  << wname << '"' << std::endl;
1356 
1357  std::vector<Attribute*> as;
1358  get_window(wname).attributes( as);
1359 
1360  str.clear();
1361  if ( natts) *natts = as.size();
1362  int i=0, n=as.size();
1363  if (n) for (;;) {
1364  str = str + as[i]->name();
1365  if ( ++i<n) str.append(" ");
1366  else break;
1367  }
1368 
1369  if ( names) {
1370  *names = new char[str.size()+1];
1371  std::copy( str.begin(), str.end(), *names);
1372  (*names)[str.size()]=0;
1373  }
1374 
1375  if ( _debug)
1376  std::cerr << "Roccom: Got attribute names: " << str << std::endl;
1377 
1378  _errorcode = 0;
1379  }
1380  catch ( COM_exception ex) {
1382  std::string s;
1383  s = s + "When processing window " + wname;
1384  proc_exception( ex, s);
1385  }
1386 }
1387 
1388 
1389 void Roccom_base::
1390 get_connectivities( const std::string &wname, int pane_id, int *natts,
1391  std::string &str, char **names) throw(int)
1392 {
1393  try {
1394  if ( _debug)
1395  std::cerr << "Roccom: get connectivities of window \"" << wname
1396  << "\" on pane " << pane_id << std::endl;
1397 
1398  std::vector<Connectivity*> as;
1399  get_window(wname).pane( pane_id).connectivities( as);
1400 
1401  if ( natts) *natts = as.size();
1402 
1403  str.clear();
1404  int i=0, n=as.size();
1405  if (n) for (;;) {
1406  str = str + as[i]->name();
1407  if ( ++i<n) str.append(" ");
1408  else break;
1409  }
1410 
1411  if ( names) {
1412  *names = new char[str.size()+1];
1413  std::copy( str.begin(), str.end(), *names);
1414  (*names)[str.size()]=0;
1415  }
1416 
1417  if ( _debug)
1418  std::cerr << "Roccom: Got connectivity names: " << str << std::endl;
1419 
1420  _errorcode = 0;
1421  }
1422  catch ( COM_exception ex) {
1424  std::string s;
1425  s = s + "When processing window " + wname;
1426  proc_exception( ex, s);
1427  }
1428 }
1429 
1430 void Roccom_base::
1431 get_parent( const std::string &waname, int pane_id,
1432  std::string &str, char **name) throw(int)
1433 {
1434  try {
1435  if ( _debug)
1436  std::cerr << "Roccom: get parent of attribute \"" << waname
1437  << "\" on pane " << pane_id << std::endl;
1438 
1439  std::string wname, aname;
1440  split_name( waname, wname, aname);
1441 
1442  get_window( wname).get_parent( aname, pane_id, str);
1443 
1444  if ( name) {
1445  *name = new char[str.size()+1];
1446  std::copy( str.begin(), str.end(), *name);
1447  (*name)[str.size()]=0;
1448  }
1449 
1450  if ( _debug)
1451  std::cerr << "Roccom: Got parent name: " << str << std::endl;
1452 
1453  _errorcode = 0;
1454  }
1455  catch ( COM_exception ex) {
1457  std::string s;
1458  s = s + "When processing attribute " + waname;
1459  proc_exception( ex, s);
1460  }
1461 }
1462 
1463 
1464 void Roccom_base::free_buffer( char **buf)
1465 { if ( buf) delete [] *buf; *buf = NULL; }
1466 
1467 void Roccom_base::free_buffer( int **buf)
1468 { if ( buf) delete [] *buf; *buf = NULL; }
1469 
1470 int Roccom_base::
1471 get_window_handle( const std::string &wname) throw(int) {
1472  int n(-1);
1473  try {
1474  if ( _debug)
1475  std::cerr << "Roccom: get handle of window \"" << wname << "\": ";
1476  std::pair<int,Window**> obj = _window_map.find( wname);
1477  COM_assertion(obj.first<0 || (*obj.second)->name()==wname);
1478 
1479  if ( obj.first>=0)
1480  n = obj.first+1;
1481 
1482  if ( _debug) {
1483  if ( n>0) std::cerr << n << std::endl;
1484  else std::cerr << " not found" << std::endl;
1485  }
1486  }
1487  catch ( COM_exception ex) {
1489  proc_exception( ex, "");
1490  }
1491  return n;
1492 }
1493 
1495 get_window_object( int hdl) throw(int)
1496 {
1497  if ( hdl <= 0)
1500  return _window_map[hdl-1];
1501 }
1502 
1503 const Window * Roccom_base::
1504 get_window_object( int hdl) const throw(int)
1505 {
1506  if ( hdl <= 0)
1509  return _window_map[hdl-1];
1510 }
1511 
1512 int Roccom_base::
1513 get_attribute_handle_const( const std::string &waname) throw(int) {
1514  int n(-1);
1515  try {
1516  if ( _debug)
1517  std::cerr << "Roccom: get const handle of attribute \""
1518  << waname << "\": ";
1519 
1520  std::string wname, aname;
1521  split_name( waname, wname, aname);
1522 
1523  Attribute *a = get_window( wname).attribute( aname);
1524 
1525  if ( a!=NULL) n=_attr_map.add_object( waname, a, true);
1526 
1527  if ( _debug) {
1528  if ( n>0) std::cerr << n << std::endl;
1529  else std::cerr << " not found" << std::endl;
1530  }
1531  }
1532  catch ( COM_exception ex) {
1534  proc_exception( ex, "");
1535  }
1536  return n;
1537 }
1538 
1539 int Roccom_base::
1540 get_attribute_handle( const std::string &waname) throw(int) {
1541  int n(-1);
1542  try {
1543  if ( _debug)
1544  std::cerr << "Roccom: get handle of attribute \"" << waname << "\": ";
1545  std::string wname, aname;
1546  split_name( waname, wname, aname);
1547 
1548  Attribute *a = get_window( wname).attribute( aname);
1549 
1550  if ( a!=NULL) n=_attr_map.add_object( waname, a, false);
1551 
1552  if ( _debug) {
1553  if ( n>0) std::cerr << n << std::endl;
1554  else std::cerr << " not found" << std::endl;
1555  }
1556  }
1557  catch ( COM_exception ex) {
1559  proc_exception( ex, "");
1560  }
1561  return n;
1562 }
1563 
1564 int Roccom_base::
1565 get_function_handle( const std::string &wfname) throw(int) {
1566  int n(-1);
1567  try {
1568  if ( _debug)
1569  std::cerr << "Roccom: get handle of function \"" << wfname << "\": ";
1570  std::string wname, fname;
1571  split_name( wfname, wname, fname);
1572 
1573  Function *f = get_window( wname).function( fname);
1574 
1575  if ( f!=NULL) n=_func_map.add_object( wfname, f);
1576 
1577  if ( _debug) {
1578  if ( n>0) std::cerr << n << std::endl;
1579  else std::cerr << " not found" << std::endl;
1580  }
1581  }
1582  catch ( COM_exception ex) {
1584  proc_exception( ex, "");
1585  }
1586  return n;
1587 }
1588 
1589 void Roccom_base::set_function( const std::string &wfname,
1590  Func_ptr ptr,
1591  const std::string &intents,
1592  const COM_Type *types, bool ff) throw(int) {
1594  try {
1595  if ( _debug) {
1596  int n=intents.size();
1597  std::cerr << "Roccom: init function \"" << wfname << '"'
1598  << " to " << ptr << " with " << n << " parameters";
1599 
1600  for ( int i=0; i<n; ++i) {
1601  std::cerr << "\nRoccom:\t" << intents[i] << ":";
1602  print_type( std::cerr, types[i]);
1603  }
1604  std::cerr << std::endl;
1605  }
1606  std::string wname, fname;
1607  split_name( wfname, wname, fname);
1608 
1609  if ( intents.size() > Function::MAX_NUMARG)
1612 
1613  for ( int i=0, size=intents.size(); i<size; ++i) {
1614  if (intents[i] != 'i' && intents[i] != 'o' && intents[i] != 'b' &&
1615  intents[i] != 'I' && intents[i] != 'O' && intents[i] != 'B')
1618  if ( types[i] < COM_MIN_TYPEID || types[i] > COM_MAX_TYPEID)
1621  }
1622 
1623  get_window( wname).set_function( fname, ptr, intents, types, NULL, ff);
1624  _errorcode = 0;
1625  }
1626  catch ( COM_exception ex) {
1628  std::string s;
1629  s = s + "When processing attribute " + wfname;
1630  proc_exception( ex, s);
1631  }
1632 }
1633 
1634 template <class T>
1635 void Roccom_base::
1636 set_member_function_helper( const std::string &wfname,
1637  T ptr,
1638  const std::string &waname,
1639  const std::string &intents,
1640  const COM_Type *types,
1641  bool ff) throw(int) {
1642  try {
1643  if ( _debug) {
1644  int n=intents.size();
1645  std::cerr << "Roccom: init function \"" << wfname << '"'
1646  << " to " << ptr << " with " << n << " parameters";
1647  for ( int i=0; i<n; ++i) {
1648  std::cerr << "\nRoccom:\t" << intents[i] << ":";
1649  print_type( std::cerr, types[i]);
1650  if ( i==0) std::cerr << "\t\"" << waname << "\"";
1651  }
1652  std::cerr << std::endl;
1653  }
1654  std::string wname, fname;
1655  split_name( wfname, wname, fname);
1656 
1657  // Allows an additional implicit argument
1658  if ( intents.size() > Function::MAX_NUMARG+1)
1661 
1662  for ( int i=0, size=intents.size(); i<size; ++i) {
1663  if (intents[i] != 'i' && intents[i] != 'o' && intents[i] != 'b' &&
1664  intents[i] != 'I' && intents[i] != 'O' && intents[i] != 'B')
1667  if ( types[i] < COM_MIN_TYPEID || types[i] > COM_MAX_TYPEID)
1670  }
1671 
1672  std::string wname1, aname1;
1673  split_name( waname, wname1, aname1);
1674 
1675  Attribute *a = get_window( wname1).attribute( aname1);
1676 
1677  if ( a==NULL)
1680  if ( !ff && a->data_type()==COM_F90POINTER)
1683 
1684  get_window( wname).set_function( fname, ptr, intents, types, a, ff);
1685  _errorcode = 0;
1686  }
1687  catch ( COM_exception ex) {
1689  std::string s;
1690  s = s + "When processing attribute " + wfname;
1691  proc_exception( ex, s);
1692  }
1693 }
1694 
1695 void Roccom_base::set_member_function( const std::string &wfname,
1696  Func_ptr ptr,
1697  const std::string &waname,
1698  const std::string &intents,
1699  const COM_Type *types,
1700  bool ff) throw(int) {
1701  set_member_function_helper( wfname, ptr, waname,
1702  intents, types, ff);
1703 }
1704 
1705 void Roccom_base::set_member_function( const std::string &wfname,
1706  Member_func_ptr ptr,
1707  const std::string &waname,
1708  const std::string &intents,
1709  const COM_Type *types,
1710  bool ff) throw(int) {
1711  set_member_function_helper( wfname, ptr, waname,
1712  intents, types, ff);
1713 }
1714 
1715 int Roccom_base::
1716 get_num_arguments( const std::string &wf) throw(COM_exception) {
1717  std::string wname, fname;
1718  split_name(wf, wname, fname);
1719 
1720  Function *func = get_window( wname).function( fname);
1721  if ( func==NULL)
1724  return func->num_of_args();
1725 }
1726 
1727 int Roccom_base::
1728 get_num_arguments( const int wf) throw(COM_exception) {
1729  Function *func = &get_function( wf);
1730  return func->num_of_args();
1731 }
1732 
1733 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1734 inline static double get_wtime() {
1735 
1736  ::timeval tv;
1737  gettimeofday( &tv, NULL);
1738 
1739  return tv.tv_sec + tv.tv_usec*1.e-6;
1740 }
1741 #endif
1742 
1743 void Roccom_base::
1744 call_function( int wf, int count,
1745  void **args, const int *lens, bool from_c) throw(int) {
1746  // Roccom prints out the trace upto (verb-1)/2 depth.
1747  // If verb is even, then it will also print the arguments.
1748  try {
1749  if ( wf==0) {
1750  if ( _debug) {
1751  std::cerr << "Roccom: ************* CALL(" << _depth << ") "
1752  << "on NOOP with " << count << " arguments" << std::endl;
1753  }
1754  return; // Null function
1755  }
1756 
1757  Function *func = &get_function( wf);
1758 
1759  int verb = std::max(_verbose, int(_func_map.verbs[ wf]))-_depth*2;
1760  if ( verb<=0) verb = 0;
1761  else verb = (verb+1)%2+1;
1762  if ( _debug) {
1763  std::cerr << "Roccom: CALL(" << _depth << ") "
1764  << _func_map.name(wf);
1765  if ( _debug) std::cerr << '(';
1766  }
1767 
1768  std::vector<char> strs[Function::MAX_NUMARG+1];
1769  bool needpostproc=false;
1770 
1771  int li=0;
1772  void *ps[2*Function::MAX_NUMARG+1];
1773  int lcount = 0;
1774  void **plen = NULL;
1775  if ( func->is_fortran()) plen = ps+func->num_of_args();
1776 
1777  // attr must be const to void throwing exception when pointer is called
1778  const Attribute *attr = func->attribute();
1779  int offset = (attr!=NULL);
1780  count += offset;
1781  args -= offset;
1782  if ( count > func->num_of_args())
1784 
1785  if ( offset) {
1786  if ( _debug)
1787  std::cerr << std::endl << "Roccom:\t" << func->intent(0) << ": ";
1788 
1789  if ( func->is_rawdata( 0)) {
1790  if ( attr->is_const() && std::tolower(func->intent(0)) != 'i')
1792 
1793  if ( attr->data_type()==COM_F90POINTER) {
1794  std::pair<int,int> offs = get_f90pntoffsets( attr);
1795 
1796  ps[0] = (char*)const_cast<void*>(attr->pointer())+offs.first;
1797  // Add pointer information for PortlandGroup Compiler
1798 
1799  COM_assertion_msg( _f90ptr_treat>=0, "No F90 pointer initalized");
1800  if ( _f90ptr_treat == FPTR_INSERT) {
1801  *plen = *(void**)((char*)const_cast<void*>(attr->pointer())
1802  +offs.second);
1803  ++lcount; ++plen;
1804  }
1805  }
1806  else
1807  ps[0] = (char*)const_cast<void*>(attr->pointer());
1808 
1809  if ( _debug)
1810  std::cerr << "VALUE OF\t@" << ps[0] << "\t\""
1811  << attr->window()->name() << '.' << attr->name() << '"';
1812  }
1813  else {
1814  ps[0] = const_cast<Attribute*>(attr);
1815  if ( _debug)
1816  std::cerr << "METADATA\t@" << ps[0] << "\t\""
1817  << attr->window()->name() << '.' << attr->name() << '"';
1818  }
1819  }
1820 
1821  for ( int i=offset; i<count; ++i) {
1822  if ( _debug) {
1823  std::cerr << std::endl << "Roccom:\t" << func->intent(i) << ": ";
1824  }
1825 
1826  if ( func->is_literal(i)) {
1827  ps[i] = args[i];
1828  COM_Type type = func->data_type(i);
1829  char intent = func->intent(i);
1830  if ( type == COM_CHARACTER || type == COM_CHAR || type == COM_STRING) {
1831  if ( type == COM_STRING) {
1832  // Make sure it is NULL terminated
1833  if (lens && (intent=='i' || intent=='I' ||
1834  intent=='b' || intent=='B') &&
1835  ( lens[li]==0 || ((char*)args[i])[lens[li]-1] != '\0')) {
1836  strs[i].resize( lens[li]+1, '\0');
1837  std::strncpy( &strs[i][0], (char*)args[i], lens[li]);
1838  ps[i] = &strs[i][0];
1839 
1840  if (intent=='b' || intent=='B') needpostproc=true;
1841  }
1842  if ( plen) { // Append the length info
1843  *plen = (char*)NULL+std::strlen((char*)ps[i]); ++plen; ++lcount;
1844  }
1845  }
1846  else if ( plen) {
1847  *plen = (char*)NULL+1; ++plen; ++lcount;
1848  }
1849  ++li;
1850  }
1851  else if ( type == COM_MPI_COMMC && !from_c) {
1852  strs[i].resize( 2*sizeof( MPI_Comm));
1853  ps[i] = &strs[i][0]+(sizeof( MPI_Comm)-
1854  ((long int)&strs[i][0])%sizeof(MPI_Comm));
1855  if (intent=='i' || intent=='I' || intent=='b' || intent=='B')
1856  *(MPI_Comm*)ps[i] = COMMPI_Comm_f2c( *(int*)args[i], MPI_Comm());
1857  if (intent=='o' || intent=='O' || intent=='b' || intent=='B')
1858  needpostproc=true;
1859  }
1860  else if ( type == COM_MPI_COMMF && from_c) {
1861  strs[i].resize( 2*sizeof( int));
1862  ps[i] = &strs[i][0]+(sizeof(int)-((long int)&strs[i][0])%sizeof(int));
1863  if (intent=='i' || intent=='I' || intent=='b' || intent=='B')
1864  *(int*)ps[i] = COMMPI_Comm_c2f( *(MPI_Comm*)args[i]);
1865  if (intent=='o' || intent=='O' || intent=='b' || intent=='B')
1866  needpostproc=true;
1867  }
1868  if (_debug) {
1869  switch (type) {
1870  case COM_STRING:
1871  std::cerr << "STRING\t@" << args[i] << "\t";
1872  if ( args[i]) std::cerr << '\"' << (char*)ps[i] << '\"';
1873  break;
1874  case COM_CHAR: case COM_CHARACTER:
1875  std::cerr << "CHAR \t@" << args[i] << "\t";
1876  if ( args[i]) std::cerr << '\'' << *(char*)args[i] << '\'';
1877  break;
1878  case COM_DOUBLE:
1879  case COM_DOUBLE_PRECISION:
1880  std::cerr << "double\t@" << args[i] << '\t';
1881  if ( args[i]) std::cerr << *(double*)args[i];
1882  break;
1883  case COM_INT:
1884  case COM_LONG:
1885  case COM_INTEGER:
1886  std::cerr << "int \t@" << args[i] << '\t';
1887  if ( args[i]) std::cerr << *(int*)args[i];
1888  break;
1889  case COM_FLOAT:
1890  case COM_REAL:
1891  std::cerr << "float \t@" << args[i] << '\t';
1892  if ( args[i]) std::cerr << *(float*)args[i];
1893  break;
1894  case COM_LOGICAL:
1895  std::cerr << "logical\t@" << args[i] << '\t';
1896  if ( args[i]) std::cerr << *(int*)args[i];
1897  break;
1898  case COM_MPI_COMMC:
1899  std::cerr << "MPI_Comm (C)\t@" << args[i] << '\t';
1900  if ( args[i]) std::cerr << *(MPI_Comm*)args[i];
1901  break;
1902  case COM_MPI_COMMF:
1903  std::cerr << "MPI_Comm (F)\t@" << args[i] << '\t';
1904  if ( args[i]) std::cerr << *(int*)args[i];
1905  break;
1906  default:
1907  std::cerr << "type(" << type << ")\t@" << args[i];
1908  }
1909  }
1910  }
1911  else {
1912  int h=*(int*)args[i];
1913  if ( h == 0 && func->intent(i)<='Z') {
1914  // Optional attribute received a 0 attribute handle
1915  ps[i] = NULL;
1916  if ( _debug)
1917  std::cerr << "ZERO ATTRIBUTE HANDLE";
1918  continue;
1919  }
1920 
1921  // attr must be const to void throwing exception when pointer is called
1922  const Attribute *attr = &get_attribute( h);
1923  if ( attr->is_const() && std::tolower(func->intent(i)) != 'i')
1925 
1926  if ( func->is_rawdata( i)) {
1927  ps[i] = const_cast<void*>(attr->pointer());
1928  if ( _debug)
1929  std::cerr << "VALUE OF\t@" << ps[i]
1930  << "\t\"" << _attr_map.name(h) << '"';
1931  }
1932  else {
1933  ps[i] = const_cast<Attribute*>(attr);
1934  if ( _debug)
1935  std::cerr << "METADATA\t@" << ps[i]
1936  << "\t\"" << _attr_map.name(h) << '"';
1937  }
1938 
1939  if ( _attr_map.is_immutable( h) &&
1940  toupper(func->intent(i)) != 'I') {
1942  }
1943  }
1944  }
1945 
1946  if (offset && func->is_rawdata(0) &&
1947  (attr = func->attribute())->data_type()==COM_F90POINTER) {
1948  COM_assertion_msg( _f90ptr_treat>=0, "No F90 pointer initalized");
1949 
1950  // Append pointer information
1951  if ( _f90ptr_treat == FPTR_APPEND) {
1952  std::pair<int,int> offs = get_f90pntoffsets( attr);
1953 
1954  // Add pointer information for Intel Compiler
1955  *plen = *(void**)((char*)const_cast<void*>(attr->pointer())+offs.second);
1956  ++lcount; ++plen;
1957  }
1958  }
1959 
1960  for ( int i=count, iend=func->num_of_args(); i<iend; ++i) {
1961  if ( !func->is_optional( i)) throw COM_exception(COM_ERR_TOO_FEW_ARGS);
1962  ps[i] = NULL;
1963  if ( _debug) {
1964  std::cerr << std::endl << "Roccom: OPT\t" << func->intent(i) << ": ";
1965  std::cerr << ps[i];
1966  }
1967  }
1968  if ( _debug) {
1969  if ( _debug) std::cerr << std::endl << "Roccom: )";
1970  std::cerr << std::endl;
1971  }
1972 
1973  // Profiling it
1974  double t = 0;
1975  if ( _profile_on) {
1976 //RAF MPI_Comm comm = func->communicator();
1977 //RAF if (comm!=MPI_COMM_NULL) MPI_Barrier( comm);
1978  t = get_wtime();
1979 #ifdef _CHARM_THREADED_
1980 //RAF if (comm!=MPI_COMM_NULL) MPI_Barrier( comm);
1981 #endif
1982  }
1983  ++_depth;
1984  if ( _debug && lcount>0) {
1985  std::cerr << "Roccom: Invoking function with " << lcount
1986  << " additional implicit arguments: " << std::endl;
1987  for ( int i=0; i<lcount; ++i) {
1988  long int p=(long int)ps[func->num_of_args()+i];
1989  if ( p<0 || p>MAX_NAMELEN)
1990  std::cerr << "Roccom:\t@" << (void *)p << std::endl;
1991  else
1992  std::cerr << "Roccom:\t" << (long int)p << std::endl;
1993  }
1994  }
1995 
1996  // Invoke the function
1997  (*func)( func->num_of_args()+lcount, ps);
1998  --_depth;
1999 
2000  if ( _profile_on) {
2001 //RAF MPI_Comm comm = func->communicator();
2002 //RAF if (comm!=MPI_COMM_NULL) MPI_Barrier( comm);
2003 
2004  double tnew = get_wtime();
2005 #ifdef _CHARM_THREADED_
2006 //RAF if (comm!=MPI_COMM_NULL) MPI_Barrier( comm);
2007 #endif
2008 
2009  _func_map.counts[wf]++;
2010 
2011  double sec = tnew-t;
2012  _func_map.wtimes_tree[wf] += sec;
2013  _func_map.wtimes_self[wf] += sec;
2014  if ( int(_timer.size()) > _depth)
2015  _func_map.wtimes_self[wf] -= _timer[_depth];
2016 
2017  _timer.resize( _depth,0);
2018  if (_depth>0) _timer[_depth-1] += sec;
2019  if (_depth==0) _func_map.wtimes_tree[0] += sec;
2020  }
2021 
2022  if ( _debug) {
2023  std::cerr << "Roccom: DONE(" << _depth << ") " << std::endl;
2024  }
2025 
2026  // Copy back strings
2027  if ( needpostproc) {
2028  for ( int i=offset; i<count; ++i) {
2029  COM_Type type = func->data_type(i);
2030  char intent = func->intent(i);
2031  if ( func->is_literal(i) && type == COM_STRING) {
2032  if (intent=='b' || intent=='B') {
2033  int n = strs[i].size();
2034  if ( n>0) std::memcpy( args[i], &strs[i][0], n-1);
2035  }
2036  }
2037  else if ( type == COM_MPI_COMMC && !from_c) {
2038  if (intent=='o' || intent=='O' || intent=='b' || intent=='B')
2039  *(int*)args[i] = COMMPI_Comm_c2f( *(MPI_Comm*)ps[i]);
2040  }
2041  else if ( type == COM_MPI_COMMF && from_c) {
2042  if (intent=='o' || intent=='O' || intent=='b' || intent=='B')
2043  *(MPI_Comm*)args[i] = COMMPI_Comm_f2c( *(int*)ps[i], MPI_Comm());
2044  }
2045  }
2046  }
2047 
2048  _errorcode = 0;
2049  }
2050  catch ( COM_exception ex) {
2052  char buf[10];
2053 
2054  std::sprintf( buf, "%d", wf);
2055  std::string msg = std::string( "When processing function ");
2056  if ( wf>0) msg.append( _func_map.name(wf));
2057 
2058  msg.append( " with handle "); msg.append( buf);
2059  proc_exception( ex, msg);
2060  }
2061 }
2062 
2063 void Roccom_base::
2064 set_function_verbose( int i, int level) throw(int) {
2065  _func_map.verbs[i] = level;
2066 }
2067 
2068 void Roccom_base::
2069 set_profiling( int i) {
2070  if ( _debug)
2071  std::cerr << "Roccom: init profiling level to " << i << std::endl;
2072  _profile_on = i;
2073  std::fill(_func_map.wtimes_tree.begin(),_func_map.wtimes_tree.end(), 0);
2074  std::fill(_func_map.wtimes_self.begin(),_func_map.wtimes_self.end(), 0);
2075  std::fill(_func_map.counts.begin(),_func_map.counts.end(), 0);
2076 }
2077 
2078 void Roccom_base::
2079 set_profiling_barrier( int hdl, MPI_Comm comm) {
2080  if ( hdl == 0) return;
2081  try {
2082  if ( hdl < 0 || hdl>=_func_map.size())
2084 
2085  if ( _debug){
2086  std::cerr << "Roccom: init profiling barrier for function handle " << hdl
2087  << std::endl << "Roccom:\tFunction: " << _func_map.name(hdl)
2088  << " Communicator: " << comm << std::endl;
2089  }
2090 
2091  // Set the communicator only if MPI has been initialized.
2092  if ( COMMPI_Initialized()) {
2093  if ( comm==MPI_COMM_NULL) comm = _comm;
2094  get_function( hdl).set_communicator( comm);
2095  }
2096  }
2097  catch ( COM_exception ex) {
2099  char buf[100];
2100  std::sprintf( buf, "Could not get function with handle %d", hdl);
2101  proc_exception( ex, buf);
2102  }
2103 }
2104 
2105 void Roccom_base::
2106 print_profile( const std::string &fname, const std::string &header) {
2107  if ( !_profile_on) return;
2108 
2109  if ( _debug){
2110  std::cerr << "Roccom: Appending profile into file \"" << fname
2111  << '"' << std::endl;
2112  }
2113  std::multimap< double, int> profs;
2114  typedef std::multimap< double, int>::value_type MMVT;
2115 
2116  for ( int i=1, n=_func_map.wtimes_self.size(); i<n; ++i) {
2117  if ( _func_map.wtimes_self[i] > 0.0)
2118  profs.insert( MMVT( -_func_map.wtimes_self[i], i));
2119  }
2120 
2121  std::FILE *of = NULL;
2122  if ( fname.size() == 0)
2123  of = stdout;
2124  else {
2125  of = std::fopen( fname.c_str(), "a");
2126  if ( of == NULL) {
2127  std::cerr << "Roccom: Could not open file \"" << fname
2128  << "\"\nRoccom: Giving up profiling" << std::endl;
2129  _profile_on = false; return;
2130  }
2131  }
2132  std::fputc('\n', of);
2133 
2134  if ( header.size() == 0)
2135  std::fputs( "************************Roccom simple profiling tool\
2136 ************************", of);
2137  else
2138  std::fputs( header.c_str(), of);
2139 
2140  std::fprintf( of, "\n%32s%12s%14s%14s\n", "Function", "#calls",
2141  "Time(tree)", "Time(self)");
2142  std::fputs( "-------------------------------------------------------\
2143 ---------------------\n", of);
2144 
2145  std::multimap< double, int>::const_iterator i, iend;
2146  for ( i=profs.begin(), iend=profs.end(); i!=iend; ++i) {
2147  std::fprintf( of, "%32.32s%12d%14g%14g\n",
2148  _func_map.name(i->second).c_str(),
2149  _func_map.counts[i->second],
2150  _func_map.wtimes_tree[i->second],
2151  _func_map.wtimes_self[i->second]);
2152  }
2153  std::fputs( "-------------------------------------------------------\
2154 ---------------------\n", of);
2155  std::fprintf( of, "%32s%40g\n", "Total(top level calls)",
2156  _func_map.wtimes_tree[0]);
2157 
2158  if ( of != stdout) std::fclose( of);
2159 }
2160 
2161 
2162 int Roccom_base::get_sizeof( COM_Type type, int count)
2163 { return Attribute::get_sizeof( type, count); }
2164 
2166 get_window( const std::string &wname) throw(COM_exception)
2167 {
2168  Window **w = _window_map.find( wname).second;
2169  if ( w ==NULL)
2172  return **w;
2173 }
2174 
2176 get_attribute( const int handle) throw(COM_exception) {
2177  Attribute *attr=NULL;
2178 
2179  if ( handle > 0 && handle<_attr_map.size())
2180  attr = _attr_map[handle];
2181  if (attr==NULL) {
2182  static char buf[10];
2183  std::sprintf( buf, "%d", handle);
2186  }
2187  return *attr;
2188 }
2189 
2190 const Attribute &Roccom_base::
2191 get_attribute( const int handle) const throw(COM_exception) {
2192  const Attribute *attr=NULL;
2193 
2194  if ( handle > 0 && handle<_attr_map.size())
2195  attr = _attr_map[handle];
2196  if (attr==NULL) {
2197  static char buf[10];
2198  std::sprintf( buf, "%d", handle);
2201  }
2202  return *attr;
2203 }
2204 
2206 get_function( const int handle) throw(COM_exception) {
2207  Function *func=NULL;
2208 
2209  if ( handle > 0 && handle<_func_map.size())
2210  func = _func_map[handle];
2211 
2212  if (func==NULL) {
2213  static char buf[10];
2214  std::sprintf( buf, "%d", handle);
2217  }
2218  return *func;
2219 }
2220 
2221 const Function &Roccom_base::
2222 get_function( const int handle) const throw(COM_exception) {
2223  const Function *func=NULL;
2224 
2225  if ( handle > 0 && handle<_func_map.size())
2226  func = _func_map[handle];
2227  if (func==NULL) {
2228  static char buf[10];
2229  std::sprintf( buf, "%d", handle);
2232  }
2233  return *func;
2234 }
2235 
2236 int Roccom_base::split_name( const std::string &wa, std::string &wname,
2237  std::string &aname, bool tothrow) throw( COM_exception)
2238 {
2239  std::string::size_type ni = wa.find(".");
2240 
2241  if ( ni == std::string::npos)
2242  wname = wa;
2243  else {
2244  wname = wa.substr( 0, ni);
2245  aname = wa.substr( ni+1, wname.size()-ni-1);
2246  }
2247 
2248  if ( wname.empty() || aname.empty())
2249  if ( tothrow)
2252  else
2253  return 1;
2254 
2255  return 0;
2256 }
2257 
2258 void Roccom_base::
2259 proc_exception( const COM_exception &ex, const std::string &s) throw( int)
2260 {
2261  extern void printStackBacktrace();
2262  _errorcode = ex.ierr;
2263 
2264  std::cerr << "\nRoccom:" << ex << s << std::endl;
2265  if ( _exception_on && _errorcode>=1000) {
2267  throw ex.ierr;
2268  }
2269 }
2270 
2271 COM_END_NAME_SPACE
2272 
2273 #if !defined(STATIC_LINK)
2274 void *p_xargc;
2275 void *p_xargv;
2276 #endif
2277 
2278 
2279 
2280 
2281 
2282 
int COMMPI_Comm_rank(MPI_Comm c)
Definition: commpi.h:162
void use_attribute(const std::string &wname, const std::string &pwname, int withghost=1, const char *cndname=NULL, int val=0)
Use the subset of panes of another window of which the given pane attribute has value val...
Definition: Roccom_base.C:782
void get_attribute(const std::string &wa_str, char *loc, int *type, int *size, std::string *unit)
Information retrieval Get the information about an attribute.
Definition: Roccom_base.C:958
static void abort(int ierr)
Definition: Roccom_base.C:205
void delete_window(const std::string &wname)
Deletes a window with given name.
Definition: Roccom_base.C:441
void set_communicator(MPI_Comm c)
Definition: Function.h:118
void swap(int &a, int &b)
Definition: buildface.cpp:88
void copy_array(const std::string &wa, const int pane_id, void *val, int v_strd=0, int v_size=0, int offset=0)
Copy an array from an attribute on a specific pane into a given buffer.
Definition: Roccom_base.C:1093
void validate_object(int *ierr)
Definition: Roccom_base.C:613
int COM_Type
Indices for derived data types.
Definition: roccom_basic.h:122
virtual ~COM_Object_derived()
Definition: Roccom_base.C:611
A Function object corresponds to a function member of a window.
Definition: Function.h:49
#define COM_assertion(EX)
Error checking utility similar to the assert macro of the C language.
here we put it at the!beginning of the common block The point to point and collective!routines know about but MPI_TYPE_STRUCT as yet does not!MPI_STATUS_IGNORE and MPI_STATUSES_IGNORE are similar objects!Until the underlying MPI library implements the C version of these are declared as arrays of MPI_STATUS_SIZE!The types and are OPTIONAL!Their values are zero if they are not available Note that!using these reduces the portability of MPI_IO INTEGER MPI_BOTTOM INTEGER MPI_DOUBLE_PRECISION INTEGER MPI_LOGICAL INTEGER MPI_2REAL INTEGER MPI_2DOUBLE_COMPLEX INTEGER MPI_LB INTEGER MPI_WTIME_IS_GLOBAL INTEGER MPI_COMM_WORLD
bool is_optional(int i) const
Check whether the ith argument is optional.
Definition: Function.h:105
#define MAX_NAMELEN
The maxinum length of name string passed by users.
Definition: roccom_basic.h:120
here we put it at the!beginning of the common block The point to point and collective!routines know about but MPI_TYPE_STRUCT as yet does not!MPI_STATUS_IGNORE and MPI_STATUSES_IGNORE are similar objects!Until the underlying MPI library implements the C version of these are declared as arrays of MPI_STATUS_SIZE!The types and are OPTIONAL!Their values are zero if they are not available Note that!using these reduces the portability of MPI_IO INTEGER MPI_BOTTOM INTEGER MPI_DOUBLE_PRECISION INTEGER MPI_LOGICAL INTEGER MPI_2REAL INTEGER MPI_2DOUBLE_COMPLEX INTEGER MPI_LB INTEGER MPI_WTIME_IS_GLOBAL INTEGER MPI_COMM_SELF
bool is_fortran() const
Definition: Function.h:114
void set_function(const std::string &wf, Func_ptr ptr, const std::string &intents, const COM_Type *types, bool ff=false)
Registers a function to the window.
Definition: Roccom_base.C:1589
Window & get_window(const std::string &wname)
Obtains a reference to the Window object from its name.
Definition: Roccom_base.C:2166
void get_attributes(const std::string &wname, int *na, std::string &str, char **names=NULL)
Obtain the user-defined attributes of the given window.
Definition: Roccom_base.C:1349
static void remove_arg(int *argc, char ***argv, int i)
Remove an argument from the argument list.
Definition: Roccom_base.C:78
An Attribute object is a data member of a window.
Definition: Attribute.h:51
int get_status(const std::string &aname, int pane_id) const
Get the status of an attribute or pane.
Definition: Window.C:535
#define COM_assertion_msg(EX, msg)
double s
Definition: blastest.C:80
void load_module(const std::string &lname, const std::string &wname)
Load a module.
Definition: Roccom_base.C:211
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:354
int get_num_arguments(const std::string &wf)
Get the number of arguments of a given function &quot;window.function&quot;.
Definition: Roccom_base.C:1716
void get_size(const std::string &wa_str, int pane_id, int *size, int *ng=0)
Get the sizes of an attribute. The opposite of set_size.
Definition: Roccom_base.C:988
A Window object contains multiple panes and multiple data attributes.
Definition: Window.h:42
void deallocate_array(const std::string &wa, const int pid=0)
Deallocate space for an attribute in a pane, asuming the memory was allocated allocate_mesh or alloca...
Definition: Roccom_base.C:934
std::pair< int, int > get_f90pntoffsets(const Attribute *a)
Definition: Roccom_base.C:1135
T COMMPI_Comm_f2c(int c, T)
Definition: commpi.h:202
int get_window_handle(const std::string &wname)
Definition: Roccom_base.C:1471
void * p_xargv
Definition: Roccom_base.C:2275
void unload_module(const std::string &lname, const std::string &wname, int dodl=1)
Unload a module.
Definition: Roccom_base.C:324
Error_code ierr
Error code.
C/C++ Data types.
Definition: roccom_basic.h:129
int size() const
Definition: maps.h:78
void proc_exception(const COM_exception &, const std::string &)
Definition: Roccom_base.C:2259
static void init(int *argc, char ***argv)
Definition: Roccom_base.C:186
const std::string & name() const
Obtain the window&#39;s name.
Definition: Window.h:92
real *8 function offset(vNorm, x2, y2, z2)
Definition: PlaneNorm.f90:211
const std::string & name(int i) const
Name of the object.
Definition: maps.h:76
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
const Window * window() const
Obtain a constant pointer to the parent window of the attribute.
Definition: Attribute.C:80
static bool cast_obj()
Definition: Roccom_base.C:598
int get_attribute_handle(const std::string &waname)
Definition: Roccom_base.C:1540
Shorter_size location() const
Obtain the location of the attribute.
Definition: Attribute.h:186
Encapsulates the states of an exception.
void free_buffer(int **buf)
Definition: Roccom_base.C:1467
void set_member_function(const std::string &wf, Func_ptr ptr, const std::string &wa, const std::string &intents, const COM_Type *types, bool ff=false)
Definition: Roccom_base.C:1695
void delete_pane(const std::string &wname, const int pid)
Deletes a pane and its associated data.
Definition: Roccom_base.C:459
std::vector< double > wtimes_tree
Accumulator of wall-clock time spent by itself and those functions called by it.
Definition: maps.h:155
int COMMPI_Comm_size(MPI_Comm c)
Definition: commpi.h:165
std::string msg
Error message.
void resize_array(const std::string &wa, const int pane_id=0, void **addr=NULL, int strd=-1, int cap=0)
Resize an attribute on a specific pane and return the address by setting addr.
Definition: Roccom_base.C:727
const void * pointer() const
Obtain a constant pointer to the physical address.
Definition: Attribute.h:150
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to ** copy
Definition: roccomf90.h:20
std::pair< void *, std::set< std::string > > value_type
Definition: maps.h:47
void set_size(const std::string &wa_str, int pane_id, int nitems, int ng=0)
Set the sizes of an attribute.
Definition: Roccom_base.C:569
void print_profile(const std::string &fname, const std::string &header)
Definition: Roccom_base.C:2106
static void finalize()
Definition: Roccom_base.C:197
Window * get_window_object(int hdl)
Definition: Roccom_base.C:1495
MPI_Comm get_communicator(const std::string &wname)
Definition: Roccom_base.C:1241
**********************************************************************Rocstar Simulation Suite Illinois Rocstar LLC All rights reserved ****Illinois Rocstar LLC IL **www illinoisrocstar com **sales illinoisrocstar com WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **Arising OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE **********************************************************************INTERFACE SUBROUTINE knode iend
std::vector< double > wtimes_self
Accumulator of wall-clock time spent by itself excluding functions called by it.
Definition: maps.h:154
void delete_attribute(const std::string &wa)
Delete an existing attribute from a window.
Definition: Roccom_base.C:548
int _f90ptr_treat
Treatement of F90 pointers.
Definition: Roccom_base.h:493
COM_BEGIN_NAME_SPACE void printStackBacktrace()
Print the stack backtrace.
MPI_Comm get_communicator() const
Obtain the communicator of the window.
Definition: Window.h:95
void get_panes(const std::string &wname, std::vector< int > &paneids_vec, int rank=-2, int **pane_ids=NULL)
Obtain the panes of a given window on a specific process.
Definition: Roccom_base.C:1264
void set_object(const std::string &wa, const int pane_id, void *obj_addr, void *casted_obj)
Associates an object with a specific window.
Definition: Roccom_base.C:625
void(* Func_ptr)()
Pointer of functions.
Definition: roccom_basic.h:123
void get_connectivities(const std::string &wname, int pane_id, int *nc, std::string &str, char **names=NULL)
Obtain the connectivity tables of a pane of the given window.
Definition: Roccom_base.C:1390
void call_function(int wf, int count, void **args, const int *lens=NULL, bool from_c=true)
Invoke a function with given arguments.
Definition: Roccom_base.C:1744
std::vector< int > counts
Counts of the number of calls.
Definition: maps.h:156
const std::string & name() const
Obtain the name of the attribute.
Definition: Attribute.h:113
bool is_literal(int i) const
Check whether the ith argument is literal type.
Definition: Function.h:101
Attribute * attribute()
Definition: Function.h:121
MPI_Comm _comm
Default communicator of Roccom.
Definition: Roccom_base.h:481
blockLoc i
Definition: read.cpp:79
int get_status(const std::string &wa_str, int pane_id)
Get the status of an attribute.
Definition: Roccom_base.C:1055
COM_Type data_type(int i) const
Definition: Function.h:115
Contains declarations of MPI subroutines used in Roccom.
void set_member_function_helper(const std::string &wf, T ptr, const std::string &wa, const std::string &intents, const COM_Type *types, bool ff=false)
Definition: Roccom_base.C:1636
void get_windows(std::vector< std::string > &)
Obtain a list of all window names.
Definition: Roccom_base.C:1294
void get_parent(const std::string &waname, int pane_id, std::string &str, char **name=NULL)
Obtain the parent attribute&#39;s name of a given attribute on a given pane.
Definition: Roccom_base.C:1431
bool _profile_on
Indicates whether should profile.
Definition: Roccom_base.h:485
void new_window(const std::string &wname, MPI_Comm comm)
Creates a window with given name.
Definition: Roccom_base.C:399
const NT & n
static int get_sizeof(MPI_Datatype i)
Get the size of a given MPI data type.
Definition: commpi.C:44
void clone_attribute(const std::string &wname, const std::string &pwname, int withghost=1, const char *cndname=NULL, int val=0)
Clone the subset of panes of another window of which the given pane attribute has value val...
Definition: Roccom_base.C:822
int COMMPI_Comm_c2f(T t)
Definition: commpi.h:190
void get_object(const std::string &wa, const int pane_id, void **ptr)
Associates an object with a specific window.
Definition: Roccom_base.C:648
static Roccom_base * roccom_base
Definition: Roccom_base.h:499
Function & get_function(const int)
Obtains a reference to an attribute from its handle.
Definition: Roccom_base.C:2206
void get_bounds(const std::string &wa, const int pane_id, void *lbnd, void *ubnd)
Definition: Roccom_base.C:1126
~Roccom_base()
Destructor.
Definition: Roccom_base.C:181
int get_attribute_handle_const(const std::string &waname)
Definition: Roccom_base.C:1513
char intent(int i) const
Definition: Function.h:116
void print_type(std::ostream &os, COM_Type type)
Definition: Roccom_base.C:480
static double get_wtime()
Definition: Roccom_base.C:1734
#define append_frame(s, frame)
Macro for appending the information about the given frame to the string s.
void get_f90pointer(const std::string &waname, void *ptr, Func_ptr f, long int l)
Definition: Roccom_base.C:1202
static void set_roccom(Roccom_base *)
Set the Roccom pointer to the given object.
Definition: Roccom_base.C:56
void set_function_verbose(int i, int level)
Definition: Roccom_base.C:2064
void append_array(const std::string &wa, const int pane_id, const void *val, int v_strd, int v_size)
Append an array to the end of the attribute on a specific pane and return the new address by setting ...
Definition: Roccom_base.C:759
j indices j
Definition: Indexing.h:6
This file indirectly includes the following files: iostream, map, string, vector, and roccom_basic...
Definition: Roccom_base.h:49
void set_profiling_barrier(int hdl, MPI_Comm comm)
Definition: Roccom_base.C:2079
void set_bounds(const std::string &wa, const int pane_id, T lbnd, T ubnd)
Definition: Roccom_base.h:146
void window_init_done(const std::string &wname, bool panechanged=true)
Marks the end of the registration of a window.
Definition: Roccom_base.C:424
bool _debug
Indicated whether debug mode is on.
Definition: Roccom_base.h:480
void set_f90pointer(const std::string &waname, void *ptr, Func_ptr f, long int l)
Definition: Roccom_base.C:1147
bool _mpi_initialized
Indicates whether MPI was initialized by Roccom.
Definition: Roccom_base.h:482
int check_bounds(const std::string &wa, int pane_id)
Definition: Roccom_base.C:1130
int num_of_args() const
Get the number of arguments.
Definition: Function.h:93
void int int REAL REAL REAL *z blockDim dim * ni
Definition: read.cpp:77
static int rank
Definition: advectest.C:66
static Roccom_base * get_roccom()
Get a pointer to the Roccom object.
Definition: Roccom_base.h:505
int split_name(const std::string &wa, std::string &wname, std::string &aname, bool tothrow=true)
Extracts the window and attribute names from &quot;window.attribute&quot;.
Definition: Roccom_base.C:2236
COM_Type data_type() const
Obtain the data type of each component of the attribute.
Definition: Attribute.h:197
Fortran Data types.
Definition: roccom_basic.h:133
int COMMPI_Initialized()
Definition: commpi.h:168
void get_modules(std::vector< std::string > &)
Obtain a list of all module names.
Definition: Roccom_base.C:1322
void set_array(const std::string &wa, const int pane_id, void *addr, int strd=0, int cap=0, bool is_const=false)
Associates an array with an attribute for a specific pane.
Definition: Roccom_base.C:658
bool is_rawdata(int i) const
Check whether the ith argument is raw.
Definition: Function.h:108
void copy_attribute(const std::string &wname, const std::string &pwname, int withghost=1, const char *cndname=NULL, int val=0)
Copy an attribute onto another.
Definition: Roccom_base.C:862
void get_array(const std::string &wa, const int pane_id, void **addr, int *strd=NULL, int *cap=0, bool is_const=false)
Get the address for an attribute on a specific pane.
Definition: Roccom_base.C:1012
void new_attribute(const std::string &wa, const char loc, const int data_type, int size, const std::string &unit)
Creates a new attribute for a window.
Definition: Roccom_base.C:516
void allocate_array(const std::string &wa, const int pane_id=0, void **addr=NULL, int strd=0, int cap=0)
Allocate space for an attribute on a specific pane and return the address by setting addr...
Definition: Roccom_base.C:695
bool empty() const
Check whether the number of items of the attribute is zero.
Definition: Attribute.h:227
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494
bool is_const() const
Returns whether the array is set to be read-only.
Definition: Attribute.h:253
Attribute * inherit(Attribute *from, const std::string &aname, int inherit_mode, bool withghost, const Attribute *cond, int val)
Inherit the attributes of another window with a different name.
Definition: Window.C:334
Function_map _func_map
Definition: Roccom_base.h:473
static int get_sizeof(COM_Type type, int count=1)
Gets the size of the data type given by its index.
Definition: Roccom_base.C:2162
void set_profiling(int i)
This subroutine turns on (or off) profiling if i==1 (or ==0).
Definition: Roccom_base.C:2069
static int get_sizeof(COM_Type type, int count=1)
Definition: Attribute.C:488
Contains declaration of the base class for Roccom implementations.
int size_of_components() const
Obtain the number of components in the attribute.
Definition: Attribute.h:203
int get_function_handle(const std::string &wfname)
Definition: Roccom_base.C:1565