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 that throws an ALE::Exception with a message identifying the function that returned the given error code,
 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+1));
 62:         sprintf(mess, "%s: %s", msg, mpi_error);
 63:       }
 64:       else {
 65:         mess = (char *)malloc(sizeof(char)*(strlen(msg)));
 66:         sprintf(mess, "%s: <unknown error>", msg);
 67:       }
 68:       ERROR(ierr, func, line, mess);
 69:     }
 70:   }// MPIERROR()

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

 86:   //
 87:   // Logging helper functions
 88:   //

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

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

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

118:   #undef  __FUNCT__
120:   void LogStagePush(int s){
122:     PetscLogStagePush(s); CHKERROR(ierr, "PetscLogStagePush failed");
123:   }//LogStagePush()

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

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

135:   #undef  __FUNCT__
137:   LogEvent LogEventRegister(LogCookie cookie, const char *name){
138:     LogEvent event;
139:     std::string event_name(name);
140:     if(_log_event.find(event_name) == _log_event.end()) {
141:       PetscErrorCode PetscLogEventRegister(&event, name, cookie);
142:       CHKERROR(ierr, "PetscLogEventRegister failed");
143:       _log_event[event_name] = event;
144:     }
145:     else {
146:       // event by that name already registered, so we retrieve its registration number.
147:       event = _log_event[event_name];
148:     }
149:     return event;
150:   }

152:   #undef  __FUNCT__
154:   LogEvent LogEventRegister(const char *name){
155:     return LogEventRegister(PETSC_SMALLEST_COOKIE, name);
156:   }

158:   #undef  __FUNCT__
160:   void LogEventBegin(LogEvent e){
162:     PetscLogEventBegin(e, 0, 0, 0, 0); CHKERROR(ierr, "PetscLogEventBegin failed");
163:   }//LogEventBegin()

165:   #undef  __FUNCT__
167:   void LogEventEnd(LogEvent e){
169:     PetscLogEventEnd(e, 0, 0, 0, 0); CHKERROR(ierr, "PetscLogEventEnd failed");
170:   }//LogEventEnd()

172: }

174: #undef ALE_ALE_cxx