-
Notifications
You must be signed in to change notification settings - Fork 4
implement tensors in binsparse #21
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
Merged
BenBrock
merged 25 commits into
GraphBLAS:main
from
junikimm717:junikimm717/binsparse-pr
Aug 7, 2025
Merged
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e6ffb12
dev environment
junikimm717 388acbd
read tensor implemented
junikimm717 29a58e7
segfault debugging
junikimm717 436863e
tensor writer implemented
junikimm717 7150a6e
tensor reader/writer successfully bootstrapped
junikimm717 b1e6d12
testing framework for tensor read/write implemented
junikimm717 138647f
tests fixed
junikimm717 3309097
parse transposes too
junikimm717 4e50d14
json reading/writing adjusted for binsparse compliance
junikimm717 1683216
change to shared library
junikimm717 c6dcbb4
so it turns out that we need int32 indices
junikimm717 467e819
change everything to int32
junikimm717 0542538
make ready for pr
junikimm717 2512685
Fix formatting
BenBrock 5ac75e6
add cmakelists back in
junikimm717 21a906f
Merge branch 'main' into junikimm717/binsparse-pr
BenBrock 093add1
Add SPDX headers, remove `compile_flags.txt`. (Use
BenBrock 0e75d8f
Add missing includes.
BenBrock 3543589
Add Tensor tests.
BenBrock 3abac30
Fix Julia install
BenBrock 53f2a76
Fix whitespace
BenBrock 79b7f95
Turn off Julia pre-compilation, install HDF5.
BenBrock 8980791
Add missing package.
BenBrock 0c3f3f8
Add missing Julia package.
BenBrock e2c21a0
Correct comment.
BenBrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,36 +24,35 @@ jobs: | |
- name: Checks | ||
uses: pre-commit/[email protected] | ||
|
||
gcc: | ||
build: | ||
runs-on: 'ubuntu-latest' | ||
strategy: | ||
matrix: | ||
include: | ||
- cc: gcc-12 | ||
cxx: g++-12 | ||
- cc: clang | ||
cxx: clang++ | ||
name: ${{ matrix.cc }} | ||
env: | ||
CXX: g++-12 | ||
CC: gcc-12 | ||
CC: ${{ matrix.cc }} | ||
CXX: ${{ matrix.cxx }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: CMake | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install libhdf5-dev $CXX $CC | ||
sudo apt-get install libhdf5-dev gcc-12 g++-12 clang | ||
cmake -B build | ||
- name: Build | ||
run: VERBOSE=true make -C build -j `nproc` | ||
- name: Test | ||
run: ctest --test-dir ./build/test/bash | ||
|
||
clang: | ||
runs-on: 'ubuntu-latest' | ||
env: | ||
CXX: clang++ | ||
CC: clang | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: CMake | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install libhdf5-dev clang | ||
cmake -B build | ||
- name: Build | ||
run: VERBOSE=true make -C build -j `nproc` | ||
- name: Test | ||
- name: Matrix Tests | ||
run: ctest --test-dir ./build/test/bash | ||
- name: Install Julia | ||
run: curl -fsSL https://install.julialang.org | sh -s -- -y | ||
- name: Disable Julia precompilation | ||
run: julia -e 'using PrecompileTools, Preferences; set_preferences!(PrecompileTools, "precompile_workloads" => false; force=true)' | ||
- name: Install Julia Packages | ||
run: julia -e 'using Pkg; Pkg.add(["Finch", "HDF5"])' | ||
- name: Tensor Tests | ||
run: ctest --test-dir ./build/test/julia |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ | |
scripts | ||
venv | ||
build* | ||
compile_flags.txt | ||
._* | ||
tensor_test_files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Binsparse Developers | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include "../tensor_test.c" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Binsparse Developers | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include <binsparse/read_tensor.h> | ||
#include <binsparse/tensor.h> | ||
#include <binsparse/write_tensor.h> | ||
|
||
int main(int argc, char** argv) { | ||
if (argc < 3) { | ||
fprintf(stderr, | ||
"usage: ./tensor_test [file_name.h5] [output_file_name.h5]\n"); | ||
return 1; | ||
} | ||
char* file_name = argv[1]; | ||
bsp_tensor_t tensor = bsp_read_tensor(argv[1], NULL); | ||
printf("rank: %d\n", tensor.rank); | ||
printf("dims:"); | ||
for (int i = 0; i < tensor.rank; i++) { | ||
printf("%ld, ", tensor.dims[i]); | ||
} | ||
printf("\n"); | ||
bsp_write_tensor(argv[2], tensor, NULL, NULL, 9); | ||
bsp_destroy_tensor_t(tensor); | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Binsparse Developers | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <binsparse/tensor.h> | ||
|
||
#ifndef __cplusplus | ||
#include <stddef.h> | ||
#else | ||
#include <cstddef> | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
char* key_with_index(const char* key, size_t index); | ||
|
||
#ifdef BSP_USE_HDF5 | ||
#include <hdf5.h> | ||
|
||
bsp_tensor_t bsp_read_tensor_from_group(hid_t f); | ||
#endif | ||
|
||
bsp_tensor_t bsp_read_tensor(const char* file_name, const char* group); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Binsparse Developers | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <binsparse/array.h> | ||
#include <binsparse/structure.h> | ||
|
||
typedef enum { | ||
BSP_TENSOR_SPARSE = 0, | ||
BSP_TENSOR_DENSE = 1, | ||
BSP_TENSOR_ELEMENT = 2, | ||
} bsp_level_kind_t; | ||
|
||
typedef struct { | ||
bsp_level_kind_t kind; | ||
// data here should be bsp_element_t*, bsp_sparse_t*, or bsp_dense_t* | ||
void* data; | ||
} bsp_level_t; | ||
|
||
// corresponds to BSP_TENSOR_ELEMENT | ||
typedef struct { | ||
bsp_array_t values; | ||
} bsp_element_t; | ||
|
||
// corresponds to BSP_TENSOR_DENSE | ||
typedef struct { | ||
int rank; | ||
// pointers_to, while it will only ever point to one bsp_array_t, must be kept | ||
// as a pointer (rather than a struct) because there are cases where it MUST | ||
// be null. | ||
bsp_array_t* pointers_to; | ||
// indices is supposed to be an array of bsp_array_t's. | ||
bsp_array_t* indices; | ||
bsp_level_t* child; | ||
} bsp_sparse_t; | ||
|
||
typedef struct { | ||
int rank; | ||
bsp_level_t* child; | ||
} bsp_dense_t; | ||
|
||
typedef struct { | ||
int rank; | ||
size_t* dims; | ||
size_t* transpose; | ||
size_t nnz; | ||
bool is_iso; | ||
|
||
bsp_level_t* level; | ||
// don't think too much about this at the moment. | ||
bsp_structure_t structure; | ||
} bsp_tensor_t; | ||
|
||
static inline bsp_tensor_t bsp_construct_default_tensor_t() { | ||
bsp_tensor_t tensor; | ||
tensor.structure = BSP_GENERAL; | ||
tensor.is_iso = false; | ||
tensor.nnz = tensor.rank = 0; | ||
tensor.dims = NULL; | ||
tensor.transpose = NULL; | ||
|
||
tensor.level = NULL; | ||
return tensor; | ||
} | ||
|
||
static void bsp_destroy_level_t(bsp_level_t* level) { | ||
if (level == NULL) | ||
return; | ||
switch (level->kind) { | ||
case BSP_TENSOR_ELEMENT: { | ||
bsp_element_t* element = (bsp_element_t*) level->data; | ||
bsp_destroy_array_t(element->values); | ||
free(element); | ||
break; | ||
} | ||
case BSP_TENSOR_DENSE: { | ||
bsp_dense_t* dense = (bsp_dense_t*) level->data; | ||
bsp_destroy_level_t(dense->child); | ||
free(dense); | ||
break; | ||
} | ||
case BSP_TENSOR_SPARSE: { | ||
bsp_sparse_t* sparse = (bsp_sparse_t*) level->data; | ||
|
||
if (sparse->pointers_to != NULL) | ||
bsp_destroy_array_t(*sparse->pointers_to); | ||
if (sparse->indices != NULL) { | ||
for (int i = 0; i < sparse->rank; i++) { | ||
bsp_destroy_array_t(sparse->indices[i]); | ||
} | ||
} | ||
bsp_destroy_level_t(sparse->child); | ||
free(sparse); | ||
break; | ||
} | ||
default:; | ||
} | ||
} | ||
|
||
static bsp_array_t bsp_get_tensor_values(bsp_tensor_t tensor) { | ||
bsp_level_t* level = tensor.level; | ||
while (level != NULL) { | ||
switch (level->kind) { | ||
case BSP_TENSOR_ELEMENT: { | ||
bsp_element_t* element = (bsp_element_t*) level->data; | ||
return element->values; | ||
break; | ||
} | ||
case BSP_TENSOR_SPARSE: { | ||
bsp_sparse_t* sparse = (bsp_sparse_t*) level->data; | ||
level = sparse->child; | ||
break; | ||
} | ||
case BSP_TENSOR_DENSE: { | ||
bsp_dense_t* dense = (bsp_dense_t*) level->data; | ||
level = dense->child; | ||
break; | ||
} | ||
default:; | ||
} | ||
} | ||
// this should never happen! | ||
assert(false); | ||
} | ||
|
||
static inline void bsp_destroy_tensor_t(bsp_tensor_t tensor) { | ||
bsp_destroy_level_t(tensor.level); | ||
if (tensor.dims != NULL) | ||
free(tensor.dims); | ||
if (tensor.transpose != NULL) | ||
free(tensor.transpose); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Binsparse Developers | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// TODO: make cJSON optional. | ||
|
||
#include <binsparse/tensor.h> | ||
#include <cJSON/cJSON.h> | ||
|
||
#ifdef BSP_USE_HDF5 | ||
#include <hdf5.h> | ||
|
||
int bsp_write_tensor_to_group(hid_t f, bsp_tensor_t tensor, cJSON* user_json, | ||
int compression_level); | ||
#endif | ||
|
||
int bsp_write_tensor(const char* fname, bsp_tensor_t tensor, const char* group, | ||
cJSON* user_json, int compression_level); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ | |
|
||
target_sources(binsparse PRIVATE | ||
read_matrix.c | ||
read_tensor.c | ||
write_matrix.c | ||
write_tensor.c | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@willow-ahrens Does turning off precompilation in Julia like this make sense?
I'm doing this because precompilation takes a while, and it seems to me that the CI job ought to be faster overall without precompilation if we're installing and using the package once. (Julia will still JIT compute intensive portions, right?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the builds, it doesn't appear to actually turn off precompilation, and I'm not sure why. Let me know if you have any thoughts.