Skip to content

Commit

Permalink
fix macos/ARM/clang compile errors (GeomScale#297)
Browse files Browse the repository at this point in the history
* fix cmake bug

* homebrew's lp_solve is not complete. fix LPSolve.cmake

* replace system_clock with high_resolution_clock

* remove header immintrin.h in FloatArray.h

* include <immintrin.h> if the architecture is x64 or x86
  • Loading branch information
congyu711 authored Feb 10, 2025
1 parent 43a5bec commit 3713bfc
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
4 changes: 3 additions & 1 deletion external/PackedCSparse/FloatArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//(https://github.com/ConstrainedSampler/PolytopeSamplerMatlab/blob/master/code/solver/PackedCSparse/PackedChol.h) by Ioannis Iakovidis

#pragma once
#include <immintrin.h>
#if defined(__x86_64__) || defined(__i386__)
#include <immintrin.h>
#endif
#include <random>
#include <type_traits>
namespace PackedCSparse {
Expand Down
2 changes: 1 addition & 1 deletion external/cmake-files/Eigen.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(EIGEN_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR})
function(GetEigen)
find_path(EIGEN_DIR NAMES Eigen PATHS ${EIGEN_CMAKE_DIR}/../_deps/eigen-src)
find_path(EIGEN_DIR NAMES signature_of_eigen3_matrix_library PATHS ${EIGEN_CMAKE_DIR}/../_deps/eigen-src PATH_SUFFIXES eigen3 eigen)

if (NOT EIGEN_DIR)
include(FetchContent)
Expand Down
2 changes: 2 additions & 0 deletions external/cmake-files/LPSolve.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(LP_SOLVE_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR})
function(GetLPSolve)
if(NOT APPLE)
find_path(LP_SOLVE_DIR NAMES lpsolve.h PATHS ${LP_SOLVE_CMAKE_DIR}/../_deps/lpsolve-src)
endif(NOT APPLE)

if (NOT LP_SOLVE_DIR)
include(FetchContent)
Expand Down
12 changes: 6 additions & 6 deletions include/ode_solvers/implicit_midpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ struct ImplicitMidpointODESolver {
num_runs++;
pts partialDerivatives;
#ifdef TIME_KEEPING
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
partialDerivatives = ham.DU(xs);
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
DU_duration += end - start;
#endif
xs = xs + partialDerivatives * (eta / 2);
Expand All @@ -119,12 +119,12 @@ struct ImplicitMidpointODESolver {
pts xs_old = xs;
pts xmid = (xs_prev + xs) / 2.0;
#ifdef TIME_KEEPING
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
partialDerivatives = ham.approxDK(xmid, nu);

#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
approxDK_duration += end - start;
#endif
xs = xs_prev + partialDerivatives * (eta);
Expand All @@ -144,11 +144,11 @@ struct ImplicitMidpointODESolver {
}
}
#ifdef TIME_KEEPING
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
partialDerivatives = ham.DU(xs);
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
DU_duration += end - start;
#endif
xs = xs + partialDerivatives * (eta / 2);
Expand Down
36 changes: 18 additions & 18 deletions include/preprocess/crhmc/crhmc_problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,56 +330,56 @@ class crhmc_problem {
void simplify() {
#ifdef TIME_KEEPING
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
rescale();
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
rescale_duration += end - start;
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
splitDenseCols(options.maxNZ);
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
sparsify_duration += end - start;
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
reorder();
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
reordering_duration += end - start;
#endif
int changed = 1;
while (changed) {
while (changed) {
changed = 0;
#ifdef TIME_KEEPING
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
changed += remove_dependent_rows();
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
rm_rows_duration += end - start;
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
changed += remove_fixed_variables();
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
rm_fixed_vars_duration += end - start;
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
reorder();
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
reordering_duration += end - start;
#endif
}
#ifdef TIME_KEEPING
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
changed += extract_collapsed_variables();
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
ex_collapsed_vars_duration += end - start;
#endif
}
Expand Down Expand Up @@ -546,7 +546,7 @@ class crhmc_problem {
simplify();
#ifdef TIME_KEEPING
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
if (isempty_center) {
std::tie(center, std::ignore, std::ignore) =
Expand All @@ -555,7 +555,7 @@ class crhmc_problem {
}
shift_barrier(center);
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
shift_barrier_duration += end - start;
#endif
reorder();
Expand All @@ -564,7 +564,7 @@ class crhmc_problem {
// Recenter again and make sure it is feasible
VT hess;
#ifdef TIME_KEEPING
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
std::tie(center, std::ignore, std::ignore, w_center) =
lewis_center<Crhmc_problem, SpMat, Opts, MT, VT, NT>(Asp, b, *this, options, center);
Expand All @@ -578,7 +578,7 @@ class crhmc_problem {
solver.solve((Tx *)input.data(), (Tx *)out.data());
center = center + (Asp.transpose() * out).cwiseProduct(Hinv);
#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
lewis_center_duration += end - start;
#endif
if ((center.array() > barrier.ub.array()).any() ||
Expand Down
4 changes: 2 additions & 2 deletions include/random_walks/crhmc/crhmc_walk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ struct CRHMCWalk {
v_tilde = solver->get_state(1);
if (metropolis_filter) {
#ifdef TIME_KEEPING
start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
#endif
// Calculate initial Hamiltonian
H = solver->ham.hamiltonian(x, v);
Expand All @@ -191,7 +191,7 @@ struct CRHMCWalk {
H_tilde = solver->ham.hamiltonian(x_tilde, -v_tilde);

#ifdef TIME_KEEPING
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();
H_duration += end - start;
#endif
VT feasible = solver->ham.feasible(x_tilde,
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ add_definitions(${CMAKE_CXX_FLAGS} "-g") # enable debuger
#add_definitions(${CMAKE_CXX_FLAGS} "-Wall")

add_definitions(${CMAKE_CXX_FLAGS} "-O3") # optimization of the compiler
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++17") #enable the c++17 support needed by autodiff
set(CMAKE_CXX_STANDARD 17) #enable the c++17 support needed by autodiff
#add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lgsl")
add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-lm")
add_definitions(${CXX_COVERAGE_COMPILE_FLAGS} "-ldl")
Expand Down
8 changes: 4 additions & 4 deletions test/benchmarks_crhmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ double benchmark(std::string fileName) {
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
std::cout << "CRHMC polytope preparation for " << fileName << std::endl;

start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
CrhmcProblem P = CrhmcProblem(input);
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();

std::cout << "Preparation completed in time, ";
std::chrono::duration<double> elapsed_seconds = end - start;
Expand All @@ -80,9 +80,9 @@ int main() {
std::cout << "CRHMC polytope preparation 100000 dimensional Cube "
<< std::endl;

start = std::chrono::system_clock::now();
start = std::chrono::high_resolution_clock::now();
CrhmcProblem P = CrhmcProblem(input);
end = std::chrono::system_clock::now();
end = std::chrono::high_resolution_clock::now();

std::cout << "Preparation completed in time, ";
std::chrono::duration<double> elapsed_seconds = end - start;
Expand Down

0 comments on commit 3713bfc

Please sign in to comment.