From ec6a548ee3555813d83f86f82bd25694bfd9c303 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Fri, 12 Jun 2015 07:45:23 +0200 Subject: all: Add GPU mining, disabled by default --- eth/backend.go | 12 ------- eth/cpu_mining.go | 54 ++++++++++++++++++++++++++++ eth/gpu_mining.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 eth/cpu_mining.go create mode 100644 eth/gpu_mining.go (limited to 'eth') diff --git a/eth/backend.go b/eth/backend.go index 349dfa613..8862e1670 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -481,18 +481,6 @@ func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) { s.chainManager.ResetWithGenesisBlock(gb) } -func (s *Ethereum) StartMining(threads int) 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 - } - - go s.miner.Start(eb, threads) - return nil -} - func (s *Ethereum) Etherbase() (eb common.Address, err error) { eb = s.etherbase if (eb == common.Address{}) { diff --git a/eth/cpu_mining.go b/eth/cpu_mining.go new file mode 100644 index 000000000..f8795fd0c --- /dev/null +++ b/eth/cpu_mining.go @@ -0,0 +1,54 @@ +// 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 . + +// +build !opencl + +package eth + +import ( + "errors" + "fmt" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" +) + +const disabledInfo = "Set GO_OPENCL and re-build to enable." + +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 + } + + if gpus != "" { + return errors.New("GPU mining disabled. " + disabledInfo) + } + + // CPU mining + go s.miner.Start(eb, threads) + return nil +} + +func GPUBench(gpuid uint64) { + fmt.Println("GPU mining disabled. " + disabledInfo) +} + +func PrintOpenCLDevices() { + fmt.Println("OpenCL disabled. " + disabledInfo) +} diff --git a/eth/gpu_mining.go b/eth/gpu_mining.go new file mode 100644 index 000000000..c351c2bdd --- /dev/null +++ b/eth/gpu_mining.go @@ -0,0 +1,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 . + +// +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() +} -- cgit v1.2.3