Skip to content

Commit 389ed51

Browse files
committed
Make buildextension for for macos with OpenMP
1 parent 0e57bd9 commit 389ed51

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ Although the last two algorithms are not specifically designed for the k-Min-Sum
1919

2020
## Installation
2121

22-
We **highly recommend** to install OpenMP. Parts of the code are parallelized and will be much faster. However, the code should run even without it. If that is not the case, open an issue!
22+
We **highly recommend** to install OpenMP. Parts of the code are parallelized and will be much faster. However, on Windows and MacOS the code also works without OpenMP. Nonetheless, the code was written for Linux and will achieve the best performance there.
2323

2424
On Linux, you can use the following command:
2525
```bash
2626
sudo apt-get install libomp-dev
2727
```
28+
2829
On MacOS, you can use the following command:
2930
```bash
30-
brew install clang-omp
31+
brew install llvm libomp
3132
```
33+
However, it might be that MacOS does not find the installed library. In [`build_extension.py`](./build_extension.py), the paths are set manually. If it does not work for you, please clone the repository and run `poetry build`. You you see a message in red if your OpenMP is not found.
3234

3335
Then, you can install the package via pip:
3436
```bash

build_extension.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
2+
import platform
23
import subprocess
34
import warnings
4-
from typing import Any, Dict
5+
from typing import Any, Dict, List
56

67
from setuptools import Extension
78
from setuptools.command.build_ext import build_ext
@@ -25,7 +26,10 @@
2526
)
2627

2728

28-
def check_openmp_support() -> bool:
29+
def check_openmp_support() -> List[str]:
30+
if platform.system() == "Windows":
31+
return []
32+
2933
openmp_test_code = """
3034
#include <omp.h>
3135
#include <stdio.h>
@@ -44,23 +48,33 @@ def check_openmp_support() -> bool:
4448
f.write(openmp_test_code)
4549

4650
try:
51+
if platform.system() == "Darwin":
52+
cmd_params = [
53+
"-Xclang",
54+
"-fopenmp",
55+
"-I/usr/local/opt/libomp/include",
56+
"-L/usr/local/opt/libomp/lib",
57+
"-lomp",
58+
]
59+
else:
60+
cmd_params = ["-fopenmp"]
4761
# Try to compile the code with OpenMP support
4862
result = subprocess.run(
49-
["gcc", "-fopenmp", "test_openmp.c", "-o", "test_openmp"],
63+
["gcc"] + cmd_params + ["test_openmp.c", "-o", "test_openmp"],
5064
stdout=subprocess.PIPE,
5165
stderr=subprocess.PIPE,
5266
)
5367
if result.returncode != 0:
54-
return False
68+
return []
5569

5670
# Run the compiled program
5771
result = subprocess.run(
5872
["./test_openmp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
5973
)
6074
if result.returncode == 0:
61-
return True
75+
return cmd_params
6276
else:
63-
return False
77+
return []
6478
finally:
6579
os.remove("test_openmp.c")
6680
if os.path.exists("test_openmp"):
@@ -71,24 +85,23 @@ class BuildExt(build_ext):
7185
"""A custom build extension for adding -stdlib arguments for clang++."""
7286

7387
def build_extensions(self) -> None:
74-
if os.name == "nt":
75-
support = True
76-
else:
77-
support = check_openmp_support()
88+
openmp_params = check_openmp_support()
7889

7990
# '-std=c++11' is added to `extra_compile_args` so the code can compile
8091
# with clang++. This works across compilers (ignored by MSVC).
8192
for extension in self.extensions:
8293
extension.extra_compile_args.append("-std=c++11")
83-
if support:
84-
extension.extra_compile_args.append("-fopenmp")
85-
extension.extra_link_args.append("-lgomp")
86-
else:
94+
if len(openmp_params) == 0:
8795
warnings.warn(
8896
"\x1b[31;20m OpenMP is not installed on this system. "
8997
"Please install it to have all the benefits from the program.\x1b[0m"
9098
)
99+
else:
100+
for param in openmp_params:
101+
extension.extra_compile_args.append(param)
91102

103+
if platform.system() == "Linux":
104+
extension.extra_link_args.append("-lgomp")
92105
try:
93106
build_ext.build_extensions(self)
94107
except CompileError:

0 commit comments

Comments
 (0)