This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgraph_utils.swig
163 lines (114 loc) · 3.99 KB
/
graph_utils.swig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// -*- C++ -*-
/* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
// This is a SWIG wrapper for CSGMatrix.h
%module graph_utils;
%include <std_string.i>
typedef int int32_t;
typedef unsigned long uint64_t;
/*******************************************************************
* Copied verbatim to wrapper
*******************************************************************/
%{
#include <omp.h>
#include "CSRMatrix.h"
%}
/*******************************************************************
* Parse headers
*******************************************************************/
%newobject read_CSRMatrix;
%newobject CSRMatrix::slice_columns;
%newobject CSRMatrix::slice_nonzeros;
%newobject CSRMatrix::transpose;
%newobject CSRMatrix::point_op;
%newobject CSRMatrix::eliminate_zeros;
%newobject mul_matmat;
%include "CSRMatrix.h"
void *memcpy(void *dest, const void *src, size_t n);
/*******************************************************************
* A way of providing float* pointers and friends to the functions
* that require it (Lua version)
*******************************************************************/
long *dummy_long();
%{
#define SWIG_FILE_WITH_INIT
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
PyObject *swig_ptr (PyObject *a)
{
if(!PyArray_Check(a)) {
PyErr_SetString(PyExc_ValueError, "input not a numpy array");
return NULL;
}
PyArrayObject *ao = (PyArrayObject *)a;
if(!PyArray_ISCONTIGUOUS(ao)) {
PyErr_SetString(PyExc_ValueError, "array is not C-contiguous");
return NULL;
}
void * data = PyArray_DATA(ao);
if(PyArray_TYPE(ao) == NPY_FLOAT32) {
return SWIG_NewPointerObj(data, SWIGTYPE_p_float, 0);
}
if(PyArray_TYPE(ao) == NPY_INT32) {
return SWIG_NewPointerObj(data, SWIGTYPE_p_int, 0);
}
if(PyArray_TYPE(ao) == NPY_UINT64) {
return SWIG_NewPointerObj(data, SWIGTYPE_p_unsigned_long, 0);
}
if(PyArray_TYPE(ao) == NPY_INT64) {
return SWIG_NewPointerObj(data, SWIGTYPE_p_long, 0);
}
PyErr_SetString(PyExc_ValueError, "did not recognize array type");
return NULL;
}
long *dummy_long() {
return NULL;
}
const char * swig_ptr_typename (PyObject *a) {
void *outptr;
if(SWIG_IsOK(SWIG_ConvertPtr(a, &outptr, SWIGTYPE_p_float, 0 | 0 )))
return "float32";
if(SWIG_IsOK(SWIG_ConvertPtr(a, &outptr, SWIGTYPE_p_long, 0 | 0 )))
return "int64";
if(SWIG_IsOK(SWIG_ConvertPtr(a, &outptr, SWIGTYPE_p_unsigned_long, 0 | 0 )))
return "int64";
if(SWIG_IsOK(SWIG_ConvertPtr(a, &outptr, SWIGTYPE_p_int, 0 | 0 )))
return "int32";
return "unknown";
}
%}
%init %{
/* needed, else crash at runtime */
import_array();
%}
// return a pointer usable as input for functions that expect pointers
PyObject *swig_ptr (PyObject *a);
const char * swig_ptr_typename (PyObject *a);
// return a pointer usable as input for functions that expect pointers
PyObject *swig_ptr (PyObject *a);
%define REV_SWIG_PTR(prefix, ctype, numpytype)
%{
PyObject * prefix ## _rev_swig_ptr(void *src, npy_intp size) {
return PyArray_SimpleNewFromData(1, &size, numpytype, src);
}
%}
PyObject * prefix ## _rev_swig_ptr(void *src, size_t size);
%enddef
REV_SWIG_PTR(fvec, float, NPY_FLOAT32);
REV_SWIG_PTR(ivec, int, NPY_INT32);
REV_SWIG_PTR(bvec, unsigned char, NPY_UINT8);
REV_SWIG_PTR(ulvec, unsigned long, NPY_UINT64);
REV_SWIG_PTR(lvec, long, NPY_INT64);
/*******************************************************************
* We want to see these types of pointers (does not use script
* matrices, so mainly for debugging)
*******************************************************************/
%include "carrays.i"
%array_class (float, FloatArray)
%array_class (double, DoubleArray)
%array_class (int, IntArray)
%array_class (uint64_t, Uint64Array)