Skip to content

Commit 859d40e

Browse files
committed
Pull request #7: Port cpu features initialization functions
Merge in ~STEPAN.SINDELAR_ORACLE.COM/numpy-hpy from hpy-port-cpu-features to labs-hpy-port * commit 'ee185b134d0e5d3d7fd4869298d129130422e5ec': minor hpy translation hack cpu features during initialization to be hpy backport version splitting from master
2 parents 36960db + ee185b1 commit 859d40e

File tree

5 files changed

+75
-66
lines changed

5 files changed

+75
-66
lines changed

numpy/core/setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def visibility_define(config):
419419

420420
def configuration(parent_package='', top_path=None):
421421
from numpy.distutils.misc_util import (
422-
Configuration, dot_join, exec_mod_from_location)
422+
Configuration, dot_join, exec_mod_from_location, get_hpy_includes)
423423
from numpy.distutils.system_info import (get_info, blas_opt_info,
424424
lapack_opt_info)
425425

@@ -742,7 +742,10 @@ def gl_if_msvc(build_cmd):
742742
sources=npymath_sources + [get_mathlib_info],
743743
install_dir='lib',
744744
build_info={
745-
'include_dirs' : [], # empty list required for creating npy_math_internal.h
745+
'include_dirs' : [
746+
# TODO: fix this properly for HPy support
747+
get_hpy_includes("")
748+
],
746749
'extra_compiler_args': [gl_if_msvc],
747750
})
748751
config.add_npy_pkg_config("npymath.ini.in", "lib/npy-pkg-config",

numpy/core/src/common/npy_cpu_features.c.src

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ npy_cpu_init(void)
4949
return 0;
5050
}
5151

52-
NPY_VISIBILITY_HIDDEN PyObject *
53-
npy_cpu_features_dict(void)
52+
NPY_VISIBILITY_HIDDEN HPy
53+
npy_cpu_features_dict(HPyContext *ctx)
5454
{
55-
PyObject *dict = PyDict_New();
56-
if (dict) {
55+
HPy dict = HPyDict_New(ctx);
56+
if (!HPy_IsNull(dict)) {
5757
/**begin repeat
5858
* #feature = MMX, SSE, SSE2, SSE3, SSSE3, SSE41, POPCNT, SSE42,
5959
* AVX, F16C, XOP, FMA4, FMA3, AVX2, AVX512F,
@@ -65,51 +65,53 @@ npy_cpu_features_dict(void)
6565
* VX, VXE, VXE2,
6666
* NEON, NEON_FP16, NEON_VFPV4, ASIMD, FPHP, ASIMDHP, ASIMDDP, ASIMDFHM#
6767
*/
68-
if (PyDict_SetItemString(dict, "@feature@",
69-
npy__cpu_have[NPY_CPU_FEATURE_@feature@] ? Py_True : Py_False) < 0) {
70-
Py_DECREF(dict);
71-
return NULL;
68+
if (HPy_SetItem_s(ctx, dict, "@feature@",
69+
npy__cpu_have[NPY_CPU_FEATURE_@feature@] ? ctx->h_True : ctx->h_False) < 0) {
70+
HPy_Close(ctx, dict);
71+
return HPy_NULL;
7272
}
7373
/**end repeat**/
7474
}
7575
return dict;
7676
}
7777

78-
#define NPY__CPU_PYLIST_APPEND_CB(FEATURE, LIST) \
79-
item = PyUnicode_FromString(NPY_TOSTRING(FEATURE)); \
80-
if (item == NULL) { \
81-
Py_DECREF(LIST); \
82-
return NULL; \
78+
#define NPY__CPU_PYLIST_APPEND_CB(FEATURE, LIST_BUILDER) \
79+
item = HPyUnicode_FromString(ctx, NPY_TOSTRING(FEATURE)); \
80+
if (HPy_IsNull(item)) { \
81+
HPyListBuilder_Cancel(ctx, LIST_BUILDER); \
82+
return HPy_NULL; \
8383
} \
84-
PyList_SET_ITEM(LIST, index++, item);
84+
HPyListBuilder_Set(ctx, LIST_BUILDER, index++, item);
8585

86-
NPY_VISIBILITY_HIDDEN PyObject *
87-
npy_cpu_baseline_list(void)
86+
NPY_VISIBILITY_HIDDEN HPy
87+
npy_cpu_baseline_list(HPyContext *ctx)
8888
{
8989
#if !defined(NPY_DISABLE_OPTIMIZATION) && NPY_WITH_CPU_BASELINE_N > 0
90-
PyObject *list = PyList_New(NPY_WITH_CPU_BASELINE_N), *item;
90+
HPyListBuilder listBuilder = HPyListBuilder_New(ctx, NPY_WITH_CPU_BASELINE_N);
91+
HPy item;
9192
int index = 0;
92-
if (list != NULL) {
93-
NPY_WITH_CPU_BASELINE_CALL(NPY__CPU_PYLIST_APPEND_CB, list)
93+
if (!HPyListBuilder_IsNull(listBuilder)) {
94+
NPY_WITH_CPU_BASELINE_CALL(NPY__CPU_PYLIST_APPEND_CB, listBuilder)
9495
}
95-
return list;
96+
return HPyListBuilder_Build(ctx, listBuilder);
9697
#else
97-
return PyList_New(0);
98+
return HPyList_New(0);
9899
#endif
99100
}
100101

101-
NPY_VISIBILITY_HIDDEN PyObject *
102-
npy_cpu_dispatch_list(void)
102+
NPY_VISIBILITY_HIDDEN HPy
103+
npy_cpu_dispatch_list(HPyContext *ctx)
103104
{
104105
#if !defined(NPY_DISABLE_OPTIMIZATION) && NPY_WITH_CPU_DISPATCH_N > 0
105-
PyObject *list = PyList_New(NPY_WITH_CPU_DISPATCH_N), *item;
106+
HPyListBuilder listBuilder = HPyListBuilder_New(ctx, NPY_WITH_CPU_DISPATCH_N);
107+
HPy item;
106108
int index = 0;
107-
if (list != NULL) {
108-
NPY_WITH_CPU_DISPATCH_CALL(NPY__CPU_PYLIST_APPEND_CB, list)
109+
if (!HPyListBuilder_IsNull(listBuilder)) {
110+
NPY_WITH_CPU_DISPATCH_CALL(NPY__CPU_PYLIST_APPEND_CB, listBuilder)
109111
}
110-
return list;
112+
return HPyListBuilder_Build(ctx, listBuilder);
111113
#else
112-
return PyList_New(0);
114+
return HPyList_New(ctx, 0);
113115
#endif
114116
}
115117

numpy/core/src/common/npy_cpu_features.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define NUMPY_CORE_SRC_COMMON_NPY_CPU_FEATURES_H_
33

44
#include <Python.h> // for PyObject
5+
#include "hpy.h"
56
#include "numpy/numpyconfig.h" // for NPY_VISIBILITY_HIDDEN
67

78
#ifdef __cplusplus
@@ -130,8 +131,8 @@ npy_cpu_have(NPY_CPU_FEATURE_##FEATURE_NAME)
130131
* with runtime availability.
131132
* same as npy_cpu_have, `npy_cpu_init` must be called first.
132133
*/
133-
NPY_VISIBILITY_HIDDEN PyObject *
134-
npy_cpu_features_dict(void);
134+
NPY_VISIBILITY_HIDDEN HPy
135+
npy_cpu_features_dict(HPyContext *);
135136
/*
136137
* Return a new a Python list contains the minimal set of required optimizations
137138
* that supported by the compiler and platform according to the specified
@@ -152,8 +153,8 @@ npy_cpu_features_dict(void);
152153
* On s390x: []
153154
* On any other arch or if the optimization is disabled: []
154155
*/
155-
NPY_VISIBILITY_HIDDEN PyObject *
156-
npy_cpu_baseline_list(void);
156+
NPY_VISIBILITY_HIDDEN HPy
157+
npy_cpu_baseline_list(HPyContext *);
157158
/*
158159
* Return a new a Python list contains the dispatched set of additional optimizations
159160
* that supported by the compiler and platform according to the specified
@@ -174,8 +175,8 @@ npy_cpu_baseline_list(void);
174175
* On s390x: ['VX', 'VXE', VXE2]
175176
* On any other arch or if the optimization is disabled: []
176177
*/
177-
NPY_VISIBILITY_HIDDEN PyObject *
178-
npy_cpu_dispatch_list(void);
178+
NPY_VISIBILITY_HIDDEN HPy
179+
npy_cpu_dispatch_list(HPyContext *);
179180

180181
#ifdef __cplusplus
181182
}

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4780,10 +4780,11 @@ static HPyModuleDef moduledef = {
47804780
HPy_MODINIT(_multiarray_umath)
47814781
static HPy init__multiarray_umath_impl(HPyContext *ctx) {
47824782
PyObject *m, *d, *s;
4783+
HPy h_mod, h_d, h_s;
47834784
PyObject *c_api;
47844785

47854786
/* Create the module and add the functions */
4786-
HPy h_mod = HPyModule_Create(ctx, &moduledef);
4787+
h_mod = HPyModule_Create(ctx, &moduledef);
47874788
m = HPy_AsPyObject(ctx, h_mod);
47884789
if (!m) {
47894790
return HPy_NULL;
@@ -4811,6 +4812,10 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
48114812
}
48124813

48134814
/* Add some symbolic constants to the module */
4815+
h_d = HPy_GetAttr_s(ctx, h_mod, "__dict__");
4816+
if (HPy_IsNull(h_d)) {
4817+
goto err;
4818+
}
48144819
d = PyModule_GetDict(m);
48154820
if (!d) {
48164821
goto err;
@@ -4941,45 +4946,45 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
49414946
49424947
* This is for backward compatibility with existing code.
49434948
*/
4944-
PyDict_SetItemString (d, "error", PyExc_Exception);
4949+
HPy_SetItem_s(ctx, h_d, "error", ctx->h_Exception);
49454950

4946-
s = PyLong_FromLong(NPY_TRACE_DOMAIN);
4947-
PyDict_SetItemString(d, "tracemalloc_domain", s);
4948-
Py_DECREF(s);
4951+
h_s = HPyLong_FromLong(ctx, NPY_TRACE_DOMAIN);
4952+
HPy_SetItem_s(ctx, h_d, "tracemalloc_domain", h_s);
4953+
HPy_Close(ctx, h_s);
49494954

4950-
s = PyUnicode_FromString("3.1");
4951-
PyDict_SetItemString(d, "__version__", s);
4952-
Py_DECREF(s);
4955+
h_s = HPyUnicode_FromString(ctx, "3.1");
4956+
HPy_SetItem_s(ctx, h_d, "__version__", h_s);
4957+
HPy_Close(ctx, h_s);
49534958

4954-
s = npy_cpu_features_dict();
4955-
if (s == NULL) {
4959+
h_s = npy_cpu_features_dict(ctx);
4960+
if (HPy_IsNull(h_s)) {
49564961
goto err;
49574962
}
4958-
if (PyDict_SetItemString(d, "__cpu_features__", s) < 0) {
4959-
Py_DECREF(s);
4963+
if (HPy_SetItem_s(ctx, h_d, "__cpu_features__", h_s) < 0) {
4964+
HPy_Close(ctx, h_s);
49604965
goto err;
49614966
}
4962-
Py_DECREF(s);
4967+
HPy_Close(ctx, h_s);
49634968

4964-
s = npy_cpu_baseline_list();
4965-
if (s == NULL) {
4969+
h_s = npy_cpu_baseline_list(ctx);
4970+
if (HPy_IsNull(h_s)) {
49664971
goto err;
49674972
}
4968-
if (PyDict_SetItemString(d, "__cpu_baseline__", s) < 0) {
4969-
Py_DECREF(s);
4973+
if (HPy_SetItem_s(ctx, h_d, "__cpu_baseline__", h_s) < 0) {
4974+
HPy_Close(ctx, h_s);
49704975
goto err;
49714976
}
4972-
Py_DECREF(s);
4977+
HPy_Close(ctx, h_s);
49734978

4974-
s = npy_cpu_dispatch_list();
4975-
if (s == NULL) {
4979+
h_s = npy_cpu_dispatch_list(ctx);
4980+
if (HPy_IsNull(h_s)) {
49764981
goto err;
49774982
}
4978-
if (PyDict_SetItemString(d, "__cpu_dispatch__", s) < 0) {
4979-
Py_DECREF(s);
4983+
if (HPy_SetItem_s(ctx, h_d, "__cpu_dispatch__", h_s) < 0) {
4984+
HPy_Close(ctx, h_s);
49804985
goto err;
49814986
}
4982-
Py_DECREF(s);
4987+
HPy_Close(ctx, h_s);
49834988

49844989
s = PyCapsule_New((void *)_datetime_strings, NULL, NULL);
49854990
if (s == NULL) {
@@ -4989,9 +4994,9 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
49894994
Py_DECREF(s);
49904995

49914996
#define ADDCONST(NAME) \
4992-
s = PyLong_FromLong(NPY_##NAME); \
4993-
PyDict_SetItemString(d, #NAME, s); \
4994-
Py_DECREF(s)
4997+
h_s = HPyLong_FromLong(ctx, NPY_##NAME); \
4998+
HPy_SetItem_s(ctx, h_d, #NAME, h_s); \
4999+
HPy_Close(ctx, h_s)
49955000

49965001

49975002
ADDCONST(ALLOW_THREADS);
@@ -5103,6 +5108,7 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
51035108
"cannot load multiarray module.");
51045109
}
51055110
HPy_Close(ctx, h_mod);
5111+
HPy_Close(ctx, h_d);
51065112
Py_DECREF(m);
51075113
return HPy_NULL;
51085114
}

setup.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@
5959
# 1.22.0 ... -> ISRELEASED == True, VERSION == 1.22.0
6060
# 1.22.0rc1 ... -> ISRELEASED == True, VERSION == 1.22.0
6161
ISRELEASED = re.search(r'(dev|\+)', FULLVERSION) is None
62-
_V_MATCH = re.match(r'(\d+)\.(\d+)\.(\d+)', FULLVERSION)
63-
if _V_MATCH is None:
64-
raise RuntimeError(f'Cannot parse version {FULLVERSION}')
65-
MAJOR, MINOR, MICRO = _V_MATCH.groups()
62+
MAJOR, MINOR, MICRO = FULLVERSION.split('.')[:3]
6663
VERSION = '{}.{}.{}'.format(MAJOR, MINOR, MICRO)
6764

6865
# The first version not in the `Programming Language :: Python :: ...` classifiers above

0 commit comments

Comments
 (0)