| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.multidimensionalscaling; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class NativeMDSSingleton { |
| 42 |
|
| 43 |
public static final String LIBRARY_NAME = "mds_jni"; |
| 44 |
public static final String LIBRARY_PATH_LABEL = "mds.library.path"; |
| 45 |
public static final String LIBRARY_PLATFORM_NAME = getPlatformSpecificLibraryName(); |
| 46 |
public static final String LIBRARY_PLATFORM_EXTENSION = getPlatformSpecificLibraryExtension(); |
| 47 |
public static final String LIBRARY_PLATFORM_PREFIX = getPlatformSpecificLibraryPrefix(); |
| 48 |
|
| 49 |
|
| 50 |
private NativeMDSSingleton() { |
| 51 |
} |
| 52 |
|
| 53 |
private static String getPlatformSpecificLibraryName() { |
| 54 |
String osName = System.getProperty("os.name").toLowerCase(); |
| 55 |
String osArch = System.getProperty("os.arch").toLowerCase(); |
| 56 |
if (osName.startsWith("windows")) { |
| 57 |
if (osArch.equals("x86") || osArch.equals("i386")) return LIBRARY_NAME + "32"; |
| 58 |
if (osArch.startsWith("amd64") || osArch.startsWith("x86_64")) return LIBRARY_NAME + "64"; |
| 59 |
} |
| 60 |
return LIBRARY_NAME; |
| 61 |
} |
| 62 |
|
| 63 |
private static String getPlatformSpecificLibraryExtension() { |
| 64 |
String osName = System.getProperty("os.name").toLowerCase(); |
| 65 |
if (osName.startsWith("windows")) { |
| 66 |
return ".dll"; |
| 67 |
} else if (osName.startsWith("mac")) { |
| 68 |
return ".dylib"; |
| 69 |
} else { |
| 70 |
return ".so"; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
private static String getPlatformSpecificLibraryPrefix() { |
| 75 |
String osName = System.getProperty("os.name").toLowerCase(); |
| 76 |
if (osName.startsWith("windows")) { |
| 77 |
return ""; |
| 78 |
} else { |
| 79 |
return "lib"; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public static NativeMDSSingleton loadLibrary() throws UnsatisfiedLinkError { |
| 84 |
|
| 85 |
if (INSTANCE == null) { |
| 86 |
System.err.println("Trying to load MDS library..."); |
| 87 |
String path = ""; |
| 88 |
if (System.getProperty(LIBRARY_PATH_LABEL) != null) { |
| 89 |
path = System.getProperty(LIBRARY_PATH_LABEL); |
| 90 |
if (path.length() > 0 && !path.endsWith("/")) { |
| 91 |
path += "/"; |
| 92 |
} |
| 93 |
System.load(path + LIBRARY_PLATFORM_PREFIX + LIBRARY_NAME + LIBRARY_PLATFORM_EXTENSION); |
| 94 |
} else { |
| 95 |
System.loadLibrary(LIBRARY_PLATFORM_NAME); |
| 96 |
} |
| 97 |
|
| 98 |
INSTANCE = new NativeMDSSingleton(); |
| 99 |
System.err.println("MDS library loaded."); |
| 100 |
} |
| 101 |
|
| 102 |
return INSTANCE; |
| 103 |
} |
| 104 |
|
| 105 |
private static NativeMDSSingleton INSTANCE = null; |
| 106 |
|
| 107 |
public native int initialize(int dimensionCount, int locationCount, long flags); |
| 108 |
|
| 109 |
public native void updateLocations(int instance, int updateCount, double[] locations); |
| 110 |
|
| 111 |
public native double getSumOfIncrements(int instance); |
| 112 |
|
| 113 |
public native void storeState(int instance); |
| 114 |
|
| 115 |
public native void restoreState(int instance); |
| 116 |
|
| 117 |
public native void acceptState(int instance); |
| 118 |
|
| 119 |
public native void makeDirty(int instance); |
| 120 |
|
| 121 |
public native void setPairwiseData(int instance, double[] observations); |
| 122 |
|
| 123 |
public native void setParameters(int instance, double[] parameters); |
| 124 |
|
| 125 |
public native double[] getPairwiseData(int instance); |
| 126 |
|
| 127 |
public native void getLocationGradient(int instance, double[] gradient); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
} |