Skip to content

Commit

Permalink
Disable CacheFunction when compiled with AppleClang≤10
Browse files Browse the repository at this point in the history
  • Loading branch information
hpwxf committed Jan 12, 2023
1 parent 95e066f commit 0b56f09
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBKRIGING_CXX_FLAGS}")

#------------------------------------------------------

# Workaround for AppleClang 10 in CRAN automated build
if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11")
add_definitions(-DLIBKRIGING_DISABLE_CACHE)
endif()

#------------------------------------------------------

# search for clang-tidy (while be used while adding library or executable)
find_program(CLANG_TIDY clang-tidy)
if (CLANG_TIDY)
Expand Down
24 changes: 12 additions & 12 deletions src/lib/Kriging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ LIBKRIGING_EXPORT void Kriging::fit(const arma::colvec& y,
m_objective = objective;
if (objective.compare("LL") == 0) {
if (Optim::reparametrize) {
fit_ofn = CacheFunction{
fit_ofn = CacheFunction(
[this](const arma::vec& _gamma, arma::vec* grad_out, arma::mat* hess_out, Kriging::OKModel* okm_data) {
// Change variable for opt: . -> 1/exp(.)
// DEBUG: if (Optim::log_level>3) arma::cout << "> gamma: " << _gamma << arma::endl;
Expand All @@ -881,9 +881,9 @@ LIBKRIGING_EXPORT void Kriging::fit(const arma::colvec& y,
*hess_out = -Optim::reparam_from_deriv2(_theta, *grad_out, *hess_out);
}
return -ll;
}};
});
} else {
fit_ofn = CacheFunction{
fit_ofn = CacheFunction(
[this](const arma::vec& _gamma, arma::vec* grad_out, arma::mat* hess_out, Kriging::OKModel* okm_data) {
const arma::vec _theta = _gamma;
// DEBUG: if (Optim::log_level>3) arma::cout << "> theta: " << _theta << arma::endl;
Expand All @@ -898,11 +898,11 @@ LIBKRIGING_EXPORT void Kriging::fit(const arma::colvec& y,
*hess_out = -*hess_out;
}
return -ll;
}};
});
}
} else if (objective.compare("LOO") == 0) {
if (Optim::reparametrize) {
fit_ofn = CacheFunction{
fit_ofn = CacheFunction(
[this](const arma::vec& _gamma, arma::vec* grad_out, arma::mat* /*hess_out*/, Kriging::OKModel* okm_data) {
// Change variable for opt: . -> 1/exp(.)
// DEBUG: if (Optim::log_level>3) arma::cout << "> gamma: " << _gamma << arma::endl;
Expand All @@ -915,9 +915,9 @@ LIBKRIGING_EXPORT void Kriging::fit(const arma::colvec& y,
*grad_out = Optim::reparam_from_deriv(_theta, *grad_out);
}
return loo;
}};
});
} else {
fit_ofn = CacheFunction{
fit_ofn = CacheFunction(
[this](const arma::vec& _gamma, arma::vec* grad_out, arma::mat* /*hess_out*/, Kriging::OKModel* okm_data) {
const arma::vec _theta = _gamma;
// DEBUG: if (Optim::log_level>3) arma::cout << "> theta: " << _theta << arma::endl;
Expand All @@ -928,13 +928,13 @@ LIBKRIGING_EXPORT void Kriging::fit(const arma::colvec& y,
// *grad_out = *grad_out; // so not necessary
// }
return loo;
}};
});
}
} else if (objective.compare("LMP") == 0) {
// Our impl. of https://github.com/cran/RobustGaSP/blob/5cf21658e6a6e327be6779482b93dfee25d24592/R/rgasp.R#L303
//@see Mengyang Gu, Xiao-jing Wang and Jim Berger, 2018, Annals of Statistics.
if (Optim::reparametrize) {
fit_ofn = CacheFunction{
fit_ofn = CacheFunction(
[this](const arma::vec& _gamma, arma::vec* grad_out, arma::mat* /*hess_out*/, Kriging::OKModel* okm_data) {
// Change variable for opt: . -> 1/exp(.)
// DEBUG: if (Optim::log_level>3) arma::cout << "> gamma: " << _gamma << arma::endl;
Expand All @@ -947,9 +947,9 @@ LIBKRIGING_EXPORT void Kriging::fit(const arma::colvec& y,
*grad_out = -Optim::reparam_from_deriv(_theta, *grad_out);
}
return -lmp;
}};
});
} else {
fit_ofn = CacheFunction{
fit_ofn = CacheFunction(
[this](const arma::vec& _gamma, arma::vec* grad_out, arma::mat* /*hess_out*/, Kriging::OKModel* okm_data) {
const arma::vec _theta = _gamma;
// DEBUG: if (Optim::log_level>3) arma::cout << "> theta: " << _theta << arma::endl;
Expand All @@ -960,7 +960,7 @@ LIBKRIGING_EXPORT void Kriging::fit(const arma::colvec& y,
*grad_out = -*grad_out;
}
return -lmp;
}};
});
}
} else
throw std::invalid_argument("Unsupported fit objective: " + objective + " (supported are: LL, LOO, LMP)");
Expand Down
8 changes: 4 additions & 4 deletions src/lib/NoiseKriging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ LIBKRIGING_EXPORT void NoiseKriging::fit(const arma::colvec& y,
m_objective = objective;
if (objective.compare("LL") == 0) {
if (Optim::reparametrize) {
fit_ofn = CacheFunction{[this](const arma::vec& _gamma, arma::vec* grad_out, NoiseKriging::OKModel* okm_data) {
fit_ofn = CacheFunction([this](const arma::vec& _gamma, arma::vec* grad_out, NoiseKriging::OKModel* okm_data) {
// Change variable for opt: . -> 1/exp(.)
// DEBUG: if (Optim::log_level>3) arma::cout << "> gamma: " << _gamma << arma::endl;
const arma::vec _theta_sigma2 = Optim::reparam_from(_gamma);
Expand All @@ -281,9 +281,9 @@ LIBKRIGING_EXPORT void NoiseKriging::fit(const arma::colvec& y,
*grad_out = -Optim::reparam_from_deriv(_theta_sigma2, *grad_out);
}
return -ll;
}};
});
} else {
fit_ofn = CacheFunction{[this](const arma::vec& _gamma, arma::vec* grad_out, NoiseKriging::OKModel* okm_data) {
fit_ofn = CacheFunction([this](const arma::vec& _gamma, arma::vec* grad_out, NoiseKriging::OKModel* okm_data) {
const arma::vec _theta_sigma2 = _gamma;
// DEBUG: if (Optim::log_level>3) arma::cout << "> theta_alpha: " << _theta_sigma2 << arma::endl;
double ll = this->_logLikelihood(_theta_sigma2, grad_out, okm_data);
Expand All @@ -293,7 +293,7 @@ LIBKRIGING_EXPORT void NoiseKriging::fit(const arma::colvec& y,
*grad_out = -*grad_out;
}
return -ll;
}};
});
}
} else
throw std::invalid_argument("Unsupported fit objective: " + objective + " (supported are: LL)");
Expand Down
16 changes: 8 additions & 8 deletions src/lib/NuggetKriging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ LIBKRIGING_EXPORT void NuggetKriging::fit(const arma::colvec& y,
m_objective = objective;
if (objective.compare("LL") == 0) {
if (Optim::reparametrize) {
fit_ofn = CacheFunction{[this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
fit_ofn = CacheFunction([this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
// Change variable for opt: . -> 1/exp(.)
// DEBUG: if (Optim::log_level>3) arma::cout << "> gamma: " << _gamma << arma::endl;
const arma::vec _theta_alpha = Optim::reparam_from(_gamma);
Expand All @@ -525,9 +525,9 @@ LIBKRIGING_EXPORT void NuggetKriging::fit(const arma::colvec& y,
*grad_out = -Optim::reparam_from_deriv(_theta_alpha, *grad_out);
}
return -ll;
}};
});
} else {
fit_ofn = CacheFunction{[this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
fit_ofn = CacheFunction([this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
const arma::vec _theta_alpha = _gamma;
// DEBUG: if (Optim::log_level>3) arma::cout << "> theta_alpha: " << _theta_alpha << arma::endl;
double ll = this->_logLikelihood(_theta_alpha, grad_out, okm_data);
Expand All @@ -537,13 +537,13 @@ LIBKRIGING_EXPORT void NuggetKriging::fit(const arma::colvec& y,
*grad_out = -*grad_out;
}
return -ll;
}};
});
}
} else if (objective.compare("LMP") == 0) {
// Our impl. of https://github.com/cran/RobustGaSP/blob/5cf21658e6a6e327be6779482b93dfee25d24592/R/rgasp.R#L303
//@see Mengyang Gu, Xiao-jing Wang and Jim Berger, 2018, Annals of Statistics.
if (Optim::reparametrize) {
fit_ofn = CacheFunction{[this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
fit_ofn = CacheFunction([this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
// Change variable for opt: . -> 1/exp(.)
// DEBUG: if (Optim::log_level>3) arma::cout << "> gamma: " << _gamma << arma::endl;
const arma::vec _theta_alpha = Optim::reparam_from(_gamma);
Expand All @@ -555,9 +555,9 @@ LIBKRIGING_EXPORT void NuggetKriging::fit(const arma::colvec& y,
*grad_out = -Optim::reparam_from_deriv(_theta_alpha, *grad_out);
}
return -lmp;
}};
});
} else {
fit_ofn = CacheFunction{[this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
fit_ofn = CacheFunction([this](const arma::vec& _gamma, arma::vec* grad_out, NuggetKriging::OKModel* okm_data) {
const arma::vec _theta_alpha = _gamma;
// DEBUG: if (Optim::log_level>3) arma::cout << "> theta_alpha: " << _theta_alpha << arma::endl;
double lmp = this->_logMargPost(_theta_alpha, grad_out, okm_data);
Expand All @@ -567,7 +567,7 @@ LIBKRIGING_EXPORT void NuggetKriging::fit(const arma::colvec& y,
*grad_out = -*grad_out;
}
return -lmp;
}};
});
}
} else
throw std::invalid_argument("Unsupported fit objective: " + objective + " (supported are: LL, LMP)");
Expand Down
16 changes: 16 additions & 0 deletions src/lib/include/libKriging/CacheFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class CacheFunctionCommon {

std::ostream& operator<<(std::ostream& o, const CacheFunctionCommon::CacheStat&);

/* ----------------------------------------------------------------------------------------------------------------- */
#ifndef LIBKRIGING_DISABLE_CACHE
/* ----------------------------------------------------------------------------------------------------------------- */

template <typename Callable, typename Signature, typename... Contexts>
Expand Down Expand Up @@ -126,4 +128,18 @@ class CacheFunction<Callable, std::function<R(Args...)>, Contexts...> : public C
template <typename F, typename... Contexts>
CacheFunction(const F& f, const Contexts&...) -> CacheFunction<F, decltype(std::function{f}), Contexts...>;

/* ----------------------------------------------------------------------------------------------------------------- */

#else /* LIBKRIGING_DISABLE_CACHE */

/* ----------------------------------------------------------------------------------------------------------------- */

#define CacheFunction(x) (x)

/* ----------------------------------------------------------------------------------------------------------------- */

#endif /* LIBKRIGING_DISABLE_CACHE */

/* ----------------------------------------------------------------------------------------------------------------- */

#endif // LIBKRIGING_SRC_LIB_CACHE_HPP
6 changes: 5 additions & 1 deletion tests/CacheFunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <tuple>
#include "libKriging/CacheFunction.hpp"

#ifndef LIBKRIGING_DISABLE_CACHE

TEST_CASE("Cache of 0-arg function", "[core]") {
auto f = []() -> double { return 1; };

Expand Down Expand Up @@ -187,4 +189,6 @@ TEST_CASE("Cache function behaves like std::function", "[core]") {
REQUIRE(stat.max_hit == 1);
REQUIRE(stat.total_hit == 2);
REQUIRE(stat.cache_size == 2);
}
}

#endif /* LIBKRIGING_DISABLE_CACHE */

0 comments on commit 0b56f09

Please sign in to comment.