Skip to content

Commit 8ead4b0

Browse files
authored
CMake: fix scope for -DHAS_BOOST_CHRONO (#2163)
* CMake: fix scope for -DHAS_BOOST_CHRONO (private -> public) Fixes the issue of potentially mixing boost and non-boost versions of CpuTimer. * CpuTimer unit test * simplify CpuTimer * document issues with compile definitions from dependencies * Add `amici.CpuTimer.uses_thread_clock` to check at runtime whether we are using `thread_clock`
1 parent 16aad6f commit 8ead4b0

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ if("$ENV{ENABLE_AMICI_DEBUGGING}"
296296
endif()
297297

298298
target_compile_definitions(
299-
${PROJECT_NAME} PRIVATE $<$<BOOL:${Boost_CHRONO_FOUND}>:HAS_BOOST_CHRONO>)
299+
${PROJECT_NAME} PUBLIC $<$<BOOL:${Boost_CHRONO_FOUND}>:HAS_BOOST_CHRONO>)
300300

301301
target_link_libraries(
302302
${PROJECT_NAME}

include/amici/misc.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ template <class T> bool is_equal(T const& a, T const& b) {
255255
}
256256

257257
#ifdef BOOST_CHRONO_HAS_THREAD_CLOCK
258-
/** Tracks elapsed CPU time. */
258+
/** Tracks elapsed CPU time using boost::chrono::thread_clock. */
259259
class CpuTimer {
260260
using clock = boost::chrono::thread_clock;
261-
using time_point = boost::chrono::thread_clock::time_point;
261+
using time_point = clock::time_point;
262262
using d_seconds = boost::chrono::duration<double>;
263263
using d_milliseconds = boost::chrono::duration<double, boost::milli>;
264264

@@ -279,8 +279,7 @@ class CpuTimer {
279279
* @return CPU time in seconds
280280
*/
281281
double elapsed_seconds() const {
282-
return boost::chrono::duration_cast<d_seconds>(clock::now() - start_)
283-
.count();
282+
return d_seconds(clock::now() - start_).count();
284283
}
285284

286285
/**
@@ -289,18 +288,17 @@ class CpuTimer {
289288
* @return CPU time in milliseconds
290289
*/
291290
double elapsed_milliseconds() const {
292-
return boost::chrono::duration_cast<d_milliseconds>(
293-
clock::now() - start_
294-
)
295-
.count();
291+
return d_milliseconds(clock::now() - start_).count();
296292
}
297293

294+
static const bool uses_thread_clock = true;
295+
298296
private:
299297
/** Start time */
300298
time_point start_;
301299
};
302300
#else
303-
/** Tracks elapsed CPU time. */
301+
/** Tracks elapsed CPU time using std::clock. */
304302
class CpuTimer {
305303
public:
306304
/**
@@ -332,6 +330,8 @@ class CpuTimer {
332330
/ CLOCKS_PER_SEC;
333331
}
334332

333+
static const bool uses_thread_clock = false;
334+
335335
private:
336336
/** Start time */
337337
std::clock_t start_;

swig/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ if(${SWIG_VERSION} VERSION_LESS 4.1.0)
112112
PROPERTY SWIG_COMPILE_OPTIONS -py3)
113113
endif()
114114

115+
# NOTE: No public definitions of any dependency are forwarded to swig,
116+
# they are only used for compiling the swig-generated source file.
117+
# Any definition that are relevant for swig-code generation, need to be
118+
# forwarded manually.
115119
target_link_libraries(_amici amici Python3::Python)
116120
if(WIN32)
117121
add_custom_command(

tests/cpp/unittests/testMisc.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,16 @@ TEST(SpanEqual, SpanEqual)
723723
EXPECT_FALSE(is_equal(a, b));
724724
}
725725

726+
TEST(CpuTimer, CpuTimer)
727+
{
728+
amici::CpuTimer timer;
729+
auto elapsed = timer.elapsed_seconds();
730+
EXPECT_LE(0.0, elapsed);
731+
EXPECT_GT(1.0, elapsed);
732+
733+
elapsed = timer.elapsed_milliseconds();
734+
EXPECT_LT(0.0, elapsed);
735+
EXPECT_GT(1000.0, elapsed);
736+
}
737+
726738
} // namespace

0 commit comments

Comments
 (0)