utilities.h

Go to the documentation of this file.
00001 /* Ergo, version 3.2, a program for linear scaling electronic structure
00002  * calculations.
00003  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  * 
00018  * Primary academic reference:
00019  * Kohn−Sham Density Functional Theory Electronic Structure Calculations 
00020  * with Linearly Scaling Computational Time and Memory Usage,
00021  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
00022  * J. Chem. Theory Comput. 7, 340 (2011),
00023  * <http://dx.doi.org/10.1021/ct100611z>
00024  * 
00025  * For further information about Ergo, see <http://www.ergoscf.org>.
00026  */
00027 
00028 #ifndef UTILITIES_HEADER
00029 #define UTILITIES_HEADER
00030 
00031 
00032 #include <time.h>
00033 #include <sys/time.h>
00034 #include <sys/resource.h>
00035 
00036 #ifdef __cplusplus
00037 #define EXTERN_C extern "C"
00038 #else
00039 #define EXTERN_C
00040 #endif
00041 
00042 
00043 #define MAX_HOST_NAME_LEN 100
00044 
00045 typedef struct
00046 {
00047   char s[MAX_HOST_NAME_LEN];
00048 } host_name_struct;
00049 
00050 #define MAX_WORKING_DIRECTORY_LEN 800
00051 
00052 typedef struct
00053 {
00054   char s[MAX_WORKING_DIRECTORY_LEN];
00055 } working_directory_struct;
00056 
00057 EXTERN_C void get_host_name(host_name_struct* result);
00058 
00059 EXTERN_C void get_working_directory(working_directory_struct* result);
00060 
00061 EXTERN_C int get_memory_usage_by_ps(double* virtualMemoryGigaBytes, double* residentMemoryGigaBytes);
00062 
00063 EXTERN_C int get_memory_usage_by_procfile(double* virtualMemGigaBytes,
00064                                           double* residentMemGigaBytes,
00065                                           double* virtualMemPeakGigaBytes);
00066 
00067 EXTERN_C int generate_unique_random_filename(char* result, unsigned n);
00068 
00069 #ifdef __cplusplus
00070 #include <stdexcept>
00071 #include "output.h"
00072 #include "realtype.h"
00073 namespace Util {
00076   class TimeMeter {
00077   private:
00078     double startTimeCPU_sys;
00079     double startTimeCPU_usr;
00080     double startTimeWall;
00081   public:
00082     double get_start_time_wall_seconds() const {
00083       return startTimeWall;
00084     }
00085     static double get_wall_seconds() {
00086       struct timeval tv;
00087       if(gettimeofday(&tv, NULL) != 0)
00088         throw std::runtime_error("Error in get_wall_seconds(), in gettimeofday().");
00089       double seconds = tv.tv_sec + (double)tv.tv_usec / 1000000;
00090       return seconds;
00091     }
00092     static void get_current_cpu_times(double & seconds_usr, double & seconds_sys) {
00093       struct rusage usage;
00094       if(getrusage (RUSAGE_SELF, &usage) != 0)
00095         throw std::runtime_error("Error in get_current_cpu_times(), in getrusage().");
00096       seconds_usr = usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000;
00097       seconds_sys = usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec / 1000000;
00098     }
00099     TimeMeter() {
00100       startTimeWall = get_wall_seconds();
00101       get_current_cpu_times(startTimeCPU_usr, startTimeCPU_sys);
00102     }
00103     void print(int area, const char *routine) {
00104       double endTimeWall = get_wall_seconds();
00105       double secondsTakenWall = endTimeWall - startTimeWall;
00106       double seconds_usr, seconds_sys;
00107       get_current_cpu_times(seconds_usr, seconds_sys);
00108       double secondsTakenCPU_usr = seconds_usr - startTimeCPU_usr;
00109       double secondsTakenCPU_sys = seconds_sys - startTimeCPU_sys;
00110       do_output(LOG_CAT_TIMINGS, area, "%s took %9.2f usr cpu s  %9.2f sys cpu s  %9.2f wall s", 
00111                 routine, secondsTakenCPU_usr, secondsTakenCPU_sys, secondsTakenWall);
00112     }
00113     
00114 
00115   };
00116 }
00117 
00118 #endif
00119 
00120 #endif /* UTILITIES_HEADER */

Generated on Mon Sep 17 14:32:57 2012 for ergo by  doxygen 1.4.7