mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 20:56:42 +00:00
up
This commit is contained in:
parent
9d134b356c
commit
ddce6abc5c
1 changed files with 33 additions and 8 deletions
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
@ -12,6 +13,7 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/ethash"
|
"github.com/ethereum/ethash"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
"github.com/samuel/go-opencl/cl"
|
"github.com/samuel/go-opencl/cl"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -27,13 +29,14 @@ type params struct {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
kernelFn = "ethash_kernel.cl"
|
kernelFn = "ethash_kernel.cl"
|
||||||
searchBuffSize = 4
|
searchBuffSize = 2
|
||||||
maxSearchResults = 63
|
maxSearchResults = 63
|
||||||
searchBatchSize = 1024
|
searchBatchSize = 1024
|
||||||
dagSize = 1024
|
dagSize = 1024 * 32
|
||||||
|
|
||||||
SIZEOF_UINT32 = 4
|
SIZEOF_UINT32 = 4
|
||||||
ETHASH_MIX_BYTES = 32
|
ETHASH_MIX_BYTES = 128
|
||||||
|
ACCESSES = 64
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -106,7 +109,7 @@ func New() (*EthashCL, error) {
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
err = tmpl.Execute(&buffer, params{
|
err = tmpl.Execute(&buffer, params{
|
||||||
GroupSize: workGroupSize, Accesses: 1, MaxOutputs: 4, DagSize: dagSize / ETHASH_MIX_BYTES,
|
GroupSize: workGroupSize, Accesses: ACCESSES, MaxOutputs: 4, DagSize: dagSize / ETHASH_MIX_BYTES,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("template err:", err)
|
return nil, fmt.Errorf("template err:", err)
|
||||||
|
|
@ -141,6 +144,7 @@ func New() (*EthashCL, error) {
|
||||||
return nil, fmt.Errorf("dag err:", err)
|
return nil, fmt.Errorf("dag err:", err)
|
||||||
}
|
}
|
||||||
dag := pow.Full.DAG(0).Ptr()
|
dag := pow.Full.DAG(0).Ptr()
|
||||||
|
fmt.Println("DAG ptr", dag)
|
||||||
|
|
||||||
queue.EnqueueWriteBuffer(dagBuff, true, 0, dagSize, dag, nil)
|
queue.EnqueueWriteBuffer(dagBuff, true, 0, dagSize, dag, nil)
|
||||||
|
|
||||||
|
|
@ -151,7 +155,7 @@ func New() (*EthashCL, error) {
|
||||||
|
|
||||||
searchBuffers := make([]*cl.MemObject, searchBuffSize)
|
searchBuffers := make([]*cl.MemObject, searchBuffSize)
|
||||||
for i := 0; i < searchBuffSize; i++ {
|
for i := 0; i < searchBuffSize; i++ {
|
||||||
searchBuff, err := context.CreateEmptyBuffer(cl.MemReadOnly, maxSearchResults+1*SIZEOF_UINT32)
|
searchBuff, err := context.CreateEmptyBuffer(cl.MemWriteOnly, maxSearchResults+1*SIZEOF_UINT32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("search buffer err:", err)
|
return nil, fmt.Errorf("search buffer err:", err)
|
||||||
}
|
}
|
||||||
|
|
@ -226,25 +230,37 @@ func (h *EthashCL) Search(header common.Hash, target uint64, doneFn func([]uint6
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("buffer mapping err: %v", err)
|
return fmt.Errorf("buffer mapping err: %v", err)
|
||||||
}
|
}
|
||||||
|
h.queue.Flush()
|
||||||
|
|
||||||
results := cres.ByteSlice()
|
results := cres.ByteSlice()
|
||||||
nfound := binary.BigEndian.Uint32(results)
|
nfound := binary.BigEndian.Uint32(results)
|
||||||
|
fmt.Println("sollutions found:", nfound)
|
||||||
nfound = uint32(math.Min(float64(nfound), float64(maxSearchResults)))
|
nfound = uint32(math.Min(float64(nfound), float64(maxSearchResults)))
|
||||||
|
|
||||||
nonces := make([]uint64, maxSearchResults)
|
nonces := make([]uint64, maxSearchResults)
|
||||||
for i := uint32(0); i < nfound; i++ {
|
for i := uint32(0); i < nfound; i++ {
|
||||||
nonces[i] = nonce + uint64(binary.BigEndian.Uint32(results[1+i*SIZEOF_UINT32:]))
|
nonces[i] = nonce + uint64(binary.BigEndian.Uint32(results[1+i*SIZEOF_UINT32:]))
|
||||||
}
|
}
|
||||||
|
h.queue.EnqueueUnmapMemObject(h.searchBuffers[buff], cres, nil)
|
||||||
if doneFn(nonces) {
|
if doneFn(nonces) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
h.queue.EnqueueUnmapMemObject(h.searchBuffers[buff], cres, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rndHash() common.Hash {
|
||||||
|
var h common.Hash
|
||||||
|
rand.Read(h[:])
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
glog.SetV(6)
|
||||||
|
glog.SetToStderr(true)
|
||||||
|
|
||||||
fmt.Println("initialising OpenCL miner...")
|
fmt.Println("initialising OpenCL miner...")
|
||||||
|
|
||||||
gpu, err := New()
|
gpu, err := New()
|
||||||
|
|
@ -254,8 +270,9 @@ func main() {
|
||||||
}
|
}
|
||||||
fmt.Println("OpenCL miner initialised")
|
fmt.Println("OpenCL miner initialised")
|
||||||
|
|
||||||
fmt.Println("Searching for solution...")
|
hash := rndHash()
|
||||||
err = gpu.Search(common.Hash{}, 1000000000000, func(res []uint64) bool {
|
fmt.Printf("Searching for solution for %x\n", hash)
|
||||||
|
err = gpu.Search(hash, 0x000000ffffffffff, func(res []uint64) bool {
|
||||||
fmt.Printf("found: %x\n", res)
|
fmt.Printf("found: %x\n", res)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
@ -263,4 +280,12 @@ func main() {
|
||||||
fmt.Println("search err:", err)
|
fmt.Println("search err:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
gpu.ctx.Release()
|
||||||
|
gpu.dagBuff.Release()
|
||||||
|
gpu.headerBuff.Release()
|
||||||
|
gpu.searchKernel.Release()
|
||||||
|
gpu.queue.Release()
|
||||||
|
for _, searchBuffer := range gpu.searchBuffers {
|
||||||
|
searchBuffer.Release()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue