Skip to content

Commit 2fb897d

Browse files
committed
Add CMake, use string attributes properly
1 parent a3a9b17 commit 2fb897d

File tree

6 files changed

+75
-81
lines changed

6 files changed

+75
-81
lines changed

CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(binsparse)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
set(CMAKE_CXX_FLAGS "-O3")
8+
9+
include(FetchContent)
10+
FetchContent_Declare(
11+
googletest
12+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
13+
)
14+
# For Windows: Prevent overriding the parent project's compiler/linker settings
15+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
16+
FetchContent_MakeAvailable(googletest)
17+
18+
FetchContent_Declare(
19+
fmt
20+
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
21+
GIT_TAG 10.1.1)
22+
FetchContent_MakeAvailable(fmt)
23+
24+
find_package(HDF5 REQUIRED COMPONENTS CXX)
25+
add_subdirectory(include)
26+
add_subdirectory(examples)

examples/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function(add_example example_name)
2+
add_executable(${example_name} ${example_name}.cpp)
3+
target_link_libraries(${example_name} binsparse fmt)
4+
endfunction()
5+
6+
add_example(test)
7+
add_example(convert_binsparse)
8+
add_example(inspect_binsparse)
9+

include/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_library(binsparse INTERFACE)
2+
3+
target_link_libraries(binsparse INTERFACE ${HDF5_CXX_LIBRARIES})
4+
5+
target_include_directories(binsparse INTERFACE . HighFive ${HDF5_INCLUDE_DIRS})

include/binsparse/binsparse.hpp

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,20 @@ csr_matrix<T, I> read_csr_matrix(std::string fname, Allocator&& alloc) {
6060

6161
auto binsparse_metadata = data["binsparse"];
6262

63-
if (binsparse_metadata["format"] == "CSR") {
64-
auto nrows = binsparse_metadata["shape"][0];
65-
auto ncols = binsparse_metadata["shape"][1];
66-
auto nnz = binsparse_metadata["nnz"];
63+
assert(binsparse_metadata["format"] == "CSR");
6764

68-
typename std::allocator_traits<std::remove_cvref_t<Allocator>>
69-
:: template rebind_alloc<I> i_alloc(alloc);
65+
auto nrows = binsparse_metadata["shape"][0];
66+
auto ncols = binsparse_metadata["shape"][1];
67+
auto nnz = binsparse_metadata["nnz"];
7068

71-
auto values = hdf5_tools::read_dataset<T>(f, "values", alloc);
72-
auto colind = hdf5_tools::read_dataset<I>(f, "indices_1", i_alloc);
73-
auto row_ptr = hdf5_tools::read_dataset<I>(f, "pointers_to_1", i_alloc);
69+
typename std::allocator_traits<std::remove_cvref_t<Allocator>>
70+
:: template rebind_alloc<I> i_alloc(alloc);
7471

75-
return csr_matrix<T, I>{values.data(), colind.data(), row_ptr.data(), nrows, ncols, nnz};
76-
} else {
77-
assert(false);
78-
}
72+
auto values = hdf5_tools::read_dataset<T>(f, "values", alloc);
73+
auto colind = hdf5_tools::read_dataset<I>(f, "indices_1", i_alloc);
74+
auto row_ptr = hdf5_tools::read_dataset<I>(f, "pointers_to_1", i_alloc);
75+
76+
return csr_matrix<T, I>{values.data(), colind.data(), row_ptr.data(), nrows, ncols, nnz};
7977
}
8078

8179
template <typename T, typename I>
@@ -130,22 +128,20 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
130128

131129
auto binsparse_metadata = data["binsparse"];
132130

133-
if (binsparse_metadata["format"] == "COO") {
134-
auto nrows = binsparse_metadata["shape"][0];
135-
auto ncols = binsparse_metadata["shape"][1];
136-
auto nnz = binsparse_metadata["nnz"];
131+
assert(binsparse_metadata["format"] == "COO");
137132

138-
typename std::allocator_traits<std::remove_cvref_t<Allocator>>
139-
:: template rebind_alloc<I> i_alloc(alloc);
133+
auto nrows = binsparse_metadata["shape"][0];
134+
auto ncols = binsparse_metadata["shape"][1];
135+
auto nnz = binsparse_metadata["nnz"];
140136

141-
auto values = hdf5_tools::read_dataset<T>(f, "values", alloc);
142-
auto rows = hdf5_tools::read_dataset<I>(f, "indices_0", i_alloc);
143-
auto cols = hdf5_tools::read_dataset<I>(f, "indices_1", i_alloc);
137+
typename std::allocator_traits<std::remove_cvref_t<Allocator>>
138+
:: template rebind_alloc<I> i_alloc(alloc);
144139

145-
return coo_matrix<T, I>{values.data(), rows.data(), cols.data(), nrows, ncols, nnz};
146-
} else {
147-
assert(false);
148-
}
140+
auto values = hdf5_tools::read_dataset<T>(f, "values", alloc);
141+
auto rows = hdf5_tools::read_dataset<I>(f, "indices_0", i_alloc);
142+
auto cols = hdf5_tools::read_dataset<I>(f, "indices_1", i_alloc);
143+
144+
return coo_matrix<T, I>{values.data(), rows.data(), cols.data(), nrows, ncols, nnz};
149145
}
150146

151147
template <typename T, typename I>
@@ -163,11 +159,9 @@ inline auto inspect(std::string fname) {
163159

164160
auto binsparse_metadata = data["binsparse"];
165161

166-
if (binsparse_metadata["version"] >= 0.1) {
167-
return data;
168-
} else {
169-
assert(false);
170-
}
162+
assert(binsparse_metadata["version"] >= 0.1);
163+
164+
return data;
171165
}
172166

173167
} // end binsparse

include/binsparse/hdf5_tools.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,28 @@ void write_dataset(H5GroupOrFile& f, const std::string& label, R&& r) {
136136
inline std::string get_attribute(H5::H5Object& f, const std::string& key) {
137137
auto attribute = f.openAttribute(key.c_str());
138138

139-
std::size_t size = attribute.getStorageSize();
139+
H5::DataType type = attribute.getDataType();
140+
141+
auto size = type.getSize();
140142

141-
auto type = attribute.getDataType();
142143
std::string attribute_string(" ", size);
143144

144-
attribute.read(type, attribute_string);
145+
attribute.read(type, attribute_string.data());
146+
147+
attribute.close();
145148

146149
return attribute_string;
147150
}
148151

149152
inline void set_attribute(H5::H5Object& f, const std::string& key, const std::string& value) {
153+
H5::StrType string_type(H5::PredType::C_S1, value.size());
150154
hsize_t size = value.size();
151155
H5::DataSpace dataspace(1, &size);
152156

153-
f.createAttribute(key.c_str(), H5::PredType::NATIVE_CHAR, dataspace);
154-
155-
auto attribute = f.openAttribute(key.c_str());
157+
auto attribute = f.createAttribute(key.c_str(), string_type, H5S_SCALAR);
156158

157-
attribute.write(H5::PredType::NATIVE_CHAR, value.data());
159+
attribute.write(string_type, value.c_str());
160+
attribute.close();
158161
}
159162

160163
template <typename T, typename Allocator, typename H5GroupOrFile>

include/binsparse/type_info.hpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -174,49 +174,6 @@ void invoke_visit_fn_impl_(std::vector<std::string> type_labels, Fn&& fn, Args&&
174174
}
175175
}
176176

177-
/*
178-
template <typename Fn, typename... Args>
179-
void invoke_visit_fn_impl_(std::vector<std::string> type_labels, Fn&& fn, Args&&... args) {
180-
if constexpr(sizeof...(Args) < 10) {
181-
if (type_labels.size() == 1) {
182-
auto type_label = type_labels.front();
183-
if (type_label == "uint8") {
184-
invoke_if_able(std::forward<Fn>(fn), std::uint8_t(), std::forward<Args>(args)...);
185-
} else if (type_label == "uint16") {
186-
invoke_if_able(std::forward<Fn>(fn), std::uint16_t(), std::forward<Args>(args)...);
187-
} else if (type_label == "uint32") {
188-
invoke_if_able(std::forward<Fn>(fn), std::uint32_t(), std::forward<Args>(args)...);
189-
} else if (type_label == "uint64") {
190-
invoke_if_able(std::forward<Fn>(fn), std::uint64_t(), std::forward<Args>(args)...);
191-
} else if (type_label == "int8") {
192-
invoke_if_able(std::forward<Fn>(fn), std::int8_t(), std::forward<Args>(args)...);
193-
} else if (type_label == "int16") {
194-
invoke_if_able(std::forward<Fn>(fn), std::int16_t(), std::forward<Args>(args)...);
195-
} else if (type_label == "int32") {
196-
invoke_if_able(std::forward<Fn>(fn), std::int32_t(), std::forward<Args>(args)...);
197-
} else if (type_label == "int64") {
198-
invoke_if_able(std::forward<Fn>(fn), std::int64_t(), std::forward<Args>(args)...);
199-
} else if (type_label == "float32") {
200-
invoke_if_able(std::forward<Fn>(fn), float(), std::forward<Args>(args)...);
201-
} else if (type_label == "float64") {
202-
invoke_if_able(std::forward<Fn>(fn), double(), std::forward<Args>(args)...);
203-
} else if (type_label == "bint8") {
204-
invoke_if_able(std::forward<Fn>(fn), bool(), std::forward<Args>(args)...);
205-
} else {
206-
assert(false);
207-
}
208-
} else {
209-
auto label = type_labels.back();
210-
type_labels.pop_back();
211-
invoke_visit_fn_impl_({label},
212-
[=](auto&& v) {
213-
invoke_visit_fn_impl_(type_labels, fn, v, args...);
214-
});
215-
}
216-
}
217-
}
218-
*/
219-
220177
} // end __detail
221178

222179
template <typename Fn>

0 commit comments

Comments
 (0)