Actual source code: ALE.cxx

petsc-3.4.5 2014-06-29
  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:   //

 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:       ostringstream mess;
 45:       mess << func << ": line " << line << ": error " << ierr << ": " << msg << ":\n";
 46:       throw ALE::Exception(mess);
 47:     }
 48:   }// ERROR()

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

 52:   // A helper function for converting MPI errors to exception
 53:   void MPIERROR(PetscErrorCode ierr, const char *func, int line, const char *msg) {
 54:     if(ierr) {
 55:       char mpi_error[MPI_MAX_ERROR_STRING+1];
 56:       int len = MPI_MAX_ERROR_STRING;
 57:       PetscErrorCode ie = MPI_Error_string(ierr, mpi_error, &len);
 58:       ostringstream mess;

 60:       if (!ie) {
 61:         mess << func << ": line " << line << ": error " << ierr << ": " << msg << ": " << mpi_error << "\n";
 62:       } else {
 63:         mess << func << ": line " << line << ": error " << ierr << ": " << msg << ": <unknown error>\n";
 64:       }
 65:       throw ALE::Exception(mess);
 66:     }
 67:   }// MPIERROR()

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

 87:   //
 88:   // Logging helper functions
 89:   //

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

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

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

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

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

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

136:   #undef  __FUNCT__
138:   LogEvent LogEventRegister(LogCookie cookie, const char *name) {
139:     std::string event_name(name);
140:     LogEvent event = 0;

142:     if (_log_event.find(event_name) == _log_event.end()) {
143:       PetscErrorCode PetscLogEventRegister(name, cookie, &event);CHKERROR(ierr, "PetscLogEventRegister failed");
144:       _log_event[event_name] = event;
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_CLASSID, 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