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

Experiment adding libhdf5 #157

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion .github/workflows/linux_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install libfftw3-dev libgomp1 python3
sudo apt-get install libfftw3-dev libhdf5-dev libgomp1 python3

- name: Install Python dependencies
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:

- name: Install dependencies
run: |
brew install fftw
brew install fftw hdf5
- name: Hack to fix omp headers not linked
run: |
brew unlink libomp
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/windows_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ jobs:
run: |
conda install fftw --yes
echo "FFTWDIR=C:\Miniconda\envs\test\Library" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
conda install hdf5
echo "HDF5_DIR=C:\Miniconda\envs\test\Library" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

- name: Install Python dependencies
shell: bash
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ where lines need to be commented in and the paths modified if cmake cannot
-DCMAKE_CXX_COMPILER=/Users/username/.local/homebrew/opt/llvm/bin/clang++
-DOMP_ROOT=/Users/username/.local/homebrew/opt/llvm/
-DCXX_ROOT=/Users/username/.local/homebrew/opt/llvm
-DHDF5_ROOT=/Users/username/.local/homebrew/opt/hdf5
```

On an ARM Mac install the x86 version of brew with
```bash
arch -x86_64 zsh
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
arch -x86_64 /usr/local/bin/brew install llvm
arch -x86_64 /usr/local/bin/brew install llvm hdf5
```
</details>

Expand Down
5 changes: 4 additions & 1 deletion tdms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# General setup ---------------------------------------------------------------
cmake_minimum_required(VERSION 3.21)

project(tdms LANGUAGES CXX)
project(tdms LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 17)

# Allow building with testing, and default to off
Expand Down Expand Up @@ -57,6 +57,9 @@ else()
find_package(OpenMP REQUIRED)
endif()

# hdf5 ------------------------------------------------------------------------
find_package(HDF5 REQUIRED COMPONENTS CXX)
include_directories(${HDF5_INCLUDE_DIR})

# spdlog ----------------------------------------------------------------------
find_package(spdlog NO_CMAKE_PACKAGE_REGISTRY QUIET)
Expand Down
2 changes: 2 additions & 0 deletions tdms/cmake/targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function(release_target)
${Matlab_MX_LIBRARY}
${Matlab_MAT_LIBRARY}
${LIBCXX_LIBRARY}
${HDF5_CXX_LIBRARIES}
OpenMP::OpenMP_CXX
spdlog::spdlog
)
Expand Down Expand Up @@ -44,6 +45,7 @@ function(test_target)
${Matlab_MX_LIBRARY}
${Matlab_MAT_LIBRARY}
${LIBCXX_LIBRARY}
${HDF5_CXX_LIBRARIES}
Copy link
Member

Choose a reason for hiding this comment

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

  • the target tdms isn't told to explicitly link to the HDF5 library found during configuration, so the linker just picks up the first one it finds. Adding

      target_link_libraries(tdms ${HDF5_CXX_LIBRARIES})
    

    should do the trick

Mosè's fix is also needed for the dynlib target (at least on my ARM64 Mac which tries to link the wrong HDF5 libraries).

OpenMP::OpenMP_CXX
spdlog::spdlog
)
Expand Down
5 changes: 5 additions & 0 deletions tdms/include/openandorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ void check_files_can_be_accessed(ArgumentNamespace &args);
* @param pointers Pointers to the matrices.
*/
void assign_matrix_pointers(MatrixCollection &expected, MatFileMatrixCollection &actual, const mxArray **pointers);

/**
* @brief Test of HDF5 I/O
*/
void test_hdf5();
56 changes: 56 additions & 0 deletions tdms/src/openandorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
*/
#include "openandorder.h"

#include <string>
#include <iostream>
#include <cstdio>
#include <stdexcept>

#include <spdlog/spdlog.h>
#include <H5Cpp.h>

#include "utils.h"
#include "fdtd_grid_initialiser.h"
Expand Down Expand Up @@ -161,3 +164,56 @@ void assign_matrix_pointers(MatrixCollection &expected, MatFileMatrixCollection
}// j
}// i
}

/********************/
#define MAX_NAME_LENGTH 32
const std::string FileName("SimpleCompound.h5");
const std::string DatasetName("PersonalInformation");
const std::string member_age("Age");
const std::string member_sex("Sex");
const std::string member_name("Name");
const std::string member_height("Height");

typedef struct {
int age;
char sex;
char name[MAX_NAME_LENGTH];
float height;
} PersonalInformation;

void test_hdf5() {

// Data to write
PersonalInformation person_list[] = {
{ 18, 'M', "Mary", 152.0 },
{ 32, 'F', "Tom", 178.6 },
{ 29, 'M', "Tarou", 166.6 }
};
// the length of the data
//int length = sizeof(person_list) / sizeof(PersonalInformation);
// the array of each length of multidimentional data.
hsize_t dim[1];
dim[0] = sizeof(person_list) / sizeof(PersonalInformation);

// the length of dim
int rank = sizeof(dim) / sizeof(hsize_t);

// defining the datatype to pass HDF55
H5::CompType mtype(sizeof(PersonalInformation));
mtype.insertMember(member_age, HOFFSET(PersonalInformation, age), H5::PredType::NATIVE_INT);
mtype.insertMember(member_sex, HOFFSET(PersonalInformation, sex), H5::PredType::C_S1);
mtype.insertMember(member_name, HOFFSET(PersonalInformation, name), H5::StrType(H5::PredType::C_S1, MAX_NAME_LENGTH));
mtype.insertMember(member_height, HOFFSET(PersonalInformation, height), H5::PredType::NATIVE_FLOAT);

// preparation of a dataset and a file.
H5::DataSpace space(rank, dim);
H5::H5File *file = new H5::H5File(FileName, H5F_ACC_TRUNC);
H5::DataSet *dataset = new H5::DataSet(file->createDataSet(DatasetName, mtype, space));
// Write
dataset->write(person_list, mtype);

delete dataset;
delete file;
return;

}