Skip to content

Add option to use 64-bit rapidhash for MacOS build #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

msricher
Copy link
Collaborator

@msricher msricher commented Mar 6, 2025

Uses 64-bit rapidhash implementation instead of 128-bit spookyhash. For playing around on a Mac laptop, this should be fine.

Sets up spookyhash or rapidhash based on whether the command uname -s outputs 'Darwin' (the MacOS kernel).

Uses 64-bit rapidhash implementation instead of 128-bit spookyhash.
For playing around on a Mac laptop, this should be fine.

Sets up spookyhash or rapidhash based on whether
the command `uname -s` outputs 'Darwin' (the MacOS kernal).
@msricher msricher requested review from FanwangM and marco-2023 March 6, 2025 14:18
@FanwangM
Copy link
Contributor

FanwangM commented Mar 6, 2025

Thanks for this improvement. I have tried to install the package in a MacOS which ends up with the following errors,

In [1]: import pyci
-------------------------------------------------------------------------
ModuleNotFoundError                     Traceback (most recent call last)
Cell In[1], line 1
----> 1 import pyci

File /xxxx/xxxxx/tmp_py311/lib/python3.11/site-packages/pyci/__init__.py:18
      1 # This file is part of PyCI.
      2 #
      3 # PyCI is free software: you can redistribute it and/or modify it under
   (...)
     13 # You should have received a copy of the GNU General Public License
     14 # along with PyCI. If not, see <http://www.gnu.org/licenses/>.
     16 r"""PyCI module."""
---> 18 from pyci._pyci import __version__, c_long, c_ulong, c_double
     19 from pyci._pyci import secondquant_op, wavefunction, one_spin_wfn, two_spin_wfn
     20 from pyci._pyci import doci_wfn, fullci_wfn, genci_wfn, sparse_op

ModuleNotFoundError: No module named 'pyci._pyci'

The package was installed with pip install . after cloning the package. @msricher

@msricher
Copy link
Collaborator Author

msricher commented Mar 6, 2025

Ok, this seems to be a problem with file paths. It must need to be installed to different directory, or with a different file extension, for it to work. Although it seems to compile now, which is the hard part.

@msricher
Copy link
Collaborator Author

msricher commented Mar 6, 2025

Can you try renaming the .so file to .dylib and trying again?

@FanwangM
Copy link
Contributor

FanwangM commented Mar 7, 2025

I don't fully understand what you mean by It must need to be installed in a different directory or with a different file extension for it to work. because I installed it with pip install ., which actually installed the pyci in the virtual environment.

Following your suggestions of renaming .so to .dylib, but I was not able to find any .so file from the source or the installed codes. Can you provide more explanations? Thanks.

@msricher

@msricher
Copy link
Collaborator Author

msricher commented Mar 7, 2025

The instruction say to run make before pip install ., which compiles the C extension module, pyci.so (or similar name). Can you do that and then try renaming it from .so to .dylib?

@FanwangM
Copy link
Contributor

FanwangM commented Mar 7, 2025

yes, I just did that. Currently, I got errors when running make. I tried to install rapidhash manually, but didn't figure out a solution. Any suggestions on how to address the issue? Thanks.

c++ -std=c++14 -Wall -Wextra -pipe -O3 -fPIC -flto=auto -fvisibility=hidden -pthread -Ipyci/include -Ixxxx/tmp_py311/include/python3.11 -Ixxxx/tmp_py311/lib/python3.11/site-packages/numpy/_core/include -Ideps/eigen -Ideps/spectra/include -Ideps/parallel-hashmap -Ideps/pybind11/include -undefined dynamic_lookup -D_PYCI_VERSION='0.6.1' -D_GIT_BRANCH='rapidhash_macos' -D_BUILD_TIME='2025-03-07 18:15:43' -D_COMPILER_VERSION='Apple clang version 16.0.0 (clang-1600.0.26.6)' -D_USE_RAPIDHASH='1' -c pyci/src/SpookyV2.cpp -o pyci/src/SpookyV2.o
clang++: warning: argument unused during compilation: '-undefined dynamic_lookup' [-Wunused-command-line-argument]
c++ -std=c++14 -Wall -Wextra -pipe -O3 -fPIC -flto=auto -fvisibility=hidden -pthread -Ipyci/include -Ixxxx/tmp_py311/include/python3.11 -Ixxxx/tmp_py311/lib/python3.11/site-packages/numpy/_core/include -Ideps/eigen -Ideps/spectra/include -Ideps/parallel-hashmap -Ideps/pybind11/include -undefined dynamic_lookup -D_PYCI_VERSION='0.6.1' -D_GIT_BRANCH='rapidhash_macos' -D_BUILD_TIME='2025-03-07 18:15:43' -D_COMPILER_VERSION='Apple clang version 16.0.0 (clang-1600.0.26.6)' -D_USE_RAPIDHASH='1' -c pyci/src/ap1rog.cpp -o pyci/src/ap1rog.o
clang++: warning: argument unused during compilation: '-undefined dynamic_lookup' [-Wunused-command-line-argument]
In file included from pyci/src/ap1rog.cpp:16:
pyci/include/pyci.h:45:10: fatal error: 'rapidhash.h' file not found
   45 | #include <rapidhash.h>
      |          ^~~~~~~~~~~~~
1 error generated.
make: *** [pyci/src/ap1rog.o] Error 1

@msricher
Copy link
Collaborator Author

msricher commented Mar 7, 2025 via email

@msricher
Copy link
Collaborator Author

msricher commented Mar 7, 2025

OK, it should build now. Can you try the latest commit, and then if not, by renaming the .so->.dylib?

@FanwangM
Copy link
Contributor

FanwangM commented Mar 8, 2025

Still got errors when run make. Here is part of the error messages,

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/sort.h:51:1: note: candidate template ignored: substitution failure [with _AlgPolicy = std::_ClassicAlgPolicy, _Compare = std::__less<void, void> &, _ForwardIterator = std::sort_with_arg::value_iterator_t<double, long>]
   51 | __sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) {
      | ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/sort.h:92:7: error: no matching function for call to object of type 'std::__less<void, void>'
   92 |   if (__c(*__x4, *__x3)) {
      |       ^~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/comp.h:40:60: note: candidate template ignored: substitution failure [with _Tp = value_reference_t<double, long>, _Up = value_reference_t<double, long>]
   40 |   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __lhs, const _Up& __rhs) const {
      |                                                            ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
1 warning and 20 errors generated.
make: *** [pyci/src/sparseop.o] Error 1

@msricher
Copy link
Collaborator Author

msricher commented Mar 8, 2025

Okay, I can't debug it over messages, I really need access to a Mac or someone with a Mac and proficiency in C++. It might be an issue with the CFLAGS in the Makefile, but I'd have to play around with them. Thanks for trying, though.

@msricher
Copy link
Collaborator Author

I've made another commit that might work. @RichRick1 would you also mind trying this?

@RichRick1
Copy link
Collaborator

So I basically have the same error that @FanwangM has
I tried to modify the makefile and it seems to be compiled correctly(?). What I mean by that is there were no errors after running make

Here is a make file

# Setup
# -----

# Set C compiler executable
CC ?= cc
export CC

# Set C++ compiler executable
CXX ?= c++
export CXX

# Set Python executable
PYTHON ?= python3

# Set C++ compile flags
CFLAGS := -std=c++14 -Wall -Wextra -pipe -O3
CFLAGS += -fPIC -flto=auto -fvisibility=hidden
CFLAGS += -pthread
CFLAGS += -Ipyci/include

ifneq ($(MAKE_NATIVE),)
CFLAGS += -mavx -mavx2 -msse4.2 -march=native -mtune=native
endif

# Set Python include directories
CFLAGS += $(shell $(PYTHON) tools/python_include_dirs.py)

# Set external projects and their include directories
DEPS := $(addprefix deps/,eigen spectra parallel-hashmap pybind11 rapidhash)
CFLAGS += $(addprefix -Ideps/,eigen spectra/include parallel-hashmap pybind11/include rapidhash)

# This C++ compile flag is needed in order for Macs to find system libraries
ifeq ($(shell uname -s),Darwin)
CFLAGS += -undefined dynamic_lookup
PYCI_EXTENSION := dylib
else
PYCI_EXTENSION := so
endif

# Set PyCI version number
VERSION_MAJOR := 0
VERSION_MINOR := 6
VERSION_PATCH := 1
PYCI_VERSION := $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)

# Set preprocessor directives
DEFS := -D_PYCI_VERSION='$(PYCI_VERSION)'

DEFS += -D_GIT_BRANCH='$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "no_git_repo")'
# DEFS += -D_GIT_BRANCH='$(shell git rev-parse --abbrev-ref HEAD)'
DEFS += -D_BUILD_TIME='$(shell date -u +%F\ %T)'
DEFS += -D_COMPILER_VERSION='$(shell $(CXX) --version | head -n 1)'
ifeq ($(shell uname -s),Darwin)
DEFS += -D_USE_RAPIDHASH='1'
endif

# Set objects
OBJECTS := $(patsubst %.cpp,%.o,$(wildcard pyci/src/*.cpp))


# Make commands
# -------------

.PHONY: all
all: pyci/_pyci.$(PYCI_EXTENSION).$(PYCI_VERSION) pyci/_pyci.$(PYCI_EXTENSION).$(VERSION_MAJOR) pyci/_pyci.$(PYCI_EXTENSION)

.PHONY: test
test:
	@set -e; $(PYTHON) -m pytest -sv ./pyci

.PHONY: clean
clean:
	rm -rf pyci/src/*.o pyci/_pyci.$(PYCI_EXTENSION)*

.PHONY: cleandeps
cleandeps:
	rm -rf deps

.PHONY: compile_flags.txt
compile_flags.txt:
	echo "$(CFLAGS)" | tr ' ' '\n' > $(@)


# Make targets
# ------------

pyci/src/%.o: pyci/src/%.cpp pyci/include/pyci.h $(DEPS)
	$(CXX) $(CFLAGS) $(DEFS) -c $(<) -o $(@)

pyci/_pyci.$(PYCI_EXTENSION).$(PYCI_VERSION): $(OBJECTS)
	$(CXX) $(CFLAGS) $(DEFS) -shared $(^) -o $(@)

pyci/_pyci.$(PYCI_EXTENSION).$(VERSION_MAJOR): pyci/_pyci.$(PYCI_EXTENSION).$(PYCI_VERSION)
	ln -sf $(notdir $(<)) $(@)

pyci/_pyci.$(PYCI_EXTENSION): pyci/_pyci.$(PYCI_EXTENSION).$(PYCI_VERSION)
	ln -sf $(notdir $(<)) $(@)

deps/eigen:
	[ -d $@ ] || git clone https://gitlab.com/libeigen/eigen.git $@

deps/spectra:
	[ -d $@ ] || git clone https://github.com/yixuan/spectra.git $@

deps/parallel-hashmap:
	[ -d $@ ] || git clone https://github.com/greg7mdp/parallel-hashmap.git $@

deps/pybind11:
	[ -d $@ ] || git clone https://github.com/pybind/pybind11.git $@

deps/rapidhash:
	[ -d $@ ] || git clone https://github.com/nicoshev/rapidhash.git $@

However now it doesn't see the the pyci library now

The error I get is >>> import pyci
Traceback (most recent call last):
File "", line 1, in
File "/Users/valeriichuiko/Desktop/projects/playground/PyCI/pyci/init.py", line 18, in
from pyci._pyci import version, c_long, c_ulong, c_double
ModuleNotFoundError: No module named 'pyci._pyci'

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.

3 participants