Skip to content

Commit 99c5ee9

Browse files
committed
move optional extra timestamp functions to utilities
1 parent b9bfccf commit 99c5ee9

File tree

4 files changed

+116
-29
lines changed

4 files changed

+116
-29
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include <string>
21+
22+
namespace cuopt {
23+
namespace utilities {
24+
25+
/**
26+
* @brief Check if extra timestamps should be printed based on environment variable
27+
*
28+
* Checks the CUOPT_EXTRA_TIMESTAMPS environment variable once and caches the result.
29+
* Returns true if the environment variable is set to "True", "true", or "1".
30+
*
31+
* @return true if extra timestamps are enabled, false otherwise
32+
*/
33+
bool extraTimestamps();
34+
35+
/**
36+
* @brief Get current timestamp as seconds since epoch
37+
*
38+
* @return Current timestamp as a double representing seconds since epoch
39+
*/
40+
double getCurrentTimestamp();
41+
42+
/**
43+
* @brief Print a timestamp with label if extra timestamps are enabled
44+
*
45+
* @param label The label to print with the timestamp
46+
*/
47+
void printTimestamp(const std::string& label);
48+
49+
} // namespace utilities
50+
} // namespace cuopt

cpp/src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
set(UTIL_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/utilities/seed_generator.cu
1717
${CMAKE_CURRENT_SOURCE_DIR}/utilities/logger_helper.cpp
18-
${CMAKE_CURRENT_SOURCE_DIR}/utilities/version_info.cpp)
18+
${CMAKE_CURRENT_SOURCE_DIR}/utilities/version_info.cpp
19+
${CMAKE_CURRENT_SOURCE_DIR}/utilities/timestamp_utils.cpp)
1920

2021
add_subdirectory(linear_programming)
2122
add_subdirectory(math_optimization)

cpp/src/linear_programming/cuopt_c.cpp

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cuopt/linear_programming/solve.hpp>
2222
#include <cuopt/linear_programming/solver_settings.hpp>
2323
#include <cuopt/logger.hpp>
24+
#include <cuopt/utilities/timestamp_utils.hpp>
2425

2526
#include <mps_parser/parser.hpp>
2627

@@ -76,29 +77,6 @@ cuopt_int_t cuOptGetVersion(cuopt_int_t* version_major,
7677
return CUOPT_SUCCESS;
7778
}
7879

79-
bool extraTimestamps() {
80-
const char* envValue = std::getenv("CUOPT_EXTRA_TIMESTAMPS");
81-
if (envValue == nullptr) {
82-
return false;
83-
}
84-
85-
std::string value(envValue);
86-
return (value == "True" || value == "true" || value == "1");
87-
}
88-
89-
90-
double getCurrentTimestamp() {
91-
auto now = std::chrono::system_clock::now();
92-
return std::chrono::duration<double>(now.time_since_epoch()).count();
93-
}
94-
95-
void printTimestamp(const std::string& label) {
96-
if (extraTimestamps()) {
97-
std::cout << std::fixed << std::setprecision(6);
98-
std::cout << std::endl << label << ": " << getCurrentTimestamp() << std::endl;
99-
}
100-
}
101-
10280
cuopt_int_t cuOptReadProblem(const char* filename, cuOptOptimizationProblem* problem_ptr)
10381
{
10482
problem_and_stream_view_t* problem_and_stream = new problem_and_stream_view_t();
@@ -141,7 +119,7 @@ cuopt_int_t cuOptCreateProblem(cuopt_int_t num_constraints,
141119
cuOptOptimizationProblem* problem_ptr)
142120
{
143121

144-
printTimestamp("CUOPT_CREATE_PROBLEM");
122+
cuopt::utilities::printTimestamp("CUOPT_CREATE_PROBLEM");
145123

146124
if (problem_ptr == nullptr || objective_coefficients == nullptr ||
147125
constraint_matrix_row_offsets == nullptr || constraint_matrix_column_indices == nullptr ||
@@ -199,7 +177,7 @@ cuopt_int_t cuOptCreateRangedProblem(cuopt_int_t num_constraints,
199177
cuOptOptimizationProblem* problem_ptr)
200178
{
201179

202-
printTimestamp("CUOPT_CREATE_PROBLEM");
180+
cuopt::utilities::printTimestamp("CUOPT_CREATE_PROBLEM");
203181

204182
if (problem_ptr == nullptr || objective_coefficients == nullptr ||
205183
constraint_matrix_row_offsets == nullptr || constraint_matrix_column_indices == nullptr ||
@@ -627,7 +605,7 @@ cuopt_int_t cuOptSolve(cuOptOptimizationProblem problem,
627605
cuOptSolverSettings settings,
628606
cuOptSolution* solution_ptr)
629607
{
630-
printTimestamp("CUOPT_SOLVE_START");
608+
cuopt::utilities::printTimestamp("CUOPT_SOLVE_START");
631609

632610
if (problem == nullptr) { return CUOPT_INVALID_ARGUMENT; }
633611
if (settings == nullptr) { return CUOPT_INVALID_ARGUMENT; }
@@ -648,7 +626,7 @@ cuopt_int_t cuOptSolve(cuOptOptimizationProblem problem,
648626
solve_mip<cuopt_int_t, cuopt_float_t>(*op_problem, mip_settings));
649627
*solution_ptr = static_cast<cuOptSolution>(solution_and_stream_view);
650628

651-
printTimestamp("CUOPT_SOLVE_RETURN");
629+
cuopt::utilities::printTimestamp("CUOPT_SOLVE_RETURN");
652630

653631
return static_cast<cuopt_int_t>(
654632
solution_and_stream_view->mip_solution_ptr->get_error_status().get_error_type());
@@ -666,7 +644,7 @@ cuopt_int_t cuOptSolve(cuOptOptimizationProblem problem,
666644
solve_lp<cuopt_int_t, cuopt_float_t>(*op_problem, pdlp_settings));
667645
*solution_ptr = static_cast<cuOptSolution>(solution_and_stream_view);
668646

669-
printTimestamp("CUOPT_SOLVE_RETURN");
647+
cuopt::utilities::printTimestamp("CUOPT_SOLVE_RETURN");
670648

671649
return static_cast<cuopt_int_t>(
672650
solution_and_stream_view->lp_solution_ptr->get_error_status().get_error_type());

cpp/src/utilities/timestamp_utils.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <cuopt/utilities/timestamp_utils.hpp>
19+
20+
#include <chrono>
21+
#include <cstdlib>
22+
#include <iomanip>
23+
#include <iostream>
24+
#include <string>
25+
26+
namespace cuopt {
27+
namespace utilities {
28+
29+
bool extraTimestamps() {
30+
static bool initialized = false;
31+
static bool enableTimestamps = false;
32+
33+
if (!initialized) {
34+
const char* envValue = std::getenv("CUOPT_EXTRA_TIMESTAMPS");
35+
if (envValue != nullptr) {
36+
std::string value(envValue);
37+
enableTimestamps = (value == "True" || value == "true" || value == "1");
38+
}
39+
initialized = true;
40+
}
41+
42+
return enableTimestamps;
43+
}
44+
45+
double getCurrentTimestamp() {
46+
auto now = std::chrono::system_clock::now();
47+
return std::chrono::duration<double>(now.time_since_epoch()).count();
48+
}
49+
50+
void printTimestamp(const std::string& label) {
51+
if (extraTimestamps()) {
52+
std::cout << std::fixed << std::setprecision(6);
53+
std::cout << std::endl << label << ": " << getCurrentTimestamp() << std::endl;
54+
}
55+
}
56+
57+
} // namespace utilities
58+
} // namespace cuopt

0 commit comments

Comments
 (0)