This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathdevice.go
357 lines (299 loc) · 9.01 KB
/
device.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"encoding/binary"
"encoding/hex"
"fmt"
"os"
"unsafe"
"github.com/Dirbaio/gominer/blake256"
"github.com/Dirbaio/gominer/cl"
)
const (
outputBufferSize = cl.CL_size_t(64)
globalWorksize = 65536 * 1024
localWorksize = 64
uint32Size = cl.CL_size_t(unsafe.Sizeof(cl.CL_uint(0)))
nonce0Word = 3
nonce1Word = 4
nonce2Word = 5
)
var zeroSlice = []cl.CL_uint{cl.CL_uint(0)}
func loadProgramSource(filename string) ([][]byte, []cl.CL_size_t, error) {
var program_buffer [1][]byte
var program_size [1]cl.CL_size_t
/* Read each program file and place content into buffer array */
program_handle, err := os.Open(filename)
if err != nil {
return nil, nil, err
}
defer program_handle.Close()
fi, err := program_handle.Stat()
if err != nil {
return nil, nil, err
}
program_size[0] = cl.CL_size_t(fi.Size())
program_buffer[0] = make([]byte, program_size[0])
read_size, err := program_handle.Read(program_buffer[0])
if err != nil || cl.CL_size_t(read_size) != program_size[0] {
return nil, nil, err
}
return program_buffer[:], program_size[:], nil
}
type Work struct {
Data [192]byte
Target [32]byte
}
type Device struct {
index int
platformID cl.CL_platform_id
deviceID cl.CL_device_id
context cl.CL_context
queue cl.CL_command_queue
outputBuffer cl.CL_mem
program cl.CL_program
kernel cl.CL_kernel
midstate [8]uint32
lastBlock [16]uint32
work Work
newWork chan *Work
workDone chan []byte
hasWork bool
workDoneEMA float64
workDoneLast float64
workDoneTotal float64
runningTime float64
quit chan struct{}
}
// Compares a and b as big endian
func hashSmaller(a, b []byte) bool {
for i := len(a) - 1; i >= 0; i-- {
if a[i] < b[i] {
return true
}
if a[i] > b[i] {
return false
}
}
return false
}
func clError(status cl.CL_int, f string) error {
return fmt.Errorf("%s returned error %s (%d)", f, cl.ERROR_CODES_STRINGS[-status], status)
}
func NewDevice(index int, platformID cl.CL_platform_id, deviceID cl.CL_device_id, workDone chan []byte) (*Device, error) {
d := &Device{
index: index,
platformID: platformID,
deviceID: deviceID,
quit: make(chan struct{}),
newWork: make(chan *Work, 5),
workDone: workDone,
}
var status cl.CL_int
// Create the CL context
d.context = cl.CLCreateContext(nil, 1, []cl.CL_device_id{deviceID}, nil, nil, &status)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLCreateContext")
}
// Create the command queue
d.queue = cl.CLCreateCommandQueue(d.context, deviceID, 0, &status)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLCreateCommandQueue")
}
// Create the output buffer
d.outputBuffer = cl.CLCreateBuffer(d.context, cl.CL_MEM_READ_WRITE, uint32Size*outputBufferSize, nil, &status)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLCreateBuffer")
}
// Load kernel source
progSrc, progSize, err := loadProgramSource("blake256.cl")
if err != nil {
return nil, fmt.Errorf("Could not load kernel source: %v", err)
}
// Create the program
d.program = cl.CLCreateProgramWithSource(d.context, 1, progSrc[:], progSize[:], &status)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLCreateProgramWithSource")
}
// Build the program for the device
compilerOptions := ""
compilerOptions += fmt.Sprintf(" -D WORKSIZE=%d", localWorksize)
status = cl.CLBuildProgram(d.program, 1, []cl.CL_device_id{deviceID}, []byte(compilerOptions), nil, nil)
if status != cl.CL_SUCCESS {
err = clError(status, "CLBuildProgram")
// Something went wrong! Print what it is.
var logSize cl.CL_size_t
status = cl.CLGetProgramBuildInfo(d.program, deviceID, cl.CL_PROGRAM_BUILD_LOG, 0, nil, &logSize)
if status != cl.CL_SUCCESS {
minrLog.Errorf("Could not obtain compilation error log: %v", clError(status, "CLGetProgramBuildInfo"))
}
var program_log interface{}
status = cl.CLGetProgramBuildInfo(d.program, deviceID, cl.CL_PROGRAM_BUILD_LOG, logSize, &program_log, nil)
if status != cl.CL_SUCCESS {
minrLog.Errorf("Could not obtain compilation error log: %v", clError(status, "CLGetProgramBuildInfo"))
}
minrLog.Errorf("%s\n", program_log)
return nil, err
}
// Create the kernel
d.kernel = cl.CLCreateKernel(d.program, []byte("search"), &status)
if status != cl.CL_SUCCESS {
return nil, clError(status, "CLCreateKernel")
}
return d, nil
}
func (d *Device) Release() {
cl.CLReleaseKernel(d.kernel)
cl.CLReleaseProgram(d.program)
cl.CLReleaseCommandQueue(d.queue)
cl.CLReleaseMemObject(d.outputBuffer)
cl.CLReleaseContext(d.context)
}
func (d *Device) updateCurrentWork() {
var w *Work
if d.hasWork {
// If we already have work, we just need to check if there's new one
// without blocking if there's not.
select {
case w = <-d.newWork:
default:
return
}
} else {
// If we don't have work, we block until we do. We need to watch for
// quit events too.
select {
case w = <-d.newWork:
case <-d.quit:
return
}
}
d.hasWork = true
d.work = *w
// Set nonce2
binary.BigEndian.PutUint32(d.work.Data[128+4*nonce2Word:], uint32(d.index))
// Reset the hash state
copy(d.midstate[:], blake256.IV256[:])
// Hash the two first blocks
blake256.Block(d.midstate[:], d.work.Data[0:64], 512)
blake256.Block(d.midstate[:], d.work.Data[64:128], 1024)
// Convert the next block to uint32 array.
for i := 0; i < 16; i++ {
d.lastBlock[i] = binary.BigEndian.Uint32(d.work.Data[128+i*4:])
}
}
func (d *Device) Run() {
err := d.runDevice()
if err != nil {
minrLog.Errorf("Error on device: %v", err)
}
}
func (d *Device) runDevice() error {
minrLog.Infof("Started GPU #%d", d.index)
outputData := make([]uint32, outputBufferSize)
var status cl.CL_int
for {
d.updateCurrentWork()
select {
case <-d.quit:
return nil
default:
}
// Increment nonce1
d.lastBlock[nonce1Word]++
// arg 0: pointer to the buffer
obuf := d.outputBuffer
status = cl.CLSetKernelArg(d.kernel, 0, cl.CL_size_t(unsafe.Sizeof(obuf)), unsafe.Pointer(&obuf))
if status != cl.CL_SUCCESS {
return clError(status, "CLSetKernelArg")
}
// args 1..8: midstate
for i := 0; i < 8; i++ {
ms := d.midstate[i]
status = cl.CLSetKernelArg(d.kernel, cl.CL_uint(i+1), uint32Size, unsafe.Pointer(&ms))
if status != cl.CL_SUCCESS {
return clError(status, "CLSetKernelArg")
}
}
// args 9..20: lastBlock except nonce
i2 := 0
for i := 0; i < 12; i++ {
if i2 == nonce0Word {
i2++
}
lb := d.lastBlock[i2]
status = cl.CLSetKernelArg(d.kernel, cl.CL_uint(i+9), uint32Size, unsafe.Pointer(&lb))
if status != cl.CL_SUCCESS {
return clError(status, "CLSetKernelArg")
}
i2++
}
// Clear the found count from the buffer
status = cl.CLEnqueueWriteBuffer(d.queue, d.outputBuffer, cl.CL_FALSE, 0, uint32Size, unsafe.Pointer(&zeroSlice[0]), 0, nil, nil)
if status != cl.CL_SUCCESS {
return clError(status, "CLEnqueueWriteBuffer")
}
// Execute the kernel
var globalWorkSize [1]cl.CL_size_t
globalWorkSize[0] = globalWorksize
var localWorkSize [1]cl.CL_size_t
localWorkSize[0] = localWorksize
status = cl.CLEnqueueNDRangeKernel(d.queue, d.kernel, 1, nil, globalWorkSize[:], localWorkSize[:], 0, nil, nil)
if status != cl.CL_SUCCESS {
return clError(status, "CLEnqueueNDRangeKernel")
}
// Read the output buffer
cl.CLEnqueueReadBuffer(d.queue, d.outputBuffer, cl.CL_TRUE, 0, uint32Size*outputBufferSize, unsafe.Pointer(&outputData[0]), 0, nil, nil)
if status != cl.CL_SUCCESS {
return clError(status, "CLEnqueueReadBuffer")
}
for i := uint32(0); i < outputData[0]; i++ {
minrLog.Debugf("Found candidate: %d", outputData[i+1])
d.foundCandidate(d.lastBlock[nonce1Word], outputData[i+1])
}
d.workDoneLast += globalWorksize
d.workDoneTotal += globalWorksize
}
}
func (d *Device) foundCandidate(nonce1 uint32, nonce0 uint32) {
// Construct the final block header
data := make([]byte, 192)
copy(data, d.work.Data[:])
binary.BigEndian.PutUint32(data[128+4*nonce1Word:], nonce1)
binary.BigEndian.PutUint32(data[128+4*nonce0Word:], nonce0)
// Perform the final hash block to get the hash
var state [8]uint32
copy(state[:], d.midstate[:])
blake256.Block(state[:], data[128:192], 1440)
var hash [32]byte
for i := 0; i < 8; i++ {
binary.BigEndian.PutUint32(hash[i*4:], state[i])
}
if hashSmaller(hash[:], d.work.Target[:]) {
minrLog.Infof("Found hash!! %s", hex.EncodeToString(hash[:]))
d.workDone <- data
}
}
func (d *Device) Stop() {
close(d.quit)
}
func (d *Device) SetWork(w *Work) {
d.newWork <- w
}
func formatHashrate(h float64) string {
if h > 1000000000 {
return fmt.Sprintf("%.3f GH/s", h/1000000000)
} else if h > 1000000 {
return fmt.Sprintf("%.3f MH/s", h/1000000)
} else if h > 1000 {
return fmt.Sprintf("%.3f kH/s", h/1000)
} else {
return fmt.Sprintf("%.3f GH/s", h)
}
}
func (d *Device) PrintStats() {
alpha := 0.95
d.workDoneEMA = d.workDoneEMA*alpha + d.workDoneLast*(1-alpha)
d.workDoneLast = 0
d.runningTime += 1.0
minrLog.Infof("EMA %s, avg %s", formatHashrate(d.workDoneEMA), formatHashrate(d.workDoneTotal/d.runningTime))
}