Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize some functions/packages. #56

Merged
merged 1 commit into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/decred/gominer/blake256"
"github.com/decred/gominer/cl"
"github.com/decred/gominer/util"
"github.com/decred/gominer/work"
)

Expand All @@ -35,6 +36,37 @@ var chainParams = &chaincfg.MainNetParams

var zeroSlice = []cl.CL_uint{cl.CL_uint(0)}

func getCLPlatforms() ([]cl.CL_platform_id, error) {
var numPlatforms cl.CL_uint
status := cl.CLGetPlatformIDs(0, nil, &numPlatforms)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetPlatformIDs")
}
platforms := make([]cl.CL_platform_id, numPlatforms)
status = cl.CLGetPlatformIDs(numPlatforms, platforms, nil)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetPlatformIDs")
}
return platforms, nil
}

// getCLDevices returns the list of devices for the given platform.
func getCLDevices(platform cl.CL_platform_id) ([]cl.CL_device_id, error) {
var numDevices cl.CL_uint
status := cl.CLGetDeviceIDs(platform, cl.CL_DEVICE_TYPE_GPU, 0, nil,
&numDevices)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetDeviceIDs")
}
devices := make([]cl.CL_device_id, numDevices)
status = cl.CLGetDeviceIDs(platform, cl.CL_DEVICE_TYPE_ALL, numDevices,
devices, nil)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetDeviceIDs")
}
return devices, nil
}

func loadProgramSource(filename string) ([][]byte, []cl.CL_size_t, error) {
var program_buffer [1][]byte
var program_size [1]cl.CL_size_t
Expand Down Expand Up @@ -101,22 +133,6 @@ type Device struct {
quit chan struct{}
}

// Uint32EndiannessSwap swaps the endianness of a uint32.
func Uint32EndiannessSwap(v uint32) uint32 {
return (v&0x000000FF)<<24 | (v&0x0000FF00)<<8 |
(v&0x00FF0000)>>8 | (v&0xFF000000)>>24
}

// rolloverExtraNonce rolls over the extraNonce if it goes over 0x00FFFFFF many
// hashes, since the first byte is reserved for the ID.
func rolloverExtraNonce(v *uint32) {
if *v&0x00FFFFFF == 0x00FFFFFF {
*v = *v & 0xFF000000
} else {
*v++
}
}

func clError(status cl.CL_int, f string) error {
if -status < 0 || int(-status) > len(cl.ERROR_CODES_STRINGS) {
return fmt.Errorf("%s returned unknown error!")
Expand Down Expand Up @@ -352,7 +368,7 @@ func (d *Device) runDevice() error {
// different work. If the extraNonce has already been
// set for valid work, restore that.
d.extraNonce += uint32(d.index) << 24
d.lastBlock[work.Nonce1Word] = Uint32EndiannessSwap(d.extraNonce)
d.lastBlock[work.Nonce1Word] = util.Uint32EndiannessSwap(d.extraNonce)

var status cl.CL_int
for {
Expand All @@ -365,8 +381,8 @@ func (d *Device) runDevice() error {
}

// Increment extraNonce.
rolloverExtraNonce(&d.extraNonce)
d.lastBlock[work.Nonce1Word] = Uint32EndiannessSwap(d.extraNonce)
util.RolloverExtraNonce(&d.extraNonce)
d.lastBlock[work.Nonce1Word] = util.Uint32EndiannessSwap(d.extraNonce)

// Update the timestamp. Only solo work allows you to roll
// the timestamp.
Expand All @@ -375,7 +391,7 @@ func (d *Device) runDevice() error {
diffSeconds := uint32(time.Now().Unix()) - d.work.TimeReceived
ts = d.work.JobTime + diffSeconds
}
d.lastBlock[work.TimestampWord] = Uint32EndiannessSwap(ts)
d.lastBlock[work.TimestampWord] = util.Uint32EndiannessSwap(ts)

// arg 0: pointer to the buffer
obuf := d.outputBuffer
Expand Down Expand Up @@ -443,7 +459,7 @@ func (d *Device) runDevice() error {
minrLog.Debugf("GPU #%d: Found candidate %v nonce %08x, "+
"extraNonce %08x, workID %08x, timestamp %08x",
d.index, i+1, outputData[i+1], d.lastBlock[work.Nonce1Word],
Uint32EndiannessSwap(d.currentWorkID),
util.Uint32EndiannessSwap(d.currentWorkID),
d.lastBlock[work.TimestampWord])

// Assess the work. If it's below target, it'll be rejected
Expand Down Expand Up @@ -505,20 +521,6 @@ func (d *Device) SetWork(w *work.Work) {
d.newWork <- w
}

func formatHashrate(h float64) string {
if h > 1000000000 {
return fmt.Sprintf("%.3fGH/s", h/1000000000)
} else if h > 1000000 {
return fmt.Sprintf("%.0fMH/s", h/1000000)
} else if h > 1000 {
return fmt.Sprintf("%.1fkH/s", h/1000)
} else if h == 0 {
return "0H/s"
}

return fmt.Sprintf("%.1f GH/s", h)
}

func getDeviceInfo(id cl.CL_device_id,
name cl.CL_device_info,
str string) string {
Expand Down Expand Up @@ -559,7 +561,7 @@ func (d *Device) PrintStats() {
minrLog.Infof("GPU #%d (%s) reporting average hash rate %v, %v/%v valid work",
d.index,
d.deviceName,
formatHashrate(averageHashRate),
util.FormatHashRate(averageHashRate),
d.validShares,
d.validShares+d.invalidShares)
}
32 changes: 0 additions & 32 deletions miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,10 @@ import (
"sync/atomic"
"time"

"github.com/decred/gominer/cl"
"github.com/decred/gominer/stratum"
"github.com/decred/gominer/work"
)

func getCLPlatforms() ([]cl.CL_platform_id, error) {
var numPlatforms cl.CL_uint
status := cl.CLGetPlatformIDs(0, nil, &numPlatforms)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetPlatformIDs")
}
platforms := make([]cl.CL_platform_id, numPlatforms)
status = cl.CLGetPlatformIDs(numPlatforms, platforms, nil)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetPlatformIDs")
}
return platforms, nil
}

// getCLDevices returns the list of devices for the given platform.
func getCLDevices(platform cl.CL_platform_id) ([]cl.CL_device_id, error) {
var numDevices cl.CL_uint
status := cl.CLGetDeviceIDs(platform, cl.CL_DEVICE_TYPE_GPU, 0, nil,
&numDevices)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetDeviceIDs")
}
devices := make([]cl.CL_device_id, numDevices)
status = cl.CLGetDeviceIDs(platform, cl.CL_DEVICE_TYPE_ALL, numDevices,
devices, nil)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLGetDeviceIDs")
}
return devices, nil
}

type Miner struct {
devices []*Device
workDone chan []byte
Expand Down
31 changes: 31 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,34 @@ func DiffToTarget(diff float64, powLimit *big.Int) (*big.Int, error) {

return target, nil
}

// RolloverExtraNonce rolls over the extraNonce if it goes over 0x00FFFFFF many
// hashes, since the first byte is reserved for the ID.
func RolloverExtraNonce(v *uint32) {
if *v&0x00FFFFFF == 0x00FFFFFF {
*v = *v & 0xFF000000
} else {
*v++
}
}

// Uint32EndiannessSwap swaps the endianness of a uint32.
func Uint32EndiannessSwap(v uint32) uint32 {
return (v&0x000000FF)<<24 | (v&0x0000FF00)<<8 |
(v&0x00FF0000)>>8 | (v&0xFF000000)>>24
}

// FormatHashRate sets the units properly when displaying a hashrate.
func FormatHashRate(h float64) string {
if h > 1000000000 {
return fmt.Sprintf("%.3fGH/s", h/1000000000)
} else if h > 1000000 {
return fmt.Sprintf("%.0fMH/s", h/1000000)
} else if h > 1000 {
return fmt.Sprintf("%.1fkH/s", h/1000)
} else if h == 0 {
return "0H/s"
}

return fmt.Sprintf("%.1f GH/s", h)
}