Skip to content

Commit

Permalink
add option to override march=native
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cortes committed Jan 20, 2022
1 parent f51959b commit 55b5ed5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,27 @@ Or if that fails:
pip install --no-use-pep517 hpfrec
```

** *
**Note for macOS users:** on macOS, the Python version of this package might compile **without** multi-threading capabilities. In order to enable multi-threading support, first install OpenMP:
```
brew install libomp
```
And then reinstall this package: `pip install --force-reinstall hpfrec`.

** *
**IMPORTANT:** the setup script will try to add compilation flag `-march=native`. This instructs the compiler to tune the package for the CPU in which it is being installed, but the result might not be usable in other computers. If building a binary wheel of this package or putting it into a docker image which will be used in different machines, this can be overriden by manually supplying compilation `CFLAGS` as an environment variable with something related to architecture. For maximum compatibility (but slowest speed), assuming `x86-64` computers, it's possible to do something like this:

```
export CFLAGS="-msse2"
pip install ctpfrec
```

or for creating wheels:
```
export CFLAGS="-msse2"
python setup.py bwheel
```
** *

As it contains Cython code, it requires a C compiler. In Windows, this usually means it requires a Visual Studio Build Tools installation (with MSVC140 component for `conda`) (or MinGW + GCC), and if using Anaconda, might also require configuring it to use said Visual Studio instead of MinGW, otherwise the installation from `pip` might fail. For more details see this guide:
[Cython Extensions On Windows](https://github.com/cython/cython/wiki/CythonExtensionsOnWindows)
Expand Down
13 changes: 11 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def build_extensions(self):
for e in self.extensions:
e.extra_compile_args += ['/openmp', '/O2', '/fp:fast']
else:
self.add_march_native()
if not self.check_cflags_contain_arch():
self.add_march_native()
self.add_openmp_linkage()
self.add_no_math_errno()
self.add_no_trapping_math()
Expand All @@ -34,6 +35,14 @@ def build_extensions(self):

build_ext.build_extensions(self)

def check_cflags_contain_arch(self):
if "CFLAGS" in os.environ:
arch_list = ["-march", "-mcpu", "-mtune", "-msse", "-msse2", "-msse3", "-mssse3", "-msse4", "-msse4a", "-msse4.1", "-msse4.2", "-mavx", "-mavx2"]
for flag in arch_list:
if flag in os.environ["CFLAGS"]:
return True
return False

def add_march_native(self):
arg_march_native = "-march=native"
arg_mcpu_native = "-mcpu=native"
Expand Down Expand Up @@ -123,7 +132,7 @@ def test_supports_compile_arg(self, comm):
'scipy',
'cython'
],
version = '0.2.5-3',
version = '0.2.5-4',
description = 'Hierarchical Poisson matrix factorization for recommender systems',
author = 'David Cortes',
author_email = 'david.cortes.rivera@gmail.com',
Expand Down

0 comments on commit 55b5ed5

Please sign in to comment.