Skip to content

Implementing buildDescriptor from vlsvvtkreader as a C function#426

Open
lassejsc wants to merge 20 commits intofmihpc:devfrom
lassejsc:pythoncapibinding_vkt
Open

Implementing buildDescriptor from vlsvvtkreader as a C function#426
lassejsc wants to merge 20 commits intofmihpc:devfrom
lassejsc:pythoncapibinding_vkt

Conversation

@lassejsc
Copy link
Contributor

@lassejsc lassejsc commented Feb 13, 2026

UPDATE: the cpphelpers is going into the back end but this adds import and call of it into the buildDescriptor() function, the pure python version is still kept as buildDescriptorPython()

Pure python buildDescriptor() is very slow, for larger files I tested from FID run this was ~60(maybe +) seconds, in contrast with the new cpphelper buildDescriptor this time is closer to under ~10 seconds (tested on turso-carrington). For smaller files the difference isn't as big, but testing with some files I found that the speed up is somewhere around 3-9x overall for files where the time was already in tens of seconds, for smaller files the difference is probably closer to 20% since at that point the overhead starts to be a more of a thing. I tested random files (8 for each) across runs FID,FIJ,ABC,BED,FHA and the overall average speedup was at slowest 1.30x for smaller files and for larger files it was around 9x.

What is happening here is extension of Python with C, see https://docs.python.org/3/extending/extending.html

With the Python C API we first convert all the python objects to C objects, we do the descriptor building for loops etc that were slow in pure python and we convert back to the python objects and these get passed directly back into Python seamlessly.

Inside python this is just a regular function call, only caveat is that the new modules needs to get built and imported first.

(Note that all this can also be done with pybind11/nano apparently but this also works fine, still testing if this is fragile in different platforms, so far it has behaved nicely on local machine,turso-carrington frontend, hile, my phone )

The compilation uses setuptools (also no it is not deprecated, only the command python setup.py install and build are, see https://packaging.python.org/en/latest/discussions/setup-py-deprecated/). Setuptools should also accept the env variables CC, CXX, etc for setting which compiler to use.

EDIT: So far tested on HILE and turso-carrington, both work and compile fine, though on HILE you need to module load gcc-native it seems. Seems like this is not necessary if you set the env flags to CC=cc,CXX=CC, however you may need to recompile matplotlib as that had some issue where if you imported this module first and then matplotlib it would segfault at some compiled matplotlib binary.

Recompiling matplotlib can be done with pip install --no-cache-dir --no-binary :all: matplotlib after setting env variables CC,CXX correctly as above.

@lassejsc lassejsc closed this Feb 13, 2026
@lassejsc lassejsc reopened this Feb 13, 2026
name="cpphelpers",
version="1.0",
ext_modules=[Extension("cpphelpers", sources=["cpphelpers.cpp"])],
include_dirs=[numpy.get_include()]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numpy is not currently needed i think but keeping this still here if new stuff gets added to cpphelpers

additionally shortened the test.py which may or may not be removed
entirely in the end
@lassejsc lassejsc marked this pull request as ready for review February 18, 2026 15:50
@lassejsc lassejsc marked this pull request as draft February 25, 2026 14:31
@lassejsc lassejsc marked this pull request as ready for review February 27, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant