Skip to content

Commit

Permalink
Pull request #7: Port cpu features initialization functions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
timfel committed Mar 10, 2022
2 parents 36960db + ee185b1 commit 859d40e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 66 deletions.
7 changes: 5 additions & 2 deletions numpy/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def visibility_define(config):

def configuration(parent_package='', top_path=None):
from numpy.distutils.misc_util import (
Configuration, dot_join, exec_mod_from_location)
Configuration, dot_join, exec_mod_from_location, get_hpy_includes)
from numpy.distutils.system_info import (get_info, blas_opt_info,
lapack_opt_info)

Expand Down Expand Up @@ -742,7 +742,10 @@ def gl_if_msvc(build_cmd):
sources=npymath_sources + [get_mathlib_info],
install_dir='lib',
build_info={
'include_dirs' : [], # empty list required for creating npy_math_internal.h
'include_dirs' : [
# TODO: fix this properly for HPy support
get_hpy_includes("")
],
'extra_compiler_args': [gl_if_msvc],
})
config.add_npy_pkg_config("npymath.ini.in", "lib/npy-pkg-config",
Expand Down
58 changes: 30 additions & 28 deletions numpy/core/src/common/npy_cpu_features.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ npy_cpu_init(void)
return 0;
}

NPY_VISIBILITY_HIDDEN PyObject *
npy_cpu_features_dict(void)
NPY_VISIBILITY_HIDDEN HPy
npy_cpu_features_dict(HPyContext *ctx)
{
PyObject *dict = PyDict_New();
if (dict) {
HPy dict = HPyDict_New(ctx);
if (!HPy_IsNull(dict)) {
/**begin repeat
* #feature = MMX, SSE, SSE2, SSE3, SSSE3, SSE41, POPCNT, SSE42,
* AVX, F16C, XOP, FMA4, FMA3, AVX2, AVX512F,
Expand All @@ -65,51 +65,53 @@ npy_cpu_features_dict(void)
* VX, VXE, VXE2,
* NEON, NEON_FP16, NEON_VFPV4, ASIMD, FPHP, ASIMDHP, ASIMDDP, ASIMDFHM#
*/
if (PyDict_SetItemString(dict, "@feature@",
npy__cpu_have[NPY_CPU_FEATURE_@feature@] ? Py_True : Py_False) < 0) {
Py_DECREF(dict);
return NULL;
if (HPy_SetItem_s(ctx, dict, "@feature@",
npy__cpu_have[NPY_CPU_FEATURE_@feature@] ? ctx->h_True : ctx->h_False) < 0) {
HPy_Close(ctx, dict);
return HPy_NULL;
}
/**end repeat**/
}
return dict;
}

#define NPY__CPU_PYLIST_APPEND_CB(FEATURE, LIST) \
item = PyUnicode_FromString(NPY_TOSTRING(FEATURE)); \
if (item == NULL) { \
Py_DECREF(LIST); \
return NULL; \
#define NPY__CPU_PYLIST_APPEND_CB(FEATURE, LIST_BUILDER) \
item = HPyUnicode_FromString(ctx, NPY_TOSTRING(FEATURE)); \
if (HPy_IsNull(item)) { \
HPyListBuilder_Cancel(ctx, LIST_BUILDER); \
return HPy_NULL; \
} \
PyList_SET_ITEM(LIST, index++, item);
HPyListBuilder_Set(ctx, LIST_BUILDER, index++, item);

NPY_VISIBILITY_HIDDEN PyObject *
npy_cpu_baseline_list(void)
NPY_VISIBILITY_HIDDEN HPy
npy_cpu_baseline_list(HPyContext *ctx)
{
#if !defined(NPY_DISABLE_OPTIMIZATION) && NPY_WITH_CPU_BASELINE_N > 0
PyObject *list = PyList_New(NPY_WITH_CPU_BASELINE_N), *item;
HPyListBuilder listBuilder = HPyListBuilder_New(ctx, NPY_WITH_CPU_BASELINE_N);
HPy item;
int index = 0;
if (list != NULL) {
NPY_WITH_CPU_BASELINE_CALL(NPY__CPU_PYLIST_APPEND_CB, list)
if (!HPyListBuilder_IsNull(listBuilder)) {
NPY_WITH_CPU_BASELINE_CALL(NPY__CPU_PYLIST_APPEND_CB, listBuilder)
}
return list;
return HPyListBuilder_Build(ctx, listBuilder);
#else
return PyList_New(0);
return HPyList_New(0);
#endif
}

NPY_VISIBILITY_HIDDEN PyObject *
npy_cpu_dispatch_list(void)
NPY_VISIBILITY_HIDDEN HPy
npy_cpu_dispatch_list(HPyContext *ctx)
{
#if !defined(NPY_DISABLE_OPTIMIZATION) && NPY_WITH_CPU_DISPATCH_N > 0
PyObject *list = PyList_New(NPY_WITH_CPU_DISPATCH_N), *item;
HPyListBuilder listBuilder = HPyListBuilder_New(ctx, NPY_WITH_CPU_DISPATCH_N);
HPy item;
int index = 0;
if (list != NULL) {
NPY_WITH_CPU_DISPATCH_CALL(NPY__CPU_PYLIST_APPEND_CB, list)
if (!HPyListBuilder_IsNull(listBuilder)) {
NPY_WITH_CPU_DISPATCH_CALL(NPY__CPU_PYLIST_APPEND_CB, listBuilder)
}
return list;
return HPyListBuilder_Build(ctx, listBuilder);
#else
return PyList_New(0);
return HPyList_New(ctx, 0);
#endif
}

Expand Down
13 changes: 7 additions & 6 deletions numpy/core/src/common/npy_cpu_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NUMPY_CORE_SRC_COMMON_NPY_CPU_FEATURES_H_

#include <Python.h> // for PyObject
#include "hpy.h"
#include "numpy/numpyconfig.h" // for NPY_VISIBILITY_HIDDEN

#ifdef __cplusplus
Expand Down Expand Up @@ -130,8 +131,8 @@ npy_cpu_have(NPY_CPU_FEATURE_##FEATURE_NAME)
* with runtime availability.
* same as npy_cpu_have, `npy_cpu_init` must be called first.
*/
NPY_VISIBILITY_HIDDEN PyObject *
npy_cpu_features_dict(void);
NPY_VISIBILITY_HIDDEN HPy
npy_cpu_features_dict(HPyContext *);
/*
* Return a new a Python list contains the minimal set of required optimizations
* that supported by the compiler and platform according to the specified
Expand All @@ -152,8 +153,8 @@ npy_cpu_features_dict(void);
* On s390x: []
* On any other arch or if the optimization is disabled: []
*/
NPY_VISIBILITY_HIDDEN PyObject *
npy_cpu_baseline_list(void);
NPY_VISIBILITY_HIDDEN HPy
npy_cpu_baseline_list(HPyContext *);
/*
* Return a new a Python list contains the dispatched set of additional optimizations
* that supported by the compiler and platform according to the specified
Expand All @@ -174,8 +175,8 @@ npy_cpu_baseline_list(void);
* On s390x: ['VX', 'VXE', VXE2]
* On any other arch or if the optimization is disabled: []
*/
NPY_VISIBILITY_HIDDEN PyObject *
npy_cpu_dispatch_list(void);
NPY_VISIBILITY_HIDDEN HPy
npy_cpu_dispatch_list(HPyContext *);

#ifdef __cplusplus
}
Expand Down
58 changes: 32 additions & 26 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4780,10 +4780,11 @@ static HPyModuleDef moduledef = {
HPy_MODINIT(_multiarray_umath)
static HPy init__multiarray_umath_impl(HPyContext *ctx) {
PyObject *m, *d, *s;
HPy h_mod, h_d, h_s;
PyObject *c_api;

/* Create the module and add the functions */
HPy h_mod = HPyModule_Create(ctx, &moduledef);
h_mod = HPyModule_Create(ctx, &moduledef);
m = HPy_AsPyObject(ctx, h_mod);
if (!m) {
return HPy_NULL;
Expand Down Expand Up @@ -4811,6 +4812,10 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
}

/* Add some symbolic constants to the module */
h_d = HPy_GetAttr_s(ctx, h_mod, "__dict__");
if (HPy_IsNull(h_d)) {
goto err;
}
d = PyModule_GetDict(m);
if (!d) {
goto err;
Expand Down Expand Up @@ -4941,45 +4946,45 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
* This is for backward compatibility with existing code.
*/
PyDict_SetItemString (d, "error", PyExc_Exception);
HPy_SetItem_s(ctx, h_d, "error", ctx->h_Exception);

s = PyLong_FromLong(NPY_TRACE_DOMAIN);
PyDict_SetItemString(d, "tracemalloc_domain", s);
Py_DECREF(s);
h_s = HPyLong_FromLong(ctx, NPY_TRACE_DOMAIN);
HPy_SetItem_s(ctx, h_d, "tracemalloc_domain", h_s);
HPy_Close(ctx, h_s);

s = PyUnicode_FromString("3.1");
PyDict_SetItemString(d, "__version__", s);
Py_DECREF(s);
h_s = HPyUnicode_FromString(ctx, "3.1");
HPy_SetItem_s(ctx, h_d, "__version__", h_s);
HPy_Close(ctx, h_s);

s = npy_cpu_features_dict();
if (s == NULL) {
h_s = npy_cpu_features_dict(ctx);
if (HPy_IsNull(h_s)) {
goto err;
}
if (PyDict_SetItemString(d, "__cpu_features__", s) < 0) {
Py_DECREF(s);
if (HPy_SetItem_s(ctx, h_d, "__cpu_features__", h_s) < 0) {
HPy_Close(ctx, h_s);
goto err;
}
Py_DECREF(s);
HPy_Close(ctx, h_s);

s = npy_cpu_baseline_list();
if (s == NULL) {
h_s = npy_cpu_baseline_list(ctx);
if (HPy_IsNull(h_s)) {
goto err;
}
if (PyDict_SetItemString(d, "__cpu_baseline__", s) < 0) {
Py_DECREF(s);
if (HPy_SetItem_s(ctx, h_d, "__cpu_baseline__", h_s) < 0) {
HPy_Close(ctx, h_s);
goto err;
}
Py_DECREF(s);
HPy_Close(ctx, h_s);

s = npy_cpu_dispatch_list();
if (s == NULL) {
h_s = npy_cpu_dispatch_list(ctx);
if (HPy_IsNull(h_s)) {
goto err;
}
if (PyDict_SetItemString(d, "__cpu_dispatch__", s) < 0) {
Py_DECREF(s);
if (HPy_SetItem_s(ctx, h_d, "__cpu_dispatch__", h_s) < 0) {
HPy_Close(ctx, h_s);
goto err;
}
Py_DECREF(s);
HPy_Close(ctx, h_s);

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

#define ADDCONST(NAME) \
s = PyLong_FromLong(NPY_##NAME); \
PyDict_SetItemString(d, #NAME, s); \
Py_DECREF(s)
h_s = HPyLong_FromLong(ctx, NPY_##NAME); \
HPy_SetItem_s(ctx, h_d, #NAME, h_s); \
HPy_Close(ctx, h_s)


ADDCONST(ALLOW_THREADS);
Expand Down Expand Up @@ -5103,6 +5108,7 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
"cannot load multiarray module.");
}
HPy_Close(ctx, h_mod);
HPy_Close(ctx, h_d);
Py_DECREF(m);
return HPy_NULL;
}
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@
# 1.22.0 ... -> ISRELEASED == True, VERSION == 1.22.0
# 1.22.0rc1 ... -> ISRELEASED == True, VERSION == 1.22.0
ISRELEASED = re.search(r'(dev|\+)', FULLVERSION) is None
_V_MATCH = re.match(r'(\d+)\.(\d+)\.(\d+)', FULLVERSION)
if _V_MATCH is None:
raise RuntimeError(f'Cannot parse version {FULLVERSION}')
MAJOR, MINOR, MICRO = _V_MATCH.groups()
MAJOR, MINOR, MICRO = FULLVERSION.split('.')[:3]
VERSION = '{}.{}.{}'.format(MAJOR, MINOR, MICRO)

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

0 comments on commit 859d40e

Please sign in to comment.