Skip to content

Commit

Permalink
added more helper functions, update dep, and change a thread value
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhack committed Sep 14, 2021
1 parent 69b3d58 commit a7c47df
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cmd/internal/tools/bec/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func runSimulation(ctx context.Context, data *tools.SimulationStats, ecc *linear
numberOfThread = runtime.NumCPU()
}

trialsPerIter := numberOfThread * 10
trialsPerIter := numberOfThread
bar := pb.StartNew(int(Trials) * len(ErrorProbability))
trialLoops:
for t := 0; t <= int(Trials); t += trialsPerIter {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/nathanhack/avgstd v0.0.0-20210531154130-989d51af96a2
github.com/nathanhack/intmat v0.0.0-20210228003141-935751eb5e16
github.com/nathanhack/sparsemat v0.0.0-20210707154153-866acabf5d96
github.com/nathanhack/sparsemat v0.0.0-20210717173404-9f64a43cf6f1
github.com/nathanhack/threadpool v0.0.0-20210629001431-9d0d768b0f6d
github.com/onsi/ginkgo v1.14.1 // indirect
github.com/onsi/gomega v1.10.2 // indirect
Expand Down
136 changes: 85 additions & 51 deletions linearblock/linearblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,69 @@ func (l *LinearBlock) EncodeBE(message mat.SparseVector) (codeword []bec.Erasure
return codeword
}

//Decode takes in a codeword and returns the message contained in it
func (l *LinearBlock) Decode(codeword mat.SparseVector) (message mat.SparseVector) {
if codeword.Len() != l.CodewordLength() {
panic(fmt.Sprintf("codeword length == %v required but found %v", l.CodewordLength(), codeword.Len()))
}

ml := l.MessageLength()

codeword = ToSystematic(codeword, l.Processing.HColumnOrder)
return codeword.Slice(0, ml)
}

func (l *LinearBlock) DecodeBE(codeword []bec.ErasureBit) (message []bec.ErasureBit) {
if len(codeword) != l.CodewordLength() {
panic(fmt.Sprintf("codeword length == %v required but found %v", l.CodewordLength(), len(codeword)))
}

ml := l.MessageLength()

codeword = ToSystematicBE(codeword, l.Processing.HColumnOrder)
return codeword[0:ml]
}

func (l *LinearBlock) Syndrome(codeword mat.SparseVector) (syndrome mat.SparseVector) {
syndrome = mat.CSRVec(l.ParitySymbols())
syndrome.MatMul(l.H, codeword)
return
}

func (l *LinearBlock) MessageLength() int {
k, _ := l.Processing.G.Dims()
return k
}
func (l *LinearBlock) ParitySymbols() int {
m, _ := l.H.Dims()
return m
}
func (l *LinearBlock) CodewordLength() int {
_, n := l.H.Dims()
return n
}
func (l *LinearBlock) CodeRate() float64 {
return float64(l.MessageLength()) / float64(l.CodewordLength())
}

//Validate will test if this linearblock satisfies G*H.T=0, where G is the generator matrix and H.T is the transpose of H
func (l *LinearBlock) Validate() bool {
//now we validate it
return internal.ValidateHGMatrices(l.Processing.G, internal.ColumnSwapped(l.H, l.Processing.HColumnOrder))
}

func (l *LinearBlock) String() string {
buf := strings.Builder{}
buf.WriteString("{\nH:\n")
buf.WriteString(l.H.String())
buf.WriteString(fmt.Sprintf("Order: %v", l.Processing.HColumnOrder))
buf.WriteString("\nG:\n")
buf.WriteString(l.Processing.G.String())
buf.WriteString("\n}\n")
return buf.String()
}

//ToNonSystematic take in a systematic codeword and the ordering, it returns the nonsystematic form of it
func ToNonSystematic(codeword mat.SparseVector, ordering []int) mat.SparseVector {
if len(ordering) > 0 && codeword.Len() != len(ordering) {
panic("vector length must equal ordering length")
Expand All @@ -89,6 +152,7 @@ func ToNonSystematic(codeword mat.SparseVector, ordering []int) mat.SparseVector
return result
}

//ToNonSystematicBE take in a systematic codeword and the ordering, it returns the nonsystematic form of it
func ToNonSystematicBE(codeword []bec.ErasureBit, ordering []int) []bec.ErasureBit {
if len(ordering) > 0 && len(codeword) != len(ordering) {
panic("vector length must equal ordering length")
Expand All @@ -102,6 +166,7 @@ func ToNonSystematicBE(codeword []bec.ErasureBit, ordering []int) []bec.ErasureB
return result
}

//ToSystematic take in a nonsystematic codeword and the ordering, it returns the systematic form
func ToSystematic(codeword mat.SparseVector, ordering []int) mat.SparseVector {
if len(ordering) > 0 && codeword.Len() != len(ordering) {
panic("vector length must equal ordering length")
Expand All @@ -115,6 +180,7 @@ func ToSystematic(codeword mat.SparseVector, ordering []int) mat.SparseVector {
return result
}

//ToSystematicBE take in a nonsystematic codeword and the ordering, it returns the systematic form
func ToSystematicBE(codeword []bec.ErasureBit, ordering []int) []bec.ErasureBit {
if len(ordering) == 0 {
panic("ordering length must be >0")
Expand All @@ -131,64 +197,32 @@ func ToSystematicBE(codeword []bec.ErasureBit, ordering []int) []bec.ErasureBit
return result
}

//Decode takes in a codeword and returns the message contained in it
func (l *LinearBlock) Decode(codeword mat.SparseVector) (message mat.SparseVector) {
if codeword.Len() != l.CodewordLength() {
panic(fmt.Sprintf("codeword length == %v required but found %v", l.CodewordLength(), codeword.Len()))
}

ml := l.MessageLength()
//NonsystematicSplit takes in a nonsystematic codeword and the linearblock, it returns the message and parity bits
func NonsystematicSplit(codeword mat.SparseVector, block *LinearBlock) (message, parity mat.SparseVector) {
systematic := ToSystematic(codeword, block.Processing.HColumnOrder)

codeword = ToSystematic(codeword, l.Processing.HColumnOrder)
return codeword.Slice(0, ml)
return SystematicSplit(systematic, block)
}

func (l *LinearBlock) DecodeBE(codeword []bec.ErasureBit) (message []bec.ErasureBit) {
if len(codeword) != l.CodewordLength() {
panic(fmt.Sprintf("codeword length == %v required but found %v", l.CodewordLength(), len(codeword)))
//SystematicSplit takes in a systematic codeword and splits it into the message and parity bits
func SystematicSplit(codeword mat.SparseVector, block *LinearBlock) (message, parity mat.SparseVector) {
if codeword.Len() != block.CodewordLength() {
panic("codeword length must block's codeword length")
}

ml := l.MessageLength()

codeword = ToSystematicBE(codeword, l.Processing.HColumnOrder)
return codeword[0:ml]
return codeword.Slice(0, block.MessageLength()), codeword.Slice(block.MessageLength(), block.ParitySymbols())
}

func (l *LinearBlock) Syndrome(codeword mat.SparseVector) (syndrome mat.SparseVector) {
syndrome = mat.CSRVec(l.ParitySymbols())
syndrome.MatMul(l.H, codeword)
return
}
//NonsystematicBESplit takes in a nonsystematic codeword and splits it into the message and parity bits
func NonsystematicBESplit(codeword []bec.ErasureBit, block *LinearBlock) (message, parity []bec.ErasureBit) {
systematic := ToSystematicBE(codeword, block.Processing.HColumnOrder)

func (l *LinearBlock) MessageLength() int {
k, _ := l.Processing.G.Dims()
return k
}
func (l *LinearBlock) ParitySymbols() int {
m, _ := l.H.Dims()
return m
}
func (l *LinearBlock) CodewordLength() int {
_, n := l.H.Dims()
return n
}
func (l *LinearBlock) CodeRate() float64 {
return float64(l.MessageLength()) / float64(l.CodewordLength())
}

//Validate will test if this linearblock satisfies G*H.T=0, where G is the generator matrix and H.T is the transpose of H
func (l *LinearBlock) Validate() bool {
//now we validate it
return internal.ValidateHGMatrices(l.Processing.G, internal.ColumnSwapped(l.H, l.Processing.HColumnOrder))
return SystematicBESplit(systematic, block)
}

func (l *LinearBlock) String() string {
buf := strings.Builder{}
buf.WriteString("{\nH:\n")
buf.WriteString(l.H.String())
buf.WriteString(fmt.Sprintf("Order: %v", l.Processing.HColumnOrder))
buf.WriteString("\nG:\n")
buf.WriteString(l.Processing.G.String())
buf.WriteString("\n}\n")
return buf.String()
//SystematicBESplit takes in a systematic codeword and splits it into the message and parity bits
func SystematicBESplit(codeword []bec.ErasureBit, block *LinearBlock) (message, parity []bec.ErasureBit) {
if len(codeword) != block.CodewordLength() {
panic("codeword length must block's codeword length")
}
return codeword[:block.MessageLength()], codeword[block.MessageLength():]
}

0 comments on commit a7c47df

Please sign in to comment.