forked from robvanmieghem/gominer
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
151 lines (131 loc) · 4.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
"time"
"github.com/robvanmieghem/go-opencl/cl"
)
//Version is the released version string of gominer
var Version = "0.5-Dev"
var intensity = 28
var devicesTypesForMining = cl.DeviceTypeGPU
const maxUint = ^uint(0)
const maxInt = int(maxUint >> 1)
func createWork(siad *SiadClient, miningWorkChannel chan *MiningWork, secondsOfWorkPerRequestedHeader int, globalItemSize int) {
for {
timeBeforeGettingWork := time.Now()
target, header, err := siad.GetHeaderForWork()
if err != nil {
log.Println("ERROR fetching work -", err)
time.Sleep(1000 * time.Millisecond)
continue
}
//copy target to header
for i := 0; i < 8; i++ {
header[i+32] = target[7-i]
}
//Fill the workchannel with work for the requested number of secondsOfWorkPerRequestedHeader
// If the GetHeaderForWork call took too long, it might be that no work is generated at all
for i := 0; i*globalItemSize < (maxInt - globalItemSize); i++ {
if time.Since(timeBeforeGettingWork) < time.Second*time.Duration(secondsOfWorkPerRequestedHeader) {
miningWorkChannel <- &MiningWork{header, i * globalItemSize}
} else {
if i == 0 {
log.Println("ERROR: Getting work took longer then", secondsOfWorkPerRequestedHeader, "seconds - No work generated")
}
break
}
}
}
}
func main() {
printVersion := flag.Bool("v", false, "Show version and exit")
useCPU := flag.Bool("cpu", false, "If set, also use the CPU for mining, only GPU's are used by default")
flag.IntVar(&intensity, "I", intensity, "Intensity")
siadHost := flag.String("H", "localhost:9980", "siad host and port")
secondsOfWorkPerRequestedHeader := flag.Int("S", 10, "Time between calls to siad")
excludedGPUs := flag.String("E", "", "Exclude GPU's: comma separated list of devicenumbers")
queryString := flag.String("Q", "", "Query string")
flag.Parse()
if *printVersion {
fmt.Println("gominer version", Version)
os.Exit(0)
}
siad := NewSiadClient(*siadHost, *queryString)
if *useCPU {
devicesTypesForMining = cl.DeviceTypeAll
}
globalItemSize := int(math.Exp2(float64(intensity)))
platforms, err := cl.GetPlatforms()
if err != nil {
log.Panic(err)
}
clDevices := make([]*cl.Device, 0, 4)
for _, platform := range platforms {
log.Println("Platform", platform.Name())
platormDevices, err := cl.GetDevices(platform, devicesTypesForMining)
if err != nil {
log.Println(err)
}
log.Println(len(platormDevices), "device(s) found:")
for i, device := range platormDevices {
log.Println(i, "-", device.Type(), "-", device.Name())
clDevices = append(clDevices, device)
}
}
nrOfMiningDevices := len(clDevices)
if nrOfMiningDevices == 0 {
log.Println("No suitable opencl devices found")
os.Exit(1)
}
//Start fetching work
workChannel := make(chan *MiningWork, nrOfMiningDevices*4)
go createWork(siad, workChannel, *secondsOfWorkPerRequestedHeader, globalItemSize)
//Start mining routines
var hashRateReportsChannel = make(chan *HashRateReport, nrOfMiningDevices*10)
for i, device := range clDevices {
if deviceExcludedForMining(i, *excludedGPUs) {
continue
}
miner := &Miner{
clDevice: device,
minerID: i,
hashRateReports: hashRateReportsChannel,
miningWorkChannel: workChannel,
GlobalItemSize: globalItemSize,
siad: siad,
}
go miner.mine()
}
//Start printing out the hashrates of the different gpu's
hashRateReports := make([]float64, nrOfMiningDevices)
for {
//No need to print at every hashreport, we have time
for i := 0; i < nrOfMiningDevices; i++ {
report := <-hashRateReportsChannel
hashRateReports[report.MinerID] = report.HashRate
}
fmt.Print("\r")
var totalHashRate float64
for minerID, hashrate := range hashRateReports {
fmt.Printf("%d-%.1f ", minerID, hashrate)
totalHashRate += hashrate
}
fmt.Printf("Total: %.1f MH/s ", totalHashRate)
}
}
//deviceExcludedForMining checks if the device is in the exclusion list
func deviceExcludedForMining(deviceID int, excludedGPUs string) bool {
excludedGPUList := strings.Split(excludedGPUs, ",")
for _, excludedGPU := range excludedGPUList {
if strconv.Itoa(deviceID) == excludedGPU {
return true
}
}
return false
}