timers.c 3.91 KB
Newer Older
Pedro Gonnet's avatar
Pedro Gonnet committed
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4
5
6
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 *               2016 John A. Regan (john.a.regan@durham.ac.uk)
 *                    Tom Theuns (tom.theuns@durham.ac.uk)
7
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
8
9
10
11
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
12
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
13
14
15
16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
17
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
18
19
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
21
22
23
24
 ******************************************************************************/

/* Config parameters. */

25
/* This object's header. */
Pedro Gonnet's avatar
Pedro Gonnet committed
26
27
#include "timers.h"

28
29
30
31
32
33
/* Some standard headers. */
#include <stdio.h>

/* Local includes. */
#include "clocks.h"

Pedro Gonnet's avatar
Pedro Gonnet committed
34
/* The timers. */
35
ticks timers[timer_count];
Pedro Gonnet's avatar
Pedro Gonnet committed
36

37
/* Timer names. */
38
const char* timers_names[timer_count] = {
39
40
41
    "none",
    "prepare",
    "init",
42
    "init_grav",
43
44
    "drift_part",
    "drift_gpart",
45
46
47
48
49
50
51
52
53
54
55
56
57
    "kick1",
    "kick2",
    "timestep",
    "endforce",
    "dosort",
    "doself_density",
    "doself_gradient",
    "doself_force",
    "doself_grav_pp",
    "dopair_density",
    "dopair_gradient",
    "dopair_force",
    "dopair_grav_mm",
58
    "dopair_grav_pp",
59
60
    "dograv_external",
    "dograv_down",
61
    "dograv_mesh",
Matthieu Schaller's avatar
Matthieu Schaller committed
62
    "dograv_top_level",
63
64
65
66
67
68
69
70
71
72
    "dograv_long_range",
    "dosource",
    "dosub_self_density",
    "dosub_self_gradient",
    "dosub_self_force",
    "dosub_self_grav",
    "dosub_pair_density",
    "dosub_pair_gradient",
    "dosub_pair_force",
    "dosub_pair_grav",
73
    "doself_subset",
74
    "dopair_subset",
75
76
    "dopair_subset_naive",
    "dosub_subset",
77
78
79
80
81
    "do_ghost",
    "do_extra_ghost",
    "dorecv_part",
    "dorecv_gpart",
    "dorecv_spart",
82
    "do_cooling",
Matthieu Schaller's avatar
Matthieu Schaller committed
83
    "do_star_formation",
84
85
86
    "gettask",
    "qget",
    "qsteal",
87
    "locktree",
88
89
    "runners",
    "step",
90
91
92
93
94
95
96
    "doself_stars_density",
    "dopair_stars_density",
    "do_stars_ghost",
    "doself_subset_stars_density",
    "dopair_subset_stars_density",
    "dosubpair_stars_density",
    "dosub_self_stars_density",
Loikki's avatar
Loikki committed
97
    "logger"
98
99
};

100
101
102
/* File to store the timers */
static FILE* timers_file;

Pedro Gonnet's avatar
Pedro Gonnet committed
103
104
105
106
107
/**
 * @brief Re-set the timers.
 *
 * @param mask A bitmask of the timers to re-set.
 *
108
 * To reset all timers, use the mask #timers_mask_all.
Pedro Gonnet's avatar
Pedro Gonnet committed
109
 */
110
void timers_reset(unsigned long long mask) {
111
112

  /* Loop over the timers and set the masked ones to zero. */
113
  for (int k = 0; k < timer_count; k++)
114
    if (mask & (1ull << k)) timers[k] = 0;
115
}
116
117
118
119
120

/**
 * @brief Re-set all the timers.
 *
 */
121
void timers_reset_all(void) { timers_reset(timers_mask_all); }
122
123
124
125
126
127
128
129
130

/**
 * @brief Outputs all the timers to the timers dump file.
 *
 * @param step The current step.
 */
void timers_print(int step) {
  fprintf(timers_file, "%d\t", step);
  for (int k = 0; k < timer_count; k++)
131
    fprintf(timers_file, "%18.3f ", clocks_from_ticks(timers[k]));
132
  fprintf(timers_file, "\n");
133
  fflush(timers_file);
134
135
136
137
}

/**
 * @brief Opens the file to contain the timers info and print a header
138
139
 *
 * @param rank The MPI rank of the file.
140
 */
141
void timers_open_file(int rank) {
142

143
144
145
  char buff[100];
  sprintf(buff, "timers_%d.txt", rank);
  timers_file = fopen(buff, "w");
146
147
148

  fprintf(timers_file, "# timers: \n# step | ");
  for (int k = 0; k < timer_count; k++)
149
    fprintf(timers_file, "%18s ", timers_names[k]);
150
151
152
153
154
155
  fprintf(timers_file, "\n");
}

/**
 * @brief Close the file containing the timer info.
 */
156
void timers_close_file(void) { fclose(timers_file); }