ergo
utilities.h
Go to the documentation of this file.
1 /* Ergo, version 3.3, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2013 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 
37 #define MAX_HOST_NAME_LEN 100
38 
39 typedef struct
40 {
43 
44 #define MAX_WORKING_DIRECTORY_LEN 800
45 
46 typedef struct
47 {
50 
51 void get_host_name(host_name_struct* result);
52 
54 
55 int get_memory_usage_by_ps(double* virtualMemoryGigaBytes, double* residentMemoryGigaBytes);
56 
57 int get_memory_usage_by_procfile(double* virtualMemGigaBytes,
58  double* residentMemGigaBytes,
59  double* virtualMemPeakGigaBytes);
60 
61 int generate_unique_random_filename(char* result, unsigned n);
62 
63 long int get_file_size(const char* fileName);
64 
65 #include <stdexcept>
66 #include "output.h"
67 #include "realtype.h"
68 namespace Util {
71  class TimeMeter {
72  private:
75  double startTimeWall;
76  public:
77  double get_start_time_wall_seconds() const {
78  return startTimeWall;
79  }
80  static double get_wall_seconds() {
81  struct timeval tv;
82  if(gettimeofday(&tv, NULL) != 0)
83  throw std::runtime_error("Error in get_wall_seconds(), in gettimeofday().");
84  double seconds = tv.tv_sec + (double)tv.tv_usec / 1000000;
85  return seconds;
86  }
87  static void get_current_cpu_times(double & seconds_usr, double & seconds_sys) {
88  struct rusage usage;
89  if(getrusage (RUSAGE_SELF, &usage) != 0)
90  throw std::runtime_error("Error in get_current_cpu_times(), in getrusage().");
91  seconds_usr = usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000;
92  seconds_sys = usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec / 1000000;
93  }
97  }
98  void print(int area, const char *routine) {
99  double endTimeWall = get_wall_seconds();
100  double secondsTakenWall = endTimeWall - startTimeWall;
101  double seconds_usr, seconds_sys;
102  get_current_cpu_times(seconds_usr, seconds_sys);
103  double secondsTakenCPU_usr = seconds_usr - startTimeCPU_usr;
104  double secondsTakenCPU_sys = seconds_sys - startTimeCPU_sys;
105  do_output(LOG_CAT_TIMINGS, area, "%s took %9.2f usr cpu s %9.2f sys cpu s %9.2f wall s",
106  routine, secondsTakenCPU_usr, secondsTakenCPU_sys, secondsTakenWall);
107  }
108 
109 
110  };
111 }
112 
113 
114 #endif /* UTILITIES_HEADER */
#define MAX_HOST_NAME_LEN
Definition: utilities.h:37
#define LOG_CAT_TIMINGS
Definition: output.h:43
#define MAX_WORKING_DIRECTORY_LEN
Definition: utilities.h:44
double startTimeCPU_sys
Definition: utilities.h:73
int get_memory_usage_by_ps(double *virtualMemoryGigaBytes, double *residentMemoryGigaBytes)
Definition: utilities.cc:127
Definition: utilities.h:46
int generate_unique_random_filename(char *result, unsigned n)
Definition: utilities.cc:50
int get_memory_usage_by_procfile(double *virtualMemGigaBytes, double *residentMemGigaBytes, double *virtualMemPeakGigaBytes)
Definition: utilities.cc:226
Time-measuring class.
Definition: utilities.h:71
void get_host_name(host_name_struct *result)
Definition: utilities.cc:75
void print(int area, const char *routine)
Definition: utilities.h:98
double startTimeCPU_usr
Definition: utilities.h:74
double get_start_time_wall_seconds() const
Definition: utilities.h:77
static double get_wall_seconds()
Definition: utilities.h:80
Definition: utilities.h:39
static void get_current_cpu_times(double &seconds_usr, double &seconds_sys)
Definition: utilities.h:87
void get_working_directory(working_directory_struct *result)
Definition: utilities.cc:85
void do_output(int logCategory, int logArea, const char *format,...)
Definition: output.cc:42
TimeMeter()
Definition: utilities.h:94
long int get_file_size(const char *fileName)
Definition: utilities.cc:64
double startTimeWall
Definition: utilities.h:75