Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ethen8181
GitHub Repository: ethen8181/machine-learning
Path: blob/master/python/cython/setup_parallel.py
2574 views
1
# usually the name should only be setup.py
2
# on the terminal run
3
# python setup_parallel.py install
4
import os
5
import sys
6
import glob
7
import numpy as np
8
from setuptools import Extension, setup
9
try:
10
from Cython.Build import cythonize
11
use_cython = True
12
except ImportError:
13
use_cython = False
14
15
# top-level information
16
NAME = 'pairwise3'
17
VERSION = '0.0.1'
18
USE_OPENMP = True
19
20
21
def set_gcc(use_openmp):
22
"""
23
Try to find and use GCC on OSX for OpenMP support
24
25
References
26
----------
27
https://github.com/maciejkula/glove-python/blob/master/setup.py
28
"""
29
# For macports and homebrew
30
patterns = ['/opt/local/bin/gcc-mp-[0-9].[0-9]',
31
'/opt/local/bin/gcc-mp-[0-9]',
32
'/usr/local/bin/gcc-[0-9].[0-9]',
33
'/usr/local/bin/gcc-[0-9]']
34
35
if 'darwin' in sys.platform.lower():
36
gcc_binaries = []
37
for pattern in patterns:
38
gcc_binaries += glob.glob(pattern)
39
40
gcc_binaries.sort()
41
42
if gcc_binaries:
43
_, gcc = os.path.split(gcc_binaries[-1])
44
os.environ['CC'] = gcc
45
46
else:
47
use_openmp = False
48
49
return use_openmp
50
51
52
def define_extensions(use_cython, use_openmp):
53
"""
54
boilerplate to compile the extension the only thing that we need to
55
worry about is the modules part, where we define the extension that
56
needs to be compiled
57
"""
58
if sys.platform.startswith('win'):
59
# compile args from
60
# https://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx
61
link_args = []
62
compile_args = ['/O2', '/openmp']
63
else:
64
link_args = []
65
compile_args = ['-Wno-unused-function', '-Wno-maybe-uninitialized', '-O3', '-ffast-math']
66
if use_openmp:
67
compile_args.append('-fopenmp')
68
link_args.append('-fopenmp')
69
70
if 'anaconda' not in sys.version.lower():
71
compile_args.append('-march=native')
72
73
# recommended approach is that the user can choose not to
74
# compile the code using cython, they can instead just use
75
# the .c file that's also distributed
76
# http://cython.readthedocs.io/en/latest/src/reference/compilation.html#distributing-cython-modules
77
src_ext = '.pyx' if use_cython else '.c'
78
names = ['pairwise3']
79
modules = [Extension(name,
80
[os.path.join(name + src_ext)],
81
extra_compile_args = compile_args,
82
extra_link_args = link_args) for name in names]
83
84
if use_cython:
85
return cythonize(modules)
86
else:
87
return modules
88
89
90
USE_OPENMP = set_gcc(USE_OPENMP)
91
setup(
92
name = NAME,
93
version = VERSION,
94
description = 'pairwise distance quickstart',
95
ext_modules = define_extensions(use_cython, USE_OPENMP),
96
include_dirs = [np.get_include()]
97
)
98
99