github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/eth/gpu_mining.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // +build opencl 18 19 package eth 20 21 import ( 22 "fmt" 23 "math/big" 24 "strconv" 25 "strings" 26 "time" 27 28 "github.com/ethereumproject/ethash" 29 "github.com/ethereumproject/go-ethereum/common" 30 "github.com/ethereumproject/go-ethereum/core/types" 31 "github.com/ethereumproject/go-ethereum/logger" 32 "github.com/ethereumproject/go-ethereum/logger/glog" 33 "github.com/ethereumproject/go-ethereum/miner" 34 ) 35 36 func (s *Ethereum) StartMining(threads int, gpus string) error { 37 eb, err := s.Etherbase() 38 if err != nil { 39 err = fmt.Errorf("Cannot start mining without etherbase address: %v", err) 40 glog.V(logger.Error).Infoln(err) 41 return err 42 } 43 44 // GPU mining 45 if gpus != "" { 46 var ids []int 47 for _, s := range strings.Split(gpus, ",") { 48 i, err := strconv.Atoi(s) 49 if err != nil { 50 return fmt.Errorf("Invalid GPU id(s): %v", err) 51 } 52 if i < 0 { 53 return fmt.Errorf("Invalid GPU id: %v", i) 54 } 55 ids = append(ids, i) 56 } 57 58 // TODO: re-creating miner is a bit ugly 59 s.miner = miner.New(s, s.chainConfig, s.EventMux(), ethash.NewCL(ids)) 60 go s.miner.Start(eb, len(ids)) 61 return nil 62 } 63 64 // CPU mining 65 go s.miner.Start(eb, threads) 66 return nil 67 } 68 69 func GPUBench(gpuid uint64) { 70 e := ethash.NewCL([]int{int(gpuid)}) 71 72 var h common.Hash 73 bogoHeader := &types.Header{ 74 ParentHash: h, 75 Number: big.NewInt(int64(42)), 76 Difficulty: big.NewInt(int64(999999999999999)), 77 } 78 bogoBlock := types.NewBlock(bogoHeader, nil, nil, nil) 79 80 err := ethash.InitCL(bogoBlock.NumberU64(), e) 81 if err != nil { 82 fmt.Println("OpenCL init error: ", err) 83 return 84 } 85 86 stopChan := make(chan struct{}) 87 reportHashRate := func() { 88 for { 89 time.Sleep(3 * time.Second) 90 fmt.Printf("hashes/s : %v\n", e.GetHashrate()) 91 } 92 } 93 fmt.Printf("Starting benchmark (%v seconds)\n", 60) 94 go reportHashRate() 95 go e.Search(bogoBlock, stopChan, 0) 96 time.Sleep(60 * time.Second) 97 fmt.Println("OK.") 98 } 99 100 func PrintOpenCLDevices() { 101 ethash.PrintDevices() 102 }