Skip to main content

Pass arrays of any dimension to Cython as memory views (no Global Interpreter Lock (GIL))

Project description

Pass arrays of any dimension to Cython as memory views (no Global Interpreter Lock (GIL))

pip install cythonanyarray

Tested against Windows / Python 3.11 / Anaconda

Cython (and a C/C++ compiler) must be installed

This module provides utility functions for creating and manipulating flattened indices and pointers for multi-dimensional arrays in Cython. It is designed to facilitate passing arrays of any dimension to Cython as memory views, enabling the release of the Global Interpreter Lock (GIL) for improved performance in parallel processing.

# Functions:
- get_pointer_array: Get a flat pointer array from the original array. # if you change it, the original data changes too
- get_iterarray: Get an array of flattened indices along with a flat pointer array for an input array.
- get_flat_iter_for_cython: Get an array of flattened indices and a flat pointer array suitable for use in Cython.
- get_iterarray_shape: Get an array of flattened indices with a specified last dimension.

import cv2
import numpy as np
from cythonanyarray import get_flat_iter_for_cython

data = cv2.imread(r"C:\Users\hansc\Pictures\socialmediaiconsmatching.png")
indexarray, flatpointerarray = get_flat_iter_for_cython(data, dtype=np.int64, unordered=True)
print(flatpointerarray)
# [[      0       0       0       0]
#  [   5745       1       0       0]
#  [  11490       2       0       0]
#  ...
#  [5543924     964    1914       2]
#  [5549669     965    1914       2]
#  [5555414     966    1914       2]]
# [51 51 51 ... 43 43 43]
# Iterate through the flattened indices - Blueprint for Cython
# This snipped is crap in Python but in Cython it is pure gold (don't forget to add types!)
flatiter = len(indexarray)
for i in range(flatiter):
    if flatpointerarray[indexarray[i][0]] == 255:
        print(indexarray[i][1:])
        break

# Accessing the original array using the flattened indices
print(data[34, 0, 0])  # Output: [34, 0, 0]

# Another example with a multi-dimensional array
data = np.arange(10 * 4 * 12 * 12 * 32).reshape((10, 4, 12, 12, 32)).astype(np.float64)
indexarray, flatpointerarray = get_flat_iter_for_cython(data, dtype=np.int64, unordered=True)
print(flatpointerarray)
# [[     0      0      0      0      0      0]
#  [ 18432      1      0      0      0      0]
#  [ 36864      2      0      0      0      0]
#  ...
#  [147455      7      3     11     11     31]
#  [165887      8      3     11     11     31]
#  [184319      9      3     11     11     31]]
# [0.00000e+00 1.00000e+00 2.00000e+00 ... 1.84317e+05 1.84318e+05
#  1.84319e+05]
# Iterate through the flattened indices
flatiter = len(indexarray)
for i in range(flatiter):
    if flatpointerarray[indexarray[i][0]] == 255.0:
        print(indexarray[i][1:])
        break

# Accessing the original array using the flattened indices
# Output: [0, 0, 0, 7, 31]


# Changing values

data = np.arange(100 * 100 * 100).reshape((100, 100, 100)).astype(np.int32)
indexarray, flatpointerarray = get_flat_iter_for_cython(data, dtype=np.int64, unordered=True)
print(flatpointerarray)
flatiter = len(indexarray)
for i in range(flatiter):
    if indexarray[i][3] % 2 == 0:
        flatpointerarray[indexarray[i][0]] = 10000000
print(data)
# [[[10000000        1 10000000 ...       97 10000000       99]
#   [10000000      101 10000000 ...      197 10000000      199]
#   [10000000      201 10000000 ...      297 10000000      299]
#   ...
#   [10000000     9701 10000000 ...     9797 10000000     9799]
#   [10000000     9801 10000000 ...     9897 10000000     9899]
#   [10000000     9901 10000000 ...     9997 10000000     9999]]

Project details


Release history Release notifications | RSS feed

This version

0.10

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cythonanyarray-0.10.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

cythonanyarray-0.10-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file cythonanyarray-0.10.tar.gz.

File metadata

  • Download URL: cythonanyarray-0.10.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for cythonanyarray-0.10.tar.gz
Algorithm Hash digest
SHA256 95ddca0e97c54c3427d99a19d77bb4ab317f6caf4088df6884d8bb4facf23407
MD5 9a3a42bd233c15eef72824709dd6491e
BLAKE2b-256 869291f91a760c1dc6c9bd576373d834edf40acbd3ece6cac8b94641cf3bd5f3

See more details on using hashes here.

File details

Details for the file cythonanyarray-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for cythonanyarray-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 1826ddfc6ede32c7651efc3ce0f3d454481801263d0eb0d67447a1763d0f02b7
MD5 0b11f8fb95e2e4fcff1b151ea89ccd8e
BLAKE2b-256 2d000803e47bdcd5a55bb616cb978e78deec67c46a2e4693ed7379250846509d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page