aboutsummaryrefslogtreecommitdiffstats
path: root/eth/gpu_mining.go
blob: c351c2bdd3ffacefbe4bd9f7ea8494f35a46f3bb (plain) (blame)
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
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// +build opencl

package eth

import (
    "fmt"
    "math/big"
    "strconv"
    "strings"
    "time"

    "github.com/ethereum/ethash"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/logger/glog"
    "github.com/ethereum/go-ethereum/miner"
)

func (s *Ethereum) StartMining(threads int, gpus string) error {
    eb, err := s.Etherbase()
    if err != nil {
        err = fmt.Errorf("Cannot start mining without etherbase address: %v", err)
        glog.V(logger.Error).Infoln(err)
        return err
    }

    // GPU mining
    if gpus != "" {
        var ids []int
        for _, s := range strings.Split(gpus, ",") {
            i, err := strconv.Atoi(s)
            if err != nil {
                return fmt.Errorf("Invalid GPU id(s): %v", err)
            }
            if i < 0 {
                return fmt.Errorf("Invalid GPU id: %v", i)
            }
            ids = append(ids, i)
        }

        // TODO: re-creating miner is a bit ugly
        cl := ethash.NewCL(ids)
        s.miner = miner.New(s, s.EventMux(), cl)
        go s.miner.Start(eb, len(ids))
        return nil
    }

    // CPU mining
    go s.miner.Start(eb, threads)
    return nil
}

func GPUBench(gpuid uint64) {
    e := ethash.NewCL([]int{int(gpuid)})

    var h common.Hash
    bogoHeader := &types.Header{
        ParentHash: h,
        Number:     big.NewInt(int64(42)),
        Difficulty: big.NewInt(int64(999999999999999)),
    }
    bogoBlock := types.NewBlock(bogoHeader, nil, nil, nil)

    err := ethash.InitCL(bogoBlock.NumberU64(), e)
    if err != nil {
        fmt.Println("OpenCL init error: ", err)
        return
    }

    stopChan := make(chan struct{})
    reportHashRate := func() {
        for {
            time.Sleep(3 * time.Second)
            fmt.Printf("hashes/s : %v\n", e.GetHashrate())
        }
    }
    fmt.Printf("Starting benchmark (%v seconds)\n", 60)
    go reportHashRate()
    go e.Search(bogoBlock, stopChan, 0)
    time.Sleep(60 * time.Second)
    fmt.Println("OK.")
}

func PrintOpenCLDevices() {
    ethash.PrintDevices()
}