Theseus
Compressible flow solver
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1// Copyright (c) 2025-2026 Board of Trustees of the University of Illinois
2//
3// This file is part of Theseus.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6#pragma once
7#include <chrono>
8#include <cstdio>
9#include <mpi.h>
10#ifdef TIMER_SYNC_DEVICE
11#include "mfem.hpp"
12#endif
13
14namespace Theseus
15{
17 {
18 public:
19#ifdef ENABLE_TIMERS
20#ifndef TIMER_SYNC_DEVICE
21 explicit ScopedTimer(const char *name)
22 : name_(name),
23 start_(clock::now())
24 {}
25#else
26 explicit ScopedTimer(const char *name)
27 : name_(name)
28 {
29 MFEM_DEVICE_SYNC;
30 // mfem::Device::Sync();
31 start_ = clock::now();
32 }
33#endif
34
36 {
37#ifdef TIMER_SYNC_DEVICE
38 MFEM_DEVICE_SYNC;
39 // mfem::Device::Sync();
40#endif
41 auto end = clock::now();
42 double local_ms = std::chrono::duration<double, std::milli>(end - start_).count();
43 double global_ms = local_ms;
44 int rank = 0;
45 int nranks = 1;
46 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
47 MPI_Comm_size(MPI_COMM_WORLD, &nranks);
48#ifdef TIMER_BARRIER
49 MPI_Barrier(MPI_COMM_WORLD);
50 end = clock::now();
51 if(rank == 0)
52 MPI_Reduce(MPI_IN_PLACE,&global_ms, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
53 else
54 MPI_Reduce(&global_ms, NULL, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
55#endif
56#ifdef TIMER_OUTPUT_ALLRANKS
57 if(nranks > 1 && rank > 0)
58 std::printf("[TIMER(%d)] %s : %.6f ms\n", rank, name_, local_ms);
59#endif
60 if(rank == 0 ){
61 std::printf("[TIMER(%d)] %s : %.6f ms\n", rank, name_, local_ms);
62#ifdef TIMER_BARRIER
63 if(nranks > 1)
64 std::printf("[TIMER(all)] %s : %.6f ms\n", name_, global_ms);
65#endif
66 }
67 }
68#else
69 explicit ScopedTimer(const char *name)
70 : name_(name),
71 start_() {}
73#endif
74 private:
75 using clock = std::chrono::steady_clock;
76 const char *name_;
77 clock::time_point start_;
78 };
79
80}
Definition timer.hpp:17
ScopedTimer(const char *name)
Definition timer.hpp:69
~ScopedTimer()
Definition timer.hpp:72
Definition AxisymmetricGeometry.hpp:15