Compare commits
No commits in common. "main" and "bb8f398cfa2a9644f3725b849174764d9b68c891" have entirely different histories.
main
...
bb8f398cfa
54
main.go
54
main.go
|
@ -6,7 +6,6 @@ import (
|
|||
"io"
|
||||
"math/big"
|
||||
mrand "math/rand"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -18,12 +17,11 @@ type job struct {
|
|||
}
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(101)
|
||||
s1 := mrand.NewSource(time.Now().UnixMicro())
|
||||
r1 := mrand.New(s1)
|
||||
start := time.Now()
|
||||
|
||||
p, _ := prime(r1, 8096, 100)
|
||||
p, _ := prime(r1, 8096, 1000)
|
||||
|
||||
finish := time.Now()
|
||||
elapsed := finish.Sub(start)
|
||||
|
@ -40,12 +38,11 @@ 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)
|
||||
jobs := make(chan job, 100)
|
||||
results := make(chan job, 100)
|
||||
numbers := make(chan *big.Int, 100)
|
||||
p = new(big.Int)
|
||||
jobs := make(chan job)
|
||||
results := make(chan job)
|
||||
done := new(bool)
|
||||
*done = false
|
||||
|
||||
|
@ -53,17 +50,21 @@ func prime(rand io.Reader, bits int, threads int) (p *big.Int, err error) {
|
|||
go worker(i, jobs, results, done)
|
||||
}
|
||||
|
||||
fmt.Printf("Seeding and starting worker pool\n")
|
||||
for i := 0; i < 10; i++ {
|
||||
go rng(i, bits, numbers, done)
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
|
||||
fmt.Printf("Starting!\n")
|
||||
for i := 0; ; i++ {
|
||||
_, err = io.ReadFull(rand, bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 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)
|
||||
|
||||
newjob := &job{
|
||||
p: <-numbers,
|
||||
i: 20,
|
||||
p: p,
|
||||
i: 10,
|
||||
id: i,
|
||||
}
|
||||
|
||||
|
@ -81,28 +82,11 @@ 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 {
|
||||
j.result = j.p.ProbablyPrime(j.i)
|
||||
fmt.Printf("Worker\t %d checked a number\n", id)
|
||||
//fmt.Printf("Worker\t %d checked a number\n", id)
|
||||
if *done {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue