I have the following simple Cython function for a parallel reduction:
# cython: boundscheck = False
# cython: initializedcheck = False
# cython: wraparound = False
# cython: cdivision = True
# cython: language_level = 3
from cython.parallel import parallel, prange
cpdef double simple_reduction(int n, int num_threads):
cdef int i
cdef int sum = 0
for i in prange(n, nogil=True, num_threads=num_threads):
sum += 1
return sum
Which horrifyingly returns the following:
In [3]: simple_reduction(n=10, num_threads=1)
Out[3]: 10.0
In [4]: simple_reduction(n=10, num_threads=2)
Out[4]: 20.0
In [5]: simple_reduction(n=10, num_threads=3)
Out[5]: 30.0
In other words, it appears to be repeating all n iterates of the loop for each thread instead of parallelizing the iterates over each thread. Any idea what's going?
I am using Python 3.7.1 and Cython 0.29.2 on macOS Mojave 10.14.3.
UPDATE: Here's my setup.py file:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import os
import sys
if sys.platform == 'darwin':
os.environ['CC'] = 'gcc-8'
os.environ['CXX'] = 'g++-8'
EXT_MODULES = [Extension('foo', ['foo.pyx'],
extra_compile_args=['-fopenmp'],
extra_link_args=['-fopenmp'])]
setup(name='foo',
ext_modules=cythonize(EXT_MODULES))
I have installed GCC separately and have to set the environment variables 'CC' and 'CXX' when using OSX to avoid the problem of OSX aliasing those clang.
I fixed this bug by first installing gcc using Anaconda:
conda install gcc
Then changing the lines in setup.py to use that new compiler:
if sys.platform == 'darwin':
os.environ['CC'] = '/anaconda3/bin/gcc'
os.environ['CXX'] = '/anaconda3/bin/g++'
Using Anaconda gcc (instead of the brew-installed one I was using originally) didn't fix the problem right away. It wouldn't compile due to the following bug:
/anaconda3/envs/python36/lib/gcc/x86_64-apple-darwin11.4.2/4.8.5/include-fixed/limits.h:168:61:
fatal error: limits.h: No such file or directory #include_next
/* recurse down to the real one */
The problem here has to due with macOS 10.14 and XCode 10.0. However the solution given by #Maxxx in this related question worked for me. After installing the .pkg hidden in the command line tool directory
/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
the code compiled and the parallelism worked as it was supposed to.
UPDATE: After updating to OSX Catalina, this fix no longer works because the .pkg file above no longer exists. I found a new solution from reading this related question. In my case, exporting the following path to CPATH fixed the problem.
export CPATH=~/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
Related
I have been trying to make ipopt work on pyomo. I've downloaded pyomo (version 5.7.3) and ipopt using Anaconda Navigator and I'm using Spyder to edit and run my .py codes. I've been using the following code from the pyomo tutorial in order to understand whether ipopt is successfully installed and works.
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()
When I set opt = SolverFactory('ipopt') and run the code, it gives me the following warning:
WARNING: Could not locate the 'ipopt' executable, which is required for solver ipopt
My first try to solve this problem by myself is to manually download the ipopt executable (ipopt 3.11.1-win64) from the link below:
https://www.coin-or.org/download/binary/Ipopt/
Afterwards, I extracted the files and placed them into the pyomo solver location:
C:\Anaconda\envs\myenv\Lib\site-packages\pyomo\solvers\plugins\solvers
This didn't work, so I also tried specifying the path to the ipopt executable using the code:
opt = pyo.SolverFactory("ipopt", executable="C:\Anaconda\envs\myenv\Lib\site-packages\pyomo\solvers\plugins\solvers\ipopt\bin\ipopt.exe")
However, doing this gives me the following warning:
WARNING: Failed to create solver with name 'ipopt': Failed to set executable
for solver ipopt. File with name=C:\Anaconda\envs\myenv\Lib\site-packages\
pyomo\solvers\plugins\solvers\ipopin\ipopt.exe
either does not exist or it is not executable. To skip this validation,
call set_executable with validate=False.
Reminder that the part ipopin\ipopt.exe was not a typo. I don't know why it happened. I also tried copy-pasting the executable outside the bin folder and placing it in the ipopt folder instead:
opt = pyo.SolverFactory("ipopt", executable="C:\Anaconda\envs\myenv\Lib\site-packages\pyomo\solvers\plugins\solvers\ipopt\ipopt.exe")
Unfortunately I still get the WARNING. Could not locate the 'ipopt' executable, which is required for solver ipopt in the end.
I also tried downloading the ipopt executable from the link below but ipopt.exe is eliminated by my virus scanner as it considers the downloaded file as a threat:
https://projects.coin-or.org/CoinBinary/browser/binary/Ipopt/Ipopt-3.13.2-win64-msvs2019-md.zip?rev=1072
Is there another way I can try to make IPOPT work in this case?
if you run your jupyter notebook from the anaconda then you should be able to see your solver in the installed list of anaconda
cx_Freeze successfully converts a .py file to an .exe if no other packages are involved.
But when pandas is included, the setup fails with this message:
File "C:\Anaconda\lib\site-packages\cx_Freeze\hooks.py", line 1324, in load_zmq
libzmq = __import__("zmq", fromlist=["libzmq"]).libzmq
AttributeError: module 'zmq' has no attribute 'libzmq'
This is the contents of the setup.py:
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["pandas"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "test",
version = "0.1",
options = {"build_exe": build_exe_options},
executables = [Executable("test.py", base=base)])
So far I've tried creating a different environment in anaconda to run cx_freeze to see if the problem resolves itself that way.
Appreciate any help.
I've spent a long time trying to find an answer to this question, but luckily it can be found here. It seems it was a bug inherent to cx_freeze.
Although the link provided to download the wheels no longer works, the patch has been added to the latest development version (6.6.0 if I recall correctly). You can install it directly from github, as such:
python -m pip install git+https://github.com/marcelotduarte/cx_Freeze.git
Doing this fixed the error for me (and enabled me to discover other errors that I had to deal with!).
I have a Python/OpenCV project and I am trying to use the xfeatures2d module from opencv-contrib. I am using a Mac and my IDE is PyCharm. I have installed the packages opencv-contrib-python and opencv-python through Preferences > Project Intepreter.
However, when I try to run the code below, I get a the following error:
import cv2
import numpy as np
img = cv2.imread("NotreDame.jpg", 0)
sift = cv2.xfeatures2d.SIFT_create()
line 6, in <module>
sift = cv2.xfeatures2d.SIFT_create()
cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv_contrib/modules/xfeatures2d/src/sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'
I have installed opencv and opencv-contrib on my computer using:
$ pip install opencv-python==3.4.2.17
$ pip install opencv-contrib-python==3.4.2.17
I am not sure how I can resolve this error. Any insights are appreciated.
I'm using python 2.7 installed via macports, pyobjc, cocoa and notably scipy (via FlowCytometryTools) with py2app to create a small mac application. Py2app's setup.py run with sudo python setup.py py2app completes nicely with -A for testing, but is unable to complete when running without that parameter to create a full .app build.
The build process will get a variable distance through the unconditional imports and then give error: [Errno 35] Resource temporarily unavailable and immediately exit. The number of unconditional import lines that complete before the error occurs changes. The longest run so far was seen when running immediately after a reboot. I've tried running with and without removing the previous build/dist folders to no effect.
Modules not found (unconditional imports):
...
* builtins.int (Cython.Build.Inline, Cython.Compiler.ExprNodes, IPython.utils._tokenizeerror: [Errno 35] Resource temporarily unavailable
My setup.py looks like this:
import sys
sys.setrecursionlimit(1500) #required to avoid recursion triggered early exit in py2app while compiling scipy. (default is 1000)
from setuptools import setup
APP = ['FlowMac.py']
DATA_FILES = ['FlowMac_Main.xib']
OPTIONS = {'argv_emulation': True}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
and the imports section of my python file FlowMac.py looks like this:
from Cocoa import *
from Foundation import *
from AppKit import *
from FlowCytometryTools import * #file/data handling via scipy and pandas
from pylab import * #graphing histograms
Commenting out both the FlowCytometryTools and the pylab imports allows the py2app build to complete but of course renders the program nonfunctional.
What is happening?
How can I get more information on which resource is unavailable?
was hitting a recursion limit a clue to my problem?
I'm running Yosemite on a MacBook Pro with 8GB RAM, so I'm very surprised to be hitting a wall here.
Thanks for your time.
UPDATE 4/29/2015:
Importing everything works fine if I remove my .xib from the datafiles array of the py2app launcher setup.py. A blank file with just import Cocoa, Foundation and Appkit works fine. Importing the xib with any one of FlowCytometryTools, pylab, scipy, matplotlib, numpy does not work. pylab and FlowCytometryTools rely on the other three, and any one of scipy, matplotlib or numpy brings in py2app recipies for the other two. One of these recipies isn't working with the xib, but I don't know why...
No experience with py2app, but I am wondering if you tried generating a more minimal version of the code that would fail to aid troubleshooting?
Maybe having FlowMac.py include nothing but the import statements:
import Cocoa
import Foundation
import AppKit
import FlowCytometryTools
import pylab
Also could you narrow it down to whether the problem appears due to pylab or due to FlowCytometryTools? (By commenting them out individually?)
From my testing, this error seems to be caused by the log output itself from py2app. Try redirecting standard error to a file:
python setup.py py2app 2>error_log
This should work around the error.
I have this problem when I try to generate an app executable. I follow this example so the code for the setup would be
application_title = "app_name" #what you want to application to be called
main_python_file = "main.py" #the name of the python file you use to run the program
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
includes = ["atexit","re"]
setup(
name = application_title,
version = "0.1",
description = "Sample cx_Freeze PyQt4 script",
options = {"build_exe" : {"includes" : includes }},
executables = [Executable(main_python_file, base = base)])
then I go to my terminal and press
python setup.py bdist_mac
for some reason that I do not know how to solve I get this output, and the file it is not generated
running bdist_mac
running build
running build_exe
copying /Applications/anaconda/lib/python3.4/site-packages/cx_Freeze/bases/Console -> build/exe.macosx-10.5-x86_64-3.4/main
copying /opt/local/Library/Frameworks/Python.framework/Versions/3.4/Python -> build/exe.macosx-10.5-x86_64-3.4/Python
error: [Errno 2] No such file or directory: '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/Python'
I have installed anaconda with Python 3.4 and cx_freeze. Any suggestions?
I met the same problem and it is fixed by installing a separated python besides anaconda. It seems to be similar with the problem here
py2app is not copying the Python.framework to the new app while using virutalenv