gpu_mining.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // +build opencl
  17. package eth
  18. import (
  19. "fmt"
  20. "math/big"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/ethereum/ethash"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/logger"
  28. "github.com/ethereum/go-ethereum/logger/glog"
  29. "github.com/ethereum/go-ethereum/miner"
  30. )
  31. func (s *Ethereum) StartMining(threads int, gpus string) error {
  32. eb, err := s.Etherbase()
  33. if err != nil {
  34. err = fmt.Errorf("Cannot start mining without etherbase address: %v", err)
  35. glog.V(logger.Error).Infoln(err)
  36. return err
  37. }
  38. // GPU mining
  39. if gpus != "" {
  40. var ids []int
  41. for _, s := range strings.Split(gpus, ",") {
  42. i, err := strconv.Atoi(s)
  43. if err != nil {
  44. return fmt.Errorf("Invalid GPU id(s): %v", err)
  45. }
  46. if i < 0 {
  47. return fmt.Errorf("Invalid GPU id: %v", i)
  48. }
  49. ids = append(ids, i)
  50. }
  51. // TODO: re-creating miner is a bit ugly
  52. s.miner = miner.New(s, s.chainConfig, s.EventMux(), ethash.NewCL(ids))
  53. go s.miner.Start(eb, len(ids))
  54. return nil
  55. }
  56. // CPU mining
  57. go s.miner.Start(eb, threads)
  58. return nil
  59. }
  60. func GPUBench(gpuid uint64) {
  61. e := ethash.NewCL([]int{int(gpuid)})
  62. var h common.Hash
  63. bogoHeader := &types.Header{
  64. ParentHash: h,
  65. Number: big.NewInt(int64(42)),
  66. Difficulty: big.NewInt(int64(999999999999999)),
  67. }
  68. bogoBlock := types.NewBlock(bogoHeader, nil, nil, nil)
  69. err := ethash.InitCL(bogoBlock.NumberU64(), e)
  70. if err != nil {
  71. fmt.Println("OpenCL init error: ", err)
  72. return
  73. }
  74. stopChan := make(chan struct{})
  75. reportHashRate := func() {
  76. for {
  77. time.Sleep(3 * time.Second)
  78. fmt.Printf("hashes/s : %v\n", e.GetHashrate())
  79. }
  80. }
  81. fmt.Printf("Starting benchmark (%v seconds)\n", 60)
  82. go reportHashRate()
  83. go e.Search(bogoBlock, stopChan, 0)
  84. time.Sleep(60 * time.Second)
  85. fmt.Println("OK.")
  86. }
  87. func PrintOpenCLDevices() {
  88. ethash.PrintDevices()
  89. }