Skip to content

Commit

Permalink
rename and fixed benchmark test
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhack committed Apr 30, 2023
1 parent e90703b commit 772032e
Show file tree
Hide file tree
Showing 34 changed files with 147 additions and 131 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# errorcorrectingcodes
# ecc
Is a simple Golang library containing error correcting codes.

#### Road map:
Expand Down
37 changes: 19 additions & 18 deletions benchmarking/benchmarking.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package benchmarking
import (
"context"
"fmt"
"math"
"sync"

"github.com/cheggaaa/pb/v3"
"github.com/nathanhack/avgstd"
"github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bec"
"github.com/nathanhack/ecc/linearblock/messagepassing/bec"
mat "github.com/nathanhack/sparsemat"
"github.com/nathanhack/threadpool"
mat2 "gonum.org/v1/gonum/mat"
"math"
"sync"
)

type Stats struct {
Expand All @@ -31,19 +32,19 @@ type Checkpoints func(updatedStats Stats)

type BinaryMessageConstructor func(trial int) (message mat.SparseVector)

//specfic to BSC
// specfic to BSC
type BinarySymmetricChannelEncoder func(message mat.SparseVector) (codeword mat.SparseVector)
type BinarySymmetricChannel func(codeword mat.SparseVector) (channelInducedCodeword mat.SparseVector)
type BinarySymmetricChannelCorrection func(originalCodeword, channelInducedCodeword mat.SparseVector) (fixedChannelInducedCodeword mat.SparseVector)
type BinarySymmetricChannelMetrics func(originalMessage, originalCodeword, fixedChannelInducedCodeword mat.SparseVector) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64)

//specific to BEC
// specific to BEC
type BinaryErasureChannelEncoder func(message mat.SparseVector) (codeword []bec.ErasureBit)
type BinaryErasureChannel func(codeword []bec.ErasureBit) (channelInducedCodeword []bec.ErasureBit)
type BinaryErasureChannelCorrection func(originalCodeword, channelInducedCodeword []bec.ErasureBit) (fixedChannelInducedCodeword []bec.ErasureBit)
type BinaryErasureChannelMetrics func(originalMessage mat.SparseVector, originalCodeword, fixedChannelInducedCodeword []bec.ErasureBit) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64)

//specific to BPSK
// specific to BPSK
type BPSKChannelEncoder func(message mat.SparseVector) (codeword mat2.Vector)
type BPSKChannel func(codeword mat2.Vector) (channelInducedCodeword mat2.Vector)
type BPSKChannelCorrection func(originalCodeword, channelInducedCodeword mat2.Vector) (fixedChannelInducedCodeword mat2.Vector)
Expand Down Expand Up @@ -71,7 +72,7 @@ func BenchmarkBSCContinueStats(ctx context.Context,
checkpoints Checkpoints,
previousStats Stats,
showProgress bool) Stats {
trialsToRun := trials - previousStats.ChannelCodewordError.Count
trialsToRun := trials - int(previousStats.ChannelCodewordError.Count)
if trialsToRun <= 0 {
return previousStats
}
Expand Down Expand Up @@ -113,7 +114,7 @@ func BenchmarkBSCContinueStats(ctx context.Context,
statsMux.Unlock()
}

for i := previousStats.ChannelCodewordError.Count; i < trials; i++ {
for i := int(previousStats.ChannelCodewordError.Count); i < trials; i++ {
tmp := i
pool.Add(func() { trial(tmp) })
}
Expand Down Expand Up @@ -151,7 +152,7 @@ func BenchmarkBECContinueStats(
checkpoints Checkpoints,
previousStats Stats,
showProgressBar bool) Stats {
trialsToRun := trials - previousStats.ChannelCodewordError.Count
trialsToRun := trials - int(previousStats.ChannelCodewordError.Count)
if trialsToRun <= 0 {
return previousStats
}
Expand Down Expand Up @@ -194,7 +195,7 @@ func BenchmarkBECContinueStats(
statsMux.Unlock()
}

for i := previousStats.ChannelCodewordError.Count; i < trials; i++ {
for i := int(previousStats.ChannelCodewordError.Count); i < trials; i++ {
pool.Add(func() { trial(i) })
}

Expand Down Expand Up @@ -231,7 +232,7 @@ func BenchmarkBPSKContinueStats(ctx context.Context,
checkpoints Checkpoints,
previousStats Stats,
showProgress bool) Stats {
trialsToRun := trials - previousStats.ChannelCodewordError.Count
trialsToRun := trials - int(previousStats.ChannelCodewordError.Count)
if trialsToRun <= 0 {
return previousStats
}
Expand Down Expand Up @@ -273,7 +274,7 @@ func BenchmarkBPSKContinueStats(ctx context.Context,
statsMux.Unlock()
}

for i := previousStats.ChannelCodewordError.Count; i < trials; i++ {
for i := int(previousStats.ChannelCodewordError.Count); i < trials; i++ {
pool.Add(func() { trial(i) })
}
pool.Wait()
Expand All @@ -288,7 +289,7 @@ func BenchmarkBPSKContinueStats(ctx context.Context,
return previousStats
}

//HammingDistanceErasuresToBits calculates number of bits different.
// HammingDistanceErasuresToBits calculates number of bits different.
// If a and b are different sizes it assumes they are
// both aligned with the zero index (the difference is at the end)
func HammingDistanceErasuresToBits(a []bec.ErasureBit, b mat.SparseVector) int {
Expand All @@ -308,7 +309,7 @@ func HammingDistanceErasuresToBits(a []bec.ErasureBit, b mat.SparseVector) int {
return max - min + count
}

//BitsToBPSK converts a [0,1] matrix to a [-1,1] matrix
// BitsToBPSK converts a [0,1] matrix to a [-1,1] matrix
func BitsToBPSK(a mat.SparseVector) mat2.Vector {
output := mat2.NewVecDense(a.Len(), nil)

Expand All @@ -323,7 +324,7 @@ func BitsToBPSK(a mat.SparseVector) mat2.Vector {
return output
}

//BPSKToBits conversts a BPSK vector [-1,1] to sparse vector [0,1].
// BPSKToBits conversts a BPSK vector [-1,1] to sparse vector [0,1].
// Values >= boundary will be considered a 1, otherwise a 0.
func BPSKToBits(a mat2.Vector, boundary float64) mat.SparseVector {
result := mat.CSRVec(a.Len())
Expand All @@ -336,7 +337,7 @@ func BPSKToBits(a mat2.Vector, boundary float64) mat.SparseVector {
return result
}

//HammingDistanceBPSK calculates number of bits different.
// HammingDistanceBPSK calculates number of bits different.
// Assumes >=0 is 1 and <0 is 0
// If a and b are different sizes it assumes they are
// both aligned with the zero index (the difference is at the end)
Expand All @@ -359,7 +360,7 @@ func HammingDistanceBPSK(a, b mat2.Vector) int {
return max - min + count
}

//BitsToErased creates a slice of ErasureBits from the codeword passed in
// BitsToErased creates a slice of ErasureBits from the codeword passed in
func BitsToErased(codeword mat.SparseVector) []bec.ErasureBit {
output := make([]bec.ErasureBit, codeword.Len())
for i := 0; i < codeword.Len(); i++ {
Expand All @@ -368,7 +369,7 @@ func BitsToErased(codeword mat.SparseVector) []bec.ErasureBit {
return output
}

//ErasedCount returns the number of erased bits
// ErasedCount returns the number of erased bits
func ErasedCount(base []bec.ErasureBit) (count int) {
for _, e := range base {
if e == bec.Erased {
Expand Down
8 changes: 4 additions & 4 deletions benchmarking/benchmarking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package benchmarking
import (
"context"
"fmt"
"github.com/nathanhack/errorcorrectingcodes/linearblock/hamming"
"github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bec"
"github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bec/iterative"
"github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bitflipping/harddecision"
"github.com/nathanhack/ecc/linearblock/hamming"
"github.com/nathanhack/ecc/linearblock/messagepassing/bec"
"github.com/nathanhack/ecc/linearblock/messagepassing/bec/iterative"
"github.com/nathanhack/ecc/linearblock/messagepassing/bitflipping/harddecision"
mat "github.com/nathanhack/sparsemat"
mat2 "gonum.org/v1/gonum/mat"
"runtime"
Expand Down
19 changes: 10 additions & 9 deletions benchmarking/randomdata.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package benchmarking

import (
"github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bec"
mat "github.com/nathanhack/sparsemat"
mat2 "gonum.org/v1/gonum/mat"
"math"
"math/rand"

"github.com/nathanhack/ecc/linearblock/messagepassing/bec"
mat "github.com/nathanhack/sparsemat"
mat2 "gonum.org/v1/gonum/mat"
)

//RandomMessage creates a random message of length len.
// RandomMessage creates a random message of length len.
func RandomMessage(len int) mat.SparseVector {
message := mat.CSRVec(len)
for i := 0; i < len; i++ {
Expand All @@ -17,7 +18,7 @@ func RandomMessage(len int) mat.SparseVector {
return message
}

//RandomMessage creates a random message o lenght len with a hamming weight equal to onesCount
// RandomMessage creates a random message o lenght len with a hamming weight equal to onesCount
func RandomMessageOnesCount(len int, onesCount int) mat.SparseVector {
message := mat.CSRVec(len)
for message.HammingWeight() < onesCount {
Expand All @@ -26,7 +27,7 @@ func RandomMessageOnesCount(len int, onesCount int) mat.SparseVector {
return message
}

//RandomFlipBitCount randomly flips min(numberOfBitsToFlip,len(input)) number of bits.
// RandomFlipBitCount randomly flips min(numberOfBitsToFlip,len(input)) number of bits.
func RandomFlipBitCount(input mat.SparseVector, numberOfBitsToFlip int) mat.SparseVector {
output := mat.CSRVecCopy(input)

Expand All @@ -41,12 +42,12 @@ func RandomFlipBitCount(input mat.SparseVector, numberOfBitsToFlip int) mat.Spar
return output
}

//RandomErase creates a new slice of ErasureBits with some of them set to Erased given the probabilityOfErasure
// RandomErase creates a new slice of ErasureBits with some of them set to Erased given the probabilityOfErasure
func RandomErase(codeword []bec.ErasureBit, probabilityOfErasure float64) []bec.ErasureBit {
return RandomEraseCount(codeword, int(math.Round(probabilityOfErasure*float64(len(codeword)))))
}

//RandomErase creates a copy of the codeword and randomly sets numberOfBitsToFlip of them to Erased
// RandomErase creates a copy of the codeword and randomly sets numberOfBitsToFlip of them to Erased
func RandomEraseCount(codeword []bec.ErasureBit, numberOfBitsToFlip int) []bec.ErasureBit {
output := make([]bec.ErasureBit, len(codeword))

Expand All @@ -69,7 +70,7 @@ func RandomEraseCount(codeword []bec.ErasureBit, numberOfBitsToFlip int) []bec.E
return output
}

//RandomNoiseBPSK creates a randomizes version of the bpsk vector using the E_b/N_0 passed in
// RandomNoiseBPSK creates a randomizes version of the bpsk vector using the E_b/N_0 passed in
func RandomNoiseBPSK(bpsk mat2.Vector, E_bPerN_0 float64) mat2.Vector {
//using σ^2 = N_0/2 and E_b=1
// we get σ = sqrt(1/(2*E_bPerN_0))
Expand Down
6 changes: 3 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"github.com/nathanhack/errorcorrectingcodes/cmd/internal/create/gallager"
"github.com/nathanhack/errorcorrectingcodes/cmd/internal/create/hamming"
"github.com/nathanhack/ecc/cmd/internal/create/gallager"
"github.com/nathanhack/ecc/cmd/internal/create/hamming"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -40,7 +40,7 @@ var createGallagerCmd = &cobra.Command{
Run: gallager.GallagerRun,
}

//createHammingCmd represents the Hamming command
// createHammingCmd represents the Hamming command
var createHammingCmd = &cobra.Command{
Use: "hamming OUTPUT_HAMMING_JSON",
Aliases: []string{"h", "ham"},
Expand Down
7 changes: 4 additions & 3 deletions cmd/internal/create/gallager/gallager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/nathanhack/errorcorrectingcodes/linearblock/ldpc/gallager"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"syscall"
"time"

"github.com/nathanhack/ecc/linearblock/ldpc/gallager"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var Message uint
Expand Down
9 changes: 5 additions & 4 deletions cmd/internal/create/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/nathanhack/errorcorrectingcodes/linearblock"
"github.com/nathanhack/errorcorrectingcodes/linearblock/ldpc/gce"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"syscall"
"time"

"github.com/nathanhack/ecc/linearblock"
"github.com/nathanhack/ecc/linearblock/ldpc/gce"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var MessageSize uint
Expand Down
7 changes: 4 additions & 3 deletions cmd/internal/create/hamming/hamming.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/nathanhack/errorcorrectingcodes/linearblock/hamming"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"syscall"
"time"

"github.com/nathanhack/ecc/linearblock/hamming"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
Expand Down
9 changes: 5 additions & 4 deletions cmd/internal/tools/bec/bec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package bec

import (
"context"
"github.com/nathanhack/errorcorrectingcodes/benchmarking"
"github.com/nathanhack/errorcorrectingcodes/linearblock"
"github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bec"
mat "github.com/nathanhack/sparsemat"
"math"
"math/rand"
"sync"

"github.com/nathanhack/ecc/benchmarking"
"github.com/nathanhack/ecc/linearblock"
"github.com/nathanhack/ecc/linearblock/messagepassing/bec"
mat "github.com/nathanhack/sparsemat"
)

const bitLimit = 64
Expand Down
17 changes: 9 additions & 8 deletions cmd/internal/tools/bec/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package simple
import (
"context"
"fmt"
"github.com/cheggaaa/pb/v3"
"github.com/nathanhack/errorcorrectingcodes/benchmarking"
"github.com/nathanhack/errorcorrectingcodes/cmd/internal/tools"
"github.com/nathanhack/errorcorrectingcodes/cmd/internal/tools/bec"
"github.com/nathanhack/errorcorrectingcodes/linearblock"
bec2 "github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bec"
"github.com/nathanhack/errorcorrectingcodes/linearblock/messagepassing/bec/iterative"
"github.com/spf13/cobra"
"os"
"os/signal"
"reflect"
"runtime"
"sync"
"syscall"

"github.com/cheggaaa/pb/v3"
"github.com/nathanhack/ecc/benchmarking"
"github.com/nathanhack/ecc/cmd/internal/tools"
"github.com/nathanhack/ecc/cmd/internal/tools/bec"
"github.com/nathanhack/ecc/linearblock"
bec2 "github.com/nathanhack/ecc/linearblock/messagepassing/bec"
"github.com/nathanhack/ecc/linearblock/messagepassing/bec/iterative"
"github.com/spf13/cobra"
)

var (
Expand Down
7 changes: 4 additions & 3 deletions cmd/internal/tools/bsc/bsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package bsc

import (
"context"
"github.com/nathanhack/errorcorrectingcodes/benchmarking"
"github.com/nathanhack/errorcorrectingcodes/linearblock"
mat "github.com/nathanhack/sparsemat"
"math"
"math/rand"
"sync"

"github.com/nathanhack/ecc/benchmarking"
"github.com/nathanhack/ecc/linearblock"
mat "github.com/nathanhack/sparsemat"
)

const bitLimit = 64
Expand Down
Loading

0 comments on commit 772032e

Please sign in to comment.