Skip to content

Commit

Permalink
fixes for apple setups, more alternatives for march
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cortes committed May 21, 2022
1 parent a493319 commit 785634a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -40,17 +40,17 @@ The variants implemented here are based on multiple oracle calls (building a ser
**Note: Python 2 is not supported and package will fail to run on Python 2.7**

** *
**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:
**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 (by e.g. using AVX instructions if available), 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 either by (a) defining an environment variable `DONT_SET_MARCH=1`, or by (b) manually supplying compilation `CFLAGS` as an environment variable with something related to architecture. For maximum compatibility (but slowest speed), it's possible to do something like this:

```
export CFLAGS="-msse2"
export DONT_SET_MARCH=1
pip install costsensitive
```

or for creating wheels:
or, by specifying some compilation flag for architecture:
```
export CFLAGS="-msse2"
python setup.py bwheel
export CFLAGS="-march=x86-64"
pip install costsensitive
```
** *

Expand Down
33 changes: 24 additions & 9 deletions setup.py
Expand Up @@ -22,7 +22,7 @@ def build_extensions(self):
for e in self.extensions:
e.extra_compile_args = ['/O2']
else:
if not self.check_cflags_contain_arch():
if not self.check_for_variable_dont_set_march() and not self.check_cflags_contain_arch():
self.add_march_native()
self.add_openmp_linkage()

Expand All @@ -41,6 +41,9 @@ def check_cflags_contain_arch(self):
return True
return False

def check_for_variable_dont_set_march(self):
return "DONT_SET_MARCH" in os.environ

def add_march_native(self):
arg_march_native = "-march=native"
arg_mcpu_native = "-mcpu=native"
Expand All @@ -56,27 +59,33 @@ def add_openmp_linkage(self):
arg_omp2 = "-qopenmp"
arg_omp3 = "-xopenmp"
args_apple_omp = ["-Xclang", "-fopenmp", "-lomp"]
if self.test_supports_compile_arg(arg_omp1):
args_apple_omp2 = ["-Xclang", "-fopenmp", "-L/usr/local/lib", "-lomp", "-I/usr/local/include"]
if self.test_supports_compile_arg(arg_omp1, with_omp=True):
for e in self.extensions:
e.extra_compile_args.append(arg_omp1)
e.extra_link_args.append(arg_omp1)
elif (platform[:3].lower() == "dar") and self.test_supports_compile_arg(args_apple_omp):
elif (sys.platform[:3].lower() == "dar") and self.test_supports_compile_arg(args_apple_omp, with_omp=True):
for e in self.extensions:
e.extra_compile_args += ["-Xclang", "-fopenmp"]
e.extra_link_args += ["-lomp"]
elif self.test_supports_compile_arg(arg_omp2):
elif (sys.platform[:3].lower() == "dar") and self.test_supports_compile_arg(args_apple_omp2, with_omp=True):
for e in self.extensions:
e.extra_compile_args += ["-Xclang", "-fopenmp"]
e.extra_link_args += ["-L/usr/local/lib", "-lomp"]
e.include_dirs += ["/usr/local/include"]
elif self.test_supports_compile_arg(arg_omp2, with_omp=True):
for e in self.extensions:
e.extra_compile_args.append(arg_omp2)
e.extra_link_args.append(arg_omp2)
elif self.test_supports_compile_arg(arg_omp3):
elif self.test_supports_compile_arg(arg_omp3, with_omp=True):
for e in self.extensions:
e.extra_compile_args.append(arg_omp3)
e.extra_link_args.append(arg_omp3)
else:
set_omp_false()


def test_supports_compile_arg(self, comm):
def test_supports_compile_arg(self, comm, with_omp=False):
is_supported = False
try:
if not hasattr(self.compiler, "compiler"):
Expand All @@ -88,10 +97,16 @@ def test_supports_compile_arg(self, comm):
with open(fname, "w") as ftest:
ftest.write(u"int main(int argc, char**argv) {return 0;}\n")
try:
cmd = [self.compiler.compiler[0]]
if not isinstance(self.compiler.compiler, list):
cmd = list(self.compiler.compiler)
else:
cmd = self.compiler.compiler
except:
cmd = list(self.compiler.compiler)
cmd = self.compiler.compiler
val_good = subprocess.call(cmd + [fname])
if with_omp:
with open(fname, "w") as ftest:
ftest.write(u"#include <omp.h>\nint main(int argc, char**argv) {return 0;}\n")
try:
val = subprocess.call(cmd + comm + [fname])
is_supported = (val == val_good)
Expand All @@ -115,7 +130,7 @@ def test_supports_compile_arg(self, comm):
'cython'
],
python_requires = ">=3",
version = '0.1.2.13-3',
version = '0.1.2.13-4',
description = 'Reductions for Cost-Sensitive Multi-Class Classification',
author = 'David Cortes',
author_email = 'david.cortes.rivera@gmail.com',
Expand Down

0 comments on commit 785634a

Please sign in to comment.