"ApplicationError: Solver (ipopt) did not exit normally" after compiling and installing ipopt by myself - anaconda

I've been trying to install/compile ipopt in my Windows computer and so far I am not having any progress. I am mostly following the instructions listed here: https://coin-or.github.io/Ipopt/INSTALL. I downloaded MSYS2 MinGW specifically for this purpose.
As dependencies I have downloaded the following
OpenBLAS;
blis;
ThirdParty-ASL;
ThirdParty-HSL (with the HSL library solvers which I need);
ThirdParty-Mumps;
In the end, I get ipopt.exe in the C:\msys64\home\me\Ipopt\src\Apps\AmplSolver folder. But there is also another ipopt.exe in the C:\msys64\home\me\Ipopt\src\Apps\AmplSolver\.libs folder, so I tried to make use of both.
I am using Anaconda Navigator and Pyomo to see whether my ipopt.exe works, so I put the resulting ipopt.exe in the Anaconda\envs\myenv\Library\bin folder. However, the ipopt executable that I got gives me errors. One of them gives me an error related to libipoptamplinterface-3.dll. When I run the below Python script using Spyder:
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
model = pyo.ConcreteModel()
model.nVars = pyo.Param(initialize=4)
model.N = pyo.RangeSet(model.nVars)
model.x = pyo.Var(model.N, within=pyo.Binary)
model.obj = pyo.Objective(expr=pyo.summation(model.x))
model.cuts = pyo.ConstraintList()
opt = SolverFactory('ipopt')
opt.solve(model)
# Iterate, adding a cut to exclude the previously found solution
for i in range(5):
expr = 0
for j in model.x:
if pyo.value(model.x[j]) < 0.5:
expr += model.x[j]
else:
expr += (1 - model.x[j])
model.cuts.add( expr >= 1 )
results = opt.solve(model)
print ("\n===== iteration",i)
model.display()
I get errors such as this:
ERROR: Solver (ipopt) returned non-zero return code (127)
ApplicationError: Solver (ipopt) did not exit normally
I don't know how to solve this.

Your model is a MILP type of optimization however the ipopt solver is not for this purpose use glpk solver instead of that

Related

The optimal way to set a breakpoint in the Python source code while debugging CPython by GDB

I use GDB to understanding how CPython executes the test.py source file and I want to stop the CPython when it starts the execution of opcode I am interested.
OS: Ubuntu 18.04.2 LTS
Debugger: GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
The first problem - many CPython's .py own files are executed before my test.py gets its turn, so I can't just break at the _PyEval_EvalFrameDefault - there are many of them, so I should distinguish my file from others.
The second problem - I can't set the condition like "when the filename is equal to the test.py", because the filename is not a simple C string, it is the CPython's Unicode object, so the standard GDB string functions can't be used for comparing.
At this moment I do the next trick for breaking the execution at the needed line of test.py source:
For example, I have the source file:
x = ['a', 'b', 'c']
# I want to set the breakpoint at this line.
for e in x:
print(e)
I add the binary left shift operator to the code:
x = ['a', 'b', 'c']
# Added for breakpoint
a = 12
b = 2 << a
for e in x:
print(e)
And then, track the BINARY_LSHIFT opcode execution in the Python/ceval.c file by this GDB command:
break ceval.c:1327
I have chosen the BINARY_LSHIFT opcode, because of its seldom usage in the code. Thus, I can reach the needed part of .py file quickly - it happens once in the all other .py modules executed before my test.py.
I look the more straightforward way of doing the same, so
the questions:
Can I catch the moment the test.py starts executing? I should mention, what the test.py filename is appearing on different stages: parsing, compilation, execution. So, it also will be good to can break the CPython execution at the any stage.
Can I specify the line of the test.py, where I want to break? It is easy for .c files, but is not for .py files.
My idea would be to use a C-extension, to make setting C-breakpoints possible in a python-script (similar to pdb.set_trace() or breakpoint() since Python3.7), which I will call cbreakpoint.
Consider the following python-script:
#example.py
from cbreakpoint import cbreakpoint
cbreakpoint(breakpoint_id=1)
print("hello")
cbreakpoint(breakpoint_id=2)
It could be used as follows in gdb:
>>> gdb --args python example.py
[gdb] b cbreakpoint
[gdb] run
Now, the debuger would stops at cbreakpoint(breakpoint_id=1) and cbreakpoint(breakpoint_id=2).
Here is proof of concept, written in Cython to avoid the otherwise needed boilerplate-code:
#cbreakpoint.pyx
cdef extern from *:
"""
long long last_breakpoint_id = -1;
void cbreakpoint(long long breakpoint_id){
last_breakpoint_id = breakpoint_id;
}
"""
void c_cbreakpoint "cbreakpoint"(long long breakpoint_id)
def cbreakpoint(breakpoint_id = 0):
c_cbreakpoint(breakpoint_id)
which can be build inplace via:
cythonize -i cbreakpoint.pyx
If Cython isn't installed, I have uploaded a version which doesn't depend on Cython (too much code for this post) on github.
It is also possible to break conditionally, given the breakpoint_id, i.e.:
>>> gdb --args python example.py
[gdb] break src/cbreakpoint.c:595 if breakpoint_id == 2
[gdb] run
will break only after hello was printed - at cbreakpoint with id=2 (while cbreakpoint with id=1 will be skipped). Depending on Cython version the line can vary, but can be found out once gdb stops at cbreakpoint.
It would also do something similar without any additional modules:
add breakpoint or import pdb; pdb.set_trace() instead of cbreakpoint
gdb --args python example.py + run
When pdb interrupts the program, hit Ctrl+C in order to interrupt in gdb.
Activate breakpoints in gdb.
continue in gdb and then in pdb (i.e. c+enter twice).
A small problem is, that after that the breakpoints might be hit while in pdb, so the first method is a little bit more robust.

How do I set up Code::Blocks build options to run plplot?

I cannot find any documentation on how to setup Code::Blocks to run plplot. I have installed plplot version 5.14.0 in /home/myname/plplot. I followed instructions on to compile at https://sourceforge.net/p/plplot/wiki/Linux/. That went smoothly, and I have a folder for build_directory and install_directory in plplot folder. I am running Ubuntu 18.04.
When I go to compile an example program in Code::Blocks, I do not know which files or folders need to be added into Project->Build Options. The error message reads: Fatal Error: Can't open module file ‘plplot.mod’ for reading at (1): No such file or directory
Here is the example I am trying to compile:
program ft_x00f
! This is a modified version of x00f.f90
! which was written by Alan Irwin
use plplot
implicit none
integer, parameter :: NSIZE = 100
real(kind=plflt), dimension(0:NSIZE) :: x, y
real(kind=plflt) :: xmin = 0.0_plflt, &
xmax = 1.0_plflt, &
ymin = 0.0_plflt, &
ymax = 100.0_plflt
integer :: i
! Prepare data to be plotted.
! x = .00, .01, .02, ..., .99, 1.00
x = [(i, i=0,NSIZE)] / real(NSIZE)
y = ymax * x**2
! Parse and process command line arguments
call plparseopts( PL_PARSE_FULL )
! Initialize plplot
call plinit( )
! Create a labelled box to hold the plot.
call plenv( xmin, xmax, ymin, ymax, just=0, axis=0 )
call pllab( "x", "y=100 x#u2#d", &
"Simple PLplot demo of a 2D line plot" )
! Plot the data that was prepared above.
call plline( x, y )
! Close PLplot library
call plend( )
end program ft_x00f
This one was painful, but I have found out the answer. First, I could not compile the above code even without Code::Blocks. I suggest copying a file directly from the plplot /examples directory and opening that in Code::Blocks. The best way to find out what commands are needed is to look at what plplot does when you issue the make command to compile the examples when installing plplot.
I then needed to add following to Code::Blocks -
1) Project -> Build Options -> Linker Settings -> Other Linker Options -lplplotfortran -lplfortrandemolib (Note, I knew these from the make command discussed above)
2) Project -> Build Options -> Search Directories -> Compiler /install_directory/lib/fortran/modules/plplot
3) Project -> Build Options -> Search Directories -> Linker /install_directory/lib
This will at least compile and run an example. I am sure more fun awaits for creating my own programs. Good luck to all.

How to clear cache (or force recompilation) in numba

I have a fairly large codebase written in numba, and I have noticed that when the cache is enabled for a function calling another numba compiled function in another file, changes in the called function are not picked up when the called function is changed. The situation occurs when I have two files:
testfile2:
import numba
#numba.njit(cache=True)
def function1(x):
return x * 10
testfile:
import numba
from tests import file1
#numba.njit(cache=True)
def function2(x, y):
return y + file1.function1(x)
If in a jupyter notebook, I run the following:
# INSIDE JUPYTER NOTEBOOK
import sys
sys.path.insert(1, "path/to/files/")
from tests import testfile
testfile.function2(3, 4)
>>> 34 # good value
However, if I change then change testfile2 to the following:
import numba
#numba.njit(cache=True)
def function1(x):
return x * 1
Then I restart the jupyter notebook kernel and rerun the notebook, I get the following
import sys
sys.path.insert(1, "path/to/files/")
from tests import testfile
testfile.function2(3, 4)
>>> 34 # bad value, should be 7
Importing both files into the notebook has no effect on the bad result. Also, setting cache=False only on function1 also has no effect. What does work is setting cache=False on all njit'ted functions, then restarting the kernel, then rerunning.
I believe that LLVM is probably inlining some of the called functions and then never checking them again.
I looked in the source and discovered there is a method that returns the cache object numba.caching.NullCache(), instantiated a cache object and ran the following:
cache = numba.caching.NullCache()
cache.flush()
Unfortunately that appears to have no effect.
Is there a numba environment setting, or another way I can manually clear all cached functions within a conda env? Or am I simply doing something wrong?
I am running numba 0.33 with Anaconda Python 3.6 on Mac OS X 10.12.3.
I "solved" this with a hack solution after seeing Josh's answer, by creating a utility in the project method to kill off the cache.
There is probably a better way, but this works. I'm leaving the question open in case someone has a less hacky way of doing this.
import os
def kill_files(folder):
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print("failed on filepath: %s" % file_path)
def kill_numba_cache():
root_folder = os.path.realpath(__file__ + "/../../")
for root, dirnames, filenames in os.walk(root_folder):
for dirname in dirnames:
if dirname == "__pycache__":
try:
kill_files(root + "/" + dirname)
except Exception as e:
print("failed on %s", root)
This is a bit of a hack, but it's something I've used before. If you put this function in the top-level of where your numba functions are (for this example, in testfile), it should recompile everything:
import inspect
import sys
def recompile_nb_code():
this_module = sys.modules[__name__]
module_members = inspect.getmembers(this_module)
for member_name, member in module_members:
if hasattr(member, 'recompile') and hasattr(member, 'inspect_llvm'):
member.recompile()
and then call it from your jupyter notebook when you want to force a recompile. The caveat is that it only works on files in the module where this function is located and their dependencies. There might be another way to generalize it.

Code works on one computer and compiles (without warning and compiler errors) on another machine but has runtime error

I've written a program in FORTRAN, it's a simple multiplication program. It seems to work fine on one computer (compiles without warnings and output is correct), but when I scp the folder to another computer, it compiles fine without warnings however, the output changes to a random number, not sure why this is? There's two outputs, one is the algorithm in recursion and the other is done iteratively. Recursion works fine but iterative gives the error. I've run make clean, and I've also tried to change the folder into a new clean folder, still same issue.
Version of the one it works fine on: GNU Fortran (Debian 4.9.2-10) 4.9.2
Version of the one with wrong output: GNU Fortran (Debian 4.4.5-8) 4.4.5
program multiplication
integer(kind = 8) :: multiplier, multiplicand, rcursvmultply, itratvmultply
write(*,*) "multiplier?"
read(*,*) multiplier
write(*,*) "multiplicand?"
read(*,*) multiplicand
write(*,*) rcursvmultply(multiplier, multiplicand)
write(*,*) itratvmultply(multiplier, multiplicand)
end program
recursive function rcursvmultply(multiplier, multiplicand) result(answer)
integer(kind = 8), intent(in) :: multiplier, multiplicand
integer(kind = 8) :: answer
if (multiplier == 0) then
answer = 0
else if (multiplier == 1) then
answer = multiplicand
else if ((multiplier > 1) .and. (mod(multiplier, 2) == 0)) then
answer = rcursvmultply((multiplier/2), (multiplicand*2))
else if ((multiplier > 1) .and. (mod(multiplier, 2) == 1)) then
answer = (multiplicand + (rcursvmultply((multiplier/2), (multiplicand*2))))
end if
end function rcursvmultply
integer(kind = 8) function itratvmultply(multiplier, multiplicand)
integer(kind = 8) :: multiplier, multiplicand
do while (multiplier > 0)
if ((mod(multiplier, 2)) == 1) then
itratvmultply = (multiplicand + itratvmultply)
end if
multiplier = multiplier/2
multiplicand = multiplicand*2
end do
end function itratvmultply
I also get the following warning on the computer that it's not working on:
make: Warning: File `Makefile' has modification time 4.6 s in the future
make: warning: Clock skew detected. Your build may be incomplete.
For the above warning I've make clean on the original computer and scp after that, it doesn't seem to get rid of it. However, when I make clean it doesn't produce the warning again on that computer. I'm not sure why this is.
When this line in itratvmultply
itratvmultply = (multiplicand + itratvmultply)
is first executed the result variable (ie itratvmultply) is not guaranteed to have any particular value; it has not been explicitly assigned to. This means that the rhs of the assignment is, effectively, junk.
The symptoms you report are entirely explicable if on one computer (using one version of a compiler and an unknown (to us) set of compilation options) the compiler sets the value to 0 whereas on another computer (different compiler version, possibly different options) no such value is provided and the variable gets whatever the bits and bytes just happen to be.
You perhaps think that the compiler sets variables to 0 when the program starts. That is not guaranteed by the Fortran language standard. And it's common that compilers at low levels of optimisation set variables to 0 on program start up, but at higher levels do not.
To fix this include the line
itratvmultply = 0
in your code prior to the first time the variable appears on the rhs of an assignment. And check your compiler's documentation to find out how to get it to warn you of the use of uninitialised variables.
And #TriskalJM's comment answers your question about the funny futurism in the make process.

IS_IOSTAT_END error in Mac OSX (Fortran)

I am writing a code in Fortran to read a list of properties from a file, and am using the IOSTAT function to skip invalid data. The relevant section of code is as follows:
do j=1,1000
read(22,*,IOSTAT=ios) step,T,K,U,Tot,P
If(IS_IOSTAT_END(ios)) Exit !exits loop if value is not number or end of file
IF(ios.ne.0) cycle
sumT=sumT+T
sumU=sumU+U
sumK = sumK + K
sumKsq = sumKsq + (K**2.d0)
end if
end do
This code has previously worked fine when running on Linux, but when running on Mac OSX I get the error message 'IS_IOSTAT_END has no implicit type'. Could somebody please explain how to correct this?
Thanks
The intrinsic procedure IS_IOSTAT_END(i) is defined since Fortran 2003. Sufficiently recent compiler version must be used.

Resources