-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete overhaul of interface. Adds support for writing C++ headers.
- Loading branch information
Adomas Baliuka
authored and
Adomas Baliuka
committed
Jan 3, 2023
1 parent
f6d4409
commit efde640
Showing
14 changed files
with
339 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
*.jl.*.cov | ||
*.jl.mem | ||
Manifest.toml | ||
.vscode/ |
This file contains 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 |
---|---|---|
@@ -1,19 +1,22 @@ | ||
name = "LDPCStorage" | ||
uuid = "d46d874d-5773-4ce9-8adb-568101dc8882" | ||
authors = ["Adomas Baliuka <[email protected]>"] | ||
version = "0.2.0" | ||
version = "0.3.0" | ||
|
||
[deps] | ||
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" | ||
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" | ||
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
|
||
[compat] | ||
julia = "1.6" | ||
|
||
[extras] | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test"] | ||
test = ["Test", "Documenter"] |
This file contains 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 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 |
---|---|---|
@@ -1,14 +1,25 @@ | ||
""" | ||
$(DocStringExtensions.README) | ||
""" | ||
module LDPCStorage | ||
|
||
using DocStringExtensions | ||
|
||
include("utils.jl") | ||
|
||
include("alist.jl") | ||
export load_alist, save_to_alist | ||
export save_to_alist, write_alist, load_alist | ||
|
||
include("cscmat.jl") # this format is deprecated in favour of csc.json | ||
# export save_to_cscmat, load_cscmat, load_matrix_from_qc_cscmat_file, CSCMAT_FORMAT_VERSION | ||
|
||
include("cscjson.jl") | ||
export load_ldpc_from_json, save_to_bincscjson, save_to_qccscjson, CSCJSON_FORMAT_VERSION | ||
export write_bincscjson, save_to_bincscjson | ||
export write_qcscjson, save_to_qccscjson | ||
export load_ldpc_from_json, CSCJSON_FORMAT_VERSION | ||
|
||
# This format stores the LDPC code as static data in a c++ header file. | ||
include("cpp_header_based.jl") | ||
export write_cpp_header | ||
|
||
end # module |
This file contains 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 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,94 @@ | ||
# This script generates a `.hpp` file (C++ header) containing | ||
# an LDPC code stored in compressed sparse column (CSC) format. | ||
# See command line help for how to use it. | ||
|
||
using SparseArrays | ||
using LinearAlgebra | ||
|
||
using Pkg | ||
|
||
const cpp_file_description = """ | ||
// This file was automatically generated using LDPCStorage.jl v$(Pkg.project().version) (https://github.com/XQP-Munich/LDPCStorage.jl). | ||
// A sparse LDPC matrix (containing only zeros and ones) is saved in compressed sparse column (CSC) format. | ||
// Since the matrix (and LDPC code) is known at compile time, there is no need to save it separately in a file. | ||
// This significantly blows up the executable size (the memory would still have to be used when saving the matrix). | ||
""" | ||
|
||
|
||
""" | ||
$(SIGNATURES) | ||
Output C++ header storing the sparse binary (containing only zeros and ones) matrix H | ||
in compressed sparse column (CSC) format. | ||
Note the conversion from Julia's one-based indices to zero-based indices in C++ (also within CSC format). | ||
""" | ||
function write_cpp_header( | ||
io::IO, | ||
H::AbstractArray{Int8, 2} | ||
; | ||
namespace_name::AbstractString = "AutogenLDPC", | ||
) | ||
H = dropzeros(H) # remove stored zeros! | ||
_, _, values = findnz(H) | ||
|
||
all(values .== 1) || throw(ArgumentError("Expected matrix containing only zeros and ones.")) | ||
|
||
num_nonzero = length(values) | ||
if log2(num_nonzero) < 16 | ||
colptr_cpp_type = "std::uint16_t" | ||
elseif log2(num_nonzero) < 32 | ||
colptr_cpp_type = "std::uint32_t" | ||
elseif log2(num_nonzero) < 64 | ||
colptr_cpp_type = "std::uint64_t" | ||
else | ||
throw(ArgumentError("Input matrix not sparse? Has $num_nonzero entries...")) | ||
end | ||
|
||
if log2(size(H, 1)) < 16 | ||
row_idx_type = "std::uint16_t" | ||
else | ||
row_idx_type = "std::uint32_t" | ||
end | ||
|
||
print(io, cpp_file_description) | ||
|
||
println(io, """ | ||
#include <cstdint> | ||
#include <array> | ||
namespace $namespace_name { | ||
constexpr inline std::size_t M = $(size(H, 1)); | ||
constexpr inline std::size_t N = $(size(H, 2)); | ||
constexpr inline std::size_t num_nz = $num_nonzero; | ||
constexpr inline std::array<$colptr_cpp_type, N + 1> colptr = {""") | ||
|
||
for (i, idx) in enumerate(H.colptr) | ||
print(io, "0x$(string(idx - 1, base=16))") # Convert index to base zero | ||
if i != length(H.colptr) | ||
print(io, ",") | ||
end | ||
if mod(i, 100) == 0 | ||
println(io, "") # for formatting. | ||
end | ||
end | ||
println(io, "\n};\n") | ||
|
||
println(io, "// ------------------------------------------------------- \n") | ||
println(io, "constexpr inline std::array<$row_idx_type, num_nz> row_idx = {") | ||
|
||
for (i, idx) in enumerate(H.rowval) | ||
print(io, "0x$(string(idx - 1, base=16))") # Convert index to base zero | ||
if i != length(H.rowval) | ||
print(io, ",") | ||
end | ||
if mod(i, 100) == 0 | ||
println(io, "") # for formatting. | ||
end | ||
end | ||
println(io, "\n};\n\n") | ||
|
||
println(io, "} // namespace $namespace_name") | ||
end |
Oops, something went wrong.