Changeset 17844 for branches/3087_Ceres_Integration/HeuristicLab.ExtLibs/HeuristicLab.NativeInterpreter/0.1/HeuristicLab.NativeInterpreter-0.1/DllImporter.cs
- Timestamp:
- 02/24/21 22:22:21 (4 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
r16333 r17844 1 1 using System; 2 using System.IO; 3 using System.IO.Compression; 2 4 using System.Runtime.InteropServices; 3 5 … … 5 7 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 6 8 public struct NativeInstruction { 7 public byte opcode; 8 public ushort narg; 9 public int childIndex; 10 public double value; 11 public double weight; 12 13 public IntPtr buf; 14 public IntPtr data; 9 public byte OpCode; 10 public ushort Arity; 11 public ushort Length; 12 public double Value; 13 public IntPtr Data; 14 public bool Optimize; 15 15 } 16 16 17 // proxy structure to pass information from Ceres back to the caller 18 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 19 public struct OptimizationSummary { 20 public double InitialCost; // value of the objective function before the optimization 21 public double FinalCost; // value of the objective function after the optimization 22 public int SuccessfulSteps; // number of minimizer iterations in which the step was accepted 23 public int UnsuccessfulSteps; // number of minimizer iterations in which the step was rejected 24 public int InnerIterationSteps; // number of times inner iterations were performed 25 public int ResidualEvaluations; // number of residual only evaluations 26 public int JacobianEvaluations; // number of Jacobian (and residual) evaluations 27 }; 28 29 public enum MinimizerType : int { 30 LINE_SEARCH = 0, 31 TRUST_REGION = 1 32 } 33 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 = 6 42 } 43 44 public enum TrustRegionStrategyType : int { 45 LEVENBERG_MARQUARDT = 0, 46 DOGLEG = 1 47 } 48 49 public enum DoglegType : int { 50 TRADITIONAL_DOGLEG = 0, 51 SUBSPACE_DOGLEG = 1 52 } 53 54 public enum LineSearchDirectionType : int { 55 STEEPEST_DESCENT = 0, 56 NONLINEAR_CONJUGATE_GRADIENT = 1, 57 LBFGS = 2, 58 BFSG = 3 59 } 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 solver 67 public int TrustRegionStrategy; // levenberg-marquardt or dogleg 68 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 17 73 public static class NativeWrapper { 18 private const string x86dll = "hl-native-interpreter-msvc-x86.dll"; 19 private const string x64dll = "hl-native-interpreter-msvc-x64.dll"; 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 private const string x64dll = "hl-native-interpreter-x64.dll"; 20 79 21 80 private readonly static bool is64; … … 23 82 static NativeWrapper() { 24 83 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 //} 25 95 } 26 96 27 public static double GetValue(NativeInstruction[] code, int len, int row) { 28 return is64 ? GetValue64(code, len, row) : GetValue32(code, len, row); 97 public static void GetValues(NativeInstruction[] code, int[] rows, double[] result, double[] target, SolverOptions options, ref OptimizationSummary summary) { 98 if (is64) 99 GetValues64(code, code.Length, rows, rows.Length, options, result, target, ref summary); 100 else 101 GetValues32(code, code.Length, rows, rows.Length, options, result, target, ref summary); 29 102 } 30 103 31 public static void GetValues(NativeInstruction[] code, int len, int[] rows, int nRows, double[] result) { 104 public static void GetValuesVarPro(NativeInstruction[] code, int[] termIndices, int[] rows, double[] coefficients, 105 double[] result, double[] target, SolverOptions options, ref OptimizationSummary summary) { 32 106 if (is64) 33 GetValues 64(code, len, rows, nRows, result);107 GetValuesVarPro64(code, code.Length, termIndices, termIndices.Length, rows, rows.Length, coefficients, options, result, target, ref summary); 34 108 else 35 GetValues32(code, len, rows, nRows, result); 36 } 37 38 public static void GetValuesVectorized(NativeInstruction[] code, int len, int[] rows, int nRows, double[] result) { 39 if (is64) 40 GetValuesVectorized64(code, len, rows, nRows, result); 41 else 42 GetValuesVectorized32(code, len, rows, nRows, result); 109 GetValuesVarPro32(code, code.Length, termIndices, termIndices.Length, rows, rows.Length, coefficients, options, result, target, ref summary); 43 110 } 44 111 45 112 // x86 46 [DllImport(x86dll, EntryPoint = "GetValue", CallingConvention = CallingConvention.Cdecl)]47 internal static extern double GetValue32(NativeInstruction[] code, int len, int row);48 49 113 [DllImport(x86dll, EntryPoint = "GetValues", CallingConvention = CallingConvention.Cdecl)] 50 internal static extern void GetValues32(NativeInstruction[] code, int len, int[] rows, int nRows, double[] result); 51 52 [DllImport(x86dll, EntryPoint = "GetValuesVectorized", CallingConvention = CallingConvention.Cdecl)] 53 internal static extern void GetValuesVectorized32(NativeInstruction[] code, int len, int[] rows, int nRows, double[] result); 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); 54 115 55 116 // x64 56 [DllImport(x64dll, EntryPoint = "GetValue ", CallingConvention = CallingConvention.Cdecl)]57 internal static extern double GetValue64(NativeInstruction[] code, int len, int row);117 [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); 58 119 59 [DllImport(x 64dll, EntryPoint = "GetValues", CallingConvention = CallingConvention.Cdecl)]60 internal static extern void GetValues 64(NativeInstruction[] code, int len, int[] rows, int nRows, double[] result);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); 61 122 62 [DllImport(x64dll, EntryPoint = "GetValuesV ectorized", CallingConvention = CallingConvention.Cdecl)]63 internal static extern void GetValuesV ectorized64(NativeInstruction[] code, int len, int[] rows, int nRows, double[] result);123 [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); 64 125 } 65 126 }
Note: See TracChangeset
for help on using the changeset viewer.