Skip to content

Commit

Permalink
Dev/serialization (#85)
Browse files Browse the repository at this point in the history
* Injector serialization via pickle working

* Remove cruft

* Undo addition to DecaySignature

* Remove printouts in DetectorModel serialization

* Remove unnecessary cereal macros

* Revert changes to link ordering

* Remove comments

* Don't need global FPIC now that it is handled with an INTERFACE target

* Attempt to perform explicit template instantiation for interpolator templates

* CMake fixes

* Improve logic for finding cfitsio. Make an IMPORT target for cfitsio

* Use lists for find_path

* Search all paths in secondary searches

* Remove printouts

* Remove redundant paths for find_library

* Link photospline with CFITSIO. Use generator expressions for -s and -stdlib=libc++

* Set policies for macos

* Use photospline from icecube git repo. Update photospline and cereal

* Remove printouts

* Upgrade pybind11

* Use shared_ptr for compatibility of classes without a default constructor

* Pickle weighter

* Move external headers

* pybind11 used in pickle serialization

* Pickle serialization for dataclasses

* Revert "Upgrade pybind11"

This reverts commit add5a6a.
  • Loading branch information
austinschneider authored Oct 23, 2024
1 parent f74be46 commit 82e4905
Show file tree
Hide file tree
Showing 80 changed files with 861 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
url = https://github.com/austinschneider/delabella.git
[submodule "vendor/photospline"]
path = vendor/photospline
url = https://github.com/austinschneider/photospline.git
url = https://github.com/icecube/photospline.git
[submodule "vendor/NamedType"]
path = vendor/NamedType
url = https://github.com/joboccara/NamedType.git
91 changes: 61 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
cmake_policy(VERSION 3.20)

if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_POLICY_DEFAULT_CMP0042 NEW)
cmake_policy(SET CMP0042 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0068 NEW)
cmake_policy(SET CMP0068 NEW)
set(MACOSX TRUE)
endif()

Expand Down Expand Up @@ -36,24 +40,50 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()

# Create an interface library for SIREN compile options
add_library(siren_compile_options INTERFACE)
function(apply_siren_compile_options target)
# Check if the target is an INTERFACE target
get_target_property(target_type ${target} TYPE)
get_target_property(target_imported ${target} IMPORTED)

# Specify the compile options
target_compile_options(siren_compile_options INTERFACE
-O2
-Wall
-fPIC
$<$<CONFIG:Debug>:-g>
$<$<CONFIG:Debug>:-O0>
$<$<CONFIG:Release>:-O2>
$<$<CONFIG:Release>:-s>
)
if("${target_type}" STREQUAL "INTERFACE_LIBRARY")
# Apply compile options to INTERFACE target
target_compile_options(${target} INTERFACE
-O2
-Wall
-fPIC
$<$<CONFIG:Debug>:-g>
$<$<CONFIG:Debug>:-O0>
$<$<CONFIG:Release>:-O2>
$<$<AND:$<COMPILE_LANG_AND_ID:CXX,GNU>,$<CONFIG:Release>>:-s>
$<$<COMPILE_LANG_AND_ID:CXX,Clang>:-stdlib=libc++>
)
elseif(target_imported)
# Apply compile options to non-INTERFACE target (PRIVATE or PUBLIC)
target_compile_options(${target} INTERFACE
-O2
-Wall
-fPIC
$<$<CONFIG:Debug>:-g>
$<$<CONFIG:Debug>:-O0>
$<$<CONFIG:Release>:-O2>
$<$<AND:$<COMPILE_LANG_AND_ID:CXX,GNU>,$<CONFIG:Release>>:-s>
$<$<COMPILE_LANG_AND_ID:CXX,Clang>:-stdlib=libc++>
)
else()
# Apply compile options to non-INTERFACE target (PRIVATE or PUBLIC)
target_compile_options(${target} PRIVATE
-O2
-Wall
-fPIC
$<$<CONFIG:Debug>:-g>
$<$<CONFIG:Debug>:-O0>
$<$<CONFIG:Release>:-O2>
$<$<AND:$<COMPILE_LANG_AND_ID:CXX,GNU>,$<CONFIG:Release>>:-s>
$<$<COMPILE_LANG_AND_ID:CXX,Clang>:-stdlib=libc++>
)
endif()
endfunction()

# Conditionally add -stdlib=libc++ for Clang
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(siren_compile_options INTERFACE -stdlib=libc++)
endif()

# override install locations when building python extensions
if(DEFINED SKBUILD_PLATLIB_DIR)
Expand All @@ -75,40 +105,44 @@ include(pybind11)
# load project dependencies
include(rk)
if(TARGET rk_static)
target_link_libraries(rk_static INTERFACE siren_compile_options)
apply_siren_compile_options(rk_static)
endif()
if(TARGET rk_shared)
target_link_libraries(rk_shared INTERFACE siren_compile_options)
apply_siren_compile_options(rk_shared)
endif()
include(cereal)
if(TARGET cereal)
target_link_libraries(cereal INTERFACE siren_compile_options)
apply_siren_compile_options(cereal)
endif()
include(delabella)
if(TARGET delabella_static)
target_link_libraries(delabella_static INTERFACE siren_compile_options)
apply_siren_compile_options(delabella_static)
endif()
if(TARGET delabella_shared)
target_link_libraries(delabella_shared INTERFACE siren_compile_options)
apply_siren_compile_options(delabella_shared)
endif()
include(CFITSIO)
if(TARGET CFITSIO)
apply_siren_compile_options(CFITSIO)
endif()
include(photospline)
if(TARGET photospline)
target_link_libraries(photospline INTERFACE siren_compile_options)
apply_siren_compile_options(photospline)
target_link_libraries(photospline PUBLIC CFITSIO)
endif()
include(googletest)
if(TARGET gtest)
target_link_libraries(gtest INTERFACE siren_compile_options)
apply_siren_compile_options(gtest)
endif()
if(TARGET gtest_main)
target_link_libraries(gtest_main INTERFACE siren_compile_options)
apply_siren_compile_options(gtest_main)
endif()
if(TARGET gmock)
target_link_libraries(gmock INTERFACE siren_compile_options)
apply_siren_compile_options(gmock)
endif()
include(NamedType)
if(TARGET NamedType)
target_link_libraries(NamedType INTERFACE siren_compile_options)
apply_siren_compile_options(NamedType)
endif()

# load macros for googletest
Expand All @@ -128,7 +162,7 @@ add_subdirectory(projects/injection)
# define the target library
add_library(SIREN SHARED)
set_property(TARGET SIREN PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(SIREN INTERFACE siren_compile_options)
apply_siren_compile_options(SIREN)

if(DEFINED MACOSX AND MACOSX)
if(CMAKE_VERSION VERSION_LESS 3.13)
Expand Down Expand Up @@ -169,9 +203,6 @@ target_link_libraries(SIREN
)
endif()

# Export siren_compile_options
install(TARGETS siren_compile_options EXPORT ${PROJECT_NAME}Config)

# define the install path normally or for python package
if(DEFINED SKBUILD_PLATLIB_DIR)
set_target_properties(SIREN PROPERTIES
Expand Down
152 changes: 101 additions & 51 deletions cmake/Packages/CFITSIO.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,105 @@
# CFITSIO_LDFLAGS #
################################################################################

SET (CFITSIO_FIND_QUIETLY TRUE)
SET (CFITSIO_FIND_REQUIRED TRUE)

IF (NOT CFITSIO_FOUND)

# Search user environment for headers, then default paths; extract version
FIND_PATH (CFITSIO_INCLUDE_DIR fitsio.h
PATHS $ENV{CFITSIOROOT}/include
NO_DEFAULT_PATH)
FIND_PATH (CFITSIO_INCLUDE_DIR fitsio.h)
if(CFITSIO_INCLUDE_DIR)
GET_FILENAME_COMPONENT (CFITSIOROOT ${CFITSIO_INCLUDE_DIR} PATH)
else()
FIND_PATH (CFITSIO_INCLUDE_DIR cfitsio/fitsio.h
PATHS $ENV{CFITSIOROOT}/include
)
SET(CFITSIO_INCLUDE_DIR "${CFITSIO_INCLUDE_DIR}/cfitsio" CACHE PATH "Path to cfitsio headers" FORCE)
endif()

SET (CFITSIO_VERSION 0)
IF (CFITSIO_INCLUDE_DIR)
FILE (READ "${CFITSIO_INCLUDE_DIR}/fitsio.h" _cfitsio_VERSION)
STRING (REGEX REPLACE ".*define CFITSIO_VERSION ([0-9]+\\.[0-9]+).*" "\\1"
CFITSIO_VERSION "${_cfitsio_VERSION}")
ENDIF (CFITSIO_INCLUDE_DIR)

# Search user environment for libraries, then default paths
FIND_LIBRARY (CFITSIO_LIBRARIES NAMES cfitsio
PATHS $ENV{CFITSIOROOT}/lib
NO_DEFAULT_PATH)
FIND_LIBRARY (CFITSIO_LIBRARIES NAMES cfitsio)
GET_FILENAME_COMPONENT (CFITSIO_LIB_DIR ${CFITSIO_LIBRARIES} PATH)

# Set CFITSIO_FOUND and error out if cfitsio is not found
INCLUDE (FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS (CFITSIO
DEFAULT_MSG CFITSIO_LIBRARIES CFITSIO_INCLUDE_DIR)
ADD_DEFINITIONS ("-I${CFITSIO_INCLUDE_DIR}")

IF (CFITSIO_FOUND)
# Set flags and print a status message
MESSAGE (STATUS "CFITSIO version ${CFITSIO_VERSION} found:")

SET (CFITSIO_CPPFLAGS "-I${CFITSIO_INCLUDE_DIR}")
SET (CFITSIO_LDFLAGS "${CFITSIO_LIBRARIES}")

MESSAGE (STATUS " * includes: ${CFITSIO_INCLUDE_DIR}")
MESSAGE (STATUS " * libs: ${CFITSIO_LIBRARIES}")
ENDIF (CFITSIO_FOUND)

ENDIF (NOT CFITSIO_FOUND)
include(FindPackageHandleStandardArgs)

set(CFITSIO_FIND_QUIETLY TRUE)
set(CFITSIO_FIND_REQUIRED TRUE)

if (NOT CFITSIO_FOUND)
# Manually parse CPLUS_INCLUDE_PATH to add paths to search
if (DEFINED ENV{CPLUS_INCLUDE_PATH})
string(REPLACE ":" ";" CFITSIO_INCLUDE_SEARCH_PATH_LIST "$ENV{CPLUS_INCLUDE_PATH}")
else()
set(CFITSIO_INCLUDE_SEARCH_PATH_LIST "")
endif()

list(PREPEND CFITSIO_INCLUDE_SEARCH_PATH_LIST
$ENV{CFITSIOROOT}/include
)

# Search user environment for headers, then default paths; extract version
find_path(CFITSIO_INCLUDE_DIR fitsio.h
PATHS ${CFITSIO_INCLUDE_SEARCH_PATH_LIST}
NO_DEFAULT_PATH
)
if(NOT CFITSIO_INCLUDE_DIR)
unset(CFITSIO_INCLUDE_DIR)
find_path(CFITSIO_INCLUDE_DIR fitsio.h)
endif()

if(NOT CFITSIO_INCLUDE_DIR)
unset(CFITSIO_INCLUDE_DIR)
find_path(CFITSIO_INCLUDE_DIR cfitsio/fitsio.h
PATHS ${CFITSIO_INCLUDE_SEARCH_PATH_LIST}
NO_DEFAULT_PATH
)
if(CFITSIO_INCLUDE_DIR)
set(CFITSIO_INCLUDE_DIR "${CFITSIO_INCLUDE_DIR}/cfitsio" CACHE PATH "Path to cfitsio headers" FORCE)
endif()
endif()

if(NOT CFITSIO_INCLUDE_DIR)
unset(CFITSIO_INCLUDE_DIR)
find_path(CFITSIO_INCLUDE_DIR cfitsio/fitsio.h)
if(CFITSIO_INCLUDE_DIR)
set(CFITSIO_INCLUDE_DIR "${CFITSIO_INCLUDE_DIR}/cfitsio" CACHE PATH "Path to cfitsio headers" FORCE)
endif()
endif()

if (CFITSIO_INCLUDE_DIR AND EXISTS "${CFITSIO_INCLUDE_DIR}/fitsio.h")
get_filename_component(CFITSIOROOT ${CFITSIO_INCLUDE_DIR} PATH)
set(CFITSIO_VERSION 0)
file(STRINGS "${CFITSIO_INCLUDE_DIR}/fitsio.h" _cfitsio_VERSION REGEX "#define CFITSIO_VERSION[ \t]+([0-9]+\.[0-9]+)")
string(REGEX REPLACE ".*#define CFITSIO_VERSION[ \t]+([0-9]+\.[0-9]+).*" "\\1" CFITSIO_VERSION "${_cfitsio_VERSION}")
else()
set(CFITSIO_INCLUDE_DIR "CFITSIO_INCLUDE_DIR-NOTFOUND")
endif()

if (DEFINED ENV{LD_LIBRARY_PATH})
string(REPLACE ":" ";" CFITSIO_LIBRARY_SEARCH_PATH_LIST "$ENV{LD_LIBRARY_PATH}")
else()
set(CFITSIO_LIBRARY_SEARCH_PATH_LIST "")
endif()

# Search user environment for libraries, then default paths
find_library(CFITSIO_LIBRARIES cfitsio
PATHS ${CFITSIO_LIBRARY_SEARCH_PATH_LIST}
NO_DEFAULT_PATH
)

if(NOT CFITSIO_LIBRARIES)
find_library(CFITSIO_LIBRARIES NAMES cfitsio)
endif()

if (CFITSIO_LIBRARIES)
get_filename_component(CFITSIO_LIB_DIR ${CFITSIO_LIBRARIES} PATH)
else()
set(CFITSIO_LIBRARIES "CFITSIO_LIBRARIES-NOTFOUND")
endif()

# Set CFITSIO_FOUND and error out if cfitsio is not found
find_package_handle_standard_args(CFITSIO
REQUIRED_VARS CFITSIO_LIBRARIES CFITSIO_INCLUDE_DIR
VERSION_VAR CFITSIO_VERSION
)

if (CFITSIO_FOUND)
# Set flags and print a status message
message(STATUS "CFITSIO version ${CFITSIO_VERSION} found:")

set(CFITSIO_CPPFLAGS "-I${CFITSIO_INCLUDE_DIR}")
set(CFITSIO_LDFLAGS "${CFITSIO_LIBRARIES}")

message(STATUS " * includes: ${CFITSIO_INCLUDE_DIR}")
message(STATUS " * libs: ${CFITSIO_LIBRARIES}")

add_library(CFITSIO SHARED IMPORTED)
target_include_directories(CFITSIO INTERFACE ${CFITSIO_INCLUDE_DIR})
set_target_properties(CFITSIO PROPERTIES
IMPORTED_LOCATION ${CFITSIO_LIBRARIES})
else()
message(WARNING "CFITSIO not found. Please ensure CFITSIO is installed and the environment variables CFITSIOROOT, CPLUS_INCLUDE_PATH, LIBRARY_PATH, and LD_LIBRARY_PATH are set correctly.")
endif()
endif()

5 changes: 2 additions & 3 deletions projects/dataclasses/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ target_include_directories(SIREN_dataclasses PUBLIC
)

target_link_libraries(SIREN_dataclasses
INTERFACE
siren_compile_options
PUBLIC
photospline
SIREN_serialization
SIREN_utilities
SIREN_serialization
SIREN_math
)
apply_siren_compile_options(SIREN_dataclasses)

install(DIRECTORY "${PROJECT_SOURCE_DIR}/projects/dataclasses/public/"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
Expand Down
23 changes: 23 additions & 0 deletions projects/dataclasses/private/pybindings/dataclasses.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "../../public/SIREN/dataclasses/InteractionSignature.h"
#include "../../public/SIREN/dataclasses/InteractionRecord.h"
#include "../../public/SIREN/dataclasses/InteractionTree.h"
#include "../../../serialization/public/SIREN/serialization/ByteString.h"

#include "SIREN/dataclasses/serializable.h"

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
Expand Down Expand Up @@ -52,6 +55,10 @@ PYBIND11_MODULE(dataclasses, m) {
.def_readwrite("length",&Particle::length)
.def_readwrite("helicity",&Particle::helicity)
.def("generate_id",&Particle::GenerateID)
.def(pybind11::pickle(
&(siren::serialization::pickle_save<Particle>),
&(siren::serialization::pickle_load<Particle>)
))
;

py::enum_<ParticleType>(m, "ParticleType", py::arithmetic())
Expand All @@ -67,6 +74,10 @@ PYBIND11_MODULE(dataclasses, m) {
.def_readwrite("primary_type",&InteractionSignature::primary_type)
.def_readwrite("target_type",&InteractionSignature::target_type)
.def_readwrite("secondary_types",&InteractionSignature::secondary_types)
.def(pybind11::pickle(
&(siren::serialization::pickle_save<InteractionSignature>),
&(siren::serialization::pickle_load<InteractionSignature>)
))
;

py::class_<PrimaryDistributionRecord, std::shared_ptr<PrimaryDistributionRecord>>(m, "PrimaryDistributionRecord")
Expand Down Expand Up @@ -174,6 +185,10 @@ PYBIND11_MODULE(dataclasses, m) {
.def_readwrite("secondary_momenta",&InteractionRecord::secondary_momenta)
.def_readwrite("secondary_helicities",&InteractionRecord::secondary_helicities)
.def_readwrite("interaction_parameters",&InteractionRecord::interaction_parameters)
.def(pybind11::pickle(
&(siren::serialization::pickle_save<InteractionRecord>),
&(siren::serialization::pickle_load<InteractionRecord>)
))
;

py::class_<InteractionTreeDatum, std::shared_ptr<InteractionTreeDatum>>(m, "InteractionTreeDatum")
Expand All @@ -182,13 +197,21 @@ PYBIND11_MODULE(dataclasses, m) {
.def_readwrite("parent",&InteractionTreeDatum::parent)
.def_readwrite("daughters",&InteractionTreeDatum::daughters)
.def("depth",&InteractionTreeDatum::depth)
.def(pybind11::pickle(
&(siren::serialization::pickle_save<InteractionTreeDatum>),
&(siren::serialization::pickle_load<InteractionTreeDatum>)
))
;

py::class_<InteractionTree, std::shared_ptr<InteractionTree>>(m, "InteractionTree")
.def(py::init<>())
.def_readwrite("tree",&InteractionTree::tree)
.def("add_entry",static_cast<std::shared_ptr<InteractionTreeDatum> (InteractionTree::*)(InteractionTreeDatum&,std::shared_ptr<InteractionTreeDatum>)>(&InteractionTree::add_entry))
.def("add_entry",static_cast<std::shared_ptr<InteractionTreeDatum> (InteractionTree::*)(InteractionRecord&,std::shared_ptr<InteractionTreeDatum>)>(&InteractionTree::add_entry))
.def(pybind11::pickle(
&(siren::serialization::pickle_save<InteractionTree>),
&(siren::serialization::pickle_load<InteractionTree>)
))
;

m.def("SaveInteractionTrees",&SaveInteractionTrees);
Expand Down
Loading

0 comments on commit 82e4905

Please sign in to comment.