Skip to content

Commit

Permalink
Updates Julia command line programs
Browse files Browse the repository at this point in the history
Improve CLI. Also allow outputting quasi-cyclic components as C++ headers (now supported by new version of LDPCStorate.jl)
  • Loading branch information
adomasbaliuka committed Apr 15, 2024
1 parent a200861 commit 72db92f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
62 changes: 45 additions & 17 deletions codes/ldpc_codegen.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 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.
# See command line help for how to use it by (in this directory) running
# `julia --project ldpc_codegen.jl --help`

using SparseArrays
using LinearAlgebra
Expand All @@ -15,12 +16,17 @@ struct ValidPath
ValidPath(s::AbstractString) = isfile(s) ? new(s) : error("No such file: '$s'")
end


function parse_my_args(args)
"""
Parses command line arguments
"""
function parse_my_args()
s = ArgParseSettings(
"Create a C++ code-file containing an LDPC matrix in compressed sparse column (CSC) format.
The matrix is given as an `.alist` file."
; allow_ambiguous_opts=false)
The matrix is given as an `.alist` file.
Example usage:
julia --project ldpc_codegen.jl --input_code_path ./filename.qccsc.json --output_path autogen_ldpc.hpp
"
; allow_ambiguous_opts=false, preformatted_description=true)

@add_arg_table! s begin
"--input_code_path"
Expand All @@ -35,16 +41,29 @@ function parse_my_args(args)
help = "Path to put the automatically generated C++ file containing the LDPC code.
If the file extension is .bincsc.json, a compressed sparse column storage of the full matrix
(actual binary matrix, not QC-exponents) is written instead."
"--output_quasi_cyclic_exponents"
action = :store_true
help = "ONLY SUPPORTED if `output_path` is a C++ header!!!
If this flag is NOT set (default), the header contains two arrays (`colptr`, `row_idx`).
If this flag is set, the resulting C++ header contains three arrays (`colptr`, `row_idx` and `values`).
"
end

parsed_args = parse_args(args, s)
parsed_args = parse_args(s)
println("Parsed args:")
for (key, val) in parsed_args
println(" $key => $(repr(val))")
end

return abspath(parsed_args["input_code_path"].val),
abspath(parsed_args["output_path"])
if parsed_args["output_quasi_cyclic_exponents"] && !endswith(abspath(parsed_args["output_path"]), ".hpp")
@show parsed_input_code_abspath
throw(ArgumentError(
"Flag `--output_quasi_cyclic_exponents` is only supported if output is a C++ header."))
end
parsed_input_code_abspath = abspath(parsed_args["input_code_path"].val)
return parsed_input_code_abspath,
abspath(parsed_args["output_path"]),
parsed_args["output_quasi_cyclic_exponents"]
end


Expand Down Expand Up @@ -103,32 +122,41 @@ function convert_all_qc_to_binary(
end


function main(args)
code_path, output_path = parse_my_args(args)
function main()
code_path, output_path, output_quasi_cyclic_exponents = parse_my_args()

if endswith(code_path, ".alist")
H = load_alist(code_path)
elseif endswith(code_path, ".qccsc.json")
H = LDPCStorage.load_ldpc_from_json(code_path; expand_qc_exponents_to_binary=true)
# When outputting QC-exponents, do not expand the matrix to be binary!
H = LDPCStorage.load_ldpc_from_json(code_path; expand_qc_exponents_to_binary=!output_quasi_cyclic_exponents)
elseif endswith(code_path, ".bincsc.json")
H = LDPCStorage.load_ldpc_from_json(code_path; expand_qc_exponents_to_binary=false)
elseif endswith(code_path, ".cscmat")
@warn "The `.cscmat` file format is deprecated. Consider converting to the json based formats instead!"
H = load_cscmat_standard_or_qc_exponents(code_path)
else
throw(ArgumentError(
"Unsupported input file at path $code_path. Expected file extension `qccsc.json`,`bincsc.json`, `.cscmat` (deprecated) or `.alist`."))
"Unsupported input file at path $code_path.
Expected file extension `qccsc.json`,`bincsc.json`, `.cscmat` (deprecated) or `.alist`."))
end

if endswith(output_path, ".alist")
LDPCStorage.save_to_alist(output_path, H)
elseif endswith(output_path, ".hpp")
open(output_path, "w+") do io
LDPCStorage.print_cpp_header(io, H)
if output_quasi_cyclic_exponents
expansion_factor = LDPCStorage.get_qc_expansion_factor(code_path)
open(output_path, "w+") do io
LDPCStorage.print_cpp_header_QC(io, H; expansion_factor)
end
else
open(output_path, "w+") do io
LDPCStorage.print_cpp_header(io, H)
end
end
elseif endswith(output_path, ".cscmat")
LDPCStorage.save_to_cscmat(H, output_path; allow_omit_entries_if_only_stored_ones=true,)
# elseif code_path_file_extension == ".qccsc.json" # would need get expansion factor from input file.
LDPCStorage.save_to_cscmat(H, output_path; allow_omit_entries_if_only_stored_ones=true,)
# elseif code_path_file_extension == ".qccsc.json" # need expansion factor. Could only do (useless) `qccsc.json` -> `qccsc.json`
elseif endswith(output_path, ".bincsc.json")
LDPCStorage.save_to_bincscjson(output_path, H)
else
Expand All @@ -140,4 +168,4 @@ function main(args)
end


main(ARGS)
main()
22 changes: 15 additions & 7 deletions codes/rateadaptive_codegen.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 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.
# See command line help for how to use it by (in this directory) running
# `julia --project rateadaptive_codegen.jl --help`

using SparseArrays
using LinearAlgebra
Expand Down Expand Up @@ -29,11 +31,17 @@ struct ValidPath
end


function parse_my_args(args)
"""
Parses command line arguments
"""
function parse_my_args()
s = ArgParseSettings(
"Create a C++ code-file containing line indices for combination,
to be applied to a given LDPC code in order to achieve rate adaption."
; allow_ambiguous_opts=false)
to be applied to a given LDPC code in order to achieve rate adaption.
Example usage:
julia --project rateadaptive_codegen.jl ./filename.csv --output_path autogen_rate_adaption.hpp
"
; allow_ambiguous_opts=false, preformatted_description=true)

@add_arg_table! s begin
"rate_adaption_file_path"
Expand All @@ -49,7 +57,7 @@ function parse_my_args(args)
action = :store_true
end

parsed_args = parse_args(args, s)
parsed_args = parse_args(s)
println("Parsed args:")
for (key,val) in parsed_args
println(" $key => $(repr(val))")
Expand Down Expand Up @@ -121,13 +129,13 @@ function write_array_to_csv(arr::AbstractArray, out_path::AbstractString)
end


function main(args)
rate_adaption_file_path, output_path, only_debug_mode = parse_my_args(args)
function main()
rate_adaption_file_path, output_path, only_debug_mode = parse_my_args()
rate_adaption_rows = read_rate_adaption_file(rate_adaption_file_path)
write_cpp_constexpr_rate_adaption(rate_adaption_rows, output_path; only_debug_mode)

@info "Saved C++ file ($(Base.stat(output_path).size) bytes) at '$(output_path)'."
end


main(ARGS)
main()

0 comments on commit 72db92f

Please sign in to comment.