|
21 | 21 | import org.slf4j.LoggerFactory; |
22 | 22 |
|
23 | 23 | import java.io.*; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.stream.Collectors; |
24 | 27 |
|
25 | 28 | import static com.google.common.io.ByteStreams.copy; |
26 | 29 |
|
@@ -56,30 +59,45 @@ public class LightGBMUtils { |
56 | 59 | static public void loadLibs() { |
57 | 60 |
|
58 | 61 | if (!libsLoaded) { |
| 62 | + final CpuArchitecture cpuArchitecture = getCpuArchitecture(System.getProperty("os.arch")); |
59 | 63 | try { |
60 | | - loadSharedLibraryFromJar("libgomp.so.1.0.0"); |
61 | | - loadSharedLibraryFromJar("lib_lightgbm.so"); |
62 | | - loadSharedLibraryFromJar("lib_lightgbm_swig.so"); |
63 | | - } catch (final IOException e) { |
64 | | - throw new RuntimeException("Failed to load LightGBM shared libraries from jar.", e); |
| 64 | + loadSharedLibraryFromJar("libgomp.so.1.0.0", cpuArchitecture); |
| 65 | + loadSharedLibraryFromJar("lib_lightgbm.so", cpuArchitecture); |
| 66 | + loadSharedLibraryFromJar("lib_lightgbm_swig.so", cpuArchitecture); |
| 67 | + } catch (final IOException ex) { |
| 68 | + throw new RuntimeException("Failed to load LightGBM shared libraries from jar.", ex); |
65 | 69 | } |
66 | 70 |
|
67 | 71 | logger.info("Loaded LightGBM libs."); |
68 | 72 | libsLoaded = true; |
69 | 73 | } |
70 | 74 | } |
71 | 75 |
|
| 76 | + static CpuArchitecture getCpuArchitecture(final String cpuArchName) { |
| 77 | + try { |
| 78 | + return CpuArchitecture.valueOf(cpuArchName.toUpperCase()); |
| 79 | + } catch (final IllegalArgumentException ex) { |
| 80 | + logger.error("Trying to use LightGBM on an unsupported architecture {}.", cpuArchName, ex); |
| 81 | + throw ex; |
| 82 | + } |
| 83 | + } |
| 84 | + |
72 | 85 | /** |
73 | 86 | * Loads a single shared library from the Jar. |
74 | 87 | * |
75 | 88 | * @param sharedLibResourceName library "filename" inside the jar. |
| 89 | + * @param cpuArchitecture cpu architecture. |
76 | 90 | * @throws IOException if any error happens loading the library. |
77 | 91 | */ |
78 | | - static private void loadSharedLibraryFromJar(final String sharedLibResourceName) throws IOException { |
| 92 | + static private void loadSharedLibraryFromJar( |
| 93 | + final String sharedLibResourceName, |
| 94 | + final CpuArchitecture cpuArchitecture |
| 95 | + ) throws IOException { |
79 | 96 |
|
80 | | - logger.debug("Loading LightGBM shared lib: {}.", sharedLibResourceName); |
| 97 | + logger.debug("Loading LightGBM shared lib: {} for {}.", sharedLibResourceName, cpuArchitecture); |
81 | 98 |
|
82 | | - final InputStream inputStream = LightGBMUtils.class.getClassLoader().getResourceAsStream(sharedLibResourceName); |
| 99 | + final InputStream inputStream = LightGBMUtils.class.getClassLoader() |
| 100 | + .getResourceAsStream(cpuArchitecture.getLgbmNativeLibsFolder() + "/" + sharedLibResourceName); |
83 | 101 | final File tempFile = File.createTempFile("lib", ".so"); |
84 | 102 | final OutputStream outputStream = new FileOutputStream(tempFile); |
85 | 103 |
|
|
0 commit comments