Changeset 13437
- Timestamp:
- Sep 9, 2019 9:59:31 PM (3 months ago)
- Location:
- branches/dev-cw-evaluator/Compiler
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/dev-cw-evaluator/Compiler/ModelicaCBackEnd/src/jastadd/CCodeGen/CCodeGenExternalCeval.jrag
r12306 r13437 288 288 } 289 289 return functionArgs; 290 } 291 292 syn String FExternalStmt.convertFtypeToExternalString(FType arg, boolean useSeparator) { 293 String input = ""; 294 if (arg.isReal()) { 295 input = input.concat("d"); 296 } else if (arg.isInteger() || arg.isEnum()) { 297 input = input.concat("i"); 298 } else if (arg.isString()) { 299 input = input.concat("s"); 300 } else if (arg.isRecord()) { 301 input = input.concat("R["); 302 for (FRecordComponentType rType: ((FRecordType)arg).getComponents()) { 303 input = input.concat(convertFtypeToExternalString(rType.getFType(), true)); 304 } 305 input = input.concat("]"); 306 } 307 if (useSeparator) { 308 input = input.concat(","); 309 } 310 return input; 311 } 312 313 syn String FExternalStmt.functionArgsSerialized(boolean returnVar) { 314 boolean skippedReturnVar = false; 315 String input = ""; 316 for (ExternalArgument var : functionArgsToSerialize()) { 317 if (returnVar) { 318 if (hasReturnVar()) { 319 return convertFtypeToExternalString(var.type(), false); 320 } else { 321 return "void"; 322 } 323 } 324 if (hasReturnVar() && skippedReturnVar != true) { 325 skippedReturnVar = true; 326 continue; 327 } 328 if (var.isOutput()) { 329 input = input.concat("*"); 330 } 331 input = input.concat(convertFtypeToExternalString(var.type(), true)); 332 } 333 return input; 290 334 } 291 335 -
branches/dev-cw-evaluator/Compiler/ModelicaCompiler/module.options
r12895 r13437 458 458 459 459 ******************************************************************************** 460 460 BOOLEAN enable_external_evaluator compiler experimental false 461 462 "If enabled, the external function will be evaluated using a pre-compiled 463 program (instead of generating and compiling one), if possible." 464 ******************************************************************************** -
branches/dev-cw-evaluator/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/common/evaluation/ExternalProcessCacheImpl.java
r12940 r13437 3 3 import java.io.File; 4 4 import java.io.FileNotFoundException; 5 import java.io.FilenameFilter; 5 6 import java.io.IOException; 6 7 import java.io.PrintStream; … … 16 17 import org.jmodelica.common.evaluation.ExternalProcessMultiCache.Variable; 17 18 import org.jmodelica.util.EnvironmentUtils; 19 import org.jmodelica.util.SystemUtil; 18 20 import org.jmodelica.util.ccompiler.CCompilerDelegator; 19 21 import org.jmodelica.util.exceptions.CcodeCompilationException; … … 42 44 return mc.log(); 43 45 } 44 46 47 private String getSharedLibrary(External<K> ext) { 48 /* Messy messy... */ 49 String sharedLib = ""; 50 final String extName = ext.getName(); 51 String platform = CCompilerDelegator.reduceBits(EnvironmentUtils.getJavaPlatform(), 52 mc.getCCompiler().getTargetPlatforms()); 53 File f = new File(ext.libraryDirectory(), platform); 54 String libLoc = f.isDirectory() ? f.getPath() : ext.libraryDirectory(); 55 File dir = new File(libLoc); 56 File[] matches = dir.listFiles(new FilenameFilter() 57 { 58 public boolean accept(File dir, String name) 59 { 60 return name.contains(extName) && name.endsWith(SystemUtil.sharedLibraryExtension()); 61 } 62 }); 63 for(File file : matches) { 64 sharedLib = file.toString(); 65 break; 66 } 67 if (sharedLib.equals("")) { 68 for (String lib : ext.library()) { 69 File tmp = new File(libLoc, lib.concat(SystemUtil.sharedLibraryExtension())); 70 if (tmp.exists() && !tmp.isDirectory()) { 71 sharedLib = tmp.toString(); 72 } 73 } 74 } 75 return sharedLib; 76 } 77 78 private String getOutputArguments(E ext) { 79 String output = ext.functionArgsSerialized(true); 80 if (output.equals("")) { 81 return "void"; 82 } 83 return output; 84 } 85 86 private String getInputArguments(E ext) { 87 return ext.functionArgsSerialized(false); 88 } 89 90 public boolean canUseEvaluator(E ext) { 91 for (K eo : ext.externalObjectsToSerialize()) { 92 return false; 93 } 94 95 if (!(ext.myOptions().getBooleanOption("enable_external_evaluator"))) { 96 return false; 97 } 98 /* TODO: 99 * Verify that the library used is a shared library, not static. */ 100 /* Builtins, such as Modelica functions should be accepted 101 Verify that the actual inputs/outputs are supported */ 102 103 return true; 104 } 105 45 106 @Override 46 107 public ExternalFunction<K, V> getExternalFunction(E ext) { … … 52 113 try { 53 114 long time = System.currentTimeMillis(); 54 String executable = mc.compileExternal(ext); 115 String executable = null; 116 ArrayList<String> arguments = null; 117 if (canUseEvaluator(ext)) { 118 String jmHome = System.getenv("JMODELICA_HOME"); 119 executable = jmHome + File.separator + "bin" + File.separator + CCompilerDelegator.reduceBits(EnvironmentUtils.getJavaPlatform(),mc.getCCompiler().getTargetPlatforms()) + File.separator + "jmi_evaluator" + SystemUtil.executableExtension(); 120 121 String sharedLibrary = getSharedLibrary(ext); 122 if (sharedLibrary.equals("")) { sharedLibrary = "NoSharedLibrary"; } 123 124 arguments = new ArrayList<String>(); 125 arguments.add(executable); 126 arguments.add(sharedLibrary); 127 arguments.add(ext.getName()); 128 arguments.add(getOutputArguments(ext)); 129 arguments.add(getInputArguments(ext)); 130 131 mc.log().debug("Using pre-compiled evaluator for outputs: '" + getOutputArguments(ext) + "' and inputs: '" + getInputArguments(ext)); 132 } else { 133 executable = mc.compileExternal(ext); 134 } 55 135 if (ext.shouldCacheProcess()) { 56 ef = new MappedExternalFunction(ext, executable );136 ef = new MappedExternalFunction(ext, executable, arguments); 57 137 } else { 58 ef = new CompiledExternalFunction(ext, executable );138 ef = new CompiledExternalFunction(ext, executable, arguments); 59 139 } 60 140 time = System.currentTimeMillis() - time; … … 142 222 private class CompiledExternalFunction implements ExternalFunction<K, V> { 143 223 protected String executable; 224 protected ArrayList<String> arguments; 144 225 protected ProcessBuilder processBuilder; 145 226 private String msg; 146 227 147 public CompiledExternalFunction(External<K> ext, String executable ) {228 public CompiledExternalFunction(External<K> ext, String executable, ArrayList<String> arguments) { 148 229 this.executable = executable; 230 this.arguments = arguments; 149 231 this.processBuilder = createProcessBuilder(ext); 150 232 this.msg = "Succesfully compiled external function '" + ext.getName() + "'"; … … 189 271 public void evaluate(External<K> ext, Map<K, V> values, int timeout, ProcessCommunicator<V, T> com) 190 272 throws IOException { 273 long time = System.currentTimeMillis(); 274 log().debug("Evaluating external function: " + ext.getName()); 191 275 com.startTimer(timeout); 192 276 com.check("EVAL"); … … 201 285 com.accept("READY"); 202 286 com.cancelTimer(); 287 time = System.currentTimeMillis() - time; 288 log().debug("Finished evaluating external function, time: " + time + "ms"); 203 289 } 204 290 … … 220 306 @Override 221 307 public void remove() { 222 new File(executable).delete(); 308 if (arguments == null) { 309 new File(executable).delete(); 310 } 311 } 312 313 public ProcessBuilder _createProcessBuilder(External<K> ext) { 314 ProcessBuilder pb; 315 if (arguments == null) { 316 pb = new ProcessBuilder(executable); 317 } else { 318 pb = new ProcessBuilder(arguments); 319 } 320 return pb; 223 321 } 224 322 225 323 private ProcessBuilder createProcessBuilder(External<K> ext) { 226 ProcessBuilder pb = new ProcessBuilder(executable);324 ProcessBuilder pb = _createProcessBuilder(ext); 227 325 Map<String, String> env = pb.environment(); 228 326 if (env.keySet().contains("Path")) { … … 268 366 private final int externalConstantEvaluationMaxProc; 269 367 270 public MappedExternalFunction(External<K> ext, String executable ) {271 super(ext, executable );368 public MappedExternalFunction(External<K> ext, String executable, ArrayList<String> arguments) { 369 super(ext, executable, arguments); 272 370 externalConstantEvaluationMaxProc = ext.myOptions() 273 371 .getIntegerOption("external_constant_evaluation_max_proc"); -
branches/dev-cw-evaluator/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/common/evaluation/ExternalProcessMultiCache.java
r13252 r13437 34 34 35 35 public Iterable<K> functionArgsToSerialize(); 36 37 public String functionArgsSerialized(boolean returnVar); 36 38 37 39 public Iterable<K> varsToDeserialize(); 40 41 public String[] library(); 38 42 } 39 43 -
branches/dev-cw-evaluator/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/util/SystemUtil.java
r11394 r13437 72 72 return isLinux() ? "" : ".exe"; 73 73 } 74 75 public static String sharedLibraryExtension() { 76 if (isWindows()) { 77 return ".dll"; 78 } else if (isLinux()) { 79 return ".so"; 80 } else { 81 return ".dylib"; 82 } 83 } 74 84 75 85 /** -
branches/dev-cw-evaluator/Compiler/ModelicaFrontEnd/test/junit/org/jmodelica/test/common/ExternalProcessCacheTest.java
r13274 r13437 223 223 return null; 224 224 } 225 226 @Override 227 public String functionArgsSerialized(boolean returnVar) { 228 // TODO Auto-generated method stub 229 return null; 230 } 225 231 226 232 @Override … … 229 235 return null; 230 236 } 237 238 @Override 239 public String[] library() { 240 // TODO Auto-generated method stub 241 return null; 242 } 231 243 232 244 }
Note: See TracChangeset
for help on using the changeset viewer.