Skip to content
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

adapt Libint library for Windows #270

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adapt Libint library and buildsystem for Windows
  • Loading branch information
loriab committed Sep 18, 2023
commit cd1ec2c41db7229a27db6ec9216803829d8a4412
23 changes: 23 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ jobs:
# note full paths depend on setup-miniconda:
# * Miniforge is miniconda3 vs. Miniconda is miniconda

- runs-on: windows-latest
lane: windows-clang-cl
libargs: >
-GNinja
-DCMAKE_BUILD_TYPE=Release
-DBUILD_SHARED_LIBS=OFF
-DCMAKE_CXX_COMPILER=clang-cl
-DCMAKE_C_COMPILER=clang-cl
testargs: >
-GNinja
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_COMPILER=clang-cl
-DCMAKE_C_COMPILER=clang-cl

- runs-on: macos-latest
lane: macos-clang
libargs: >
Expand Down Expand Up @@ -217,6 +231,9 @@ jobs:
- pybind11
#- dpcpp_linux-64
EOF
if [[ "${{ runner.os }}" == "Windows" ]]; then
sed -i "s/- cxx/#- cxx/g" export.yaml
fi
if [[ "${{ matrix.cfg.lane }}" == "ubuntu-intel" ]]; then
sed -i "s/#- dpcpp_linux-64/- dpcpp_linux-64/g" export.yaml
fi
Expand All @@ -239,6 +256,12 @@ jobs:
conda info
conda list

- name: Prepare compiler environment for Windows
if: ${{ runner.os == 'Windows' }}
uses: ilammy/msvc-dev-cmd@v1
with:
arch: x64

- uses: actions/download-artifact@v3
with:
name: Linux-g++-10
Expand Down
54 changes: 53 additions & 1 deletion export/cmake/CMakeLists.txt.export
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,29 @@ if(BUILD_SHARED_LIBS OR LIBINT2_BUILD_SHARED_AND_STATIC_LIBS OR LIBINT2_PYTHON)
set_target_properties(libint2_obj PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

if (MSVC)
set_target_properties(
libint2_obj
PROPERTIES
# Increase stack size from 1 MB to 4 MB
LINK_FLAGS "/STACK:4194304"
)
endif()

# shared and static libraries built from the same object files
if (LIBINT2_BUILD_SHARED_AND_STATIC_LIBS)
add_library(libint2 SHARED $<TARGET_OBJECTS:libint2_obj>)
set_target_properties(libint2 PROPERTIES LIBRARY_OUTPUT_NAME int2)
if(MSVC)
target_compile_definitions(libint2 PUBLIC _USE_MATH_DEFINES)
target_compile_options(libint2 PUBLIC "/EHsc")
endif()
add_library(libint2-static STATIC $<TARGET_OBJECTS:libint2_obj>)
set_target_properties(libint2-static PROPERTIES ARCHIVE_OUTPUT_NAME int2)
if(MSVC)
target_compile_definitions(libint2-static PUBLIC _USE_MATH_DEFINES)
target_compile_options(libint2-static PUBLIC "/EHsc")
endif()
target_include_directories(libint2-static INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/libint2>
Expand Down Expand Up @@ -279,10 +296,40 @@ if (LIBINT_HAS_CXX_API)
if (LIBINT_HAS_SYSTEM_BOOST_PREPROCESSOR_VARIADICS)
target_link_libraries(libint2_cxx INTERFACE Boost::boost)
endif(LIBINT_HAS_SYSTEM_BOOST_PREPROCESSOR_VARIADICS)
if(MSVC)
# MSVC does not include <cmath> constants, unless _USE_MATH_DEFINES is defined.
# _CRT_* to squash some getenv, strdup, strncpy, ctime, fopen warnings
target_compile_definitions(
libint2_cxx
INTERFACE
_USE_MATH_DEFINES
_CRT_NONSTDC_NO_DEPRECATE
_CRT_NONSTDC_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS
)
# Set the exception handling model
target_compile_options(
libint2_cxx
INTERFACE
"/EHsc"
)
endif()
get_filename_component(DATADIR_ABSOLUTE "${CMAKE_INSTALL_PREFIX}/${LIBINT2_INSTALL_DATADIR}" ABSOLUTE)
target_compile_definitions(libint2_cxx INTERFACE
$<BUILD_INTERFACE:SRCDATADIR="${PROJECT_SOURCE_DIR}/lib/basis">
$<INSTALL_INTERFACE:DATADIR="${DATADIR_ABSOLUTE}">)
)
if (NOT MSVC)
# TODO fix the DATADIR define escaping on Windows
# * below works fine in tests
# * but fails in Psi4 compile
# * prefix replacement in conda used instead on Windows
# * LIBINT2_INSTALL_DATADIR -> LIBINT2_INSTALL_BASISDIR
target_compile_definitions(
libint2_cxx
INTERFACE
$<INSTALL_INTERFACE:DATADIR="\$\{_IMPORT_PREFIX\}/${LIBINT2_INSTALL_DATADIR}">
)
endif()
# Add library to the list of installed components
install(TARGETS libint2_cxx EXPORT libint2
COMPONENT cxx
Expand All @@ -303,6 +350,11 @@ add_custom_target_subproject(libint2 check USES_TERMINAL COMMAND ${CMAKE_CTEST_C
add_executable(eritest-libint2 EXCLUDE_FROM_ALL tests/eri/test.cc)
target_link_libraries(eritest-libint2 ${int2_library})
target_include_directories(eritest-libint2 PRIVATE ${PROJECT_SOURCE_DIR}/tests/eri)
if(MSVC)
# TODO on future target pass, def should be added to the L2 target, not added to test
target_compile_definitions(eritest-libint2 PUBLIC _USE_MATH_DEFINES)
target_compile_options(eritest-libint2 PUBLIC "/EHsc")
endif()

add_test(libint2/eritest/build "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target eritest-libint2)
set_tests_properties(libint2/eritest/build PROPERTIES FIXTURES_SETUP LIBINT2_ERITEST_EXEC)
Expand Down
20 changes: 16 additions & 4 deletions include/libint2/basis.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@

#include <sys/types.h>
#include <sys/stat.h>
#ifdef _MSC_VER
#include <io.h>
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
#define PATH_SEPARATOR "\\"
#else
#include <unistd.h>
#define PATH_SEPARATOR "/"
#endif

#include <libint2.h>
#include <libint2/shell.h>
Expand Down Expand Up @@ -139,7 +146,7 @@ namespace libint2 {

// read in ALL basis set components
for(const auto& basis_component_name: basis_component_names) {
auto file_dot_g94 = basis_lib_path + "/" + basis_component_name + ".g94";
auto file_dot_g94 = basis_lib_path + PATH_SEPARATOR + basis_component_name + ".g94";

// use same cartesian_d convention for all components!
component_basis_sets.emplace_back(read_g94_basis_library(file_dot_g94, force_cartesian_d, throw_if_no_match));
Expand All @@ -161,7 +168,7 @@ namespace libint2 {
}
else if (throw_if_no_match) { // not found? throw, if needed
std::string errmsg(std::string("did not find the basis for this Z in ") +
basis_lib_path + "/" + basis_component_names[comp_idx] + ".g94");
basis_lib_path + PATH_SEPARATOR + basis_component_names[comp_idx] + ".g94");
throw std::logic_error(errmsg);
}
} // basis component loop
Expand Down Expand Up @@ -411,7 +418,7 @@ namespace libint2 {
#endif
}
// validate basis_path = path + "/basis"
std::string basis_path = path + std::string("/basis");
std::string basis_path = path + PATH_SEPARATOR + std::string("basis");
bool error = true;
std::error_code ec;
auto validate_basis_path = [&basis_path, &error, &ec]() -> void {
Expand Down Expand Up @@ -459,7 +466,12 @@ namespace libint2 {
static std::vector<std::vector<libint2::Shell>> read_g94_basis_library(std::string file_dot_g94,
bool force_cartesian_d = false,
bool throw_if_missing = true,
std::string locale_name = std::string("POSIX")) {
#ifdef _MSC_VER
std::string locale_name = std::string("en-US")
#else
std::string locale_name = std::string("POSIX") // "en_US"
#endif
) {

std::locale locale(locale_name.c_str()); // TODO omit c_str() with up-to-date stdlib
std::vector<std::vector<libint2::Shell>> ref_shells(118); // 118 = number of chemical elements
Expand Down
13 changes: 10 additions & 3 deletions include/libint2/boys.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
#include <type_traits>
#include <memory>

#ifdef _MSC_VER
#define posix_memalign(p, a, s) (((*(p)) = _aligned_malloc((s), (a))), *(p) ?0 :errno)
#define posix_memfree(p) ((_aligned_free((p))))
#else
#define posix_memfree(p) ((free((p))))
#endif

// from now on at least C++11 is required by default
#include <libint2/util/cxxstd.h>
#if LIBINT2_CPLUSPLUS_STD < 2011
Expand Down Expand Up @@ -290,7 +297,7 @@ namespace libint2 {
}
~FmEval_Chebyshev7() {
if (mmax >= 0) {
free(c);
posix_memfree(c);
}
}

Expand Down Expand Up @@ -881,7 +888,7 @@ namespace libint2 {

~TennoGmEval() {
if (c_ != nullptr)
free(c_);
posix_memfree(c_);
}

/// Singleton interface allows to manage the lone instance; adjusts max m values as needed in thread-safe fashion
Expand Down Expand Up @@ -1313,7 +1320,7 @@ namespace libint2 {

// get memory
void* result;
int status = posix_memalign(&result, std::max(sizeof(Real), 32ul), (mmax_ - mmin_ + 1) * cheb_table_nintervals * ORDERp1 * ORDERp1 * sizeof(Real));
int status = posix_memalign(&result, std::max(sizeof(Real), (size_t)32), (mmax_ - mmin_ + 1) * cheb_table_nintervals * ORDERp1 * ORDERp1 * sizeof(Real));
if (status != 0) {
if (status == EINVAL)
throw std::logic_error(
Expand Down
4 changes: 4 additions & 0 deletions include/libint2/util/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include <cstdlib>
#include <libint2/util/generated/libint2_params.h>

#ifdef _MSC_VER
#define posix_memalign(p, a, s) (((*(p)) = _aligned_malloc((s), (a))), *(p) ?0 :errno)
#endif

namespace libint2 {

/// Aligned version of malloc().
Expand Down
4 changes: 3 additions & 1 deletion include/libint2/util/vector_x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#include <libint2/util/cxxstd.h>
#include <libint2/util/type_traits.h>

#if defined(__SSE2__) || defined(__SSE__) || defined(__AVX__)
#if defined(_MSC_VER)
# include <intrin.h>
#elif defined(__SSE2__) || defined(__SSE__) || defined(__AVX__)
# include <x86intrin.h>
#endif

Expand Down
23 changes: 22 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ if (pybind11_VERSION VERSION_LESS_EQUAL 2.5.0)
set(CMAKE_CXX_STANDARD 17)
endif()

if (pybind11_VERSION VERSION_GREATER_EQUAL 2.11.0)
# ipo/lto triggering changed https://github.com/pybind/pybind11/pull/4643
if ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC"))
# clang-cl

# this variable needs to be defined, not the property
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)
endif()
endif()

pybind11_add_module(
libint2-python MODULE
#EXCLUDE_FROM_ALL
Expand All @@ -52,7 +62,12 @@ if (pybind11_VERSION VERSION_GREATER_EQUAL 2.7.0)
target_compile_definitions(libint2-python PRIVATE NDEBUG=1)
endif()

add_compile_options(-Wall)
target_compile_options(libint2-python
PRIVATE
# too many warnings on Windows
# $<$<CXX_COMPILER_ID:MSVC>:/W4>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall>
)

#find_package(Eigen3 3.3 REQUIRED)

Expand All @@ -65,6 +80,12 @@ set_target_properties(

if (TARGET libint2_obj)
set(libint2_python_target libint2_obj)

if(MSVC)
target_compile_definitions(libint2-python PUBLIC _USE_MATH_DEFINES)
target_compile_options(libint2-python PUBLIC "/EHsc")
endif()

target_link_libraries(libint2-python PRIVATE libint2_obj)
target_link_libraries(libint2-python PRIVATE Boost::boost)
else()
Expand Down
6 changes: 6 additions & 0 deletions python/src/libint2/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <atomic>
#include <thread>

#if defined(_MSC_VER)
#include <BaseTsd.h>
// handles ssize_t in pybind11/numpy.h
typedef SSIZE_T ssize_t;
#endif

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
Expand Down
6 changes: 6 additions & 0 deletions python/src/libint2/libint2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <tuple>
#include <vector>

#if defined(_MSC_VER)
#include <BaseTsd.h>
// handles ssize_t in pybind11/numpy.h
typedef SSIZE_T ssize_t;
#endif

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
Expand Down
37 changes: 36 additions & 1 deletion tests/eri/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,42 @@

#include <iostream>
#include <cmath>
#include <sys/time.h>
#ifdef _MSC_VER
// thanks, https://stackoverflow.com/a/26085827
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
# include <stdint.h> // portable: uint64_t MSVC: __int64

// MSVC defines this in winsock2.h!?
typedef struct timeval {
long tv_sec;
long tv_usec;
} timeval;

int gettimeofday(struct timeval * tp, struct timezone * tzp)
{
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);

SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;

GetSystemTime( &system_time );
SystemTimeToFileTime( &system_time, &file_time );
time = ((uint64_t)file_time.dwLowDateTime ) ;
time += ((uint64_t)file_time.dwHighDateTime) << 32;

tp->tv_sec = (long) ((time - EPOCH) / 10000000L);
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
return 0;
}
#else // _MSC_VER
# include <sys/time.h>
#endif // _MSC_VER
#include <cassert>

#include <libint2.h>
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test-2body.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# include <test_eri/eri.h>
#endif

typedef unsigned int uint;

TEST_CASE("Slater/Yukawa integrals", "[engine][2-body]") {

std::vector<Shell> obs{
Expand Down