Debian Codegraph @

src/dr/inference/multidimensionalscaling/NativeMDSSingleton.java

raw symbols SCIP
1 /*
2 * NativeMDSSingleton.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.multidimensionalscaling;
27
28 /**
29 * NativeMDSSingleton
30 *
31 * @author Marc Suchard
32 * @author Andrew Rambaut
33 * @version $Id$
34 * <p/>
35 * $HeadURL$
36 * <p/>
37 * $LastChangedBy$
38 * $LastChangedDate$
39 * $LastChangedRevision$
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 } // ensure singleton
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 //jsize size = env->GetArrayLength( arr );
130 //std::vector<double> input( size );
131 //env->GetDoubleArrayRegion( arr, 0, size, &input[0] );
132 //
133 //// ...
134 //
135 //jdoubleArray output = env->NewDoubleArray( results.size() );
136 //env->SetDoubleArrayRegion( output, 0, results.size(), &results[0] );
137
138 }