Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/08/16 20:48:54 (8 years ago)
Author:
abeham
Message:

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

  • added unit tests
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/DllImporter.cs

    r14234 r14244  
    2323using System.Runtime.InteropServices;
    2424
    25 namespace HeuristicLab.igraph {
     25namespace HeuristicLab.IGraph {
    2626  internal static class DllImporter {
    2727    private const string X86Dll = "igraph-0.8.0-pre-x86.dll";
    2828    private const string X64Dll = "igraph-0.8.0-pre-x64.dll";
    29     private readonly static bool X64 = false;
     29    private readonly static bool X86 = false;
    3030
    3131    static DllImporter() {
    32       X64 = Environment.Is64BitProcess;
     32      X86 = !Environment.Is64BitProcess;
    3333    }
    3434
     
    3636    #region igraph init/finalize
    3737    internal static void igraph_empty(igraph_t graph, int n, bool directed) {
    38       if (X64) igraph_empty_x64(graph, n, directed);
    39       else igraph_empty_x86(graph, n, directed);
     38      if (X86) igraph_empty_x86(graph, n, directed);
     39      else igraph_empty_x64(graph, n, directed);
    4040    }
    4141    internal static int igraph_destroy(igraph_t graph) {
    42       return X64 ? igraph_destroy_x64(graph) : igraph_destroy_x86(graph);
     42      return X86 ? igraph_destroy_x86(graph) : igraph_destroy_x64(graph);
    4343    }
    4444    #endregion
     
    4646    #region igraph query
    4747    internal static int igraph_vcount(igraph_t graph) {
    48       return X64 ? igraph_vcount_x64(graph) : igraph_vcount_x86(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;
    49105    }
    50106    #endregion
     
    52108    #region igraph manipulation
    53109    internal static int igraph_add_edge(igraph_t graph, int from, int to) {
    54       return X64 ? igraph_add_edge_x64(graph, from, to) : igraph_add_edge_x86(graph, from, to);
     110      return X86 ? igraph_add_edge_x86(graph, from, to) : igraph_add_edge_x64(graph, from, to);
    55111    }
    56112    #endregion
     
    73129    [DllImport(X64Dll, EntryPoint = "igraph_add_edge", CallingConvention = CallingConvention.Cdecl)]
    74130    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);
    75139    #endregion
    76140    #endregion
     
    78142    #region igraph_rng
    79143    internal static int igraph_rng_get_integer(int l, int h) {
    80       return X64 ? igraph_rng_get_integer_x64(igraph_rng_default_x64(), l, h) : igraph_rng_get_integer_x86(igraph_rng_default_x86(), l, 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);
    81145    }
    82146    internal static int igraph_rng_seed(uint seed) {
    83       return X64 ? igraph_rng_seed_x64(igraph_rng_default_x64(), seed) : igraph_rng_seed_x86(igraph_rng_default_x86(), seed);
     147      return X86 ? igraph_rng_seed_x86(igraph_rng_default_x86(), seed) : igraph_rng_seed_x64(igraph_rng_default_x64(), seed);
    84148    }
    85149
     
    100164    #endregion
    101165
     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
    102219    #region igraph_matrix
    103220    internal static int igraph_matrix_init(igraph_matrix_t matrix, int nrows, int ncols) {
    104       return X64 ? igraph_matrix_init_x64(matrix, nrows, ncols) : igraph_matrix_init_x86(matrix, nrows, ncols);
     221      return X86 ? igraph_matrix_init_x86(matrix, nrows, ncols) : igraph_matrix_init_x64(matrix, nrows, ncols);
    105222    }
    106223    internal static void igraph_matrix_destroy(igraph_matrix_t matrix) {
    107       if (X64) igraph_matrix_destroy_x64(matrix);
    108       else igraph_matrix_destroy_x86(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);
    109229    }
    110230
    111231    internal static double igraph_matrix_e(igraph_matrix_t matrix, int row, int col) {
    112       return X64 ? igraph_matrix_e_x64(matrix, row, col) : igraph_matrix_e_x86(matrix, row, col);
     232      return X86 ? igraph_matrix_e_x86(matrix, row, col) : igraph_matrix_e_x64(matrix, row, col);
    113233    }
    114234
    115235    internal static void igraph_matrix_set(igraph_matrix_t matrix, int row, int col, double value) {
    116       if (X64) igraph_matrix_set_x64(matrix, row, col, value);
    117       else igraph_matrix_set_x86(matrix, row, col, value);
     236      if (X86) igraph_matrix_set_x86(matrix, row, col, value);
     237      else igraph_matrix_set_x64(matrix, row, col, value);
    118238    }
    119239
     
    132252    private static extern double igraph_matrix_e_x64(igraph_matrix_t matrix, int row, int col);
    133253    [DllImport(X86Dll, EntryPoint = "igraph_matrix_set", CallingConvention = CallingConvention.Cdecl)]
    134     private static extern double igraph_matrix_set_x86(igraph_matrix_t matrix, int row, int col, double value);
     254    private static extern double igraph_matrix_set_x86([In, Out]igraph_matrix_t matrix, int row, int col, double value);
    135255    [DllImport(X64Dll, EntryPoint = "igraph_matrix_set", CallingConvention = CallingConvention.Cdecl)]
    136     private static extern double igraph_matrix_set_x64(igraph_matrix_t matrix, int row, int col, double value);
     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);
    137261    #endregion
    138262    #endregion
     
    140264    #region igraph_layout
    141265    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) {
    142       return X64 ? igraph_layout_fruchterman_reingold_x64(graph, res, use_seed, niter, start_temp, grid, weights, minx, maxx, miny, maxy) : igraph_layout_fruchterman_reingold_x86(graph, res, use_seed, niter, start_temp, grid, weights, minx, maxx, miny, 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);
    143273    }
    144274
     
    148278    [DllImport(X64Dll, EntryPoint = "igraph_layout_fruchterman_reingold", CallingConvention = CallingConvention.Cdecl)]
    149279    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);
    150     #endregion
    151     #endregion
    152 
     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
    153311  }
    154312}
Note: See TracChangeset for help on using the changeset viewer.