ocl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. extern int opt_vectors;
  16. extern int opt_worksize;
  17. char *file_contents(const char *filename, int *length)
  18. {
  19. FILE *f = fopen(filename, "r");
  20. void *buffer;
  21. if (!f) {
  22. applog(LOG_ERR, "Unable to open %s for reading", filename);
  23. return NULL;
  24. }
  25. fseek(f, 0, SEEK_END);
  26. *length = ftell(f);
  27. fseek(f, 0, SEEK_SET);
  28. buffer = malloc(*length+1);
  29. *length = fread(buffer, 1, *length, f);
  30. fclose(f);
  31. ((char*)buffer)[*length] = '\0';
  32. return (char*)buffer;
  33. }
  34. int clDevicesNum() {
  35. cl_int status = 0;
  36. cl_uint numPlatforms;
  37. cl_platform_id platform = NULL;
  38. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  39. if(status != CL_SUCCESS)
  40. {
  41. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  42. return -1;
  43. }
  44. if(numPlatforms > 0)
  45. {
  46. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  47. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  48. if(status != CL_SUCCESS)
  49. {
  50. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  51. return -1;
  52. }
  53. unsigned int i;
  54. for(i=0; i < numPlatforms; ++i)
  55. {
  56. char pbuff[100];
  57. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  58. if(status != CL_SUCCESS)
  59. {
  60. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  61. free(platforms);
  62. return -1;
  63. }
  64. platform = platforms[i];
  65. if(!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  66. {
  67. break;
  68. }
  69. }
  70. free(platforms);
  71. }
  72. if(platform == NULL) {
  73. perror("NULL platform found!\n");
  74. return -1;
  75. }
  76. cl_uint numDevices;
  77. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  78. if(status != CL_SUCCESS)
  79. {
  80. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  81. return -1;
  82. }
  83. return numDevices;
  84. }
  85. static int advance(char **area, unsigned *remaining, const char *marker)
  86. {
  87. char *find = memmem(*area, *remaining, marker, strlen(marker));
  88. if (!find) {
  89. if (opt_debug)
  90. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  91. return 0;
  92. }
  93. *remaining -= find - *area;
  94. *area = find;
  95. return 1;
  96. }
  97. #define OP3_INST_BFE_UINT 4ULL
  98. #define OP3_INST_BFE_INT 5ULL
  99. #define OP3_INST_BFI_INT 6ULL
  100. #define OP3_INST_BIT_ALIGN_INT 12ULL
  101. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  102. void patch_opcodes(char *w, unsigned remaining)
  103. {
  104. uint64_t *opcode = (uint64_t *)w;
  105. int patched = 0;
  106. int count_bfe_int = 0;
  107. int count_bfe_uint = 0;
  108. int count_byte_align = 0;
  109. while (42)
  110. {
  111. int clamp = (*opcode >> (32 + 31)) & 0x1;
  112. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  113. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  114. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  115. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  116. int pred_sel = (*opcode >> 29) & 0x3;
  117. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  118. if (alu_inst == OP3_INST_BFE_INT) {
  119. count_bfe_int++;
  120. } else if (alu_inst == OP3_INST_BFE_UINT) {
  121. count_bfe_uint++;
  122. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  123. count_byte_align++;
  124. // patch this instruction to BFI_INT
  125. *opcode &= 0xfffc1fffffffffffUL;
  126. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  127. patched++;
  128. }
  129. }
  130. if (remaining <= 8) {
  131. break;
  132. }
  133. opcode++;
  134. remaining -= 8;
  135. }
  136. if (opt_debug) {
  137. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  138. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  139. count_bfe_int, count_bfe_uint, count_byte_align);
  140. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  141. }
  142. }
  143. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  144. {
  145. int patchbfi = 0;
  146. cl_int status = 0;
  147. unsigned int i;
  148. _clState *clState = calloc(1, sizeof(_clState));
  149. cl_uint numPlatforms;
  150. cl_platform_id platform = NULL;
  151. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  152. if(status != CL_SUCCESS)
  153. {
  154. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  155. return NULL;
  156. }
  157. if(numPlatforms > 0)
  158. {
  159. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  160. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  161. if(status != CL_SUCCESS)
  162. {
  163. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  164. return NULL;
  165. }
  166. for(i = 0; i < numPlatforms; ++i)
  167. {
  168. char pbuff[100];
  169. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  170. if(status != CL_SUCCESS)
  171. {
  172. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  173. free(platforms);
  174. return NULL;
  175. }
  176. platform = platforms[i];
  177. if(!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  178. {
  179. break;
  180. }
  181. }
  182. free(platforms);
  183. }
  184. if(platform == NULL) {
  185. perror("NULL platform found!\n");
  186. return NULL;
  187. }
  188. cl_uint numDevices;
  189. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  190. if(status != CL_SUCCESS)
  191. {
  192. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  193. return NULL;
  194. }
  195. cl_device_id *devices;
  196. if (numDevices > 0 ) {
  197. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  198. /* Now, get the device list data */
  199. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  200. if(status != CL_SUCCESS)
  201. {
  202. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  203. return NULL;
  204. }
  205. applog(LOG_INFO, "List of devices:");
  206. unsigned int i;
  207. for(i=0; i<numDevices; i++) {
  208. char pbuff[100];
  209. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  210. if(status != CL_SUCCESS)
  211. {
  212. applog(LOG_ERR, "Error: Getting Device Info");
  213. return NULL;
  214. }
  215. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  216. }
  217. if (gpu < numDevices) {
  218. char pbuff[100];
  219. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  220. if(status != CL_SUCCESS)
  221. {
  222. applog(LOG_ERR, "Error: Getting Device Info");
  223. return NULL;
  224. }
  225. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  226. strncpy(name, pbuff, nameSize);
  227. } else {
  228. applog(LOG_ERR, "Invalid GPU %i", gpu);
  229. return NULL;
  230. }
  231. } else return NULL;
  232. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  233. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  234. if(status != CL_SUCCESS)
  235. {
  236. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  237. return NULL;
  238. }
  239. /* Check for BFI INT support. Hopefully people don't mix devices with
  240. * and without it! */
  241. char * extensions = malloc(1024);
  242. const char * camo = "cl_amd_media_ops";
  243. char *find;
  244. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  245. if (status != CL_SUCCESS) {
  246. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  247. return NULL;
  248. }
  249. find = strstr(extensions, camo);
  250. if (find)
  251. clState->hasBitAlign = patchbfi = 1;
  252. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  253. if (status != CL_SUCCESS) {
  254. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  255. return NULL;
  256. }
  257. if (opt_debug)
  258. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  259. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  260. if (status != CL_SUCCESS) {
  261. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  262. return NULL;
  263. }
  264. if (opt_debug)
  265. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  266. /////////////////////////////////////////////////////////////////
  267. // Load CL file, build CL program object, create CL kernel object
  268. /////////////////////////////////////////////////////////////////
  269. /* Load a different kernel depending on whether it supports
  270. * cl_amd_media_ops or not */
  271. char filename[10];
  272. if (clState->hasBitAlign)
  273. strcpy(filename, "phatk.cl");
  274. else
  275. strcpy(filename, "poclbm.cl");
  276. int pl;
  277. char *source, *rawsource = file_contents(filename, &pl);
  278. size_t sourceSize[] = {(size_t)pl};
  279. source = malloc(pl);
  280. retry:
  281. if (!source) {
  282. applog(LOG_ERR, "Unable to malloc source");
  283. return NULL;
  284. }
  285. memcpy(source, rawsource, pl);
  286. /* For some reason 2 vectors is still better even if the card says
  287. * otherwise */
  288. if (clState->preferred_vwidth > 1)
  289. clState->preferred_vwidth = 2;
  290. if (opt_vectors)
  291. clState->preferred_vwidth = opt_vectors;
  292. if (opt_worksize && opt_worksize <= clState->max_work_size)
  293. clState->work_size = opt_worksize;
  294. else
  295. clState->work_size = clState->max_work_size / clState->preferred_vwidth;
  296. /* Patch the source file with the preferred_vwidth */
  297. if (clState->preferred_vwidth > 1) {
  298. char *find = strstr(source, "VECTORSX");
  299. if (unlikely(!find)) {
  300. applog(LOG_ERR, "Unable to find VECTORSX in source");
  301. return NULL;
  302. }
  303. find += 7; // "VECTORS"
  304. if (clState->preferred_vwidth == 2)
  305. strncpy(find, "2", 1);
  306. else
  307. strncpy(find, "4", 1);
  308. if (opt_debug)
  309. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  310. }
  311. /* Patch the source file defining BITALIGN */
  312. if (clState->hasBitAlign) {
  313. char *find = strstr(source, "BITALIGNX");
  314. if (unlikely(!find)) {
  315. applog(LOG_ERR, "Unable to find BITALIGNX in source");
  316. return NULL;
  317. }
  318. find += 8; // "BITALIGN"
  319. strncpy(find, " ", 1);
  320. if (opt_debug)
  321. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BITALIGN");
  322. } else if (opt_debug)
  323. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BITALIGN patch");
  324. if (patchbfi) {
  325. char *find = strstr(source, "BFI_INTX");
  326. if (unlikely(!find)) {
  327. applog(LOG_ERR, "Unable to find BFI_INTX in source");
  328. return NULL;
  329. }
  330. find += 7; // "BFI_INT"
  331. strncpy(find, " ", 1);
  332. if (opt_debug)
  333. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BFI_INT");
  334. } else if (opt_debug)
  335. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BFI_INT patch");
  336. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  337. if (status != CL_SUCCESS)
  338. {
  339. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  340. return NULL;
  341. }
  342. /* create a cl program executable for all the devices specified */
  343. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  344. if (status != CL_SUCCESS)
  345. {
  346. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  347. size_t logSize;
  348. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  349. char *log = malloc(logSize);
  350. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  351. applog(LOG_INFO, "%s", log);
  352. return NULL;
  353. }
  354. /* Patch the kernel if the hardware supports BFI_INT */
  355. if (patchbfi) {
  356. size_t nDevices;
  357. size_t * binary_sizes;
  358. char ** binaries;
  359. int err;
  360. /* figure out number of devices and the sizes of the binary for each device. */
  361. err = clGetProgramInfo( clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(nDevices), &nDevices, NULL );
  362. binary_sizes = (size_t *)malloc( sizeof(size_t)*nDevices );
  363. err = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*nDevices, binary_sizes, NULL );
  364. /* copy over all of the generated binaries. */
  365. binaries = (char **)malloc( sizeof(char *)*nDevices );
  366. if (opt_debug)
  367. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  368. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu] );
  369. err = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*nDevices, binaries, NULL );
  370. unsigned remaining = binary_sizes[gpu];
  371. char *w = binaries[gpu];
  372. unsigned int start, length;
  373. /* Find 2nd incidence of .text, and copy the program's
  374. * position and length at a fixed offset from that. Then go
  375. * back and find the 2nd incidence of \x7ELF (rewind by one
  376. * from ELF) and then patch the opcocdes */
  377. if (!advance(&w, &remaining, ".text"))
  378. {patchbfi = 0; goto retry;}
  379. w++; remaining--;
  380. if (!advance(&w, &remaining, ".text")) {
  381. /* 32 bit builds only one ELF */
  382. w--; remaining++;
  383. }
  384. memcpy(&start, w + 285, 4);
  385. memcpy(&length, w + 289, 4);
  386. w = binaries[gpu]; remaining = binary_sizes[gpu];
  387. if (!advance(&w, &remaining, "ELF"))
  388. {patchbfi = 0; goto retry;}
  389. w++; remaining--;
  390. if (!advance(&w, &remaining, "ELF")) {
  391. /* 32 bit builds only one ELF */
  392. w--; remaining++;
  393. }
  394. w--; remaining++;
  395. w += start; remaining -= start;
  396. if (opt_debug)
  397. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  398. w, remaining);
  399. patch_opcodes(w, length);
  400. status = clReleaseProgram(clState->program);
  401. if(status != CL_SUCCESS)
  402. {
  403. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  404. return NULL;
  405. }
  406. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  407. if(status != CL_SUCCESS)
  408. {
  409. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  410. return NULL;
  411. }
  412. }
  413. free(source);
  414. free(rawsource);
  415. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT patching, %d vectors and worksize %d",
  416. filename, patchbfi ? "" : "out", clState->preferred_vwidth, clState->work_size);
  417. /* create a cl program executable for all the devices specified */
  418. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  419. if(status != CL_SUCCESS)
  420. {
  421. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  422. size_t logSize;
  423. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  424. char *log = malloc(logSize);
  425. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  426. applog(LOG_INFO, "%s", log);
  427. return NULL;
  428. }
  429. /* get a kernel object handle for a kernel with the given name */
  430. clState->kernel = clCreateKernel(clState->program, "search", &status);
  431. if(status != CL_SUCCESS)
  432. {
  433. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  434. return NULL;
  435. }
  436. /////////////////////////////////////////////////////////////////
  437. // Create an OpenCL command queue
  438. /////////////////////////////////////////////////////////////////
  439. clState->commandQueue = clCreateCommandQueue( clState->context, devices[gpu], 0, &status);
  440. if(status != CL_SUCCESS)
  441. {
  442. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  443. return NULL;
  444. }
  445. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, sizeof(uint32_t) * 128, NULL, &status);
  446. if(status != CL_SUCCESS) {
  447. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  448. return NULL;
  449. }
  450. return clState;
  451. }