Fabcoin Core  0.16.2
P2P Digital Currency
eqcuda.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdexcept>
9 #include <vector>
10 
11 #include <cuda.h>
12 #include <cuda_runtime.h>
13 #include <device_launch_parameters.h>
14 
15 #ifdef WIN32
16 #define _SNPRINTF _snprintf
17 #else
18 #define _SNPRINTF snprintf
19 #endif
20 
21 #ifndef nullptr
22 #define nullptr NULL
23 #endif
24 
25 
26 #ifdef WIN32
27 #define rt_error std::runtime_error
28 #else
29 class rt_error : public std::runtime_error
30 {
31 public:
32  explicit rt_error(const std::string& str) : std::runtime_error(str) {}
33 };
34 #endif
35 
36 #define checkCudaErrors(call) \
37 do { \
38  cudaError_t err = call; \
39  if (cudaSuccess != err) { \
40  char errorBuff[512]; \
41  _SNPRINTF(errorBuff, sizeof(errorBuff) - 1, \
42  "CUDA error '%s' in func '%s' line %d", \
43  cudaGetErrorString(err), __FUNCTION__, __LINE__); \
44  throw std::runtime_error(errorBuff); \
45  } \
46 } while (0)
47 
48 #define checkCudaDriverErrors(call) \
49  do { \
50  CUresult err = call; \
51  if (CUDA_SUCCESS != err) { \
52  char errorBuff[512]; \
53  _SNPRINTF(errorBuff, sizeof(errorBuff) - 1, \
54  "CUDA error DRIVER: '%d' in func '%s' line %d", \
55  err, __FUNCTION__, __LINE__); \
56  throw rt_error(errorBuff); \
57  } \
58 } while (0)
59 
60 typedef uint64_t u64;
61 typedef uint32_t u32;
62 typedef uint16_t u16;
63 typedef uint8_t u8;
64 typedef unsigned char uchar;
65 
66 struct packer_default;
67 struct packer_cantor;
68 
69 
70 typedef bool (*fn_cancel)();
71 typedef bool (*fn_validate)(std::vector<unsigned char>, unsigned char *, int);
72 
73 #define PROOFSIZE1 (1<<7)
74 typedef u32 proof[PROOFSIZE1];
75 struct equi1847;
77 {
80  int device_id;
81  int thread_id;
82  equi1847* eq;
83  equi1847* device_eq;
84  uint32_t *heap0, *heap1;
85  void* sol_memory;
87 
90 
91  eq_cuda_context1847(int thrid, int devid, fn_validate validate, fn_cancel cancel);
93 
94  bool solve(unsigned char *pblock, unsigned char *header, unsigned int headerlen);
95 };
96 
97 
98 #define MAXREALSOLS 10
99 struct scontainerreal {
100  u32 sols[MAXREALSOLS][512];
101  u32 nsols;
102 };
103 
104 template <u32 RB, u32 SM> struct equi;
105 template <u32 RB, u32 SM, u32 SSM, u32 THREADS, typename PACKER>
106 class eq_cuda_context
107 {
108  equi<RB, SM>* device_eq;
109  scontainerreal* solutions;
110  CUcontext pctx;
111 
112 public:
113  eq_cuda_context(int thr_id, int dev_id, fn_validate validate, fn_cancel cancel);
114  void freemem();
115  ~eq_cuda_context();
116 
117  bool solve(unsigned char *pblock,
118  unsigned char *header,
119  unsigned int headerlen);
120 
121  fn_validate m_fnValidate;
122  fn_cancel m_fnCancel;
123  int thread_id;
124  int device_id;
125  int throughput;
126  int totalblocks;
127  int threadsperblock;
128  int threadsperblock_digits;
129  size_t equi_mem_sz;
130  u8 *m_buf;
131 };
132 
133 // RB, SM, SSM, TPB, PACKER... but any change only here will fail..
134 #define CONFIG_MODE_1 9, 1248, 12, 640, packer_cantor
rt_error(const std::string &str)
Definition: eqcuda.hpp:32
equi1847 * device_eq
Definition: eqcuda.hpp:83
#define PROOFSIZE1
Definition: eqcuda.hpp:73
#define MAXREALSOLS
Definition: eqcuda.hpp:98
proof * solutions
Definition: eqcuda.hpp:86
unsigned char uchar
Definition: eqcuda.hpp:64
uint32_t u32
Definition: eqcuda.hpp:61
Definition: eqcuda.hpp:104
uint16_t u16
Definition: eqcuda.hpp:62
std::hash for asio::adress
Definition: Common.h:323
equi1847 * eq
Definition: eqcuda.hpp:82
uint64_t u64
Definition: eqcuda.hpp:60
bool(* fn_validate)(std::vector< unsigned char >, unsigned char *, int)
Definition: eqcuda.hpp:71
bool(* fn_cancel)()
Definition: eqcuda.hpp:70
uint8_t u8
Definition: eqcuda.hpp:63
fn_validate m_fnValidate
Definition: eqcuda.hpp:88
u32 proof[PROOFSIZE1]
Definition: eqcuda.hpp:74
uint32_t * heap1
Definition: eqcuda.hpp:84
fn_cancel m_fnCancel
Definition: eqcuda.hpp:89