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

Remove reliance on extension modules #394

Closed
wants to merge 9 commits into from
80 changes: 1 addition & 79 deletions .github/workflows/python_wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,12 @@ jobs:
--output python_installer.pkg
sudo installer -pkg python_installer.pkg -target /

curl \
https://www.python.org/ftp/python/3.7.9/python-3.7.9-macosx10.9.pkg \
--output python_installer.pkg
sudo installer -pkg python_installer.pkg -target /

curl \
https://www.python.org/ftp/python/3.8.3/python-3.8.3-macosx10.9.pkg \
--output python_installer.pkg
sudo installer -pkg python_installer.pkg -target /

curl \
https://www.python.org/ftp/python/3.9.7/python-3.9.7-macos11.pkg \
--output python_installer.pkg
sudo installer -pkg python_installer.pkg -target /

curl \
https://www.python.org/ftp/python/3.10.1/python-3.10.1-macos11.pkg \
--output python_installer.pkg
sudo installer -pkg python_installer.pkg -target /

curl \
https://www.python.org/ftp/python/3.11.0/python-3.11.0-macos11.pkg \
--output python_installer.pkg
sudo installer -pkg python_installer.pkg -target /


- name: Compile python bindings
run: |
make lib
Expand All @@ -107,18 +88,6 @@ jobs:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -m pip install --upgrade setuptools wheel numpy pip
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -m pip install -U wheel --user
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -m pip wheel . -w wheelhouse
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install --upgrade setuptools wheel numpy pip
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install -U wheel --user
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip wheel . -w wheelhouse
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 -m pip install --upgrade setuptools wheel numpy pip
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 -m pip install -U wheel --user
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 -m pip wheel . -w wheelhouse
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 -m pip install --upgrade setuptools wheel numpy pip
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 -m pip install -U wheel --user
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 -m pip wheel . -w wheelhouse
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pip install --upgrade setuptools wheel numpy pip
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pip install -U wheel --user
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pip wheel . -w wheelhouse
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 -m pip install --upgrade setuptools wheel numpy pip
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 -m pip install -U wheel --user
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 -m pip wheel . -w wheelhouse
Expand All @@ -130,26 +99,6 @@ jobs:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 examples/simple1d1.py
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -m pip install pytest
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 -m pytest test
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install --pre finufft -f fixed_wheel/
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 test/run_accuracy_tests.py
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 examples/simple1d1.py
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install pytest
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pytest test
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 -m pip install --pre finufft -f fixed_wheel/
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 test/run_accuracy_tests.py
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 examples/simple1d1.py
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 -m pip install pytest
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 -m pytest test
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 -m pip install --pre finufft -f fixed_wheel/
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 test/run_accuracy_tests.py
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 examples/simple1d1.py
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 -m pip install pytest
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 -m pytest test
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pip install --pre finufft -f fixed_wheel/
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 test/run_accuracy_tests.py
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 examples/simple1d1.py
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pip install pytest
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 -m pytest test
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 -m pip install --pre finufft -f fixed_wheel/
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 test/run_accuracy_tests.py
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 examples/simple1d1.py
Expand Down Expand Up @@ -180,33 +129,6 @@ jobs:
.\.github\workflows\python_build_win.ps1
.\.github\workflows\python_test_win.ps1

- name: Build and Test Python 3.9
uses: actions/setup-python@v4
with:
python-version: '3.9'
architecture: 'x64'
- run: |
.\.github\workflows\python_build_win.ps1
.\.github\workflows\python_test_win.ps1

- name: Build and Test Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
- run: |
.\.github\workflows\python_build_win.ps1
.\.github\workflows\python_test_win.ps1

- name: Build and Test Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'
architecture: 'x64'
- run: |
.\.github\workflows\python_build_win.ps1
.\.github\workflows\python_test_win.ps1

- name: Upload wheels
uses: actions/upload-artifact@v3
with:
Expand Down
37 changes: 4 additions & 33 deletions python/cufinufft/cufinufft/_cufinufft.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""
This file contains low level python bindings for the cufinufft CUDA libraries.
Seperate bindings are provided for single and double precision libraries,
Expand All @@ -7,15 +6,6 @@

import ctypes
import os
import warnings

# While imp is deprecated, it is currently the inspection solution
# that works for all versions of Python 2 and 3.
# One day if that changes, can be replaced
# with importlib.find_spec.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
import imp

from ctypes import c_double
from ctypes import c_int
Expand All @@ -27,30 +17,11 @@
c_float_p = ctypes.POINTER(c_float)
c_double_p = ctypes.POINTER(c_double)

# TODO: See if there is a way to improve this so it is less hacky.
lib = None
# Try to load a local library directly.
try:
lib = ctypes.cdll.LoadLibrary('libcufinufft.so')
except OSError:
pass

# Should that not work, try to find the full path of a packaged lib.
# The packaged lib should have a py/platform decorated name,
# and be rpath'ed the true CUDA C cufinufft library through the
# Extension and wheel systems.
try:
if lib is None:
# Find the library.
fh = imp.find_module('cufinufftc')[0]
# Get the full path for the ctypes loader.
full_lib_path = os.path.realpath(fh.name)
fh.close() # Be nice and close the open file handle.

# Load the library,
# which rpaths the libraries we care about.
lib = ctypes.cdll.LoadLibrary(full_lib_path)

try:
lib = ctypes.cdll.LoadLibrary(os.path.join(os.path.dirname(__file__), 'libcufinufft.so'))
except:
lib = ctypes.cdll.LoadLibrary('libcufinufft.so')
except Exception:
raise ImportError('Failed to find a suitable cufinufft library')

Expand Down
1 change: 0 additions & 1 deletion python/cufinufft/cufinufft/_plan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""
This module contains the high level python wrapper for
the cufinufft CUDA libraries.
Expand Down
36 changes: 13 additions & 23 deletions python/cufinufft/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import os
import ctypes
from pathlib import Path
import shutil

from setuptools import setup, Extension
from setuptools import setup

# Description
DESCRIPTION = "Non-uniform fast Fourier transforms on the GPU"
Expand All @@ -17,36 +18,39 @@
requirements = [item.strip() for item in fh.readlines()]

cufinufft_dir = os.environ.get('CUFINUFFT_DIR')

if cufinufft_dir == None or cufinufft_dir == '':
if not cufinufft_dir:
cufinufft_dir = Path(__file__).resolve().parents[2]

library_dir = os.path.join(cufinufft_dir, "build")
shared_obj = os.path.join(cufinufft_dir, 'build', 'libcufinufft.so')

# Sanity check that we can find the CUDA cufinufft libraries before we get too far.
# Sanity check that we can find and load the CUDA cufinufft libraries before we get too far.
try:
lib = ctypes.cdll.LoadLibrary('libcufinufft.so')
lib = ctypes.cdll.LoadLibrary(shared_obj)
except Exception as e:
print('CUDA shared libraries not found in library path.'
' Please refer to installation documentation at http://github.com/flatironinstitute/cufinufft'
' and ensure CUDA installation is successful first before attempting to install the Python wrappers.')
' Please refer to installation documentation at '
'https://finufft.readthedocs.io/en/latest/install_gpu.html '
' and ensure CUDA installation is successful first before '
'attempting to install the Python wrappers.')
raise(e)
print('cufinufft CUDA shared libraries found, continuing...')

shutil.copy(shared_obj, os.path.join(os.getcwd(), 'cufinufft'))

# Python Package Setup
setup(
name='cufinufft',
version='2.2.0beta',
author='Yu-shuan Melody Shih, Garrett Wright, Joakim Anden, Johannes Blaschke, Alex Barnett',
author_email='[email protected]',
url='https://github.com/flatironinstitute/cufinufft',
url='https://github.com/flatironinstitute/finufft',
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
license="Apache 2",
packages=['cufinufft'],
package_dir={'': '.'},
package_data={'cufinufft': ['libcufinufft.so']},
install_requires=requirements,
# If you'd like to build or alter the docs you may additionally require these.
extras_require={
Expand All @@ -61,18 +65,4 @@
'Topic :: Scientific/Engineering :: Mathematics'],
python_requires='>=3.6',
zip_safe=False,
# This explicitly tells the wheel systems that we're platform specific.
# Addiitonally, will create a new cPython library with a decorated name
# that is rpath linked to CUDA library, also decorated (by auditwheel).
# Most importantly, pip will manage to install all this stuff in
# in places Python can find it (with a little help).
py_modules=['cufinufftc'],
ext_modules=[
Extension(name='cufinufftc',
sources=[],
libraries=['cufinufft'],
library_dirs=[library_dir],
runtime_library_dirs=[library_dir],
)
]
)
52 changes: 14 additions & 38 deletions python/finufft/finufft/_finufft.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@

import ctypes
import os
import warnings
import platform

# While imp is deprecated, it is currently the inspection solution
# that works for all versions of Python 2 and 3.
# One day if that changes, can be replaced
# with importlib.find_spec.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
import imp

import numpy as np

from ctypes import c_double
from ctypes import c_int
from ctypes import c_float
Expand All @@ -33,34 +22,21 @@
c_double_p = ctypes.POINTER(c_double)
c_longlong_p = ctypes.POINTER(c_longlong)

# TODO: See if there is a way to improve this so it is less hacky.
lib = None
# Try to load a local library directly.
try:
lib = ctypes.cdll.LoadLibrary('libfinufft.so')
except OSError:
pass
# Attempt to load library, first from package install, then from path as fallback
pkgroot = os.path.dirname(__file__)
if platform.system() == 'Windows':
os.environ["PATH"] += os.pathsep + pkgroot

# Should that not work, try to find the full path of a packaged lib.
# The packaged lib should have a py/platform decorated name,
# and be rpath'ed the true FINUFFT library through the Extension and wheel
# systems.
try:
if lib is None:
# Find the library.
fh = imp.find_module('finufft/finufftc')[0]
# Get the full path for the ctypes loader.
if platform.system() == 'Windows':
os.environ["PATH"] += os.pathsep + os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(fh.name))),'finufft')
full_lib_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(fh.name))),'finufft','libfinufft.dll')
else:
full_lib_path = os.path.realpath(fh.name)
fh.close() # Be nice and close the open file handle.

# Load the library,
# which rpaths the libraries we care about.
lib = ctypes.cdll.LoadLibrary(full_lib_path)
except Exception:
lib = None
for ext in ("dll", "lib", "so"):
full_lib_path = os.path.join(pkgroot, 'libfinufft.' + ext)
if os.path.isfile(full_lib_path):
try:
lib = ctypes.cdll.LoadLibrary(full_lib_path)
except:
pass

if lib is None:
raise ImportError('Failed to find a suitable finufft library')


Expand Down
Loading