ocl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #define _GNU_SOURCE
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include <pthread.h>
  13. #include "findnonce.h"
  14. #include "ocl.h"
  15. char *file_contents(const char *filename, int *length)
  16. {
  17. FILE *f = fopen(filename, "r");
  18. void *buffer;
  19. if (!f) {
  20. fprintf(stderr, "Unable to open %s for reading\n", filename);
  21. return NULL;
  22. }
  23. fseek(f, 0, SEEK_END);
  24. *length = ftell(f);
  25. fseek(f, 0, SEEK_SET);
  26. buffer = malloc(*length+1);
  27. *length = fread(buffer, 1, *length, f);
  28. fclose(f);
  29. ((char*)buffer)[*length] = '\0';
  30. return (char*)buffer;
  31. }
  32. int clDevicesNum() {
  33. cl_int status = 0;
  34. cl_uint numPlatforms;
  35. cl_platform_id platform = NULL;
  36. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  37. if(status != CL_SUCCESS)
  38. {
  39. printf("Error: Getting Platforms. (clGetPlatformsIDs)\n");
  40. return -1;
  41. }
  42. if(numPlatforms > 0)
  43. {
  44. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  45. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  46. if(status != CL_SUCCESS)
  47. {
  48. printf("Error: Getting Platform Ids. (clGetPlatformsIDs)\n");
  49. return -1;
  50. }
  51. unsigned int i;
  52. for(i=0; i < numPlatforms; ++i)
  53. {
  54. char pbuff[100];
  55. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  56. if(status != CL_SUCCESS)
  57. {
  58. printf("Error: Getting Platform Info. (clGetPlatformInfo)\n");
  59. free(platforms);
  60. return -1;
  61. }
  62. platform = platforms[i];
  63. if(!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  64. {
  65. break;
  66. }
  67. }
  68. free(platforms);
  69. }
  70. if(platform == NULL) {
  71. perror("NULL platform found!\n");
  72. return -1;
  73. }
  74. cl_uint numDevices;
  75. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  76. if(status != CL_SUCCESS)
  77. {
  78. printf("Error: Getting Device IDs (num)\n");
  79. return -1;
  80. }
  81. return numDevices;
  82. }
  83. void advance(char **area, unsigned *remaining, const char *marker)
  84. {
  85. char *find = memmem(*area, *remaining, marker, strlen(marker));
  86. if (!find)
  87. fprintf(stderr, "Marker \"%s\" not found\n", marker), exit(1);
  88. *remaining -= find - *area;
  89. *area = find;
  90. }
  91. #define OP3_INST_BFE_UINT 4UL
  92. #define OP3_INST_BFE_INT 5UL
  93. #define OP3_INST_BFI_INT 6UL
  94. #define OP3_INST_BIT_ALIGN_INT 12UL
  95. #define OP3_INST_BYTE_ALIGN_INT 13UL
  96. void patch_opcodes(char *w, unsigned remaining)
  97. {
  98. uint64_t *opcode = (uint64_t *)w;
  99. int patched = 0;
  100. int count_bfe_int = 0;
  101. int count_bfe_uint = 0;
  102. int count_byte_align = 0;
  103. while (42)
  104. {
  105. int clamp = (*opcode >> (32 + 31)) & 0x1;
  106. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  107. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  108. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  109. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  110. int pred_sel = (*opcode >> 29) & 0x3;
  111. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  112. if (alu_inst == OP3_INST_BFE_INT) {
  113. count_bfe_int++;
  114. } else if (alu_inst == OP3_INST_BFE_UINT) {
  115. count_bfe_uint++;
  116. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  117. count_byte_align++;
  118. // patch this instruction to BFI_INT
  119. *opcode &= 0xfffc1fffffffffffUL;
  120. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  121. patched++;
  122. }
  123. }
  124. if (remaining <= 8) {
  125. break;
  126. }
  127. opcode++;
  128. remaining -= 8;
  129. }
  130. if (opt_debug) {
  131. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  132. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  133. count_bfe_int, count_bfe_uint, count_byte_align);
  134. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  135. }
  136. }
  137. _clState *initCl(int gpu, char *name, size_t nameSize) {
  138. cl_int status = 0;
  139. _clState *clState = malloc(sizeof(_clState));;
  140. cl_uint numPlatforms;
  141. cl_platform_id platform = NULL;
  142. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  143. if(status != CL_SUCCESS)
  144. {
  145. printf("Error: Getting Platforms. (clGetPlatformsIDs)\n");
  146. return NULL;
  147. }
  148. if(numPlatforms > 0)
  149. {
  150. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  151. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  152. if(status != CL_SUCCESS)
  153. {
  154. printf("Error: Getting Platform Ids. (clGetPlatformsIDs)\n");
  155. return NULL;
  156. }
  157. unsigned int i;
  158. for(i=0; i < numPlatforms; ++i)
  159. {
  160. char pbuff[100];
  161. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  162. if(status != CL_SUCCESS)
  163. {
  164. printf("Error: Getting Platform Info. (clGetPlatformInfo)\n");
  165. free(platforms);
  166. return NULL;
  167. }
  168. platform = platforms[i];
  169. if(!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  170. {
  171. break;
  172. }
  173. }
  174. free(platforms);
  175. }
  176. if(platform == NULL) {
  177. perror("NULL platform found!\n");
  178. return NULL;
  179. }
  180. cl_uint numDevices;
  181. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  182. if(status != CL_SUCCESS)
  183. {
  184. printf("Error: Getting Device IDs (num)\n");
  185. return NULL;
  186. }
  187. cl_device_id *devices;
  188. if(numDevices > 0 ) {
  189. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  190. /* Now, get the device list data */
  191. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  192. if(status != CL_SUCCESS)
  193. {
  194. printf("Error: Getting Device IDs (list)\n");
  195. return NULL;
  196. }
  197. printf("List of devices:\n");
  198. unsigned int i;
  199. for(i=0; i<numDevices; i++) {
  200. char pbuff[100];
  201. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  202. if(status != CL_SUCCESS)
  203. {
  204. printf("Error: Getting Device Info\n");
  205. return NULL;
  206. }
  207. printf("\t%i\t%s\n", i, pbuff);
  208. }
  209. if (gpu >= 0 && gpu < numDevices) {
  210. char pbuff[100];
  211. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  212. if(status != CL_SUCCESS)
  213. {
  214. printf("Error: Getting Device Info\n");
  215. return NULL;
  216. }
  217. printf("Selected %i: %s\n", gpu, pbuff);
  218. strncpy(name, pbuff, nameSize);
  219. } else {
  220. printf("Invalid GPU %i\n", gpu);
  221. return NULL;
  222. }
  223. } else return NULL;
  224. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  225. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  226. if(status != CL_SUCCESS)
  227. {
  228. printf("Error: Creating Context. (clCreateContextFromType)\n");
  229. return NULL;
  230. }
  231. /////////////////////////////////////////////////////////////////
  232. // Load CL file, build CL program object, create CL kernel object
  233. /////////////////////////////////////////////////////////////////
  234. //
  235. const char * filename = "oclminer.cl";
  236. int pl;
  237. char *source = file_contents(filename, &pl);
  238. size_t sourceSize[] = {(size_t)pl};
  239. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  240. if(status != CL_SUCCESS)
  241. {
  242. printf("Error: Loading Binary into cl_program (clCreateProgramWithBinary)\n");
  243. return NULL;
  244. }
  245. /* create a cl program executable for all the devices specified */
  246. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  247. if(status != CL_SUCCESS)
  248. {
  249. printf("Error: Building Program (clBuildProgram)\n");
  250. size_t logSize;
  251. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  252. char *log = malloc(logSize);
  253. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  254. printf("%s\n", log);
  255. return NULL;
  256. }
  257. size_t nDevices;
  258. size_t * binary_sizes;
  259. char ** binaries;
  260. unsigned int i;
  261. int err;
  262. /* figure out number of devices and the sizes of the binary for each device. */
  263. err = clGetProgramInfo( clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(nDevices), &nDevices, NULL );
  264. binary_sizes = (size_t *)malloc( sizeof(size_t)*nDevices );
  265. err = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*nDevices, binary_sizes, NULL );
  266. /* copy over all of the generated binaries. */
  267. binaries = (char **)malloc( sizeof(char *)*nDevices );
  268. for( i = 0; i < nDevices; i++ ) {
  269. if (opt_debug)
  270. applog(LOG_DEBUG, "binary size %d : %d\n", i, binary_sizes[i]);
  271. if( binary_sizes[i] != 0 )
  272. binaries[i] = (char *)malloc( sizeof(char)*binary_sizes[i] );
  273. else
  274. binaries[i] = NULL;
  275. }
  276. err = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*nDevices, binaries, NULL );
  277. for (i = 0; i < nDevices; i++) {
  278. if (!binaries[i])
  279. continue;
  280. unsigned remaining = binary_sizes[i];
  281. char *w = binaries[i];
  282. unsigned int start, length;
  283. /* Find 2nd incidence of .text, and copy the program's
  284. * position and length at a fixed offset from that. Then go
  285. * back and find the 2nd incidence of \x7ELF (rewind by one
  286. * from ELF) and then patch the opcocdes */
  287. advance(&w, &remaining, ".text");
  288. w++; remaining--;
  289. advance(&w, &remaining, ".text");
  290. memcpy(&start, w + 285, 4);
  291. memcpy(&length, w + 289, 4);
  292. w = binaries[i]; remaining = binary_sizes[i];
  293. advance(&w, &remaining, "ELF");
  294. w++; remaining--;
  295. advance(&w, &remaining, "ELF");
  296. w--; remaining++;
  297. w += start; remaining -= start;
  298. if (opt_debug)
  299. printf("At %p (%u rem. bytes), to begin patching\n",
  300. w, remaining);
  301. patch_opcodes(w, length);
  302. }
  303. status = clReleaseProgram(clState->program);
  304. if(status != CL_SUCCESS)
  305. {
  306. printf("Error: Releasing program. (clReleaseProgram)\n");
  307. return NULL;
  308. }
  309. clState->program = clCreateProgramWithBinary(clState->context, numDevices, &devices[gpu], binary_sizes, binaries, &status, NULL);
  310. if(status != CL_SUCCESS)
  311. {
  312. printf("Error: Loading Binary into cl_program (clCreateProgramWithBinary)\n");
  313. return NULL;
  314. }
  315. /* create a cl program executable for all the devices specified */
  316. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  317. if(status != CL_SUCCESS)
  318. {
  319. printf("Error: Building Program (clBuildProgram)\n");
  320. size_t logSize;
  321. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  322. char *log = malloc(logSize);
  323. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  324. printf("%s\n", log);
  325. return NULL;
  326. }
  327. /* get a kernel object handle for a kernel with the given name */
  328. clState->kernel = clCreateKernel(clState->program, "oclminer", &status);
  329. if(status != CL_SUCCESS)
  330. {
  331. printf("Error: Creating Kernel from program. (clCreateKernel)\n");
  332. return NULL;
  333. }
  334. /////////////////////////////////////////////////////////////////
  335. // Create an OpenCL command queue
  336. /////////////////////////////////////////////////////////////////
  337. clState->commandQueue = clCreateCommandQueue( clState->context, devices[gpu], 0, &status);
  338. if(status != CL_SUCCESS)
  339. {
  340. printf("Creating Command Queue. (clCreateCommandQueue)\n");
  341. return NULL;
  342. }
  343. clState->inputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, sizeof(dev_blk_ctx), NULL, &status);
  344. if(status != CL_SUCCESS) {
  345. printf("Error: clCreateBuffer (inputBuffer)\n");
  346. return NULL;
  347. }
  348. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, sizeof(uint32_t) * 128, NULL, &status);
  349. if(status != CL_SUCCESS) {
  350. printf("Error: clCreateBuffer (outputBuffer)\n");
  351. return NULL;
  352. }
  353. return clState;
  354. }