| 1 |
#pykdtree, Fast kd-tree implementation with OpenMP-enabled queries |
| 2 |
# |
| 3 |
#Copyright (C) 2013 - present Esben S. Nielsen |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify it under |
| 6 |
# the terms of the GNU Lesser General Public License as published by the Free |
| 7 |
# Software Foundation, either version 3 of the License, or |
| 8 |
#(at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 13 |
# details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public License along |
| 16 |
# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
import os |
| 19 |
import sys |
| 20 |
import re |
| 21 |
|
| 22 |
import numpy as np |
| 23 |
from Cython.Build import build_ext |
| 24 |
from Cython.Distutils import Extension |
| 25 |
from setuptools import setup |
| 26 |
from setuptools.command.build_ext import build_ext |
| 27 |
|
| 28 |
|
| 29 |
OMP_SETTING_TABLE = { |
| 30 |
'1': 'probe', |
| 31 |
'0': None, |
| 32 |
'gcc': 'gomp', |
| 33 |
'gomp': 'gomp', |
| 34 |
'clang': 'omp', |
| 35 |
'omp': 'omp', |
| 36 |
'msvc': 'msvc', |
| 37 |
'probe': 'probe' |
| 38 |
} |
| 39 |
|
| 40 |
OMP_COMPILE_ARGS = { |
| 41 |
'gomp': ['-fopenmp'], |
| 42 |
'omp': ['-Xpreprocessor', '-fopenmp'], |
| 43 |
'msvc': ['/openmp'] |
| 44 |
} |
| 45 |
|
| 46 |
OMP_LINK_ARGS = { |
| 47 |
'gomp': ['-lgomp'], |
| 48 |
'omp': ['-lomp'], |
| 49 |
'msvc': [] |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
class build_ext_subclass(build_ext): |
| 54 |
"""Custom extension building to have platform and compiler specific flags.""" |
| 55 |
|
| 56 |
def build_extensions(self): |
| 57 |
comp = self.compiler.compiler_type |
| 58 |
omp_comp, omp_link = _omp_compile_link_args(comp) |
| 59 |
if comp in ('unix', 'cygwin', 'mingw32'): |
| 60 |
extra_compile_args = ['-std=c17', '-O3'] + omp_comp |
| 61 |
extra_link_args = omp_link |
| 62 |
elif comp == 'msvc': |
| 63 |
extra_compile_args = ['/Ox'] + omp_comp |
| 64 |
extra_link_args = omp_link |
| 65 |
else: |
| 66 |
# Add support for more compilers here |
| 67 |
raise ValueError('Compiler flags undefined for %s. Please modify setup.py and add compiler flags' |
| 68 |
% comp) |
| 69 |
self.extensions[0].extra_compile_args = extra_compile_args |
| 70 |
self.extensions[0].extra_link_args = extra_link_args |
| 71 |
build_ext.build_extensions(self) |
| 72 |
|
| 73 |
|
| 74 |
def _omp_compile_link_args(compiler): |
| 75 |
# Get OpenMP setting from environment |
| 76 |
use_omp = OMP_SETTING_TABLE[os.environ.get('USE_OMP', 'probe')] |
| 77 |
|
| 78 |
compile_args = [] |
| 79 |
link_args = [] |
| 80 |
if use_omp == "probe": |
| 81 |
use_omp, compile_args, link_args = _probe_omp_for_compiler_and_platform(compiler) |
| 82 |
|
| 83 |
print(f"Will use {use_omp} for OpenMP." if use_omp else "OpenMP support not available.") |
| 84 |
compile_args += OMP_COMPILE_ARGS.get(use_omp, []) |
| 85 |
link_args += OMP_LINK_ARGS.get(use_omp, []) |
| 86 |
print(f"Compiler: {compiler} / OpenMP: {use_omp} / OpenMP compile args: {compile_args} / OpenMP link args: {link_args}") |
| 87 |
return compile_args, link_args |
| 88 |
|
| 89 |
|
| 90 |
def _probe_omp_for_compiler_and_platform(compiler): |
| 91 |
compile_args = [] |
| 92 |
link_args = [] |
| 93 |
if compiler == "msvc": |
| 94 |
use_omp = "msvc" |
| 95 |
elif _is_conda_interpreter(): |
| 96 |
# Conda provides its own compiler which does support openmp |
| 97 |
use_omp = "gomp" |
| 98 |
elif _is_macOS(): |
| 99 |
# OpenMP is not supported with system clang but homebrew and macports have libomp packages |
| 100 |
compile_args, link_args = _macOS_omp_options_from_probe() |
| 101 |
if not (compile_args or link_args): |
| 102 |
print("Probe for libomp failed, skipping use of OpenMP with clang.") |
| 103 |
print("It may be possible to build with OpenMP using USE_OMP=clang with CFLAGS and LDFLAGS explicit settings to use libomp.") |
| 104 |
use_omp = None |
| 105 |
else: |
| 106 |
use_omp = "omp" |
| 107 |
else: |
| 108 |
use_omp = "gomp" |
| 109 |
return use_omp, compile_args, link_args |
| 110 |
|
| 111 |
|
| 112 |
def _is_conda_interpreter(): |
| 113 |
"""Is the running interpreter from Anaconda or miniconda? |
| 114 |
|
| 115 |
See https://stackoverflow.com/a/21318941/433202 |
| 116 |
|
| 117 |
Examples:: |
| 118 |
|
| 119 |
2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Jan 10 2014, 11:23:15) |
| 120 |
2.7.6 |Continuum Analytics, Inc.| (default, Jan 10 2014, 11:23:15) |
| 121 |
3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 09:55:02) |
| 122 |
|
| 123 |
""" |
| 124 |
return 'conda' in sys.version or 'Continuum' in sys.version |
| 125 |
|
| 126 |
|
| 127 |
def _is_macOS(): |
| 128 |
return 'darwin' in sys.platform |
| 129 |
|
| 130 |
|
| 131 |
def _macOS_omp_options_from_probe(): |
| 132 |
"""Get common include and library paths for libomp installation on macOS. |
| 133 |
|
| 134 |
For example ``(['-I/opt/local/include/libomp'], ['-L/opt/local/lib/libomp'])``. |
| 135 |
|
| 136 |
""" |
| 137 |
for cmd in ["brew ls --verbose libomp", "port contents libomp"]: |
| 138 |
inc, lib = _compile_link_paths_from_manifest(cmd) |
| 139 |
if inc and lib: |
| 140 |
return [f"-I{inc}"], [f"-L{lib}"] |
| 141 |
return [], [] |
| 142 |
|
| 143 |
|
| 144 |
def _compile_link_paths_from_manifest(cmd): |
| 145 |
"""Parse include and library paths from OSX package managers. |
| 146 |
|
| 147 |
Example executions:: |
| 148 |
|
| 149 |
# Homebrew |
| 150 |
$ brew ls --verbose libomp |
| 151 |
/opt/homebrew/Cellar/libomp/15.0.7/INSTALL_RECEIPT.json |
| 152 |
/opt/homebrew/Cellar/libomp/15.0.7/.brew/libomp.rb |
| 153 |
/opt/homebrew/Cellar/libomp/15.0.7/include/ompt.h |
| 154 |
/opt/homebrew/Cellar/libomp/15.0.7/include/omp.h |
| 155 |
/opt/homebrew/Cellar/libomp/15.0.7/include/omp-tools.h |
| 156 |
/opt/homebrew/Cellar/libomp/15.0.7/lib/libomp.dylib |
| 157 |
/opt/homebrew/Cellar/libomp/15.0.7/lib/libomp.a |
| 158 |
|
| 159 |
# MacPorts |
| 160 |
$ port contents libomp |
| 161 |
Port libomp contains: |
| 162 |
/opt/local/include/libomp/omp-tools.h |
| 163 |
/opt/local/include/libomp/omp.h |
| 164 |
/opt/local/include/libomp/ompt.h |
| 165 |
/opt/local/lib/libomp/libgomp.dylib |
| 166 |
/opt/local/lib/libomp/libiomp5.dylib |
| 167 |
/opt/local/lib/libomp/libomp.dylib |
| 168 |
/opt/local/share/doc/libomp/LICENSE.TXT |
| 169 |
/opt/local/share/doc/libomp/README.txt |
| 170 |
|
| 171 |
""" |
| 172 |
from subprocess import run |
| 173 |
query = run(cmd, shell=True, check=False, capture_output=True) |
| 174 |
if query.returncode != 0: |
| 175 |
return None, None |
| 176 |
manifest = query.stdout.decode("UTF-8") |
| 177 |
# find all the unique directories mentioned in the manifest |
| 178 |
dirs = set(os.path.split(filename)[0] for filename in re.findall(r'^\s*(/.*?)\s*$', manifest, re.MULTILINE)) |
| 179 |
# find a unique libdir and incdir |
| 180 |
inc = tuple(d for d in dirs if re.search(r'/include(\W|$)', d)) |
| 181 |
lib = tuple(d for d in dirs if re.search(r'/lib(\W|$)', d)) |
| 182 |
# only return success if there's no ambiguity |
| 183 |
return (inc + lib) if len(inc) == 1 and len(lib) == 1 else (None, None) |
| 184 |
|
| 185 |
|
| 186 |
with open('README.rst', 'r') as readme_file: |
| 187 |
readme = readme_file.read() |
| 188 |
|
| 189 |
extensions = [ |
| 190 |
Extension('pykdtree.kdtree', sources=['pykdtree/kdtree.pyx', 'pykdtree/_kdtree_core.c'], |
| 191 |
include_dirs=[np.get_include()], |
| 192 |
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_25_API_VERSION")], |
| 193 |
cython_directives={"language_level": "3", "freethreading_compatible": True}, |
| 194 |
), |
| 195 |
] |
| 196 |
|
| 197 |
|
| 198 |
setup( |
| 199 |
name='pykdtree', |
| 200 |
version='1.4.3', |
| 201 |
url="https://github.com/storpipfugl/pykdtree", |
| 202 |
description='Fast kd-tree implementation with OpenMP-enabled queries', |
| 203 |
long_description=readme, |
| 204 |
long_description_content_type="text/x-rst", |
| 205 |
author='Esben S. Nielsen', |
| 206 |
author_email='[email protected]', |
| 207 |
packages=['pykdtree'], |
| 208 |
python_requires='>=3.9', |
| 209 |
install_requires=['numpy'], |
| 210 |
extras_require={ |
| 211 |
'test': ['pytest', 'mypy'], |
| 212 |
}, |
| 213 |
zip_safe=False, |
| 214 |
ext_modules=extensions, |
| 215 |
cmdclass={'build_ext': build_ext_subclass}, |
| 216 |
license="LGPL-3.0-or-later", |
| 217 |
license_files=["LICENSE.txt"], |
| 218 |
classifiers=[ |
| 219 |
'Development Status :: 5 - Production/Stable', |
| 220 |
'Programming Language :: Python', |
| 221 |
'Operating System :: OS Independent', |
| 222 |
'Intended Audience :: Science/Research', |
| 223 |
'Topic :: Scientific/Engineering', |
| 224 |
'Programming Language :: Python :: Free Threading :: 3 - Stable', |
| 225 |
] |
| 226 |
) |
| 227 |
|