Fabcoin Core  0.16.2
P2P Digital Currency
libgpusolver.h
Go to the documentation of this file.
1 /* MIT License
2  *
3  * Copyright (c) 2016 Omar Alvarez <omar.alvarez@udc.es>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #ifndef __GPU_SOLVER_H
25 #define __GPU_SOLVER_H
26 
27 #include <cstdio>
28 #include <csignal>
29 #include <iostream>
30 
31 #include "crypto/equihash.h"
32 #include "libclwrapper.h"
33 #include "uint256.h"
34 
35 #include <cassert>
36 
37 #ifdef __APPLE__
38 #include <OpenCL/opencl.h>
39 #else
40 #include <CL/cl.h>
41 #endif
42 
43 // The maximum size of the .cl file we read in and compile
44 #define MAX_SOURCE_SIZE (0x200000)
45 
46 class GPUSolverCancelledException : public std::exception
47 {
48  virtual const char* what() const throw() {
49  return "GPU Equihash solver was cancelled";
50  }
51 };
52 
54 {
57 };
58 
59 class GPUSolver {
60 
61 public:
62  GPUSolver(unsigned int n, unsigned k);
63  GPUSolver(unsigned platform, unsigned device, unsigned int n, unsigned int k);
64  ~GPUSolver();
65 
66  std::pair<int, int> getparam()
67  {
68  std::pair<int, int> param;
69  if( miner )
70  {
71  param.first = miner->PARAM_N;
72  param.second = miner->PARAM_K;
73  }
74  else
75  {
76  param.first = 0;
77  param.second = 0;
78  }
79  return param;
80  }
81  bool run(unsigned int n, unsigned int k, uint8_t *header, size_t header_len, uint256 nonce,
82  const std::function<bool(std::vector<unsigned char>)> validBlock,
83  const std::function<bool(GPUSolverCancelCheck)> cancelled,
84  crypto_generichash_blake2b_state base_state);
85 
86 private:
87  cl_gpuminer * miner;
88  bool GPU;
89  bool initOK;
90  //TODO 20?
91  sols_t *indices;
92  uint32_t n_sol;
93  //Avg
94  uint32_t counter = 0;
95  float sum = 0.f;
96  float avg = 0.f;
97 
98  bool GPUSolve(unsigned int n, unsigned int k, uint8_t *header, size_t header_len, uint256 &nonce,
99  const std::function<bool(std::vector<unsigned char>)> validBlock,
100  const std::function<bool(GPUSolverCancelCheck)> cancelled,
101  crypto_generichash_blake2b_state base_state);
102 
103 };
104 
105 #endif // __GPU_SOLVER_H
#define function(a, b, c, d, k, s)
std::pair< int, int > getparam()
Definition: libgpusolver.h:66
GPUSolverCancelCheck
Definition: libgpusolver.h:57
virtual const char * what() const
Definition: libgpusolver.h:48
volatile double sum
Definition: Examples.cpp:23
256-bit opaque blob.
Definition: uint256.h:132