source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/DllImporter.cs @ 14244

Last change on this file since 14244 was 14244, checked in by abeham, 5 years ago

#2651: worked on igraph integration, additional layout algorithms, density, page rank

  • added unit tests
File size: 21.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Runtime.InteropServices;
24
25namespace HeuristicLab.IGraph {
26  internal static class DllImporter {
27    private const string X86Dll = "igraph-0.8.0-pre-x86.dll";
28    private const string X64Dll = "igraph-0.8.0-pre-x64.dll";
29    private readonly static bool X86 = false;
30
31    static DllImporter() {
32      X86 = !Environment.Is64BitProcess;
33    }
34
35    #region igraph
36    #region igraph init/finalize
37    internal static void igraph_empty(igraph_t graph, int n, bool directed) {
38      if (X86) igraph_empty_x86(graph, n, directed);
39      else igraph_empty_x64(graph, n, directed);
40    }
41    internal static int igraph_destroy(igraph_t graph) {
42      return X86 ? igraph_destroy_x86(graph) : igraph_destroy_x64(graph);
43    }
44    #endregion
45
46    #region igraph query
47    internal static int igraph_vcount(igraph_t graph) {
48      return X86 ? igraph_vcount_x86(graph) : igraph_vcount_x64(graph);
49    }
50    #endregion
51
52    #region igraph characteristics
53    internal static int igraph_density(igraph_t graph, out double density, bool loops) {
54      density = double.NaN;
55      return X86 ? igraph_density_x86(graph, ref density, loops) : igraph_density_x64(graph, ref density, loops);
56    }
57
58    internal static int igraph_pagerank(igraph_t graph, igraph_pagerank_algo_t algo, igraph_vector_t vector, out double value, igraph_vs_t vids, bool directed, double damping, igraph_vector_t weights) {
59      value = 1;
60      var options = IntPtr.Zero;
61      if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_ARPACK) {
62        var arpackoptions = GetDefaultArpackOptions();
63        options = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(igraph_arpack_options_t)));
64        Marshal.StructureToPtr(arpackoptions, options, false);
65      } else if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_POWER) {
66        var poweroptions = GetDefaultPowerOptions();
67        options = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(igraph_pagerank_power_options_t)));
68        Marshal.StructureToPtr(poweroptions, options, false);
69      }
70      try {
71        return X86 ? igraph_pagerank_x86(graph, algo, vector, ref value, vids, directed, damping, weights, options) : igraph_pagerank_x64(graph, algo, vector, ref value, vids, directed, damping, weights, options);
72      } finally {
73        if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_ARPACK) {
74          Marshal.DestroyStructure(options, typeof(igraph_arpack_options_t));
75          Marshal.FreeHGlobal(options);
76        } else if (algo == igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_POWER) {
77          Marshal.DestroyStructure(options, typeof(igraph_pagerank_power_options_t));
78          Marshal.FreeHGlobal(options);
79        }
80      }
81    }
82
83    unsafe private static igraph_arpack_options_t GetDefaultArpackOptions() {
84      var arpackoptions = new igraph_arpack_options_t();
85      arpackoptions.bmat[0] = 'I';
86      arpackoptions.which[0] = arpackoptions.which[1] = 'X';
87      arpackoptions.nev = 1;
88      arpackoptions.ishift = 1;
89      arpackoptions.mxiter = 3000;
90      arpackoptions.nb = 1;
91      arpackoptions.mode = 1;
92      arpackoptions.iparam[0] = arpackoptions.ishift;
93      arpackoptions.iparam[1] = arpackoptions.iparam[4] = arpackoptions.iparam[5] = arpackoptions.iparam[7] = arpackoptions.iparam[8] = arpackoptions.iparam[9] = arpackoptions.iparam[10] = 0;
94      arpackoptions.iparam[2] = arpackoptions.mxiter;
95      arpackoptions.iparam[3] = arpackoptions.nb;
96      arpackoptions.iparam[6] = arpackoptions.mode;
97      return arpackoptions;
98    }
99
100    private static igraph_pagerank_power_options_t GetDefaultPowerOptions() {
101      var poweroptions = new igraph_pagerank_power_options_t();
102      poweroptions.niter = 50;
103      poweroptions.eps = 1e-5;
104      return poweroptions;
105    }
106    #endregion
107
108    #region igraph manipulation
109    internal static int igraph_add_edge(igraph_t graph, int from, int to) {
110      return X86 ? igraph_add_edge_x86(graph, from, to) : igraph_add_edge_x64(graph, from, to);
111    }
112    #endregion
113
114    #region Platform specific DLL imports
115    [DllImport(X86Dll, EntryPoint = "igraph_empty", CallingConvention = CallingConvention.Cdecl)]
116    private static extern void igraph_empty_x86([In, Out]igraph_t graph, int n, bool directed);
117    [DllImport(X64Dll, EntryPoint = "igraph_empty", CallingConvention = CallingConvention.Cdecl)]
118    private static extern void igraph_empty_x64([In, Out]igraph_t graph, int n, bool directed);
119    [DllImport(X86Dll, EntryPoint = "igraph_destroy", CallingConvention = CallingConvention.Cdecl)]
120    private static extern int igraph_destroy_x86([In, Out]igraph_t graph);
121    [DllImport(X64Dll, EntryPoint = "igraph_destroy", CallingConvention = CallingConvention.Cdecl)]
122    private static extern int igraph_destroy_x64([In, Out]igraph_t graph);
123    [DllImport(X86Dll, EntryPoint = "igraph_vcount", CallingConvention = CallingConvention.Cdecl)]
124    private static extern int igraph_vcount_x86(igraph_t graph);
125    [DllImport(X64Dll, EntryPoint = "igraph_vcount", CallingConvention = CallingConvention.Cdecl)]
126    private static extern int igraph_vcount_x64(igraph_t graph);
127    [DllImport(X86Dll, EntryPoint = "igraph_add_edge", CallingConvention = CallingConvention.Cdecl)]
128    private static extern int igraph_add_edge_x86([In, Out]igraph_t graph, int from, int to);
129    [DllImport(X64Dll, EntryPoint = "igraph_add_edge", CallingConvention = CallingConvention.Cdecl)]
130    private static extern int igraph_add_edge_x64([In, Out]igraph_t graph, int from, int to);
131    [DllImport(X86Dll, EntryPoint = "igraph_density", CallingConvention = CallingConvention.Cdecl)]
132    private static extern int igraph_density_x86(igraph_t graph, ref double density, bool loops);
133    [DllImport(X64Dll, EntryPoint = "igraph_density", CallingConvention = CallingConvention.Cdecl)]
134    private static extern int igraph_density_x64(igraph_t graph, ref double density, bool loops);
135    [DllImport(X86Dll, EntryPoint = "igraph_pagerank", CallingConvention = CallingConvention.Cdecl)]
136    private static extern int igraph_pagerank_x86(igraph_t graph, igraph_pagerank_algo_t algo, [In, Out]igraph_vector_t vector, ref double value, igraph_vs_t vids, bool directed, double damping, igraph_vector_t weights, IntPtr options);
137    [DllImport(X64Dll, EntryPoint = "igraph_pagerank", CallingConvention = CallingConvention.Cdecl)]
138    private static extern int igraph_pagerank_x64(igraph_t graph, igraph_pagerank_algo_t algo, [In, Out]igraph_vector_t vector, ref double value, igraph_vs_t vids, bool directed, double damping, igraph_vector_t weights, IntPtr options);
139    #endregion
140    #endregion
141
142    #region igraph_rng
143    internal static int igraph_rng_get_integer(int l, int h) {
144      return X86 ? igraph_rng_get_integer_x86(igraph_rng_default_x86(), l, h) : igraph_rng_get_integer_x64(igraph_rng_default_x64(), l, h);
145    }
146    internal static int igraph_rng_seed(uint seed) {
147      return X86 ? igraph_rng_seed_x86(igraph_rng_default_x86(), seed) : igraph_rng_seed_x64(igraph_rng_default_x64(), seed);
148    }
149
150    #region Platform specific DLL imports
151    [DllImport(X86Dll, EntryPoint = "igraph_rng_default", CallingConvention = CallingConvention.Cdecl)]
152    private static extern IntPtr igraph_rng_default_x86();
153    [DllImport(X64Dll, EntryPoint = "igraph_rng_default", CallingConvention = CallingConvention.Cdecl)]
154    private static extern IntPtr igraph_rng_default_x64();
155    [DllImport(X86Dll, EntryPoint = "igraph_rng_get_integer", CallingConvention = CallingConvention.Cdecl)]
156    private static extern int igraph_rng_get_integer_x86(IntPtr rng, int l, int h);
157    [DllImport(X64Dll, EntryPoint = "igraph_rng_get_integer", CallingConvention = CallingConvention.Cdecl)]
158    private static extern int igraph_rng_get_integer_x64(IntPtr rng, int l, int h);
159    [DllImport(X86Dll, EntryPoint = "igraph_rng_seed", CallingConvention = CallingConvention.Cdecl)]
160    private static extern int igraph_rng_seed_x86(IntPtr rng, uint seed);
161    [DllImport(X64Dll, EntryPoint = "igraph_rng_seed", CallingConvention = CallingConvention.Cdecl)]
162    private static extern int igraph_rng_seed_x64(IntPtr rng, uint seed);
163    #endregion
164    #endregion
165
166    #region igraph_vector
167    internal static int igraph_vector_init(igraph_vector_t vector, int length) {
168      return X86 ? igraph_vector_init_x86(vector, length) : igraph_vector_init_x64(vector, length);
169    }
170    internal static void igraph_vector_destroy(igraph_vector_t vector) {
171      if (X86) igraph_vector_destroy_x86(vector);
172      else igraph_vector_destroy_x64(vector);
173    }
174    internal static int igraph_vector_copy(igraph_vector_t to, igraph_vector_t from) {
175      return X86 ? igraph_vector_copy_x86(to, from) : igraph_vector_copy_x64(to, from);
176    }
177
178    internal static int igraph_vector_size(igraph_vector_t vector) {
179      return X86 ? igraph_vector_size_x86(vector) : igraph_vector_size_x64(vector);
180    }
181
182    internal static double igraph_vector_e(igraph_vector_t vector, int index) {
183      return X86 ? igraph_vector_e_x86(vector, index) : igraph_vector_e_x64(vector, index);
184    }
185
186    internal static void igraph_vector_set(igraph_vector_t vector, int index, double value) {
187      if (X86) igraph_vector_set_x86(vector, index, value);
188      else igraph_vector_set_x64(vector, index, value);
189    }
190
191    #region Platform specific DLL imports
192    [DllImport(X86Dll, EntryPoint = "igraph_vector_init", CallingConvention = CallingConvention.Cdecl)]
193    private static extern int igraph_vector_init_x86([In, Out]igraph_vector_t vector, int length);
194    [DllImport(X64Dll, EntryPoint = "igraph_vector_init", CallingConvention = CallingConvention.Cdecl)]
195    private static extern int igraph_vector_init_x64([In, Out]igraph_vector_t vector, int length);
196    [DllImport(X86Dll, EntryPoint = "igraph_vector_destroy", CallingConvention = CallingConvention.Cdecl)]
197    private static extern void igraph_vector_destroy_x86([In, Out]igraph_vector_t vector);
198    [DllImport(X64Dll, EntryPoint = "igraph_vector_destroy", CallingConvention = CallingConvention.Cdecl)]
199    private static extern void igraph_vector_destroy_x64([In, Out]igraph_vector_t vector);
200    [DllImport(X86Dll, EntryPoint = "igraph_vector_size", CallingConvention = CallingConvention.Cdecl)]
201    private static extern int igraph_vector_size_x86(igraph_vector_t vector);
202    [DllImport(X64Dll, EntryPoint = "igraph_vector_size", CallingConvention = CallingConvention.Cdecl)]
203    private static extern int igraph_vector_size_x64(igraph_vector_t vector);
204    [DllImport(X86Dll, EntryPoint = "igraph_vector_e", CallingConvention = CallingConvention.Cdecl)]
205    private static extern double igraph_vector_e_x86(igraph_vector_t vector, int index);
206    [DllImport(X64Dll, EntryPoint = "igraph_vector_e", CallingConvention = CallingConvention.Cdecl)]
207    private static extern double igraph_vector_e_x64(igraph_vector_t vector, int index);
208    [DllImport(X86Dll, EntryPoint = "igraph_vector_set", CallingConvention = CallingConvention.Cdecl)]
209    private static extern double igraph_vector_set_x86([In, Out]igraph_vector_t vector, int index, double value);
210    [DllImport(X64Dll, EntryPoint = "igraph_vector_set", CallingConvention = CallingConvention.Cdecl)]
211    private static extern double igraph_vector_set_x64([In, Out]igraph_vector_t vector, int index, double value);
212    [DllImport(X86Dll, EntryPoint = "igraph_vector_copy", CallingConvention = CallingConvention.Cdecl)]
213    private static extern int igraph_vector_copy_x86([In, Out]igraph_vector_t to, igraph_vector_t from);
214    [DllImport(X64Dll, EntryPoint = "igraph_vector_copy", CallingConvention = CallingConvention.Cdecl)]
215    private static extern int igraph_vector_copy_x64([In, Out]igraph_vector_t to, igraph_vector_t from);
216    #endregion
217    #endregion
218
219    #region igraph_matrix
220    internal static int igraph_matrix_init(igraph_matrix_t matrix, int nrows, int ncols) {
221      return X86 ? igraph_matrix_init_x86(matrix, nrows, ncols) : igraph_matrix_init_x64(matrix, nrows, ncols);
222    }
223    internal static void igraph_matrix_destroy(igraph_matrix_t matrix) {
224      if (X86) igraph_matrix_destroy_x86(matrix);
225      else igraph_matrix_destroy_x64(matrix);
226    }
227    internal static int igraph_matrix_copy(igraph_matrix_t to, igraph_matrix_t from) {
228      return X86 ? igraph_matrix_copy_x86(to, from) : igraph_matrix_copy_x64(to, from);
229    }
230
231    internal static double igraph_matrix_e(igraph_matrix_t matrix, int row, int col) {
232      return X86 ? igraph_matrix_e_x86(matrix, row, col) : igraph_matrix_e_x64(matrix, row, col);
233    }
234
235    internal static void igraph_matrix_set(igraph_matrix_t matrix, int row, int col, double value) {
236      if (X86) igraph_matrix_set_x86(matrix, row, col, value);
237      else igraph_matrix_set_x64(matrix, row, col, value);
238    }
239
240    #region Platform specific DLL imports
241    [DllImport(X86Dll, EntryPoint = "igraph_matrix_init", CallingConvention = CallingConvention.Cdecl)]
242    private static extern int igraph_matrix_init_x86([In, Out]igraph_matrix_t matrix, int nrow, int ncol);
243    [DllImport(X64Dll, EntryPoint = "igraph_matrix_init", CallingConvention = CallingConvention.Cdecl)]
244    private static extern int igraph_matrix_init_x64([In, Out]igraph_matrix_t matrix, int nrow, int ncol);
245    [DllImport(X86Dll, EntryPoint = "igraph_matrix_destroy", CallingConvention = CallingConvention.Cdecl)]
246    private static extern void igraph_matrix_destroy_x86([In, Out]igraph_matrix_t matrix);
247    [DllImport(X64Dll, EntryPoint = "igraph_matrix_destroy", CallingConvention = CallingConvention.Cdecl)]
248    private static extern void igraph_matrix_destroy_x64([In, Out]igraph_matrix_t matrix);
249    [DllImport(X86Dll, EntryPoint = "igraph_matrix_e", CallingConvention = CallingConvention.Cdecl)]
250    private static extern double igraph_matrix_e_x86(igraph_matrix_t matrix, int row, int col);
251    [DllImport(X64Dll, EntryPoint = "igraph_matrix_e", CallingConvention = CallingConvention.Cdecl)]
252    private static extern double igraph_matrix_e_x64(igraph_matrix_t matrix, int row, int col);
253    [DllImport(X86Dll, EntryPoint = "igraph_matrix_set", CallingConvention = CallingConvention.Cdecl)]
254    private static extern double igraph_matrix_set_x86([In, Out]igraph_matrix_t matrix, int row, int col, double value);
255    [DllImport(X64Dll, EntryPoint = "igraph_matrix_set", CallingConvention = CallingConvention.Cdecl)]
256    private static extern double igraph_matrix_set_x64([In, Out]igraph_matrix_t matrix, int row, int col, double value);
257    [DllImport(X86Dll, EntryPoint = "igraph_matrix_copy", CallingConvention = CallingConvention.Cdecl)]
258    private static extern int igraph_matrix_copy_x86([In, Out]igraph_matrix_t to, igraph_matrix_t from);
259    [DllImport(X64Dll, EntryPoint = "igraph_matrix_copy", CallingConvention = CallingConvention.Cdecl)]
260    private static extern int igraph_matrix_copy_x64([In, Out]igraph_matrix_t to, igraph_matrix_t from);
261    #endregion
262    #endregion
263
264    #region igraph_layout
265    internal static int igraph_layout_fruchterman_reingold(igraph_t graph, igraph_matrix_t res, bool use_seed, int niter, double start_temp, igraph_layout_grid_t grid, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy) {
266      return X86 ? igraph_layout_fruchterman_reingold_x86(graph, res, use_seed, niter, start_temp, grid, weights, minx, maxx, miny, maxy) : igraph_layout_fruchterman_reingold_x64(graph, res, use_seed, niter, start_temp, grid, weights, minx, maxx, miny, maxy);
267    }
268    internal static int igraph_layout_kamada_kawai(igraph_t graph, igraph_matrix_t res, bool use_seed, int maxiter, double epsilon, double kkconst, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy) {
269      return X86 ? igraph_layout_kamada_kawai_x86(graph, res, use_seed, maxiter, epsilon, kkconst, weights, minx, maxx, miny, maxy) : igraph_layout_kamada_kawai_x64(graph, res, use_seed, maxiter, epsilon, kkconst, weights, minx, maxx, miny, maxy);
270    }
271    internal static int igraph_layout_davidson_harel(igraph_t graph, igraph_matrix_t res, bool use_seed, int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist) {
272      return X86 ? igraph_layout_davidson_harel_x86(graph, res, use_seed, maxiter, fineiter, cool_fact, weight_node_dist, weight_border, weight_edge_lengths, weight_edge_crossings, weight_node_edge_dist) : igraph_layout_davidson_harel_x64(graph, res, use_seed, maxiter, fineiter, cool_fact, weight_node_dist, weight_border, weight_edge_lengths, weight_edge_crossings, weight_node_edge_dist);
273    }
274
275    #region Platform specific DLL imports
276    [DllImport(X86Dll, EntryPoint = "igraph_layout_fruchterman_reingold", CallingConvention = CallingConvention.Cdecl)]
277    private static extern int igraph_layout_fruchterman_reingold_x86(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int niter, double start_temp, igraph_layout_grid_t grid, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
278    [DllImport(X64Dll, EntryPoint = "igraph_layout_fruchterman_reingold", CallingConvention = CallingConvention.Cdecl)]
279    private static extern int igraph_layout_fruchterman_reingold_x64(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int niter, double start_temp, igraph_layout_grid_t grid, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
280    [DllImport(X86Dll, EntryPoint = "igraph_layout_kamada_kawai", CallingConvention = CallingConvention.Cdecl)]
281    private static extern int igraph_layout_kamada_kawai_x86(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, double epsilon, double kkconst, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
282    [DllImport(X64Dll, EntryPoint = "igraph_layout_kamada_kawai", CallingConvention = CallingConvention.Cdecl)]
283    private static extern int igraph_layout_kamada_kawai_x64(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, double epsilon, double kkconst, igraph_vector_t weights, igraph_vector_t minx, igraph_vector_t maxx, igraph_vector_t miny, igraph_vector_t maxy);
284    [DllImport(X86Dll, EntryPoint = "igraph_layout_davidson_harel", CallingConvention = CallingConvention.Cdecl)]
285    private static extern int igraph_layout_davidson_harel_x86(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist);
286    [DllImport(X64Dll, EntryPoint = "igraph_layout_davidson_harel", CallingConvention = CallingConvention.Cdecl)]
287    private static extern int igraph_layout_davidson_harel_x64(igraph_t graph, [In, Out]igraph_matrix_t res, bool use_seed, int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist);
288    #endregion
289    #endregion
290
291    #region igraph_iterators
292    internal static int igraph_vs_all(ref igraph_vs_t vs) {
293      return X86 ? igraph_vs_all_x86(ref vs) : igraph_vs_all_x64(ref vs);
294    }
295    internal static void igraph_vs_destroy(ref igraph_vs_t vs) {
296      if (X86) igraph_vs_destroy_x86(ref vs);
297      else igraph_vs_destroy_x64(ref vs);
298    }
299
300    #region Platform specific DLL imports
301    [DllImport(X86Dll, EntryPoint = "igraph_vs_all", CallingConvention = CallingConvention.Cdecl)]
302    private static extern int igraph_vs_all_x86([In, Out]ref igraph_vs_t vs);
303    [DllImport(X64Dll, EntryPoint = "igraph_vs_all", CallingConvention = CallingConvention.Cdecl)]
304    private static extern int igraph_vs_all_x64([In, Out]ref igraph_vs_t vs);
305    [DllImport(X86Dll, EntryPoint = "igraph_vs_destroy", CallingConvention = CallingConvention.Cdecl)]
306    private static extern void igraph_vs_destroy_x86([In, Out]ref igraph_vs_t vs);
307    [DllImport(X64Dll, EntryPoint = "igraph_vs_destroy", CallingConvention = CallingConvention.Cdecl)]
308    private static extern void igraph_vs_destroy_x64([In, Out]ref igraph_vs_t vs);
309    #endregion
310    #endregion
311  }
312}
Note: See TracBrowser for help on using the repository browser.