Moved rng to another worker pool

This commit is contained in:
DutchEllie 2022-02-13 13:16:38 +01:00
parent bb8f398cfa
commit 58d5c52886
1 changed files with 26 additions and 15 deletions

39
main.go
View File

@ -21,7 +21,7 @@ func main() {
r1 := mrand.New(s1)
start := time.Now()
p, _ := prime(r1, 8096, 1000)
p, _ := prime(r1, 8096, 32)
finish := time.Now()
elapsed := finish.Sub(start)
@ -38,11 +38,12 @@ func main() {
}
func prime(rand io.Reader, bits int, threads int) (p *big.Int, err error) {
bytes := make([]byte, (bits+7)/8)
//bytes := make([]byte, (bits+7)/8)
p = new(big.Int)
//p = new(big.Int)
jobs := make(chan job)
results := make(chan job)
numbers := make(chan *big.Int, 100)
done := new(bool)
*done = false
@ -50,20 +51,13 @@ func prime(rand io.Reader, bits int, threads int) (p *big.Int, err error) {
go worker(i, jobs, results, done)
}
for i := 0; ; i++ {
_, err = io.ReadFull(rand, bytes)
if err != nil {
return nil, err
for i := 0; i < 4; i++ {
go rng(i, bits, numbers, done)
}
// This makes sure that the least significant bit is always 1
// meaning that the number is always odd, since even numbers
// aren't prime
bytes[len(bytes)-1] |= 1
p.SetBytes(bytes)
for i := 0; ; i++ {
newjob := &job{
p: p,
p: <-numbers,
i: 10,
id: i,
}
@ -82,6 +76,23 @@ func prime(rand io.Reader, bits int, threads int) (p *big.Int, err error) {
}
func rng(id int, bits int, num chan<- *big.Int, done *bool) {
s1 := mrand.NewSource(time.Hour.Microseconds())
rand := mrand.New(s1)
bytes := make([]byte, (bits+7)/8)
for !*done {
p := new(big.Int)
io.ReadFull(rand, bytes)
// This makes sure that the least significant bit is always 1
// meaning that the number is always odd, since even numbers
// aren't prime
bytes[len(bytes)-1] |= 1
p.SetBytes(bytes)
num <- p
}
}
func worker(id int, jobs <-chan job, results chan<- job, done *bool) {
//fmt.Printf("Worker %d starting now!\n", id)
for j := range jobs {