8000 Pull request #25: Singleton builtin descriptors instances (arraytypes… · mattip/numpy@2c86d41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c86d41

Browse files
committed
Pull request #25: Singleton builtin descriptors instances (arraytypes.c) converted to HPy
Merge in numpy-hpy from ss/PyArray_InitializeCasts1 to labs-hpy-port * commit '7082e229794ccf0d5bed7f5a339da15d8dd0e9e6': Singleton builtin descriptors instances (arraytypes.c) converted to HPy
2 parents cf02941 + 7082e22 commit 2c86d41

File tree

9 files changed

+233
-184
lines changed

9 files changed

+233
-184
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 118 additions & 150 deletions
Large diffs are not rendered by default.

numpy/core/src/multiarray/arraytypes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ small_correlate(const char * d_, npy_intp dstride,
2828
npy_intp nk, enum NPY_TYPES ktype,
2929
char * out_, npy_intp ostride);
3030

31-
void init_hpy_global_state(HPyContext *ctx);
31+
void init_arraytypes_hpy_global_state(HPyContext *ctx);
3232

3333
NPY_NO_EXPORT HPy
3434
HPyArray_DescrFromType(HPyContext *ctx, int type);
3535

3636
NPY_NO_EXPORT void
3737
init_static_descrs_type_objs(HPyContext *ctx);
3838

39+
extern HPyGlobal _hpy_builtin_descrs[NPY_NTYPES];
40+
3941
#endif /* NUMPY_CORE_SRC_MULTIARRAY_ARRAYTYPES_H_ */

numpy/core/src/multiarray/convert_datatype.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,11 +2848,10 @@ complex_to_noncomplex_get_loop(
28482848

28492849

28502850
static int
2851-
add_numeric_cast(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
2851+
add_numeric_cast(HPyContext *ctx, HPy h_from, HPy h_to)
28522852
{
2853-
HPyContext *ctx = npy_get_context();
2854-
HPy h_from = HPy_FromPyObject(ctx, (PyObject *)from);
2855-
HPy h_to = HPy_FromPyObject(ctx, (PyObject *)to);
2853+
PyArray_DTypeMeta *from = PyArray_DTypeMeta_AsStruct(ctx, h_from);
2854+
PyArray_DTypeMeta *to = PyArray_DTypeMeta_AsStruct(ctx, h_to);
28562855

28572856
PyType_Slot slots[7];
28582857
HPy dtypes[2] = {h_from, h_to}; /* PyArray_DTypeMeta *dtypes[2] */
@@ -2865,11 +2864,14 @@ add_numeric_cast(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
28652864
.dtypes = dtypes,
28662865
};
28672866

2868-
HPy h_from_singleton = HPyField_Load(ctx, h_from, PyArray_DTypeMeta_AsStruct(ctx, h_from)->singleton);
2869-
HPy h_to_singleton = HPyField_Load(ctx, h_to, PyArray_DTypeMeta_AsStruct(ctx, h_to)->singleton);
2867+
HPy h_from_singleton = HPyField_Load(ctx, h_from, from->singleton);
2868+
HPy h_to_singleton = HPyField_Load(ctx, h_to, to->singleton);
28702869

2871-
npy_intp from_itemsize = PyArray_Descr_AsStruct(ctx, h_from_singleton)->elsize;
2872-
npy_intp to_itemsize = PyArray_Descr_AsStruct(ctx, h_to_singleton)->elsize;
2870+
PyArray_Descr *h_from_singleton_data = PyArray_Descr_AsStruct(ctx, h_from_singleton);
2871+
PyArray_Descr *h_to_singleton_data = PyArray_Descr_AsStruct(ctx, h_to_singleton);
2872+
2873+
npy_intp from_itemsize = h_from_singleton_data->elsize;
2874+
npy_intp to_itemsize = h_to_singleton_data->elsize;
28732875

28742876
slots[0].slot = NPY_METH_resolve_descriptors;
28752877
slots[0].pfunc = &simple_cast_resolve_descriptors;
@@ -2907,9 +2909,6 @@ add_numeric_cast(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
29072909

29082910
assert(slots[1].pfunc && slots[2].pfunc && slots[3].pfunc && slots[4].pfunc);
29092911

2910-
PyArray_Descr *h_from_singleton_data = PyArray_Descr_AsStruct(ctx, h_from_singleton);
2911-
PyArray_Descr *h_to_singleton_data = PyArray_Descr_AsStruct(ctx, h_to_singleton);
2912-
29132912
/* Find the correct casting level, and special case no-cast */
29142913
if (h_from_singleton_data->kind == h_to_singleton_data->kind
29152914
&& from_itemsize == to_itemsize) {
@@ -2943,10 +2942,6 @@ add_numeric_cast(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
29432942
/* Create a bound method, unbind and store it */
29442943
int res = PyArray_AddCastingImplementation_FromSpec(&spec, 1);
29452944

2946-
/* 'h_to', 'h_from' are used in the spec */
2947-
HPy_Close(ctx, h_to);
2948-
HPy_Close(ctx, h_from);
2949-
29502945
return res;
29512946
}
29522947

@@ -2957,26 +2952,27 @@ add_numeric_cast(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
29572952
* file to remove `PyArray_GetStridedNumericCastFn` entirely.
29582953
*/
29592954
static int
2960-
PyArray_InitializeNumericCasts(void)
2955+
PyArray_InitializeNumericCasts(HPyContext *ctx)
29612956
{
29622957
for (int from = 0; from < NPY_NTYPES; from++) {
29632958
if (!PyTypeNum_ISNUMBER(from) && from != NPY_BOOL) {
29642959
continue;
29652960
}
2966-
PyArray_DTypeMeta *from_dt = PyArray_DTypeFromTypeNum(from);
2961+
HPy from_dt = HPyArray_DTypeFromTypeNum(ctx, from);
29672962

29682963
for (int to = 0; < F438 span class=pl-s1>to < NPY_NTYPES; to++) {
29692964
if (!PyTypeNum_ISNUMBER(to) && to != NPY_BOOL) {
29702965
continue;
29712966
}
2972-
PyArray_DTypeMeta *to_dt = PyArray_DTypeFromTypeNum(to);
2973-
int res = add_numeric_cast(from_dt, to_dt);
2974-
Py_DECREF(to_dt);
2967+
HPy to_dt = HPyArray_DTypeFromTypeNum(ctx, to);
2968+
int res = add_numeric_cast(ctx, from_dt, to_dt);
2969+
HPy_Close(ctx, to_dt);
29752970
if (res < 0) {
2976-
Py_DECREF(from_dt);
2971+
HPy_Close(ctx, from_dt);
29772972
return -1;
29782973
}
29792974
}
2975+
HPy_Close(ctx, from_dt);
29802976
}
29812977
return 0;
29822978
}
@@ -4169,12 +4165,12 @@ PyArray_InitializeObjectToObjectCast(void)
41694165

41704166

41714167
NPY_NO_EXPORT int
4172-
PyArray_InitializeCasts()
4168+
PyArray_InitializeCasts(HPyContext *ctx)
41734169
{
4174-
CAPI_WARN("startup: PyArray_InitializeCasts");
4175-
if (PyArray_InitializeNumericCasts() < 0) {
4170+
if (PyArray_InitializeNumericCasts(ctx) < 0) {
41764171
return -1;
41774172
}
4173+
CAPI_WARN("startup: PyArray_InitializeCasts");
41784174
if (PyArray_InitializeStringCasts() < 0) {
41794175
return -1;
41804176
}

numpy/core/src/multiarray/convert_datatype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ simple_cast_resolve_descriptors(
119119
npy_intp *view_offset);
120120

121121
NPY_NO_EXPORT int
122-
PyArray_InitializeCasts(void);
122+
PyArray_InitializeCasts(HPyContext *ctx);
123123

124124
NPY_NO_EXPORT npy_bool
125125
HPyArray_CanCastTypeTo(HPyContext *ctx, HPy h_from, HPy h_to,

numpy/core/src/multiarray/dtypemeta.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,14 +733,19 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, PyArray_Descr *descr)
733733
}
734734

735735
/* Finally, replace the current class of the descr */
736+
// TODO HPY LABS PORT: probably expose HPy_SetType for the HPy example port,
737+
// ATTENTION: Py_SET_TYPE steals the reference, HPy API will not steal it
738+
// in longer term, this can be refactored: it seems that it's here to support
739+
// some legacy API, which we can keep in C API, and to initialize singleton
740+
// descriptors like BOOL_Descr, which we can initialize with the right type
741+
// already to avoid setting it ex-post
736742
Py_SET_TYPE(descr, (PyTypeObject *)dtype_class);
737743
result = 0;
738744

739745
cleanup:
740746
HPy_Close(ctx, h_PyArrayDescr_Type);
741747
HPy_Close(ctx, h_PyArrayDTypeMeta_Type);
742748
HPy_Close(ctx, h_new_dtype_type);
743-
Py_DecRef(dtype_class);
744749
return result;
745750
}
746751

numpy/core/src/multiarray/dtypemeta.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ PyArray_DTypeFromTypeNum(int typenum)
174174
return dtype;
175175
}
176176

177+
static NPY_INLINE HPy
178+
HPyArray_DTypeFromTypeNum(HPyContext *ctx, int typenum)
179+
{
180+
HPy descr = HPyArray_DescrFromType(ctx, typenum);
181+
HPy dtype = HNPY_DTYPE(ctx, descr);
182+
HPy_Close(ctx, descr);
183+
return dtype;
184+
}
185+
177186
static NPY_INLINE PyObject *
178187
dtypemeta_get_castingimpls(PyArray_DTypeMeta *cls)
179188
{

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4779,6 +4779,8 @@ setup_scalartypes(HPyContext *ctx)
47794779
goto cleanup; \
47804780
} \
47814781
HPyTracker_Add(ctx, tracker, h_Py##child##ArrType_Type); \
4782+
HPyGlobal_Store(ctx, &HPy##child##ArrType_Type, \
4783+
h_Py##child##ArrType_Type); \
47824784
_Py##child##ArrType_Type_p = \
47834785
(PyTypeObject*) HPy_AsPyObject(ctx, h_Py##child##ArrType_Type);
47844786

@@ -4826,6 +4828,8 @@ setup_scalartypes(HPyContext *ctx)
48264828
goto cleanup; \
48274829
} \
48284830
HPyTracker_Add(ctx, tracker, h_Py##child##ArrType_Type); \
4831+
HPyGlobal_Store(ctx, &HPy##child##ArrType_Type, \
4832+
h_Py##child##ArrType_Type); \
48294833
_Py##child##ArrType_Type_p = \
48304834
(PyTypeObject*) HPy_AsPyObject(ctx, h_Py##child##ArrType_Type);
48314835

@@ -4856,6 +4860,8 @@ setup_scalartypes(HPyContext *ctx)
48564860
goto cleanup; \
48574861
} \
48584862
HPyTracker_Add(ctx, tracker, h_Py##child##ArrType_Type); \
4863+
HPyGlobal_Store(ctx, &HPy##child##ArrType_Type, \
4864+
h_Py##child##ArrType_Type); \
48594865
_Py##child##ArrType_Type_p = \
48604866
(PyTypeObject*) HPy_AsPyObject(ctx, h_Py##child##ArrType_Type);
48614867

@@ -4999,6 +5005,71 @@ static HPyGlobal *module_globals[] = {
49995005
&HPyArrayMethod_Type,
50005006
&HPyBoundArrayMethod_Type,
50015007
&HPyGenericArrType_Type,
5008+
5009+
// scalartypes:
5010+
&HPyGenericArrType_Type,
5011+
&HPyGenericArrType_Type,
5012+
&HPyBoolArrType_Type,
5013+
&HPyNumberArrType_Type,
5014+
&HPyIntegerArrType_Type,
5015+
&HPySignedIntegerArrType_Type,
5016+
&HPyUnsignedIntegerArrType_Type,
5017+
&HPyInexactArrType_Type,
5018+
&HPyFloatingArrType_Type,
5019+
&HPyComplexFloatingArrType_Type,
5020+
&HPyFlexibleArrType_Type,
5021+
&HPyCharacterArrType_Type,
5022+
&HPyByteArrType_Type,
5023+
&HPyShortArrType_Type,
5024+
&HPyIntArrType_Type,
5025+
&HPyLongArrType_Type,
5026+
&HPyLongLongArrType_Type,
5027+
&HPyUByteArrType_Type,
5028+
&HPyUShortArrType_Type,
5029+
&HPyUIntArrType_Type,
5030+
&HPyULongArrType_Type,
5031+
&HPyULongLongArrType_Type,
5032+
&HPyFloatArrType_Type,
5033+
&HPyDoubleArrType_Type,
5034+
&HPyLongDoubleArrType_Type,
5035+
&HPyCFloatArrType_Type,
5036+
&HPyCDoubleArrType_Type,
5037+
&HPyCLongDoubleArrType_Type,
5038+
&HPyObjectArrType_Type,
5039+
&HPyStringArrType_Type,
5040+
&HPyUnicodeArrType_Type,
5041+
&HPyVoidArrType_Type,
5042+
&HPyDatetimeArrType_Type,
5043+
&HPyTimedeltaArrType_Type,
5044+
&HPyHalfArrType_Type,
5045+
5046+
// arraytypes
5047+
&_hpy_builtin_descrs[NPY_VOID],
5048+
&_hpy_builtin_descrs[NPY_STRING],
5049+
&_hpy_builtin_descrs[NPY_UNICODE],
5050+
&_hpy_builtin_descrs[NPY_BOOL],
5051+
&_hpy_builtin_descrs[NPY_BYTE],
5052+
&_hpy_builtin_descrs[NPY_UBYTE],
5053+
&_hpy_builtin_descrs[NPY_SHORT],
5054+
&_hpy_builtin_descrs[NPY_USHORT],
5055+
&_hpy_builtin_descrs[NPY_INT],
5056+
&_hpy_builtin_descrs[NPY_UINT],
5057+
&_hpy_builtin_descrs[NPY_LONG],
5058+
&_hpy_builtin_descrs[NPY_ULONG],
5059+
&_hpy_builtin_descrs[NPY_LONGLONG],
5060+
&_hpy_builtin_descrs[NPY_ULONGLONG],
5061+
&_hpy_builtin_descrs[NPY_HALF],
5062+
&_hpy_builtin_descrs[NPY_FLOAT],
5063+
&_hpy_builtin_descrs[NPY_DOUBLE],
5064+
&_hpy_builtin_descrs[NPY_LONGDOUBLE],
5065+
&_hpy_builtin_descrs[NPY_CFLOAT],
5066+
&_hpy_builtin_descrs[NPY_CDOUBLE],
5067+
&_hpy_builtin_descrs[NPY_CLONGDOUBLE],
5068+
&_hpy_builtin_descrs[NPY_OBJECT],
5069+
&_hpy_builtin_descrs[NPY_DATETIME],
5070+
&_hpy_builtin_descrs[NPY_TIMEDELTA],
5071+
5072+
// hpy_n_ops struct/cache:
50025073
&hpy_n_ops.add,
50035074
&hpy_n_ops.subtract,
50045075
&hpy_n_ops.multiply,
@@ -5176,9 +5247,8 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
51765247
if (setup_scalartypes(ctx) < 0) {
51775248
goto err;
51785249
}
5179-
// HPY: static descr objects, e.g., BOOL_Descr, have typeobj set to numpy scalar types
5180-
// Numpy scalar types are now heap allocated, so we must set them after their initialization
5181-
init_static_descrs_type_objs(ctx);
5250+
5251+
init_arraytypes_hpy_global_state(ctx);
51825252
// HPY: TODO comment on this
51835253
init_scalartypes_basetypes(ctx);
51845254

@@ -5366,7 +5436,7 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
53665436
goto err;
53675437
}
53685438

5369-
if (PyArray_InitializeCasts() < 0) {
5439+
if (PyArray_InitializeCasts(ctx) < 0) {
53705440
goto err;
53715441
}
53725442

@@ -5402,8 +5472,6 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
54025472
HPy_Close(ctx, h_current_handler);
54035473
#endif
54045474

5405-
// HPY TODO: this is a temporary solution to mimic the static types and global state:
5406-
init_hpy_global_state(ctx);
54075475
result = h_mod;
54085476

54095477
cleanup:

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ NPY_NO_EXPORT PyTypeObject PyTimeIntegerArrType_Type;
6868
*/
6969
NPY_NO_EXPORT PyTypeObject *_Py@NAME@ArrType_Type_p;
7070

71+
NPY_NO_EXPORT HPyGlobal HPy@NAME@ArrType_Type;
72+
7173
static PyType_Slot Py@NAME@ArrType_Type_slots_legacy[100];
7274

7375
NPY_NO_EXPORT HPyType_Spec Py@NAME@ArrType_Type_spec = {

numpy/core/src/multiarray/scalartypes.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ extern NPY_NO_EXPORT HPyGlobal HPyObjectArrType_Type;
151151
extern NPY_NO_EXPORT HPyGlobal HPyStringArrType_Type;
152152
extern NPY_NO_EXPORT HPyGlobal HPyUnicodeArrType_Type;
153153
extern NPY_NO_EXPORT HPyGlobal HPyVoidArrType_Type;
154-
extern NPY_NO_EXPORT HPyGlobal HPyTimeIntegerArrType_Type;
155154
extern NPY_NO_EXPORT HPyGlobal HPyDatetimeArrType_Type;
156155
extern NPY_NO_EXPORT HPyGlobal HPyTimedeltaArrType_Type;
157156
extern NPY_NO_EXPORT HPyGlobal HPyHalfArrType_Type;

0 commit comments

Comments
 (0)
0