Changeset 17989 for branches/3087_Ceres_Integration/HeuristicLab.ExtLibs/HeuristicLab.NativeInterpreter/0.1/HeuristicLab.NativeInterpreter-0.1/DllImporter.cs
- Timestamp:
- 06/16/21 21:15:16 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3087_Ceres_Integration/HeuristicLab.ExtLibs/HeuristicLab.NativeInterpreter/0.1/HeuristicLab.NativeInterpreter-0.1/DllImporter.cs
r17844 r17989 1 1 using System; 2 using System.IO;3 using System.IO.Compression;4 2 using System.Runtime.InteropServices; 5 3 6 namespace HeuristicLab. Problems.DataAnalysis.Symbolic{4 namespace HeuristicLab.NativeInterpreter { 7 5 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 8 6 public struct NativeInstruction { … … 10 8 public ushort Arity; 11 9 public ushort Length; 12 public double Value; 13 public IntPtr Data; 14 public bool Optimize; 10 public double Value; // weights for variables, values for parameters 11 public IntPtr Data; // used for variables only 12 public bool Optimize; // used for parameters only 15 13 } 16 14 17 // proxy structure to pass information from Ceres back to the caller 15 public enum Algorithm : int { 16 Krogh = 0, 17 RuheWedin1 = 1, 18 RuheWedin2 = 2, 19 RuheWedin3 = 3 20 } 21 18 22 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 19 public struct OptimizationSummary { 23 public class SolverOptions { 24 public int Iterations = 10; 25 public int UseNonmonotonicSteps = 0; // = false 26 public CeresTypes.MinimizerType Minimizer = CeresTypes.MinimizerType.TRUST_REGION; 27 public CeresTypes.LinearSolverType LinearSolver = CeresTypes.LinearSolverType.DENSE_QR; 28 public CeresTypes.TrustRegionStrategyType TrustRegionStrategy = CeresTypes.TrustRegionStrategyType.LEVENBERG_MARQUARDT; 29 public CeresTypes.DoglegType Dogleg = CeresTypes.DoglegType.TRADITIONAL_DOGLEG; 30 public CeresTypes.LineSearchDirectionType LineSearchDirection = CeresTypes.LineSearchDirectionType.LBFGS; 31 public Algorithm Algorithm = Algorithm.Krogh; 32 } 33 34 // proxy structure to pass information from ceres back to the caller 35 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 36 public class OptimizationSummary { 20 37 public double InitialCost; // value of the objective function before the optimization 21 38 public double FinalCost; // value of the objective function after the optimization … … 25 42 public int ResidualEvaluations; // number of residual only evaluations 26 43 public int JacobianEvaluations; // number of Jacobian (and residual) evaluations 27 };28 29 public enum MinimizerType : int {30 LINE_SEARCH = 0,31 TRUST_REGION = 132 44 } 33 45 34 public enum LinearSolverType : int {35 DENSE_NORMAL_CHOLESKY = 0,36 DENSE_QR = 1,37 SPARSE_NORMAL_CHOLESKY = 2,38 DENSE_SCHUR = 3,39 SPARSE_SCHUR = 4,40 ITERATIVE_SCHUR = 5,41 CGNR = 642 }43 44 public enum TrustRegionStrategyType : int {45 LEVENBERG_MARQUARDT = 0,46 DOGLEG = 147 }48 49 public enum DoglegType : int {50 TRADITIONAL_DOGLEG = 0,51 SUBSPACE_DOGLEG = 152 }53 54 public enum LineSearchDirectionType : int {55 STEEPEST_DESCENT = 0,56 NONLINEAR_CONJUGATE_GRADIENT = 1,57 LBFGS = 2,58 BFSG = 359 }60 61 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]62 public struct SolverOptions {63 public int Iterations;64 public int UseNonmonotonicSteps;65 public int Minimizer; // type of minimizer (trust region or line search)66 public int LinearSolver; // type of linear solver67 public int TrustRegionStrategy; // levenberg-marquardt or dogleg68 public int Dogleg; // type of dogleg (traditional or subspace)69 public int LineSearchDirection; // line search direction type (BFGS, LBFGS, etc)70 public int Algorithm;71 };72 73 46 public static class NativeWrapper { 74 private const string x86zip = "hl-native-interpreter-x86.zip";75 private const string x64zip = "hl-native-interpreter-x64.zip";76 77 private const string x86dll = "hl-native-interpreter-x86.dll";78 47 private const string x64dll = "hl-native-interpreter-x64.dll"; 79 80 48 private readonly static bool is64; 81 49 82 50 static NativeWrapper() { 83 51 is64 = Environment.Is64BitProcess; 84 85 //using (var zip = new FileStream(is64 ? x64zip : x86zip, FileMode.Open)) {86 // using (var archive = new ZipArchive(zip, ZipArchiveMode.Read)) {87 // foreach (var entry in archive.Entries) {88 // if (File.Exists(entry.Name)) {89 // File.Delete(entry.Name);90 // }91 // }92 // ZipFileExtensions.ExtractToDirectory(archive, Environment.CurrentDirectory);93 // }94 //}95 52 } 96 53 97 public static void GetValues(NativeInstruction[] code, int[] rows, double[] result, double[] target, SolverOptions options, ref OptimizationSummary summary) { 54 public static void GetValues(NativeInstruction[] code, int[] rows, SolverOptions options, double[] result, double[] target, out OptimizationSummary optSummary) { 55 optSummary = new OptimizationSummary(); 98 56 if (is64) 99 GetValues64(code, code.Length, rows, rows.Length, options, result, target, ref summary);57 GetValues64(code, code.Length, rows, rows.Length, options, result, target, optSummary); 100 58 else 101 GetValues32(code, code.Length, rows, rows.Length, options, result, target, ref summary); 59 throw new NotSupportedException("Native interpreter is only available on x64 builds"); 60 } 61 public static void GetValuesVarPro(NativeInstruction[] code, int[] termIndices, int[] rows, double[] coefficients, SolverOptions options, double[] result, double[] target, out OptimizationSummary optSummary) { 62 optSummary = new OptimizationSummary(); 63 if (is64) 64 GetValuesVarPro64(code, code.Length, termIndices, termIndices.Length, rows, rows.Length, coefficients, options, result, target, optSummary); 65 else 66 throw new NotSupportedException("Native interpreter is only available on x64 builds"); 102 67 } 103 68 104 public static void GetValuesVarPro(NativeInstruction[] code, int[] termIndices, int[] rows, double[] coefficients,105 double[] result, double[] target, SolverOptions options, ref OptimizationSummary summary) {106 if (is64)107 GetValuesVarPro64(code, code.Length, termIndices, termIndices.Length, rows, rows.Length, coefficients, options, result, target, ref summary);108 else109 GetValuesVarPro32(code, code.Length, termIndices, termIndices.Length, rows, rows.Length, coefficients, options, result, target, ref summary);110 }111 112 // x86113 [DllImport(x86dll, EntryPoint = "GetValues", CallingConvention = CallingConvention.Cdecl)]114 internal static extern void GetValues32([In, Out] NativeInstruction[] code, int len, int[] rows, int nRows, SolverOptions options, [In, Out] double[] result, double[] target, ref OptimizationSummary summary);115 116 // x64117 69 [DllImport(x64dll, EntryPoint = "GetValues", CallingConvention = CallingConvention.Cdecl)] 118 internal static extern void GetValues64([In, Out] NativeInstruction[] code, int len, int[] rows, int nRows, SolverOptions options, [In, Out] double[] result, double[] target, ref OptimizationSummary summary); 119 120 [DllImport(x86dll, EntryPoint = "GetValuesVarPro", CallingConvention = CallingConvention.Cdecl)] 121 internal static extern void GetValuesVarPro32([In, Out] NativeInstruction[] code, int len, int[] termIndices, int nTerms, int[] rows, int nRows, [In, Out] double[] coefficients, SolverOptions options, [In, Out] double[] result, double[] target, ref OptimizationSummary summary); 70 internal static extern void GetValues64( 71 [In,Out] NativeInstruction[] code, // parameters are optimized by callee 72 [In] int len, 73 [In] int[] rows, 74 [In] int nRows, 75 [In] SolverOptions options, 76 [Out] double[] result, 77 [In] double[] target, 78 [Out] OptimizationSummary optSummary); 122 79 123 80 [DllImport(x64dll, EntryPoint = "GetValuesVarPro", CallingConvention = CallingConvention.Cdecl)] 124 internal static extern void GetValuesVarPro64([In, Out] NativeInstruction[] code, int len, int[] termIndices, int nTerms, int[] rows, int nRows, [In, Out] double[] coefficients, SolverOptions options, [In, Out] double[] result, double[] target, ref OptimizationSummary summary); 81 internal static extern void GetValuesVarPro64( 82 [In,Out] NativeInstruction[] code, // the values fields for non-linear parameters are changed by the callee 83 int len, 84 int[] termIndices, 85 int nTerms, 86 int[] rows, 87 int nRows, 88 [In,Out] double[] coefficients, 89 [In] SolverOptions options, 90 [In, Out] double[] result, 91 [In] double[] target, 92 [In,Out] OptimizationSummary optSummary); 125 93 } 126 94 }
Note: See TracChangeset
for help on using the changeset viewer.