Skip to content

Commit 16e1317

Browse files
authored
Proper error message for Macos when there's no libomp (#65)
* disable omp for mac ci * disable omp for mac ci v2 * disable omp for mac ci v3 * disable omp for mac ci v4 * add macos libomp warning * add macos libomp warning v2 * add macos libomp warning v3 * resurrect CI again * add note about libomp to readme
1 parent 78fe247 commit 16e1317

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ${{ matrix.platform }}
1515
strategy:
1616
matrix:
17-
java: [8, 11, 17]
17+
java: [11, 17]
1818
platform: [ubuntu-20.04, macos-11, windows-2019]
1919
steps:
2020
- name: install libomp

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ To install, use the following maven coordinates:
4848
Versioning schema attempts to match the upstream, but with extra `-N` suffix, if there were a couple of extra lightgbm4j-specific
4949
changes released on top.
5050

51+
### MacOS & Linux native library dependencies installation
52+
53+
LightGBM native library requires the `libomp` dependency for OpenMP support, but this library is often not missing on some systems by default.
54+
55+
For MacOS:
56+
```
57+
brew install libomp
58+
```
59+
60+
For Debian Linux:
61+
```
62+
apt install libgomp1
63+
```
64+
5165
## Usage
5266

5367
There are two main classes available:

src/main/java/io/github/metarank/lightgbm4j/LGBMBooster.java

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,57 @@ public synchronized static void loadNative() throws IOException {
4747
String os = System.getProperty("os.name");
4848
String arch = System.getProperty("os.arch", "generic").toLowerCase(Locale.ENGLISH);
4949
if (os.startsWith("Linux") || os.startsWith("LINUX")) {
50-
if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
51-
loadNative("linux/x86_64/lib_lightgbm.so", "lib_lightgbm.so");
52-
loadNative("linux/x86_64/lib_lightgbm_swig.so", "lib_lightgbm_swig.so");
53-
nativeLoaded = true;
54-
} else if (arch.startsWith("aarch64") || arch.startsWith("arm64")) {
55-
loadNative("linux/aarch64/lib_lightgbm.so", "lib_lightgbm.so");
56-
loadNative("linux/aarch64/lib_lightgbm_swig.so", "lib_lightgbm_swig.so");
57-
nativeLoaded = true;
50+
try {
51+
if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
52+
loadNative("linux/x86_64/lib_lightgbm.so", "lib_lightgbm.so");
53+
loadNative("linux/x86_64/lib_lightgbm_swig.so", "lib_lightgbm_swig.so");
54+
nativeLoaded = true;
55+
} else if (arch.startsWith("aarch64") || arch.startsWith("arm64")) {
56+
loadNative("linux/aarch64/lib_lightgbm.so", "lib_lightgbm.so");
57+
loadNative("linux/aarch64/lib_lightgbm_swig.so", "lib_lightgbm_swig.so");
58+
nativeLoaded = true;
59+
}
60+
} catch (UnsatisfiedLinkError err) {
61+
String message = err.getMessage();
62+
if (message.contains("libgomp")) {
63+
System.out.println("\n\n\n");
64+
System.out.println("****************************************************");
65+
System.out.println("Your Linux system probably has no 'libgomp' library installed!");
66+
System.out.println("Please double-check the lightgbm4j install instructions:");
67+
System.out.println("- https://github.com/metarank/lightgbm4j/");
68+
System.out.println("- or just install the libgomp with your package manager");
69+
System.out.println("****************************************************");
70+
System.out.println("\n\n\n");
71+
}
5872
}
5973
} else if (os.startsWith("Mac")) {
60-
if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
61-
loadNative("osx/x86_64/lib_lightgbm.dylib", "lib_lightgbm.dylib");
62-
loadNative("osx/x86_64/lib_lightgbm_swig.dylib", "lib_lightgbm_swig.dylib");
63-
nativeLoaded = true;
64-
} else if (arch.startsWith("aarch64") || arch.startsWith("arm64")) {
65-
loadNative("osx/aarch64/lib_lightgbm.dylib", "lib_lightgbm.dylib");
66-
loadNative("osx/aarch64/lib_lightgbm_swig.dylib", "lib_lightgbm_swig.dylib");
67-
nativeLoaded = true;
68-
} else {
69-
System.out.println("arch " + arch + " is not supported");
74+
try {
75+
if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
76+
loadNative("osx/x86_64/lib_lightgbm.dylib", "lib_lightgbm.dylib");
77+
loadNative("osx/x86_64/lib_lightgbm_swig.dylib", "lib_lightgbm_swig.dylib");
78+
nativeLoaded = true;
79+
} else if (arch.startsWith("aarch64") || arch.startsWith("arm64")) {
80+
loadNative("osx/aarch64/lib_lightgbm.dylib", "lib_lightgbm.dylib");
81+
loadNative("osx/aarch64/lib_lightgbm_swig.dylib", "lib_lightgbm_swig.dylib");
82+
nativeLoaded = true;
83+
} else {
84+
System.out.println("arch " + arch + " is not supported");
85+
throw new UnsatisfiedLinkError("no native lightgbm library found for your OS "+os);
86+
}
87+
} catch (UnsatisfiedLinkError err) {
88+
String message = err.getMessage();
89+
if (message.contains("libomp.dylib")) {
90+
System.out.println("\n\n\n");
91+
System.out.println("****************************************************");
92+
System.out.println("Your MacOS system probably has no 'libomp' library installed!");
93+
System.out.println("Please double-check the lightgbm4j install instructions:");
94+
System.out.println("- https://github.com/metarank/lightgbm4j/");
95+
System.out.println("- or just do 'brew install libomp'");
96+
System.out.println("****************************************************");
97+
System.out.println("\n\n\n");
98+
99+
}
100+
throw err;
70101
}
71102
} else if (os.startsWith("Windows")) {
72103
loadNative("windows/x86_64/lib_lightgbm.dll", "lib_lightgbm.dll");
@@ -78,7 +109,7 @@ public synchronized static void loadNative() throws IOException {
78109
}
79110
}
80111

81-
private static void loadNative(String path, String name) throws IOException {
112+
private static void loadNative(String path, String name) throws IOException, UnsatisfiedLinkError {
82113
System.out.println("Loading native lib " + path);
83114
String tmp = System.getProperty("java.io.tmpdir");
84115
File libFile = new File(tmp + File.separator + name);
@@ -91,7 +122,8 @@ private static void loadNative(String path, String name) throws IOException {
91122
try {
92123
System.load(libFile.toString());
93124
} catch (UnsatisfiedLinkError err) {
94-
System.out.println("Cannot load library: " + err + " cause: " + err.getMessage());
125+
System.out.println("Cannot load library:" + err.getMessage());
126+
throw err;
95127
}
96128
}
97129

0 commit comments

Comments
 (0)