From 5c4a0230b3c29f7ab2a01a03c7f3c9db037ede89 Mon Sep 17 00:00:00 2001 From: Alexis Montoison Date: Wed, 24 Apr 2024 08:46:43 -0400 Subject: [PATCH 1/6] Add primal_internal.h --- c/include/prima/primal_internal.h | 36 +++++++++++++++++++++++++++++++ c/prima.c | 28 +----------------------- 2 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 c/include/prima/primal_internal.h diff --git a/c/include/prima/primal_internal.h b/c/include/prima/primal_internal.h new file mode 100644 index 0000000000..f5e3772a44 --- /dev/null +++ b/c/include/prima/primal_internal.h @@ -0,0 +1,36 @@ +#ifndef PRIMA_INTERNAL_H +#define PRIMA_INTERNAL_H + +#include "prima/prima.h" + +// Function to check whether the problem matches the algorithm +prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm) + +// Function to initialize the result +prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t problem) + +// Functions implemented in Fortran (*_c.f90) +int cobyla_c(const int m_nlcon, const prima_objcon_t calcfc, const void *data, const int n, double x[], double *const f, double *const cstrv, double nlconstr[], + const int m_ineq, const double Aineq[], const double bineq[], + const int m_eq, const double Aeq[], const double beq[], + const double xl[], const double xu[], + const double f0, const double nlconstr0[], + int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const double ctol, + const prima_callback_t callback, int *const info); + +int bobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, const double xl[], const double xu[], + int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info); + +int newuoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, + int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info); + +int uobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, + int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const prima_callback_t callback, int *const info); + +int lincoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, + double *const cstrv, const int m_ineq, const double Aineq[], const double bineq[], + const int m_eq, const double Aeq[], const double beq[], const double xl[], const double xu[], + int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const double ctol, + const prima_callback_t callback, int *const info); + +#endif diff --git a/c/prima.c b/c/prima.c index bfa45e02d7..188abe91e5 100644 --- a/c/prima.c +++ b/c/prima.c @@ -1,7 +1,7 @@ // Dedicated to the late Professor M. J. D. Powell FRS (1936--2015). -#include "prima/prima.h" +#include "prima/prima_internal.h" #include #include #include @@ -203,32 +203,6 @@ const char *prima_get_rc_string(const prima_rc_t rc) } } - -// Functions implemented in Fortran (*_c.f90) -int cobyla_c(const int m_nlcon, const prima_objcon_t calcfc, const void *data, const int n, double x[], double *const f, double *const cstrv, double nlconstr[], - const int m_ineq, const double Aineq[], const double bineq[], - const int m_eq, const double Aeq[], const double beq[], - const double xl[], const double xu[], - const double f0, const double nlconstr0[], - int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const double ctol, - const prima_callback_t callback, int *const info); - -int bobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, const double xl[], const double xu[], - int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info); - -int newuoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, - int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info); - -int uobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, - int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const prima_callback_t callback, int *const info); - -int lincoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, - double *const cstrv, const int m_ineq, const double Aineq[], const double bineq[], - const int m_eq, const double Aeq[], const double beq[], const double xl[], const double xu[], - int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const double ctol, - const prima_callback_t callback, int *const info); - - // The function that does the minimization using a PRIMA solver prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t problem, const prima_options_t options, prima_result_t *const result) { From c80fd21f5a55232602892b5bf01b895cc7d5807b Mon Sep 17 00:00:00 2001 From: Alexis Montoison Date: Wed, 24 Apr 2024 08:52:54 -0400 Subject: [PATCH 2/6] Rename the header file --- c/include/prima/{primal_internal.h => prima_internal.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename c/include/prima/{primal_internal.h => prima_internal.h} (100%) diff --git a/c/include/prima/primal_internal.h b/c/include/prima/prima_internal.h similarity index 100% rename from c/include/prima/primal_internal.h rename to c/include/prima/prima_internal.h From e91f2c5c8aad6518f3dae4710b1c5ea8c0441a39 Mon Sep 17 00:00:00 2001 From: Alexis Montoison Date: Wed, 24 Apr 2024 09:00:59 -0400 Subject: [PATCH 3/6] Update prima_internal.h --- c/include/prima/prima_internal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c/include/prima/prima_internal.h b/c/include/prima/prima_internal.h index f5e3772a44..707658c105 100644 --- a/c/include/prima/prima_internal.h +++ b/c/include/prima/prima_internal.h @@ -4,10 +4,10 @@ #include "prima/prima.h" // Function to check whether the problem matches the algorithm -prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm) +prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm); // Function to initialize the result -prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t problem) +prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t problem); // Functions implemented in Fortran (*_c.f90) int cobyla_c(const int m_nlcon, const prima_objcon_t calcfc, const void *data, const int n, double x[], double *const f, double *const cstrv, double nlconstr[], From 0a9fe95d21d65bcc9caa8dc0423baf13769fa48c Mon Sep 17 00:00:00 2001 From: Alexis Montoison Date: Wed, 24 Apr 2024 09:10:20 -0400 Subject: [PATCH 4/6] Fix more errors --- c/include/prima/prima_internal.h | 2 ++ c/tests/stress.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/c/include/prima/prima_internal.h b/c/include/prima/prima_internal.h index 707658c105..c577ada223 100644 --- a/c/include/prima/prima_internal.h +++ b/c/include/prima/prima_internal.h @@ -3,6 +3,8 @@ #include "prima/prima.h" +unsigned int get_random_seed(void); + // Function to check whether the problem matches the algorithm prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm); diff --git a/c/tests/stress.c b/c/tests/stress.c index d66d685046..cb77898ecf 100644 --- a/c/tests/stress.c +++ b/c/tests/stress.c @@ -7,7 +7,7 @@ #include // Make PRIMA available -#include "prima/prima.h" +#include "prima/prima_internal.h" #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define N_MAX 2000 @@ -87,7 +87,7 @@ int main(int argc, char * argv[]) printf("Debug = %d\n", debug); unsigned int seed = get_random_seed(); - printf("Random seed = %d\n", seed); + printf("Random seed = %u\n", seed); srand(seed); // Set up the options From 8d1bf26982cf57fce0394fba643c507e2b977b0d Mon Sep 17 00:00:00 2001 From: Alexis Montoison <35051714+amontoison@users.noreply.github.com> Date: Fri, 26 Apr 2024 08:08:07 -0400 Subject: [PATCH 5/6] Update stress.c --- c/tests/stress.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/c/tests/stress.c b/c/tests/stress.c index cb77898ecf..ab4ca67026 100644 --- a/c/tests/stress.c +++ b/c/tests/stress.c @@ -9,6 +9,13 @@ // Make PRIMA available #include "prima/prima_internal.h" +// Thread-safe version of localtime +#ifdef _WIN32 +#define localtime_safe(a, b) localtime_s(a, b) +#else +#define localtime_safe(a, b) localtime_r(b, a) +#endif + #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define N_MAX 2000 #define M_INEQ_MAX 1000 @@ -65,8 +72,9 @@ unsigned int get_random_seed(void) { // Set the random seed to year/week char buf[10] = {0}; time_t t = time(NULL); - struct tm *tmp = localtime(&t); - int rc = strftime(buf, 10, "%y%W", tmp); + struct tm timeinfo; + localtime_safe(&timeinfo, &t); + int rc = strftime(buf, 10, "%y%W", timeinfo); if (!rc) return 42; else From 3505156110db32c316bfcf998f138e3609f3d23d Mon Sep 17 00:00:00 2001 From: Alexis Montoison <35051714+amontoison@users.noreply.github.com> Date: Fri, 26 Apr 2024 08:15:47 -0400 Subject: [PATCH 6/6] Update stress.c --- c/tests/stress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c/tests/stress.c b/c/tests/stress.c index ab4ca67026..ccd7f44a3a 100644 --- a/c/tests/stress.c +++ b/c/tests/stress.c @@ -74,7 +74,7 @@ unsigned int get_random_seed(void) { time_t t = time(NULL); struct tm timeinfo; localtime_safe(&timeinfo, &t); - int rc = strftime(buf, 10, "%y%W", timeinfo); + int rc = strftime(buf, 10, "%y%W", &timeinfo); if (!rc) return 42; else