Actual source code: ALE.cxx

  1: #define ALE_ALE_cxx

  3: #include <ALE.hh>

  5: namespace ALE {
  6:   //
  7:   // Package-wide verbosity
  8:   //
  9:   static int verbosity = 0;

 11:   int  getVerbosity() {return ALE::verbosity;};
 12:   void setVerbosity(const int& verbosity) {ALE::verbosity = verbosity;};

 14:   //
 15:   //  Memory handling stuff (ALE_mem.hh).
 16:   //
 17: 
 18:   // static instance of a standard char allocator;  this is the only allocator used by ALE;
 19:   // it is defined here -- in an .cxx file -- to ensure that exactly one copy exists;
 20:   // its services are presented through a static interface defined in universal_allocator.

 22:   std::allocator<char> _alloc;

 24:   char *universal_allocator::allocate(const universal_allocator::size_type& sz) {
 25:     return _alloc.allocate(sz);
 26:   }

 28:   void universal_allocator::deallocate(char *p, const universal_allocator::size_type& sz) {
 29:     return _alloc.deallocate(p,sz);
 30:   }

 32:   universal_allocator::size_type universal_allocator::max_size() {
 33:     return _alloc.max_size();
 34:   }

 36:   //
 37:   // Error/exception handling helper functions (ALE_exception.hh).
 38:   //

 40:   // A helper function for converting PETSc errors to exception
 41:   // including the function and the line where the error occured.
 42:   void ERROR(PetscErrorCode ierr, const char *func, int line, const char *msg) {
 43:     if(ierr) {
 44:         int32_t buf_size = 2*1024;
 45:         char *mess = (char *)malloc(sizeof(char)*(buf_size+1));
 46:         snprintf(mess, buf_size, "%s: line %d: error %d: %s:\n", func, line, (int)ierr, msg);
 47:         throw ALE::Exception(mess);
 48:     }
 49:   }// ERROR()

 51:   const char *ERRORMSG(const char *fmt, ...);

 53:   // A helper function for converting MPI errors to exception
 54:   void MPIERROR(PetscErrorCode ierr, const char *func, int line, const char *msg) {
 55:     if(ierr) {
 56:       char mpi_error[MPI_MAX_ERROR_STRING+1];
 57:       int32_t len = MPI_MAX_ERROR_STRING;
 58:       PetscErrorCode ie = MPI_Error_string(ierr, mpi_error, &len);
 59:       char *mess;
 60:       if(!ie) {
 61:         mess = (char *) malloc(sizeof(char)*(strlen(msg)+len+3));
 62:         sprintf(mess, "%s: %s", msg, mpi_error);
 63:       } else {
 64:         mess = (char *) malloc(sizeof(char)*(strlen(msg)+18));
 65:         sprintf(mess, "%s: <unknown error>", msg);
 66:       }
 67:       ERROR(ierr, func, line, mess);
 68:     }
 69:   }// MPIERROR()

 71:   // A helper function that allocates and assembles an error message from a format string
 72:   const char *ERRORMSG(const char *fmt, ...) {
 73:     va_list Argp;
 74:     int32_t buf_size = 2*MPI_MAX_ERROR_STRING;
 75:     if(fmt) {
 76:       va_start(Argp, fmt);
 77:       char *msg = (char *)malloc(sizeof(char)*(buf_size+1));
 78:       snprintf(msg, buf_size, fmt, Argp);
 79:       va_end(Argp);
 80:       return msg;
 81:     }
 82:     return fmt;
 83:   }// ERRORMSG()

 85:   //
 86:   // Logging helper functions
 87:   //

 89:   static std::map<std::string, LogStage> _log_stage;  // a map from stage names to stage numbers

 91:   #undef  __FUNCT__
 93:   LogCookie LogCookieRegister(const char *name){
 94:     LogCookie cookie;
 95:     PetscErrorCode PetscLogClassRegister(&cookie, name);
 96:     CHKERROR(ierr, "PetscLogClassRegister failed");
 97:     return cookie;
 98:   }

100:   #undef  __FUNCT__
102:   LogStage LogStageRegister(const char *name){
103:     int stage;
104:     std::string stage_name(name);
105:     if(_log_stage.find(stage_name) == _log_stage.end()) {
106:       // stage by that name not yet registered, so we register it and store its registration number.
107:       PetscErrorCode PetscLogStageRegister(&stage, name); CHKERROR(ierr, "PetscLogStageRegister failed");
108:       _log_stage[stage_name] = stage;
109:     }
110:     else {
111:       // stage by that name already registered, so we retrieve its registration number.
112:       stage = _log_stage[stage_name];
113:     }
114:     return stage;
115:   }

117:   #undef  __FUNCT__
119:   void LogStagePush(int s){
121:     PetscLogStagePush(s); CHKERROR(ierr, "PetscLogStagePush failed");
122:   }//LogStagePush()

124:   #undef  __FUNCT__
126:   void LogStagePop(int s){
127:     // A future implementation may use 's' to check for the correct order of stage push/pop events.
129:     PetscLogStagePop(); CHKERROR(ierr, "PetscLogStagePop failed");
130:   }//LogStagePop()

132:   static std::map<std::string, LogEvent> _log_event;  // a map from event names to event numbers

134:   #undef  __FUNCT__
136:   LogEvent LogEventRegister(LogCookie cookie, const char *name){
137:     LogEvent event = 0;
138: #if 0
139:     std::string event_name(name);
140:     if(_log_event.find(event_name) == _log_event.end()) {
143: #ifdef ALE_LOGGING_VERBOSE
144:       //std::cout << "Registered event " << name << " to " << event << std::endl;
145: #endif
146:       _log_event[event_name] = event;
147:     }
148:     else {
149:       // event by that name already registered, so we retrieve its registration number.
150:       event = _log_event[event_name];
151:     }
152: #endif                                                        
153:     return event;
154:   }

156:   #undef  __FUNCT__
158:   LogEvent LogEventRegister(const char *name){
159:     return LogEventRegister(PETSC_SMALLEST_COOKIE, name);
160:   }

162:   #undef  __FUNCT__
164:   void LogEventBegin(LogEvent e){
167:   }//LogEventBegin()

169:   #undef  __FUNCT__
171:   void LogEventEnd(LogEvent e){
174:   }//LogEventEnd()

176: }

178: #undef ALE_ALE_cxx