-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.c
More file actions
139 lines (108 loc) · 3.37 KB
/
Copy pathmain.c
File metadata and controls
139 lines (108 loc) · 3.37 KB
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
#include <Python.h>
#define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL stringdtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"
#include "dtype.h"
#include "static_string.h"
#include "umath.h"
static PyObject *
_memory_usage(PyObject *NPY_UNUSED(self), PyObject *obj)
{
if (!PyArray_Check(obj)) {
PyErr_SetString(PyExc_TypeError,
"can only be called with ndarray object");
return NULL;
}
PyArrayObject *arr = (PyArrayObject *)obj;
PyArray_Descr *descr = PyArray_DESCR(arr);
PyArray_DTypeMeta *dtype = NPY_DTYPE(descr);
if (dtype != (PyArray_DTypeMeta *)&StringDType) {
PyErr_SetString(PyExc_TypeError,
"can only be called with a StringDType array");
return NULL;
}
NpyIter *iter = NpyIter_New(
arr, NPY_ITER_READONLY | NPY_ITER_EXTERNAL_LOOP | NPY_ITER_REFS_OK,
NPY_KEEPORDER, NPY_NO_CASTING, NULL);
if (iter == NULL) {
return NULL;
}
NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(iter, NULL);
if (iternext == NULL) {
NpyIter_Deallocate(iter);
return NULL;
}
char **dataptr = NpyIter_GetDataPtrArray(iter);
npy_intp *strideptr = NpyIter_GetInnerStrideArray(iter);
npy_intp *innersizeptr = NpyIter_GetInnerLoopSizePtr(iter);
// initialize with the size of the internal buffer
size_t memory_usage = PyArray_NBYTES(arr);
do {
char *in = dataptr[0];
npy_intp stride = *strideptr;
npy_intp count = *innersizeptr;
while (count--) {
size_t size = _NpyString_size(((npy_packed_static_string *)in));
// FIXME: add a way for a string to report its heap size usage
if (size > (sizeof(_npy_static_string) - 1)) {
memory_usage += size;
}
in += stride;
}
} while (iternext(iter));
NpyIter_Deallocate(iter);
PyObject *ret = PyLong_FromSize_t(memory_usage);
return ret;
}
static PyMethodDef string_methods[] = {
{"_memory_usage", _memory_usage, METH_O,
"get memory usage for an array"},
{NULL, NULL, 0, NULL},
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
.m_name = "stringdtype_main",
.m_size = -1,
.m_methods = string_methods,
};
/* Module initialization function */
PyMODINIT_FUNC
PyInit__main(void)
{
import_array();
import_umath();
PyObject *m = PyModule_Create(&moduledef);
if (m == NULL) {
return NULL;
}
PyObject *mod = PyImport_ImportModule("stringdtype");
if (mod == NULL) {
goto error;
}
StringScalar_Type =
(PyTypeObject *)PyObject_GetAttrString(mod, "StringScalar");
if (StringScalar_Type == NULL) {
goto error;
}
Py_DECREF(mod);
if (init_string_dtype() < 0) {
goto error;
}
Py_INCREF((PyObject *)&StringDType);
if (PyModule_AddObject(m, "StringDType", (PyObject *)&StringDType) < 0) {
Py_DECREF((PyObject *)&StringDType);
goto error;
}
if (init_ufuncs() == -1) {
goto error;
}
return m;
error:
Py_DECREF(m);
return NULL;
}