Skip to content
Snippets Groups Projects
Commit 858e67d7 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Use new clocks functions in engine

After tests prefer sleep() cpu freq estimate, that works well on i7 (with constant_tsc), sys maxfreq looks like the turbo boost value so not as good
parent 12f69efd
No related branches found
No related tags found
2 merge requests!136Master,!105Show time in real milli seconds
...@@ -94,40 +94,40 @@ unsigned long long clocks_cpufreq() { ...@@ -94,40 +94,40 @@ unsigned long long clocks_cpufreq() {
if (cpufreq > 0) if (cpufreq > 0)
return cpufreq; return cpufreq;
/* Look for the system value, if available. */
#ifdef __linux__
FILE *file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
"r");
if (file != NULL) {
unsigned long long maxfreq;
if (fscanf(file, "%llu", &maxfreq) == 1) {
cpufreq = maxfreq;
}
fclose(file);
}
#endif
#ifdef HAVE_CLOCK_GETTIME #ifdef HAVE_CLOCK_GETTIME
/* If not try to time a nanosleep() in ticks. */ /* Try to time a nanosleep() in ticks. */
if (cpufreq == 0) { struct clockstime time1;
struct clockstime time1; struct clockstime time2;
struct clockstime time2;
struct timespec sleep; struct timespec sleep;
sleep.tv_sec = 0; sleep.tv_sec = 0;
sleep.tv_nsec = SLEEPTIME; sleep.tv_nsec = SLEEPTIME;
clocks_gettime(&time1); clocks_gettime(&time1);
ticks tic = getticks(); ticks tic = getticks();
/* Could do some calculation, but constant_tsc should protect us. */ /* Could do some calculation, but constant_tsc should protect us. */
nanosleep(&sleep, NULL); nanosleep(&sleep, NULL);
clocks_gettime(&time2); clocks_gettime(&time2);
ticks toc = getticks(); ticks toc = getticks();
double realsleep = clocks_diff(&time1, &time2); double realsleep = clocks_diff(&time1, &time2);
cpufreq = (signed long long) (double)(toc - tic) * 1.0/realsleep * 1000.0;
#endif
cpufreq = (signed long long) (double)(toc - tic) * 1.0 / realsleep; /* Look for the system value, if available. Tends to be too large. */
#ifdef __linux__
if (cpufreq == 0) {
FILE *file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
"r");
if (file != NULL) {
unsigned long long maxfreq;
if (fscanf(file, "%llu", &maxfreq) == 1) {
cpufreq = maxfreq * 1000;
}
fclose(file);
}
} }
#endif #endif
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -47,6 +46,7 @@ ...@@ -47,6 +46,7 @@
/* Local headers. */ /* Local headers. */
#include "atomic.h" #include "atomic.h"
#include "cell.h" #include "cell.h"
#include "clocks.h"
#include "cycle.h" #include "cycle.h"
#include "debug.h" #include "debug.h"
#include "error.h" #include "error.h"
...@@ -1465,29 +1465,6 @@ void engine_init_particles(struct engine *e) { ...@@ -1465,29 +1465,6 @@ void engine_init_particles(struct engine *e) {
e->step = -1; e->step = -1;
} }
/**
* brief Get difference in milli-seconds between two timespecs.
*
* @param start the start time.
* @param end the end time.
*
* @return the difference in milli-secinds.
*/
static double time_diff(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return (double)temp.tv_sec*1000.0 + (double)temp.tv_nsec*1.0E-6;
}
/** /**
* @brief Let the #engine loose to compute the forces. * @brief Let the #engine loose to compute the forces.
* *
...@@ -1506,8 +1483,8 @@ void engine_step(struct engine *e) { ...@@ -1506,8 +1483,8 @@ void engine_step(struct engine *e) {
TIMER_TIC2; TIMER_TIC2;
struct timespec time1, time2; struct clockstime time1, time2;
clock_gettime(CLOCK_REALTIME, &time1); clocks_gettime(&time1);
/* Collect the cell data. */ /* Collect the cell data. */
for (k = 0; k < s->nr_cells; k++) for (k = 0; k < s->nr_cells; k++)
...@@ -1647,11 +1624,12 @@ void engine_step(struct engine *e) { ...@@ -1647,11 +1624,12 @@ void engine_step(struct engine *e) {
TIMER_TOC2(timer_step); TIMER_TOC2(timer_step);
clock_gettime(CLOCK_REALTIME, &time2); clocks_gettime(&time2);
e->wallclock_time_ticks = ((double)timers[timer_count - 1]) / CPU_TPS * 1000; e->wallclock_time_ticks =
((double)timers[timer_count - 1]) / clocks_cpufreq() * 1000;
e->wallclock_time = (float) time_diff(time1, time2); e->wallclock_time = (float) clocks_diff(&time1, &time2);
// printParticle(e->s->parts, e->s->xparts,1000, e->s->nr_parts); // printParticle(e->s->parts, e->s->xparts,1000, e->s->nr_parts);
// printParticle(e->s->parts, e->s->xparts,515050, e->s->nr_parts); // printParticle(e->s->parts, e->s->xparts,515050, e->s->nr_parts);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment