optim
optim copied to clipboard
Last step of GD leads to crash (eigen version)
The problem is in "error_reporting.ipp", line 80
//settings_inp->opt_fn_value = opt_objfn(x_p,nullptr,opt_data);
https://drive.google.com/file/d/1XyAOyllOcnY3q10nHETtd6gvrWqGU4ov/view?usp=sharing https://drive.google.com/file/d/12qemu3_UEhJGcZ21mwypk5RPIFuXCXgf/view?usp=sharing
Seems something was "std::moved" out of memory. Probably this is Eigen problem but may be you can work around it.
Ubuntu 21.10. PS: could provide the full source if needed Btw: many thanks for the library )
I cannot replicate this issue. The gd
tests include calls to this line of code.
What version of Eigen are you using? (If those links contain additional information, can you please paste the text here?)
Eigen 3.4 I have all my code here https://github.com/FROL256/diffrender_tutorials
Hi, I also have this problem. It seems that each time the optimization stops due to one of the parameters I set (e.g. iter_max), it gives "segmentation fault". Strangely enough, if I configure optim using the -g flag, I won't run in the same issue, and the optimization ends without segmentation fault. I am using Eigen 3.4.0 under Ubuntu 20.04. Thank you!
Can you help me identify the issue by providing a minimal reproducible example?
Sure.
#define OPTIM_ENABLE_EIGEN_WRAPPERS
#include <optim.hpp>
#include <Eigen/Dense>
using namespace Eigen;
inline double fun_optim(const Eigen::VectorXd& theta, Eigen::VectorXd* grad_out, void* opt_data)
{
MatrixXd K = MatrixXd::Random(4,4);
double out = theta.transpose() * K * theta; // Classic paraboloid
if (grad_out)
{
VectorXd grad = (K + K.transpose()) * theta;
*grad_out = grad;
}
return out;
}
int main(int argc, char** argv)
{
Eigen::VectorXd theta0(4);
theta0 << 10, 2, 0.1, 5;
optim::gd_settings_t gd_setting;
gd_setting.method = 6; // ADAM optimizer
optim::algo_settings_t algo_settings;
algo_settings.print_level = 2;
algo_settings.iter_max = 100;
algo_settings.gd_settings = gd_setting;
// Running solver
bool success = optim::gd(theta0, fun_optim, nullptr, algo_settings);
std::cout << "Success: " << success << std::endl;
return 0;
}
While I was writing this code, I noticed that if theta
has size greater than 3, it will give me segmentation fault.
Thank you very much for helping me!
Thanks for the example. I am unable to replicate the seg fault on my system--your example compiles and runs without issue. Can you provide the compiler flags you're using?
Thank you for trying my code. I am using CMake to build the code, I paste here the CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(NLLoptim VERSION 0.1.0)
add_compile_options(-std=c++17)
add_compile_options( -DEIGEN_NO_DEBUG -msse -msse2 -O3 -fopenmp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
set(CMAKE_BUILD_TYPE Release)
include(CTest)
enable_testing()
find_package(Eigen3)
find_package(OpenMP)
find_path(OPTIMLIB_INCLUDES
NAMES optim.hpp
HINTS ~/.local/include/optim
)
find_library(OPTIMLIB_LIB
NAMES liboptim.so
HINTS ~/.local/lib
)
include_directories(${EIGEN_INCLUDE_DIRS}
${OPTIMLIB_INCLUDES}
include
)
add_executable(mwe mwe.cpp)
target_link_libraries( mwe
Eigen3::Eigen
OpenMP::OpenMP_CXX
${OPTIMLIB_LIB}
)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
The compiler I use is g++ 9.4.0 and I have OpenMP 4.5.
I wonder if a mismatch between SSE/AVX flags might be causing issues. By default, on x86 systems, OptimLib uses -march=native
. Unless there's a particular reason why you need to limit to SSE2, can you change this line:
add_compile_options( -DEIGEN_NO_DEBUG -msse -msse2 -O3 -fopenmp)
to
add_compile_options(-march=native -O3 -ffp-contract=fast -fopenmp)
and try again?
I am sorry for replying this late. Now it works! I changed those compile options as you suggested and the segmentation fault is gone. Thank you very much.