Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSVC compatibility #54

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions include/misc/optim_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@

//

#ifdef _MSC_VER
#error OptimLib: MSVC is not supported
#endif

//

Expand Down Expand Up @@ -76,15 +73,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
Expand All @@ -96,7 +98,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;

Expand Down
16 changes: 14 additions & 2 deletions src/unconstrained/de.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an ugly code change. I suggest using an alias that depends on the build system.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, this is done now (see commit (effb64) ).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an ugly code change. I suggest using an alias that depends on the build system.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, this is done now (see commit (effb64) ).

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<RowVec_t>(X_next.row(i), bounds_type, lower_bounds, upper_bounds);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/unconstrained/de_prmm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<RowVec_t>(X_next.row(i), bounds_type, lower_bounds, upper_bounds);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/unconstrained/nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/unconstrained/pso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<RowVec_t>(P.row(i), bounds_type, lower_bounds, upper_bounds);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/unconstrained/pso_dv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down