Skip to content

use dll wrappers on windows#3241

Merged
sunqm merged 7 commits into
pyscf:masterfrom
jeanwsr:win-dll-wrap
Jun 7, 2026
Merged

use dll wrappers on windows#3241
sunqm merged 7 commits into
pyscf:masterfrom
jeanwsr:win-dll-wrap

Conversation

@jeanwsr

@jeanwsr jeanwsr commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

corresponds to patch 1, 2 and 4 of conda-forge/pyscf-feedstock#63, and passes the win-64 tests there.

  1. DLL wrapping on Windows
    On Unix, ctypes' getattr(dll, name) searches a flat process-wide symbol namespace, which means a function compiled into libcint.so is visible when accessing libcgto.so. On Windows, it is not visible. The fix: whenever a library like libcgto is loaded on Windows, it's wrapped via make_dll_wrapper() with its dependency libraries as fallbacks for symbol lookup. For example, pyscf/gto/moleintor.py now wraps libcgto with libcint as fallback, and pyscf/dft/numint.py wraps libdft with libcvhf, libcgto, libcint as fallbacks.
    UPDATE: the wrap is now automatically considered in load_library.
  2. _ctypes.dlsym replaced with ctypes.cast(getattr(...))
    _ctypes.dlsym(handle, name) is a CPython internal that resolves symbols by name from a raw dlopen handle. It exists on Linux/macOS but Windows has no equivalent.
    The fix: ctypes.cast(getattr(lib, name), ctypes.c_void_p) achieves the same result by accessing the function via the wrapper's __getattr__, then casting the returned function pointer to c_void_p.
    3. Reusing wrapped library objects instead of reloading
    Some modules were calling lib.load_library() again on their own, creating a separate unwrapped copy. Fixed by importing the already-wrapped object from the canonical module, e.g., pyscf/ao2mo/nrr_outcore.py now imports libao2mo from _ao2mo instead of reloading.

    4. Tests fixed to use wrapped libraries
    Five test files were calling lib.load_library() directly. Fixed by importing from the module that already provides the wrapped library.
  3. Platform-agnostic memory reporting in lib/misc.py
    current_memory() used Linux-only /proc. Fixed by adding a psutil fallback. Also added conda-typical library search paths to load_library() for Windows.
  4. Update cmake options for win-64 conda-forge build (gnu only).

@jeanwsr jeanwsr mentioned this pull request Jun 6, 2026
9 tasks
@jeanwsr jeanwsr marked this pull request as ready for review June 6, 2026 10:40
Comment thread pyscf/ao2mo/_ao2mo.py
Comment thread pyscf/lib/misc.py
#Fixme, the standard resource module gives wrong number when objects are released
# http://fa.bianp.net/blog/2013/different-ways-to-get-memory-consumption-or-lessons-learned-from-memory_profiler/#fn:1
#or use slow functions as memory_profiler._get_memory did
CLOCK_TICKS = os.sysconf("SC_CLK_TCK")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

create the two variables in the module space. In the case windows, set them to None

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

ok, but is CLOCK_TICKS used anywhere?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not in the PySCF package. No harm to keep it there in case it's used by other code built on top of PySCF for now. In the future, it can be cleaned up when an explicit __all__ or explicit documentation for these parameters are introduced.

@sunqm

sunqm commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

please also update pyproject.toml, to include the dependency
"psutils; sys_platform == 'win32'"

@sunqm sunqm merged commit d6af026 into pyscf:master Jun 7, 2026
6 of 7 checks passed
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.

2 participants