From 48750a5d1ec4b90b3f09430e6a02f55f76bc1b29 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Tue, 14 Nov 2017 13:24:39 +0100 Subject: [PATCH 01/14] Do not add openmp if it is not requested --- sample/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt index 877f155..0b8d250 100644 --- a/sample/CMakeLists.txt +++ b/sample/CMakeLists.txt @@ -68,7 +68,9 @@ if (ENABLE_SHARP AND SHARP_LIBRARY AND SHARP_INCLUDE_PATH AND EIGEN3_FOUND) include_directories(${SHARP_INCLUDE_PATH}) add_executable(test_healpix_calls test_healpix_calls.cpp) target_link_libraries(test_healpix_calls ${tolink} ${SHARP_LIBRARIES}) - set_target_properties(test_healpix_calls PROPERTIES COMPILE_FLAGS "${OpenMP_CXX_FLAGS}" LINK_FLAGS "${OpenMP_CXX_FLAGS}") + if (ENABLE_OPENMP) + set_target_properties(test_healpix_calls PROPERTIES COMPILE_FLAGS "${OpenMP_CXX_FLAGS}" LINK_FLAGS "${OpenMP_CXX_FLAGS}") + endif() add_dependencies(test_healpix_calls sharp) endif (ENABLE_SHARP AND SHARP_LIBRARY AND SHARP_INCLUDE_PATH AND EIGEN3_FOUND) From 1d261c625176e12aaf1cd9401d85ce1e2777cdbd Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Wed, 15 Nov 2017 13:30:19 +0100 Subject: [PATCH 02/14] Fix openmp support --- external/external_build.cmake | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/external/external_build.cmake b/external/external_build.cmake index ace4c8f..6a97597 100644 --- a/external/external_build.cmake +++ b/external/external_build.cmake @@ -52,11 +52,14 @@ SET(CONFIGURE_LDFLAGS "") if (ENABLE_SHARP) SET(DEP_BUILD ${BUILD_PREFIX}/sharp-prefix/src/sharp/auto) + IF(NOT ENABLE_OPENMP) + SET(SHARP_OPENMP --disable-openmp) + ENDIF() ExternalProject_Add(sharp URL ${CMAKE_SOURCE_DIR}/external/libsharp-6077806.tar.gz PREFIX ${BUILD_PREFIX}/sharp-prefix BUILD_IN_SOURCE 1 - CONFIGURE_COMMAND autoconf && ./configure "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" --prefix=${DEP_BUILD} + CONFIGURE_COMMAND autoconf && ./configure "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" --prefix=${DEP_BUILD} ${SHARP_OPENMP} BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} INSTALL_COMMAND echo "No install" ) From aa42376cbe9e3ff975b237fcb0c25c8756edcd72 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 8 Dec 2017 11:11:50 +0100 Subject: [PATCH 03/14] Update requirements. Split HDF5 compilation into its own virtual target. --- external/external_build.cmake | 11 +++++++++-- python/CMakeLists.txt | 9 +++++++-- python/_cosmotool.pyx | 12 ++++++++---- python/cosmotool/fftw.py | 2 +- python/cosmotool/grafic.py | 18 ++++++++++++------ src/CMakeLists.txt | 18 ++++++++++-------- src/fortran.cpp | 2 ++ src/hdf5_array.hpp | 10 +++++----- src/loadRamses.cpp | 8 ++++++-- 9 files changed, 60 insertions(+), 30 deletions(-) diff --git a/external/external_build.cmake b/external/external_build.cmake index 6a97597..b14d382 100644 --- a/external/external_build.cmake +++ b/external/external_build.cmake @@ -5,7 +5,7 @@ OPTION(ENABLE_OPENMP "Set to Yes if Healpix and/or you need openMP" OFF) SET(FFTW_URL "http://www.fftw.org/fftw-3.3.3.tar.gz" CACHE URL "URL to download FFTW from") SET(EIGEN_URL "http://bitbucket.org/eigen/eigen/get/3.2.10.tar.gz" CACHE URL "URL to download Eigen from") SET(GENGETOPT_URL "ftp://ftp.gnu.org/gnu/gengetopt/gengetopt-2.22.5.tar.gz" CACHE STRING "URL to download gengetopt from") -SET(HDF5_URL "https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8/hdf5-1.8.18/src/hdf5-1.8.18.tar.gz" CACHE STRING "URL to download HDF5 from") +SET(HDF5_URL "https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8/hdf5-1.8.18/src/hdf5-1.8.18.tar.bz2" CACHE STRING "URL to download HDF5 from") SET(NETCDF_URL "ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.4.1.1.tar.gz" CACHE STRING "URL to download NetCDF from") SET(NETCDFCXX_URL "https://github.com/Unidata/netcdf-cxx4/archive/v4.3.0.tar.gz" CACHE STRING "URL to download NetCDF-C++ from") SET(BOOST_URL "http://sourceforge.net/projects/boost/files/boost/1.61.0/boost_1_61_0.tar.gz/download" CACHE STRING "URL to download Boost from") @@ -81,6 +81,7 @@ if (INTERNAL_HDF5) ExternalProject_Add(hdf5 PREFIX ${BUILD_PREFIX}/hdf5-prefix URL ${HDF5_URL} + URL_HASH MD5=29117bf488887f89888f9304c8ebea0b CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXT_INSTALL} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} @@ -168,8 +169,10 @@ if (INTERNAL_NETCDF) -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release -DENABLE_NETCDF4=ON - -DENABLE_DAP=OFF + -DCMAKE_POSITION_INDEPENDENT_CODE=ON + -DENABLE_DAP=OFF -DCMAKE_INSTALL_PREFIX=${NETCDF_BIN_DIR} + -DCMAKE_INSTALL_LIBDIR=lib ) SET(NETCDFCXX_SOURCE_DIR ${BUILD_PREFIX}/netcdf-c++-prefix/src/netcdf-c++) @@ -181,13 +184,17 @@ if (INTERNAL_NETCDF) -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DBUILD_SHARED_LIBS=OFF + -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${NETCDF_BIN_DIR} + -DCMAKE_INSTALL_LIBDIR=lib ) # SET(CONFIGURE_CPP_LDFLAGS "${CONFIGURE_LDFLAGS}") # SET(EXTRA_NC_FLAGS CPPFLAGS=${CONFIGURE_CPP_FLAGS} LDFLAGS=${CONFIGURE_CPP_LDFLAGS}) SET(cosmotool_DEPS ${cosmotool_DEPS} netcdf netcdf-c++) +# find_library(NETCDF_LIBRARY netcdf NO_DEFAULT_PATH HINTS ${NETCDF_BIN_DIR}/lib ${NETCDF_BIN_DIR}/lib32 ${NETCDF_BIN_DIR}/lib64) +# find_library(NETCDFCPP_LIBRARY netcdf-cxx4 NO_DEFAULT_PATH HINTS ${NETCDF_BIN_DIR}/lib ${NETCDF_BIN_DIR}/lib32 ${NETCDF_BIN_DIR}/lib64) SET(NETCDF_LIBRARY ${NETCDF_BIN_DIR}/lib/libnetcdf.a CACHE STRING "NetCDF lib" FORCE) SET(NETCDFCPP_LIBRARY ${NETCDF_BIN_DIR}/lib/libnetcdf-cxx4.a CACHE STRING "NetCDF-C++ lib" FORCE) SET(NETCDF_INCLUDE_PATH ${NETCDF_BIN_DIR}/include CACHE STRING "NetCDF include" FORCE) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index f5c963d..f3c01b5 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -1,7 +1,8 @@ set(CMAKE_SHARED_MODULE_PREFIX) -include_directories(${NUMPY_INCLUDE_DIRS} ${PYTHON_INCLUDE_PATH} ${CMAKE_SOURCE_DIR}/src ${CMAKE_BINARY_DIR}/src ${CMAKE_SOURCE_DIR}/python) +set(PYTHON_INCLUDES ${NUMPY_INCLUDE_DIRS} ${PYTHON_INCLUDE_PATH} ${CMAKE_SOURCE_DIR}/python) +include_directories(${CMAKE_SOURCE_DIR}/src ${CMAKE_BINARY_DIR}/src) IF(CYTHON) add_custom_command( @@ -37,7 +38,11 @@ add_library(_cosmo_power MODULE ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_power.cpp) add_library(_cosmo_cic MODULE ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_cic.cpp) add_library(_fast_interp MODULE ${CMAKE_CURRENT_BINARY_DIR}/_fast_interp.cpp) add_library(_project MODULE ${CMAKE_CURRENT_BINARY_DIR}/_project.cpp) - +target_include_directories(_cosmotool PRIVATE ${PYTHON_INCLUDES}) +target_include_directories(_cosmo_power PRIVATE ${PYTHON_INCLUDES}) +target_include_directories(_cosmo_cic PRIVATE ${PYTHON_INCLUDES}) +target_include_directories(_fast_interp PRIVATE ${PYTHON_INCLUDES}) +target_include_directories(_project PRIVATE ${PYTHON_INCLUDES}) SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Bsymbolic-functions") diff --git a/python/_cosmotool.pyx b/python/_cosmotool.pyx index 5ef1c1a..a80c3cd 100644 --- a/python/_cosmotool.pyx +++ b/python/_cosmotool.pyx @@ -515,7 +515,7 @@ def writeGadget(str filename, object simulation): cxx_writeGadget(filename, &simdata) -def loadRamses(str basepath, int snapshot_id, int cpu_id, bool doublePrecision = False, bool loadPosition = True, bool loadVelocity = False, bool loadId = False): +def loadRamses(str basepath, int snapshot_id, int cpu_id, bool doublePrecision = False, bool loadPosition = True, bool loadVelocity = False, bool loadId = False, bool loadMass = False): """ loadRamses(basepath, snapshot_id, cpu_id, doublePrecision = False, loadPosition = True, loadVelocity = False) Loads the indicated snapshot based on the cpu id, snapshot id and basepath. It is important to specify the correct precision in doublePrecision otherwise the loading will fail. There is no way of auto-detecting properly the precision of the snapshot file. @@ -528,7 +528,8 @@ def loadRamses(str basepath, int snapshot_id, int cpu_id, bool doublePrecision = doublePrecision (bool): By default it is False, thus singlePrecision loadPosition (bool): Whether to load positions loadVelocity (bool): Whether to load velocities - loadId (bol): Whether to load identifiers + loadId (bool): Whether to load identifiers + loadMass (bool): Whether to load mass value Returns: An object derived from PySimulationBase_. @@ -544,13 +545,16 @@ def loadRamses(str basepath, int snapshot_id, int cpu_id, bool doublePrecision = flags |= NEED_VELOCITY if loadId: flags |= NEED_GADGET_ID + if loadMass: + flags |= NEED_MASS + encpath = basepath.encode('utf-8') try: - data = loadRamsesSimu(basepath, snapshot_id, cpu_id, doublePrecision, flags) + data = loadRamsesSimu(encpath, snapshot_id, cpu_id, doublePrecision, flags) if data == 0: return None except RuntimeError as e: - raise RuntimeError(e.message + ' (check the float precision in snapshot)') + raise RuntimeError(str(e) + ' (check the float precision in snapshot)') return PySimulationAdaptor(wrap_simudata(data, flags)) diff --git a/python/cosmotool/fftw.py b/python/cosmotool/fftw.py index ec0cb6a..57f43d1 100644 --- a/python/cosmotool/fftw.py +++ b/python/cosmotool/fftw.py @@ -19,7 +19,7 @@ class CubeFT(object): self.align = pyfftw.simd_alignment self.L = float(L) self.max_cpu = multiprocessing.cpu_count() if max_cpu < 0 else max_cpu - self._dhat = pyfftw.n_byte_align_empty((self.N,self.N,self.N/2+1), self.align, dtype=fourier_type) + self._dhat = pyfftw.n_byte_align_empty((self.N,self.N,self.N//2+1), self.align, dtype=fourier_type) self._density = pyfftw.n_byte_align_empty((self.N,self.N,self.N), self.align, dtype=real_type) self._irfft = pyfftw.FFTW(self._dhat, self._density, axes=(0,1,2), direction='FFTW_BACKWARD', threads=self.max_cpu)#, normalize_idft=False) self._rfft = pyfftw.FFTW(self._density, self._dhat, axes=(0,1,2), threads=self.max_cpu) #, normalize_idft=False) diff --git a/python/cosmotool/grafic.py b/python/cosmotool/grafic.py index c23073b..04eb5b6 100644 --- a/python/cosmotool/grafic.py +++ b/python/cosmotool/grafic.py @@ -15,16 +15,22 @@ def readGrafic(filename): * the mean matter density :math:`\Omega_\mathrm{m}` * the dark energy density :math:`\Omega_\Lambda` * the hubble constant, relative to 100 km/s/Mpc + * xoffset + * yoffset + * zoffset """ - with file(filename, mode="rb") as f: + with open(filename, mode="rb") as f: p = struct.unpack("IIIIffffffffI", f.read(4*11 + 2*4)) - checkPoint0, Nx, Ny, Nz, delta, _, _, _, scalefac, omega0, omegalambda0, h, checkPoint1 = p + checkPoint0, Nx, Ny, Nz, delta, xoff, yoff, zoff, scalefac, omega0, omegalambda0, h, checkPoint1 = p if checkPoint0 != checkPoint1 or checkPoint0 != 4*11: raise ValueError("Invalid unformatted access") a = np.empty((Nx,Ny,Nz), dtype=np.float32) BoxSize = delta * Nx * h + xoff *= h + yoff *= h + zoff *= h checkPoint = 4*Ny*Nz for i in range(Nx): @@ -38,11 +44,11 @@ def readGrafic(filename): if checkPoint != 4*Ny*Nz: raise ValueError("Invalid unformatted access") - return a, BoxSize, scalefac, omega0, omegalambda0, h + return a, BoxSize, scalefac, omega0, omegalambda0, h, xoff, yoff,zoff def writeGrafic(filename, field, BoxSize, scalefac, **cosmo): - with file(filename, mode="wb") as f: + with open(filename, mode="wb") as f: checkPoint = 4*11 Nx,Ny,Nz = field.shape @@ -65,7 +71,7 @@ def writeGrafic(filename, field, BoxSize, scalefac, **cosmo): def writeWhitePhase(filename, field): - with file(filename, mode="wb") as f: + with open(filename, mode="wb") as f: Nx,Ny,Nz = field.shape checkPoint = 4*4 @@ -80,7 +86,7 @@ def writeWhitePhase(filename, field): def readWhitePhase(filename): - with file(filename, mode="rb") as f: + with open(filename, mode="rb") as f: _, Nx, Ny, Nz, _, _ = struct.unpack("IIIIII", f.read(4*4+2*4)) a = np.empty((Nx,Ny,Nz), dtype=np.float32) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8b0c90e..ee65f47 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,12 +34,13 @@ ENDIF(YORICK_SUPPORT) if (HDF5_FOUND) - set(CosmoTool_SRCS ${CosmoTool_SRCS} + add_library(CosmoHDF5 OBJECT h5_readFlash.cpp loadFlash.cpp ) + target_include_directories(CosmoHDF5 BEFORE PRIVATE ${HDF5_INCLUDE_DIR}) else(HDF5_FOUND) - set(CosmoTool_SRCS ${CosmoTool_SRCS} + add_library(CosmoHdf5 OBJECT loadFlash_dummy.cpp ) endif (HDF5_FOUND) @@ -78,22 +79,23 @@ endif(YORICK_SUPPORT) if (HDF5_FOUND) set(CosmoTool_LIBS ${CosmoTool_LIBS} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES} ${HDF5_CXX_LIBRARIES} ${ZLIB}) - include_directories(${HDF5_INCLUDE_DIR}) message(STATUS "CosmoTool_LIBS: ${CosmoTool_LIBS}") endif (HDF5_FOUND) set(CosmoTool_LIBS ${CosmoTool_LIBS} PARENT_SCOPE) if (BUILD_SHARED_LIBS) - add_library(CosmoTool SHARED ${CosmoTool_SRCS}) + add_library(CosmoTool SHARED ${CosmoTool_SRCS} $) target_link_libraries(CosmoTool ${CosmoTool_LIBS}) if (BUILD_STATIC_LIBS) - add_library(CosmoTool_static STATIC ${CosmoTool_SRCS}) - set_target_properties(CosmoTool_static PROPERTIES COMPILE_FLAGS "${CMAKE_C_COMPILE_OPTIONS_PIC}") + add_library(CosmoTool_static STATIC ${CosmoTool_SRCS} $) + target_link_libraries(CosmoTool_static) + set_property(TARGET CosmoTool_static PROPERTY POSITION_INDEPENDENT_CODE ON) endif(BUILD_STATIC_LIBS) else (BUILD_SHARED_LIBS) - add_library(CosmoTool STATIC ${CosmoTool_SRCS}) - set_target_properties(CosmoTool PROPERTIES COMPILE_FLAGS "${CMAKE_C_COMPILE_OPTIONS_PIC}") + add_library(CosmoTool STATIC ${CosmoTool_SRCS} $) + target_link_libraries(CosmoTool) + set_property(TARGET CosmoTool PROPERTY POSITION_INDEPENDENT_CODE ON) endif (BUILD_SHARED_LIBS) install(TARGETS CosmoTool diff --git a/src/fortran.cpp b/src/fortran.cpp index 1170a9a..83e9730 100644 --- a/src/fortran.cpp +++ b/src/fortran.cpp @@ -51,6 +51,7 @@ UnformattedRead::UnformattedRead(const string& fname) swapOrdering = false; cSize = Check_32bits; checkPointRef = checkPointAccum = 0; + recordBuffer = 0; } UnformattedRead::UnformattedRead(const char *fname) @@ -130,6 +131,7 @@ void UnformattedRead::beginCheckpoint(bool bufferRecord) throw EndOfFileException(); if (bufferRecord) { + std::cout << "Use fast I/O mode" << std::endl; recordBuffer = new uint8_t[checkPointRef]; f->read(reinterpret_cast(recordBuffer), checkPointRef); } diff --git a/src/hdf5_array.hpp b/src/hdf5_array.hpp index f9835a1..f012bc4 100644 --- a/src/hdf5_array.hpp +++ b/src/hdf5_array.hpp @@ -129,7 +129,7 @@ namespace CosmoTool { //! //!_______________________________________________________________________________________ template - void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, + void hdf5_write_array(H5::H5Location& fg, const std::string& data_set_name, const ArrayType& data, const hdf5_data_type& datatype, const std::vector& dimensions, @@ -156,7 +156,7 @@ namespace CosmoTool { template - void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, + void hdf5_write_array(H5::H5Location& fg, const std::string& data_set_name, const ArrayType& data, const hdf5_data_type& datatype, bool doCreate = true, @@ -255,7 +255,7 @@ namespace CosmoTool { }; template - void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, const ArrayType& data ) + void hdf5_write_array(H5::H5Location& fg, const std::string& data_set_name, const ArrayType& data ) { typedef typename ArrayType::element T; get_hdf5_data_type hdf_data_type; @@ -341,7 +341,7 @@ namespace CosmoTool { // ---------------------------------------------------------------------- template - void hdf5_read_array_typed(H5::CommonFG& fg, const std::string& data_set_name, + void hdf5_read_array_typed(H5::H5Location& fg, const std::string& data_set_name, ArrayType& data, const hdf5_data_type& datatype, bool auto_resize = true, bool useBases = false) { @@ -377,7 +377,7 @@ namespace CosmoTool { } template - void hdf5_read_array(H5::CommonFG& fg, const std::string& data_set_name, ArrayType& data, bool auto_resize = true, + void hdf5_read_array(H5::H5Location& fg, const std::string& data_set_name, ArrayType& data, bool auto_resize = true, bool useBases = false ) { typedef typename ArrayType::element T; diff --git a/src/loadRamses.cpp b/src/loadRamses.cpp index 8e218d2..3e841d5 100644 --- a/src/loadRamses.cpp +++ b/src/loadRamses.cpp @@ -417,6 +417,9 @@ CosmoTool::SimuData *CosmoTool::loadRamsesSimu(const char *basename, int outputI data->Vel[1] = new float[nPar]; data->Vel[2] = new float[nPar]; } + if (flags & NEED_MASS) { + data->Mass = new float[nPar]; + } if (flags & NEED_GADGET_ID) { data->Id = new int64_t[nPar]; @@ -455,9 +458,10 @@ CosmoTool::SimuData *CosmoTool::loadRamsesSimu(const char *basename, int outputI float minMass = INFINITY; infile.beginCheckpoint(); - for (uint32_t i = nPar; i > 0; i--) - { + for (uint32_t i = 0; i < nPar; i++) { float dummyF = dp ? infile.readReal64() : infile.readReal32(); + if (flags & NEED_MASS) + data->Mass[i] = dummyF; if (dummyF < minMass) minMass = dummyF; } infile.endCheckpoint(); From 2cf9274b2e8fb46e6c53097da619e8791f9ff207 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Sat, 16 Dec 2017 17:33:21 +0100 Subject: [PATCH 04/14] Use a type alias to ensure compatibility with more versions of HDF5 --- src/hdf5_array.hpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/hdf5_array.hpp b/src/hdf5_array.hpp index f012bc4..2e73b9a 100644 --- a/src/hdf5_array.hpp +++ b/src/hdf5_array.hpp @@ -51,6 +51,11 @@ knowledge of the CeCILL license and that you accept its terms. #include namespace CosmoTool { +#if (H5_VERS_MAJOR == 1) && (H5_VERS_MINOR <= 8) + typedef H5::CommonFG H5_CommonFileGroup; +#else + typedef H5::Group H5_CommonFileGroup; +#endif //!_______________________________________________________________________________________ //! @@ -129,7 +134,7 @@ namespace CosmoTool { //! //!_______________________________________________________________________________________ template - void hdf5_write_array(H5::H5Location& fg, const std::string& data_set_name, + void hdf5_write_array(H5_CommonFileGroup& fg, const std::string& data_set_name, const ArrayType& data, const hdf5_data_type& datatype, const std::vector& dimensions, @@ -156,7 +161,7 @@ namespace CosmoTool { template - void hdf5_write_array(H5::H5Location& fg, const std::string& data_set_name, + void hdf5_write_array(H5_CommonFileGroup& fg, const std::string& data_set_name, const ArrayType& data, const hdf5_data_type& datatype, bool doCreate = true, @@ -165,7 +170,7 @@ namespace CosmoTool { std::vector dimensions(data.shape(), data.shape() + data.num_dimensions()); hdf5_write_array(fg, data_set_name, data, datatype, dimensions, doCreate, useBases); } - + /* HDF5 complex type */ template class hdf5_ComplexType @@ -255,14 +260,14 @@ namespace CosmoTool { }; template - void hdf5_write_array(H5::H5Location& fg, const std::string& data_set_name, const ArrayType& data ) + void hdf5_write_array(H5_CommonFileGroup& fg, const std::string& data_set_name, const ArrayType& data ) { typedef typename ArrayType::element T; get_hdf5_data_type hdf_data_type; hdf5_write_array(fg, data_set_name, data, hdf_data_type.type()); } - + // HDF5 array reader // // Author Guilhem Lavaux (May 2014) @@ -341,7 +346,7 @@ namespace CosmoTool { // ---------------------------------------------------------------------- template - void hdf5_read_array_typed(H5::H5Location& fg, const std::string& data_set_name, + void hdf5_read_array_typed(H5_CommonFileGroup& fg, const std::string& data_set_name, ArrayType& data, const hdf5_data_type& datatype, bool auto_resize = true, bool useBases = false) { @@ -377,7 +382,7 @@ namespace CosmoTool { } template - void hdf5_read_array(H5::H5Location& fg, const std::string& data_set_name, ArrayType& data, bool auto_resize = true, + void hdf5_read_array(H5_CommonFileGroup& fg, const std::string& data_set_name, ArrayType& data, bool auto_resize = true, bool useBases = false ) { typedef typename ArrayType::element T; From 9389a30379849ff167be8cae2f95a17da2f1f7b4 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Tue, 19 Dec 2017 12:04:03 +0100 Subject: [PATCH 05/14] Upgrade dependencies. Port to python3 --- external/external_build.cmake | 14 ++++++++++---- python/_cosmotool.pyx | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/external/external_build.cmake b/external/external_build.cmake index ace4c8f..c3d63ab 100644 --- a/external/external_build.cmake +++ b/external/external_build.cmake @@ -6,7 +6,7 @@ SET(FFTW_URL "http://www.fftw.org/fftw-3.3.3.tar.gz" CACHE URL "URL to download SET(EIGEN_URL "http://bitbucket.org/eigen/eigen/get/3.2.10.tar.gz" CACHE URL "URL to download Eigen from") SET(GENGETOPT_URL "ftp://ftp.gnu.org/gnu/gengetopt/gengetopt-2.22.5.tar.gz" CACHE STRING "URL to download gengetopt from") SET(HDF5_URL "https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8/hdf5-1.8.18/src/hdf5-1.8.18.tar.gz" CACHE STRING "URL to download HDF5 from") -SET(NETCDF_URL "ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.4.1.1.tar.gz" CACHE STRING "URL to download NetCDF from") +SET(NETCDF_URL "ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.5.0.tar.gz" CACHE STRING "URL to download NetCDF from") SET(NETCDFCXX_URL "https://github.com/Unidata/netcdf-cxx4/archive/v4.3.0.tar.gz" CACHE STRING "URL to download NetCDF-C++ from") SET(BOOST_URL "http://sourceforge.net/projects/boost/files/boost/1.61.0/boost_1_61_0.tar.gz/download" CACHE STRING "URL to download Boost from") SET(GSL_URL "ftp://ftp.gnu.org/gnu/gsl/gsl-1.15.tar.gz" CACHE STRING "URL to download GSL from ") @@ -93,9 +93,11 @@ if (INTERNAL_HDF5) SET(HDF5_ROOTDIR ${HDF5_BIN_DIR}) SET(CONFIGURE_LDFLAGS "${CONFIGURE_LDFLAGS} -L${HDF5_BIN_DIR}/lib") SET(CONFIGURE_LIBS "${CONFIGURE_LIBS} -ldl") - set(HDF5_LIBRARIES ${HDF5_BIN_DIR}/lib/libhdf5.a CACHE STRING "HDF5 lib" FORCE) - set(HDF5_HL_LIBRARIES ${HDF5_BIN_DIR}/lib/libhdf5_hl.a CACHE STRING "HDF5 HL lib" FORCE) - set(HDF5_CXX_LIBRARIES ${HDF5_BIN_DIR}/lib/libhdf5_cpp.a CACHE STRING "HDF5 C++ lib" FORCE) + set(HDF5_C_STATIC_LIBRARY ${HDF5_BIN_DIR}/lib/libhdf5-static.a) + set(HDF5_HL_STATIC_LIBRARY ${HDF5_BIN_DIR}/lib/libhdf5_hl-static.a) + set(HDF5_LIBRARIES ${HDF5_BIN_DIR}/lib/libhdf5-static.a CACHE STRING "HDF5 lib" FORCE) + set(HDF5_HL_LIBRARIES ${HDF5_BIN_DIR}/lib/libhdf5_hl-static.a CACHE STRING "HDF5 HL lib" FORCE) + set(HDF5_CXX_LIBRARIES ${HDF5_BIN_DIR}/lib/libhdf5_cpp-static.a CACHE STRING "HDF5 C++ lib" FORCE) SET(HDF5_INCLUDE_DIRS ${HDF5_BIN_DIR}/include CACHE STRING "HDF5 include path" FORCE) mark_as_advanced(HDF5_LIBRARIES HDF5_CXX_LIBRARIES HDF5_INCLUDE_DIRS) @@ -114,6 +116,7 @@ else (INTERNAL_HDF5) cmessage(STATUS "HDF5 lib: ${HDF5_LIBRARIES}") cmessage(STATUS "HDF5 includes: ${HDF5_INCLUDE_DIRS}") cmessage(STATUS "HDF5 C lib: ${HDF5_C_LIBRARY}") + cmessage(STATUS "HDF5 HL lib: ${HDF5_HL_LIBRARY}") cmessage(STATUS "HDF5 BIN: ${HDF5_BIN_DIR}") foreach(hdf5lib IN LISTS HDF5_LIBRARIES) if (${hdf5lib} MATCHES "(hdf5)|(HDF5)") @@ -167,6 +170,9 @@ if (INTERNAL_NETCDF) -DENABLE_NETCDF4=ON -DENABLE_DAP=OFF -DCMAKE_INSTALL_PREFIX=${NETCDF_BIN_DIR} + -DHDF5_C_LIBRARY=${HDF5_C_STATIC_LIBRARY} + -DHDF5_HL_LIBRARY=${HDF5_HL_STATIC_LIBRARY} + -DHDF5_INCLUDE_DIR=${HDF5_INCLUDE_DIRS} ) SET(NETCDFCXX_SOURCE_DIR ${BUILD_PREFIX}/netcdf-c++-prefix/src/netcdf-c++) diff --git a/python/_cosmotool.pyx b/python/_cosmotool.pyx index 5ef1c1a..d8865e7 100644 --- a/python/_cosmotool.pyx +++ b/python/_cosmotool.pyx @@ -452,7 +452,7 @@ def loadParallelGadget(object filename_list, int gadgetFormat = 1, bool loadPosi filenames.resize(num_files) data = alloc_simudata(num_files) for i,l in enumerate(filename_list): - filenames[i] = l + filenames[i] = l.encode('utf-8') with nogil: for i in prange(num_files): From 4ae8fee211c79770114351c742dec44f0777cae1 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 15:09:40 +0100 Subject: [PATCH 06/14] simple3DFilter do not need yorick anymore --- sample/CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt index 0b8d250..5221474 100644 --- a/sample/CMakeLists.txt +++ b/sample/CMakeLists.txt @@ -84,10 +84,11 @@ if (Boost_FOUND) add_executable(testSmooth testSmooth.cpp) target_link_libraries(testSmooth ${tolink}) - add_executable(simple3DFilter simple3DFilter.cpp) - target_link_libraries(simple3DFilter ${tolink}) ENDIF (YORICK_SUPPORT) if (HDF5_FOUND) + add_executable(simple3DFilter simple3DFilter.cpp) + target_link_libraries(simple3DFilter ${tolink}) + add_executable(gadgetToArray gadgetToArray.cpp) target_link_libraries(gadgetToArray ${tolink}) From f052cd244de80fe1514c0e4c6507fba5bebaa20f Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 15:40:06 +0100 Subject: [PATCH 07/14] Install helper program too --- sample/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt index 5221474..ee87487 100644 --- a/sample/CMakeLists.txt +++ b/sample/CMakeLists.txt @@ -89,6 +89,11 @@ if (Boost_FOUND) add_executable(simple3DFilter simple3DFilter.cpp) target_link_libraries(simple3DFilter ${tolink}) + install(TARGETS simple3DFilter + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) + add_executable(gadgetToArray gadgetToArray.cpp) target_link_libraries(gadgetToArray ${tolink}) From 72d58447aca6593f80763971720ab7829977b58f Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 16:21:12 +0100 Subject: [PATCH 08/14] Remove quietness as it seems to cause problems --- external/external_build.cmake | 4 +++- python/CMakeLists.txt | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/external/external_build.cmake b/external/external_build.cmake index f05e79e..bfadc56 100644 --- a/external/external_build.cmake +++ b/external/external_build.cmake @@ -337,9 +337,11 @@ IF (INTERNAL_EIGEN) SET(EIGEN3_INCLUDE_DIRS ${EXT_INSTALL}/include/eigen3) ELSE (INTERNAL_EIGEN) - pkg_check_modules(EIGEN3 QUIET eigen3 ) + pkg_check_modules(EIGEN3 eigen3 ) if (NOT EIGEN3_FOUND) cmessage(CWARNING "Eigen library not found") + else() + cmessage(STATUS "Found EIGEN3 in ${EIGEN3_INCLUDE_DIRS}") endif() ENDIF(INTERNAL_EIGEN) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index f3c01b5..18f626a 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -100,6 +100,8 @@ if (WIN32 AND NOT CYGWIN) SET_TARGET_PROPERTIES(_cosmotool PROPERTIES SUFFIX ".pyd") endif (WIN32 AND NOT CYGWIN) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cosmotool/config.py.in ${CMAKE_CURRENT_BINARY_DIR}/cosmotool/config.py @ONLY) + INSTALL(TARGETS ${ct_TARGETS} LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}/cosmotool From cef016ad0b98e8d10980e7ebbdb4c3e1a4c3f973 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 16:22:05 +0100 Subject: [PATCH 09/14] Add missing py --- python/cosmotool/config.py.in | 1 + 1 file changed, 1 insertion(+) create mode 100644 python/cosmotool/config.py.in diff --git a/python/cosmotool/config.py.in b/python/cosmotool/config.py.in new file mode 100644 index 0000000..a59d25e --- /dev/null +++ b/python/cosmotool/config.py.in @@ -0,0 +1 @@ +install_prefix=@CMAKE_INSTALL_PREFIX@ From 9428a6cced4a9777f7766eb9cb5c4ff0c66a9f7f Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 16:32:35 +0100 Subject: [PATCH 10/14] Install config file at correct position --- python/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 18f626a..7513f38 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -107,5 +107,5 @@ INSTALL(TARGETS LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}/cosmotool ) -INSTALL(DIRECTORY cosmotool DESTINATION ${PYTHON_SITE_PACKAGES} +INSTALL(DIRECTORY cosmotool ${CMAKE_CURRENT_BINARY_DIR}/cosmotool DESTINATION ${PYTHON_SITE_PACKAGES} FILES_MATCHING PATTERN "*.py") From e23bcd7df32e15240c536bb81ae23662f2798674 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 19:14:43 +0100 Subject: [PATCH 11/14] Add smooth particle density wrapper for simple3DFilter --- python/cosmotool/__init__.py | 1 + python/cosmotool/config.py.in | 2 +- python/cosmotool/smooth.py | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 python/cosmotool/smooth.py diff --git a/python/cosmotool/__init__.py b/python/cosmotool/__init__.py index 63a339e..c5b1c79 100644 --- a/python/cosmotool/__init__.py +++ b/python/cosmotool/__init__.py @@ -15,6 +15,7 @@ except: from .simu import loadRamsesAll, simpleWriteGadget, SimulationBare from .timing import time_block, timeit, timeit_quiet from .bispectrum import bispectrum, powerspectrum +from .smooth import smooth_particle_density try: from .fftw import CubeFT diff --git a/python/cosmotool/config.py.in b/python/cosmotool/config.py.in index a59d25e..7603b85 100644 --- a/python/cosmotool/config.py.in +++ b/python/cosmotool/config.py.in @@ -1 +1 @@ -install_prefix=@CMAKE_INSTALL_PREFIX@ +install_prefix="@CMAKE_INSTALL_PREFIX@" diff --git a/python/cosmotool/smooth.py b/python/cosmotool/smooth.py new file mode 100644 index 0000000..b28d329 --- /dev/null +++ b/python/cosmotool/smooth.py @@ -0,0 +1,54 @@ +from .config import install_prefix +import subprocess +import os +from tempfile import TemporaryDirectory +import h5py as h5 +import numpy as np +import weakref + +def smooth_particle_density(position, velocities=None, radius=1e6, boxsize=None, resolution=128, center=None ): + if len(position.shape) != 2: + raise ValueError("Invalid position array shape") + + if velocities is None: + if position.shape[1] != 6: + raise ValueError("Position must be phase space if no velocities are given") + + + if boxsize is None: + raise ValueError("Need a boxsize") + + cx,cy,cz=center + with TemporaryDirectory() as tmpdir: + h5_file = os.path.join(tmpdir, 'particles.h5') + with h5.File(h5_file, mode="w") as f: + data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32) + data[:,:3] = position[:,:3] + if velocities is not None: + data[:,3:6] = velocities[:,:3] + else: + data[:,3:6] = position[:,3:] + data[:,6] = 1 + + + ret = \ + subprocess.run([ + os.path.join(install_prefix,'bin','simple3DFilter'), + h5_file, + str(radius), + str(boxsize), + str(resolution), + str(cx), str(cy), str(cz) + ], cwd=tmpdir) + + + f0 = h5.File(os.path.join(tmpdir,'fields.h5'), mode="r") + def cleanup_f0(): + f0.close() + + class Dict(dict): + pass + + t = Dict(rho=f0['density'], p=[f0['p0'], f0['p1'], f0['p2']]) + weakref.finalize(t, cleanup_f0) + return t From 4e1004f8f8208f2e440b52a79c0f89e4e41355dd Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 19:51:36 +0100 Subject: [PATCH 12/14] Add doc --- python/cosmotool/smooth.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/python/cosmotool/smooth.py b/python/cosmotool/smooth.py index b28d329..a377ed6 100644 --- a/python/cosmotool/smooth.py +++ b/python/cosmotool/smooth.py @@ -6,7 +6,37 @@ import h5py as h5 import numpy as np import weakref -def smooth_particle_density(position, velocities=None, radius=1e6, boxsize=None, resolution=128, center=None ): +def smooth_particle_density( + position, + velocities=None, + radius=1e6, + boxsize=None, + resolution=128,i + center=None, tmpprefix=None ): + """Use adaptive smoothing to produce density and momentum fields. + + Parameters: + position : numpy array NxQ + the particle positions + if Q==3, only positions. Q==6 means full space phase + velocities : Optional numpy array Nx3. + It is only optional if the above Q is 6. + radius : float + Maximum radius to which we need to compute fields + boxsize : float + Size of the box for the generated fields + resolution : int + Resolution of the output boxes + center : list of 3 floats + Center of the new box. It depends on the convention + for particles. If those are between [0, L], then [0,0,0] + is correct. If those are [-L/2,L/2] then you should set + [L/2,L/2,L/2]. + tmpprefix : string + prefix of the temporary directory that will be used. + It needs to have a lot of space available. By default + '/tmp/ will be typically used. + """ if len(position.shape) != 2: raise ValueError("Invalid position array shape") @@ -19,7 +49,7 @@ def smooth_particle_density(position, velocities=None, radius=1e6, boxsize=None, raise ValueError("Need a boxsize") cx,cy,cz=center - with TemporaryDirectory() as tmpdir: + with TemporaryDirectory(prefix=tmpprefix) as tmpdir: h5_file = os.path.join(tmpdir, 'particles.h5') with h5.File(h5_file, mode="w") as f: data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32) From 531084dbc189431e5be2a5b6402c6054b331d85b Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 19:55:34 +0100 Subject: [PATCH 13/14] Add doc --- python/cosmotool/smooth.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/python/cosmotool/smooth.py b/python/cosmotool/smooth.py index a377ed6..a9a12f3 100644 --- a/python/cosmotool/smooth.py +++ b/python/cosmotool/smooth.py @@ -14,6 +14,7 @@ def smooth_particle_density( resolution=128,i center=None, tmpprefix=None ): """Use adaptive smoothing to produce density and momentum fields. + The algorithm is originally described in [1]. Parameters: position : numpy array NxQ @@ -36,6 +37,24 @@ def smooth_particle_density( prefix of the temporary directory that will be used. It needs to have a lot of space available. By default '/tmp/ will be typically used. + + + Returns + ------- + dictionnary + The dict has two entries: 'rho' for the density, and 'p' for the momenta. + Once the dictionary is garbage collected all temporary files and directories + will be cleared automatically. + + + Raises + ------ + ValueError + if arguments are invalid + + + .. [1] S. Colombi, M. Chodorowski, + "Cosmic velocity-gravity in redshift space", MNRAS, 2007, 375, 1 """ if len(position.shape) != 2: raise ValueError("Invalid position array shape") From 945166c1f992a9ce18348b221c3f78286c13f419 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 5 Jan 2018 22:17:26 +0100 Subject: [PATCH 14/14] Rearrange cleanup for NFS stale handles --- python/cosmotool/smooth.py | 60 ++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/python/cosmotool/smooth.py b/python/cosmotool/smooth.py index a9a12f3..7c39a75 100644 --- a/python/cosmotool/smooth.py +++ b/python/cosmotool/smooth.py @@ -11,7 +11,7 @@ def smooth_particle_density( velocities=None, radius=1e6, boxsize=None, - resolution=128,i + resolution=128, center=None, tmpprefix=None ): """Use adaptive smoothing to produce density and momentum fields. The algorithm is originally described in [1]. @@ -68,36 +68,38 @@ def smooth_particle_density( raise ValueError("Need a boxsize") cx,cy,cz=center - with TemporaryDirectory(prefix=tmpprefix) as tmpdir: - h5_file = os.path.join(tmpdir, 'particles.h5') - with h5.File(h5_file, mode="w") as f: - data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32) - data[:,:3] = position[:,:3] - if velocities is not None: - data[:,3:6] = velocities[:,:3] - else: - data[:,3:6] = position[:,3:] - data[:,6] = 1 + tmpdir = TemporaryDirectory(prefix=tmpprefix) + h5_file = os.path.join(tmpdir.name, 'particles.h5') + with h5.File(h5_file, mode="w") as f: + data = f.create_dataset('particles', shape=(position.shape[0],7), dtype=np.float32) + data[:,:3] = position[:,:3] + if velocities is not None: + data[:,3:6] = velocities[:,:3] + else: + data[:,3:6] = position[:,3:] + data[:,6] = 1 - ret = \ - subprocess.run([ - os.path.join(install_prefix,'bin','simple3DFilter'), - h5_file, - str(radius), - str(boxsize), - str(resolution), - str(cx), str(cy), str(cz) - ], cwd=tmpdir) + ret = \ + subprocess.run([ + os.path.join(install_prefix,'bin','simple3DFilter'), + h5_file, + str(radius), + str(boxsize), + str(resolution), + str(cx), str(cy), str(cz) + ], cwd=tmpdir.name) - - f0 = h5.File(os.path.join(tmpdir,'fields.h5'), mode="r") - def cleanup_f0(): - f0.close() + + f0 = h5.File(os.path.join(tmpdir.name,'fields.h5'), mode="r") + def cleanup_f0(): + f0.close() + tmpdir.cleanup() - class Dict(dict): - pass + class Dict(dict): + pass - t = Dict(rho=f0['density'], p=[f0['p0'], f0['p1'], f0['p2']]) - weakref.finalize(t, cleanup_f0) - return t + t = Dict(rho=f0['density'], p=[f0['p0'], f0['p1'], f0['p2']]) + t._tmpdir_=tmpdir + weakref.finalize(t, cleanup_f0) + return t