chao-peng / numpy

The fundamental package for scientific computing with Python.
https://numpy.org
Other
0 stars 0 forks source link

BUG: NPY_UINTP enum missing from Cython headers #1

Open chao-peng opened 18 hours ago

chao-peng commented 18 hours ago

Describe the issue:

Need: To wrap a C pointer array (via Cython) that is of type size_t using Numpy ndarrays.

Issue: Numpy does not provide a type enum for size_t (e.g., "NPY_UINTP"). It is missing from the numpy's cython header here, thus can't be imported. But is defined in the documentation and in the C header here. Without this enum then the numpy function PyArray_SimpleNewFromData is not able to wrap the pointer.

Workaround: Use cnp.NPY_UINT64 instead but this is may not be the same size on all systems.

Reproduce the code example:

cimport numpy as cnp
from libcpp.vector cimport vector
cnp.import_array()

cdef size_t num_layers = 10
cdef vector[size_t] steps_taken_vec = vector[size_t]()
steps_taken_vec.resize(3 * num_layers)
cdef size_t* steps_taken_ptr = steps_taken_vec.data()

cdef cnp.npy_intp[2] shape = [3, num_layers]
cdef cnp.npy_intp ndim     = 2
self.shooting_method_steps_taken_array = cnp.PyArray_SimpleNewFromData(
    ndim,
    &shape[0],
    cnp.NPY_UINTP,    # <-- This is the problem
    steps_taken_ptr)  # steps_taken_ptr is a size_t*

Error message:

"cimported module has no attribute 'NPY_UINTP'"

Python and NumPy Versions:

Numpy: 1.26.4 Python: 3.11.10 Cython: 3.0.11

Runtime Environment:

[{'numpy_version': '1.26.4', 'python': '3.11.10 | packaged by Anaconda, Inc. | (main, Oct 3 2024, ' '07:22:26) [MSC v.1929 64 bit (AMD64)]', 'uname': uname_result(system='Windows', node='JoeDesktop', release='10', version='10.0.19045', machine='AMD64')}, {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'], 'found': ['SSSE3', 'SSE41', 'POPCNT', 'SSE42', 'AVX', 'F16C', 'FMA3', 'AVX2'], 'not_found': ['AVX512F', 'AVX512CD', 'AVX512_SKX', 'AVX512_CLX', 'AVX512_CNL', 'AVX512_ICL']}}, {'filepath': 'C:\Users\joepr\anaconda3\envs\cyrk11py311\Library\bin\mkl_rt.2.dll', 'internal_api': 'mkl', 'num_threads': 8, 'prefix': 'mkl_rt', 'threading_layer': 'intel', 'user_api': 'blas', 'version': '2023.1-Product'}]

Context for the issue:

As a workaround, I am using cnp.NPY_UINT64 which works but is likely not guaranteed to be the same size as size_t across platforms and into the future.

chao-peng commented 18 hours ago

@marscode

marscode-agent[bot] commented 18 hours ago

Start working on this issue.

marscode-agent[bot] commented 17 hours ago

Please review the PR to solve this issue at #2

Note that all contents are generated by large language models. Use the generated code with cautions.