ergo
utilities.h
Go to the documentation of this file.
1 /* Ergo, version 3.2, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
28 #ifndef UTILITIES_HEADER
29 #define UTILITIES_HEADER
30 
31 
32 #include <time.h>
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 
36 #ifdef __cplusplus
37 #define EXTERN_C extern "C"
38 #else
39 #define EXTERN_C
40 #endif
41 
42 
43 #define MAX_HOST_NAME_LEN 100
44 
45 typedef struct
46 {
49 
50 #define MAX_WORKING_DIRECTORY_LEN 800
51 
52 typedef struct
53 {
56 
58 
60 
61 EXTERN_C int get_memory_usage_by_ps(double* virtualMemoryGigaBytes, double* residentMemoryGigaBytes);
62 
63 EXTERN_C int get_memory_usage_by_procfile(double* virtualMemGigaBytes,
64  double* residentMemGigaBytes,
65  double* virtualMemPeakGigaBytes);
66 
67 EXTERN_C int generate_unique_random_filename(char* result, unsigned n);
68 
69 #ifdef __cplusplus
70 #include <stdexcept>
71 #include "output.h"
72 #include "realtype.h"
73 namespace Util {
76  class TimeMeter {
77  private:
78  double startTimeCPU_sys;
79  double startTimeCPU_usr;
80  double startTimeWall;
81  public:
82  double get_start_time_wall_seconds() const {
83  return startTimeWall;
84  }
85  static double get_wall_seconds() {
86  struct timeval tv;
87  if(gettimeofday(&tv, NULL) != 0)
88  throw std::runtime_error("Error in get_wall_seconds(), in gettimeofday().");
89  double seconds = tv.tv_sec + (double)tv.tv_usec / 1000000;
90  return seconds;
91  }
92  static void get_current_cpu_times(double & seconds_usr, double & seconds_sys) {
93  struct rusage usage;
94  if(getrusage (RUSAGE_SELF, &usage) != 0)
95  throw std::runtime_error("Error in get_current_cpu_times(), in getrusage().");
96  seconds_usr = usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000;
97  seconds_sys = usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec / 1000000;
98  }
99  TimeMeter() {
100  startTimeWall = get_wall_seconds();
101  get_current_cpu_times(startTimeCPU_usr, startTimeCPU_sys);
102  }
103  void print(int area, const char *routine) {
104  double endTimeWall = get_wall_seconds();
105  double secondsTakenWall = endTimeWall - startTimeWall;
106  double seconds_usr, seconds_sys;
107  get_current_cpu_times(seconds_usr, seconds_sys);
108  double secondsTakenCPU_usr = seconds_usr - startTimeCPU_usr;
109  double secondsTakenCPU_sys = seconds_sys - startTimeCPU_sys;
110  do_output(LOG_CAT_TIMINGS, area, "%s took %9.2f usr cpu s %9.2f sys cpu s %9.2f wall s",
111  routine, secondsTakenCPU_usr, secondsTakenCPU_sys, secondsTakenWall);
112  }
113 
114 
115  };
116 }
117 
118 #endif
119 
120 #endif /* UTILITIES_HEADER */