From f65099ec210ce5d8ea9b82045db6fc24130c48c0 Mon Sep 17 00:00:00 2001 From: Konrad Schatz <> Date: Wed, 16 Mar 2022 19:54:41 +0100 Subject: [PATCH 1/4] fixed macro definitions (MSVC) --- include/misc/optim_options.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/include/misc/optim_options.hpp b/include/misc/optim_options.hpp index bae1fbf..ce83a3b 100644 --- a/include/misc/optim_options.hpp +++ b/include/misc/optim_options.hpp @@ -42,7 +42,7 @@ // #ifdef _MSC_VER - #error OptimLib: MSVC is not supported +// #error OptimLib: MSVC is not supported #endif // @@ -76,15 +76,20 @@ #endif // floating point number type - +#ifndef FLOAT_TYPE_KEY + #define FLOAT_TYPE_KEY 0 +#endif +#ifndef DOUBLE_TYPE_KEY + #define DOUBLE_TYPE_KEY 1 +#endif #ifndef OPTIM_FPN_TYPE - #define OPTIM_FPN_TYPE double + #define OPTIM_FPN_TYPE DOUBLE_TYPE_KEY #endif -#if OPTIM_FPN_TYPE == float +#if OPTIM_FPN_TYPE == FLOAT_TYPE_KEY #undef OPTIM_FPN_SMALL_NUMBER #define OPTIM_FPN_SMALL_NUMBER fp_t(1e-05) -#elif OPTIM_FPN_TYPE == double +#elif OPTIM_FPN_TYPE == DOUBLE_TYPE_KEY #undef OPTIM_FPN_SMALL_NUMBER #define OPTIM_FPN_SMALL_NUMBER fp_t(1e-08) #else @@ -96,7 +101,11 @@ namespace optim { using uint_t = unsigned int; - using fp_t = OPTIM_FPN_TYPE; +#if OPTIM_FPN_TYPE == FLOAT_TYPE_KEY + using fp_t = float; +#else + using fp_t = double; +#endif using rand_engine_t = std::mt19937_64; From 242c76456d0697a3de7122d328f15d59cd65db56 Mon Sep 17 00:00:00 2001 From: Konrad Schatz <> Date: Mon, 13 Jun 2022 19:58:03 +0200 Subject: [PATCH 2/4] cleanup --- include/misc/optim_options.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/misc/optim_options.hpp b/include/misc/optim_options.hpp index ce83a3b..56ac35d 100644 --- a/include/misc/optim_options.hpp +++ b/include/misc/optim_options.hpp @@ -41,9 +41,6 @@ // -#ifdef _MSC_VER -// #error OptimLib: MSVC is not supported -#endif // From ccad04a6ffcba60ead640e33c393d53350145c9a Mon Sep 17 00:00:00 2001 From: Konrad Schatz <> Date: Mon, 13 Jun 2022 22:19:10 +0200 Subject: [PATCH 3/4] MSVC/OpenMP compatibility --- src/unconstrained/de.cpp | 16 ++++++++++++++-- src/unconstrained/de_prmm.cpp | 20 ++++++++++++++++++++ src/unconstrained/nm.cpp | 4 ++++ src/unconstrained/pso.cpp | 12 ++++++++++++ src/unconstrained/pso_dv.cpp | 8 ++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/unconstrained/de.cpp b/src/unconstrained/de.cpp index 083e6b4..1f9cc2f 100644 --- a/src/unconstrained/de.cpp +++ b/src/unconstrained/de.cpp @@ -117,7 +117,11 @@ optim::internal::de_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else for (size_t i = 0; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -164,7 +168,11 @@ optim::internal::de_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif - for (size_t i = 0; i < n_pop; ++i) { +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else + for (size_t i = 0; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -258,7 +266,11 @@ optim::internal::de_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif - for (size_t i = 0; i < n_pop; ++i) { +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else + for (size_t i = 0; i < n_pop; ++i) { +#endif X_next.row(i) = inv_transform(X_next.row(i), bounds_type, lower_bounds, upper_bounds); } } diff --git a/src/unconstrained/de_prmm.cpp b/src/unconstrained/de_prmm.cpp index 70e3457..dda60a3 100644 --- a/src/unconstrained/de_prmm.cpp +++ b/src/unconstrained/de_prmm.cpp @@ -134,7 +134,11 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else for (size_t i = 0; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -195,7 +199,11 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif +#ifdef _MSC_VER + for (int64_t j = 0; j < n_pop_temp; ++j) { +#else for (size_t j = 0; j < n_pop_temp; ++j) { +#endif if (objfn_vals(j) < objfn_vals(j + n_pop_temp)) { X_reset.row(j) = X_next.row(j); objfn_vals_reset(j) = objfn_vals(j); @@ -223,7 +231,11 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec,rand_pars) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop - n_pop_best; ++i) { +#else for (size_t i = 0; i < n_pop - n_pop_best; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -306,7 +318,11 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec,rand_pars) #endif +#ifdef _MSC_VER + for (int64_t i = n_pop - n_pop_best; i < n_pop; ++i) { +#else for (size_t i = n_pop - n_pop_best; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -417,7 +433,11 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else for (size_t i = 0; i < n_pop; ++i) { +#endif X_next.row(i) = inv_transform(X_next.row(i), bounds_type, lower_bounds, upper_bounds); } } diff --git a/src/unconstrained/nm.cpp b/src/unconstrained/nm.cpp index 00a22c6..c2e9514 100644 --- a/src/unconstrained/nm.cpp +++ b/src/unconstrained/nm.cpp @@ -251,7 +251,11 @@ optim::internal::nm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif +#ifdef _MSC_VER + for (int64_t i = 1; i < n_vals + 1; i++) { +#else for (size_t i = 1; i < n_vals + 1; i++) { +#endif simplex_fn_vals(i) = box_objfn( BMO_MATOPS_TRANSPOSE(simplex_points.row(i)), nullptr, opt_data); } } diff --git a/src/unconstrained/pso.cpp b/src/unconstrained/pso.cpp index 5361f03..2a01a4d 100644 --- a/src/unconstrained/pso.cpp +++ b/src/unconstrained/pso.cpp @@ -133,7 +133,11 @@ optim::internal::pso_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else for (size_t i = 0; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -202,7 +206,11 @@ optim::internal::pso_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec_1,rand_vec_2) #endif +#ifdef _MSC_VER + for (int64_t i=0; i < n_pop; ++i) { +#else for (size_t i=0; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -270,7 +278,11 @@ optim::internal::pso_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else for (size_t i = 0; i < n_pop; ++i) { +#endif P.row(i) = inv_transform(P.row(i), bounds_type, lower_bounds, upper_bounds); } } diff --git a/src/unconstrained/pso_dv.cpp b/src/unconstrained/pso_dv.cpp index 9b44239..7f25de0 100644 --- a/src/unconstrained/pso_dv.cpp +++ b/src/unconstrained/pso_dv.cpp @@ -125,7 +125,11 @@ optim::internal::pso_dv_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else for (size_t i = 0; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -178,7 +182,11 @@ optim::internal::pso_dv_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec,rand_CR) #endif +#ifdef _MSC_VER + for (int64_t i = 0; i < n_pop; ++i) { +#else for (size_t i = 0; i < n_pop; ++i) { +#endif size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP From effb64f01d44930c52013c7d0ee879e950e81ea6 Mon Sep 17 00:00:00 2001 From: Konrad Schatz <> Date: Thu, 16 Jun 2022 23:18:26 +0200 Subject: [PATCH 4/4] added integer alias for OpenMP (build system dependent) --- include/misc/optim_options.hpp | 9 ++++++--- src/unconstrained/de.cpp | 18 +++--------------- src/unconstrained/de_prmm.cpp | 30 +++++------------------------- src/unconstrained/nm.cpp | 6 +----- src/unconstrained/pso.cpp | 18 +++--------------- src/unconstrained/pso_dv.cpp | 12 ++---------- 6 files changed, 20 insertions(+), 73 deletions(-) diff --git a/include/misc/optim_options.hpp b/include/misc/optim_options.hpp index cb49c19..7f1fe62 100644 --- a/include/misc/optim_options.hpp +++ b/include/misc/optim_options.hpp @@ -39,9 +39,6 @@ #define OPTIM_VERSION_PATCH 1 #endif -// - - // #if defined(_OPENMP) && !defined(OPTIM_DONT_USE_OPENMP) @@ -108,6 +105,12 @@ namespace optim static const double eps_dbl = std::numeric_limits::epsilon(); static const double inf = std::numeric_limits::infinity(); + +#ifdef _MSC_VER + using ompint_t = int64_t; +#else + using ompint_t = size_t; +#endif } // diff --git a/src/unconstrained/de.cpp b/src/unconstrained/de.cpp index 1f9cc2f..db57541 100644 --- a/src/unconstrained/de.cpp +++ b/src/unconstrained/de.cpp @@ -117,11 +117,7 @@ optim::internal::de_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -168,11 +164,7 @@ optim::internal::de_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -266,11 +258,7 @@ optim::internal::de_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { X_next.row(i) = inv_transform(X_next.row(i), bounds_type, lower_bounds, upper_bounds); } } diff --git a/src/unconstrained/de_prmm.cpp b/src/unconstrained/de_prmm.cpp index dda60a3..2c040ec 100644 --- a/src/unconstrained/de_prmm.cpp +++ b/src/unconstrained/de_prmm.cpp @@ -134,11 +134,7 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -199,11 +195,7 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif -#ifdef _MSC_VER - for (int64_t j = 0; j < n_pop_temp; ++j) { -#else - for (size_t j = 0; j < n_pop_temp; ++j) { -#endif + for (ompint_t j = 0; j < n_pop_temp; ++j) { if (objfn_vals(j) < objfn_vals(j + n_pop_temp)) { X_reset.row(j) = X_next.row(j); objfn_vals_reset(j) = objfn_vals(j); @@ -231,11 +223,7 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec,rand_pars) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop - n_pop_best; ++i) { -#else - for (size_t i = 0; i < n_pop - n_pop_best; ++i) { -#endif + for (ompint_t i = 0; i < n_pop - n_pop_best; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -318,11 +306,7 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec,rand_pars) #endif -#ifdef _MSC_VER - for (int64_t i = n_pop - n_pop_best; i < n_pop; ++i) { -#else - for (size_t i = n_pop - n_pop_best; i < n_pop; ++i) { -#endif + for (ompint_t i = n_pop - n_pop_best; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -433,11 +417,7 @@ optim::internal::de_prmm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { X_next.row(i) = inv_transform(X_next.row(i), bounds_type, lower_bounds, upper_bounds); } } diff --git a/src/unconstrained/nm.cpp b/src/unconstrained/nm.cpp index c2e9514..46fffe6 100644 --- a/src/unconstrained/nm.cpp +++ b/src/unconstrained/nm.cpp @@ -251,11 +251,7 @@ optim::internal::nm_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif -#ifdef _MSC_VER - for (int64_t i = 1; i < n_vals + 1; i++) { -#else - for (size_t i = 1; i < n_vals + 1; i++) { -#endif + for (ompint_t i = 1; i < n_vals + 1; i++) { simplex_fn_vals(i) = box_objfn( BMO_MATOPS_TRANSPOSE(simplex_points.row(i)), nullptr, opt_data); } } diff --git a/src/unconstrained/pso.cpp b/src/unconstrained/pso.cpp index 2a01a4d..57121c6 100644 --- a/src/unconstrained/pso.cpp +++ b/src/unconstrained/pso.cpp @@ -133,11 +133,7 @@ optim::internal::pso_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -206,11 +202,7 @@ optim::internal::pso_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec_1,rand_vec_2) #endif -#ifdef _MSC_VER - for (int64_t i=0; i < n_pop; ++i) { -#else - for (size_t i=0; i < n_pop; ++i) { -#endif + for (ompint_t i=0; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -278,11 +270,7 @@ optim::internal::pso_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { P.row(i) = inv_transform(P.row(i), bounds_type, lower_bounds, upper_bounds); } } diff --git a/src/unconstrained/pso_dv.cpp b/src/unconstrained/pso_dv.cpp index 7f25de0..7ef0b79 100644 --- a/src/unconstrained/pso_dv.cpp +++ b/src/unconstrained/pso_dv.cpp @@ -125,11 +125,7 @@ optim::internal::pso_dv_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP @@ -182,11 +178,7 @@ optim::internal::pso_dv_impl( #ifdef OPTIM_USE_OPENMP #pragma omp parallel for num_threads(omp_n_threads) firstprivate(rand_vec,rand_CR) #endif -#ifdef _MSC_VER - for (int64_t i = 0; i < n_pop; ++i) { -#else - for (size_t i = 0; i < n_pop; ++i) { -#endif + for (ompint_t i = 0; i < n_pop; ++i) { size_t thread_num = 0; #ifdef OPTIM_USE_OPENMP