ocl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. */
  4. #include "config.h"
  5. #ifdef HAVE_OPENCL
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #ifdef WIN32
  12. #include <winsock2.h>
  13. #else
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #endif
  18. #include <time.h>
  19. #include <sys/time.h>
  20. #include <pthread.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include "findnonce.h"
  24. #include "ocl.h"
  25. extern int opt_vectors;
  26. extern int opt_worksize;
  27. char *file_contents(const char *filename, int *length)
  28. {
  29. FILE *f = fopen(filename, "rb");
  30. void *buffer;
  31. if (!f) {
  32. applog(LOG_ERR, "Unable to open %s for reading", filename);
  33. return NULL;
  34. }
  35. fseek(f, 0, SEEK_END);
  36. *length = ftell(f);
  37. fseek(f, 0, SEEK_SET);
  38. buffer = malloc(*length+1);
  39. *length = fread(buffer, 1, *length, f);
  40. fclose(f);
  41. ((char*)buffer)[*length] = '\0';
  42. return (char*)buffer;
  43. }
  44. static cl_uint numDevices;
  45. int clDevicesNum() {
  46. cl_int status = 0;
  47. cl_uint numPlatforms;
  48. cl_platform_id platform = NULL;
  49. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  50. /* If this fails, assume no GPUs. */
  51. if (status != CL_SUCCESS)
  52. {
  53. applog(LOG_INFO, "clGetPlatformsIDs failed (no GPU?)");
  54. return 0;
  55. }
  56. if (numPlatforms > 0)
  57. {
  58. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  59. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  60. if (status != CL_SUCCESS)
  61. {
  62. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  63. return -1;
  64. }
  65. unsigned int i;
  66. for(i=0; i < numPlatforms; ++i)
  67. {
  68. char pbuff[100];
  69. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  70. if (status != CL_SUCCESS)
  71. {
  72. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  73. free(platforms);
  74. return -1;
  75. }
  76. platform = platforms[i];
  77. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  78. {
  79. break;
  80. }
  81. }
  82. free(platforms);
  83. }
  84. if (platform == NULL) {
  85. perror("NULL platform found!\n");
  86. return -1;
  87. }
  88. cl_uint numDevices;
  89. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  90. if (status != CL_SUCCESS)
  91. {
  92. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  93. return -1;
  94. }
  95. return numDevices;
  96. }
  97. static cl_platform_id platform = NULL;
  98. static cl_device_id *devices;
  99. int preinit_devices(void)
  100. {
  101. cl_int status;
  102. cl_uint numPlatforms;
  103. int i;
  104. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  105. if (status != CL_SUCCESS)
  106. {
  107. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  108. return -1;
  109. }
  110. if (numPlatforms > 0)
  111. {
  112. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  113. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  114. if (status != CL_SUCCESS)
  115. {
  116. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  117. return -1;
  118. }
  119. for(i = 0; i < numPlatforms; ++i)
  120. {
  121. char pbuff[100];
  122. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  123. if (status != CL_SUCCESS)
  124. {
  125. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  126. free(platforms);
  127. return -1;
  128. }
  129. platform = platforms[i];
  130. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  131. {
  132. break;
  133. }
  134. }
  135. free(platforms);
  136. }
  137. if (platform == NULL) {
  138. perror("NULL platform found!\n");
  139. return -1;
  140. }
  141. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  142. if (status != CL_SUCCESS)
  143. {
  144. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  145. return -1;
  146. }
  147. if (numDevices > 0 ) {
  148. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  149. /* Now, get the device list data */
  150. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  151. if (status != CL_SUCCESS)
  152. {
  153. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  154. return -1;
  155. }
  156. applog(LOG_INFO, "List of devices:");
  157. unsigned int i;
  158. for(i=0; i<numDevices; i++) {
  159. char pbuff[100];
  160. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  161. if (status != CL_SUCCESS)
  162. {
  163. applog(LOG_ERR, "Error: Getting Device Info");
  164. return -1;
  165. }
  166. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  167. }
  168. } else return -1;
  169. return 0;
  170. }
  171. static int advance(char **area, unsigned *remaining, const char *marker)
  172. {
  173. char *find = memmem(*area, *remaining, marker, strlen(marker));
  174. if (!find) {
  175. if (opt_debug)
  176. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  177. return 0;
  178. }
  179. *remaining -= find - *area;
  180. *area = find;
  181. return 1;
  182. }
  183. #define OP3_INST_BFE_UINT 4ULL
  184. #define OP3_INST_BFE_INT 5ULL
  185. #define OP3_INST_BFI_INT 6ULL
  186. #define OP3_INST_BIT_ALIGN_INT 12ULL
  187. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  188. void patch_opcodes(char *w, unsigned remaining)
  189. {
  190. uint64_t *opcode = (uint64_t *)w;
  191. int patched = 0;
  192. int count_bfe_int = 0;
  193. int count_bfe_uint = 0;
  194. int count_byte_align = 0;
  195. while (42)
  196. {
  197. int clamp = (*opcode >> (32 + 31)) & 0x1;
  198. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  199. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  200. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  201. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  202. int pred_sel = (*opcode >> 29) & 0x3;
  203. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  204. if (alu_inst == OP3_INST_BFE_INT) {
  205. count_bfe_int++;
  206. } else if (alu_inst == OP3_INST_BFE_UINT) {
  207. count_bfe_uint++;
  208. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  209. count_byte_align++;
  210. // patch this instruction to BFI_INT
  211. *opcode &= 0xfffc1fffffffffffULL;
  212. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  213. patched++;
  214. }
  215. }
  216. if (remaining <= 8) {
  217. break;
  218. }
  219. opcode++;
  220. remaining -= 8;
  221. }
  222. if (opt_debug) {
  223. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  224. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  225. count_bfe_int, count_bfe_uint, count_byte_align);
  226. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  227. }
  228. }
  229. _clState *initCQ(_clState *clState, unsigned int gpu)
  230. {
  231. cl_int status = 0;
  232. /* create a cl program executable for all the devices specified */
  233. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  234. if (status != CL_SUCCESS)
  235. {
  236. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  237. size_t logSize;
  238. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  239. char *log = malloc(logSize);
  240. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  241. applog(LOG_INFO, "%s", log);
  242. return NULL;
  243. }
  244. /* get a kernel object handle for a kernel with the given name */
  245. clState->kernel = clCreateKernel(clState->program, "search", &status);
  246. if (status != CL_SUCCESS)
  247. {
  248. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  249. return NULL;
  250. }
  251. /////////////////////////////////////////////////////////////////
  252. // Create an OpenCL command queue
  253. /////////////////////////////////////////////////////////////////
  254. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  255. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  256. if (status != CL_SUCCESS) /* Try again without OOE enable */
  257. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  258. if (status != CL_SUCCESS)
  259. {
  260. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  261. return NULL;
  262. }
  263. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, BUFFERSIZE, NULL, &status);
  264. if (status != CL_SUCCESS) {
  265. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  266. return NULL;
  267. }
  268. return clState;
  269. }
  270. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  271. {
  272. int patchbfi = 0;
  273. cl_int status = 0;
  274. size_t nDevices;
  275. _clState *clState = calloc(1, sizeof(_clState));
  276. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  277. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  278. if (status != CL_SUCCESS)
  279. {
  280. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  281. return NULL;
  282. }
  283. if (gpu < numDevices) {
  284. char pbuff[100];
  285. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, &nDevices);
  286. if (status != CL_SUCCESS)
  287. {
  288. applog(LOG_ERR, "Error: Getting Device Info");
  289. return NULL;
  290. }
  291. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  292. strncpy(name, pbuff, nameSize);
  293. } else {
  294. applog(LOG_ERR, "Invalid GPU %i", gpu);
  295. return NULL;
  296. }
  297. /* Check for BFI INT support. Hopefully people don't mix devices with
  298. * and without it! */
  299. char * extensions = malloc(1024);
  300. const char * camo = "cl_amd_media_ops";
  301. char *find;
  302. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  303. if (status != CL_SUCCESS) {
  304. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  305. return NULL;
  306. }
  307. find = strstr(extensions, camo);
  308. if (find)
  309. clState->hasBitAlign = patchbfi = 1;
  310. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  311. if (status != CL_SUCCESS) {
  312. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  313. return NULL;
  314. }
  315. if (opt_debug)
  316. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  317. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  318. if (status != CL_SUCCESS) {
  319. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  320. return NULL;
  321. }
  322. if (opt_debug)
  323. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  324. /* For some reason 2 vectors is still better even if the card says
  325. * otherwise, and many cards lie about their max so use 256 as max
  326. * unless explicitly set on the command line */
  327. if (clState->preferred_vwidth > 1)
  328. clState->preferred_vwidth = 2;
  329. if (opt_vectors)
  330. clState->preferred_vwidth = opt_vectors;
  331. if (opt_worksize && opt_worksize <= clState->max_work_size)
  332. clState->work_size = opt_worksize;
  333. else
  334. clState->work_size = (clState->max_work_size <= 256 ? clState->max_work_size : 256) /
  335. clState->preferred_vwidth;
  336. /* Create binary filename based on parameters passed to opencl
  337. * compiler to ensure we only load a binary that matches what would
  338. * have otherwise created. The filename is:
  339. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  340. */
  341. char binaryfilename[255];
  342. char numbuf[10];
  343. char filename[16];
  344. if (chosen_kernel == KL_NONE) {
  345. if (clState->hasBitAlign)
  346. chosen_kernel = KL_PHATK;
  347. else
  348. chosen_kernel = KL_POCLBM;
  349. }
  350. switch (chosen_kernel) {
  351. case KL_POCLBM:
  352. strcpy(filename, "poclbm110717.cl");
  353. strcpy(binaryfilename, "poclbm110717");
  354. break;
  355. case KL_NONE: /* Shouldn't happen */
  356. case KL_PHATK:
  357. strcpy(filename, "phatk110722.cl");
  358. strcpy(binaryfilename, "phatk110722");
  359. break;
  360. }
  361. FILE *binaryfile;
  362. size_t *binary_sizes;
  363. char **binaries;
  364. int pl;
  365. char *source, *rawsource = file_contents(filename, &pl);
  366. size_t sourceSize[] = {(size_t)pl};
  367. source = malloc(pl);
  368. if (!source) {
  369. applog(LOG_ERR, "Unable to malloc source");
  370. return NULL;
  371. }
  372. binary_sizes = (size_t *)malloc(sizeof(size_t)*nDevices);
  373. if (unlikely(!binary_sizes)) {
  374. applog(LOG_ERR, "Unable to malloc binary_sizes");
  375. return NULL;
  376. }
  377. binaries = (char **)malloc(sizeof(char *)*nDevices);
  378. if (unlikely(!binaries)) {
  379. applog(LOG_ERR, "Unable to malloc binaries");
  380. return NULL;
  381. }
  382. strcat(binaryfilename, name);
  383. if (clState->hasBitAlign)
  384. strcat(binaryfilename, "bitalign");
  385. strcat(binaryfilename, "v");
  386. sprintf(numbuf, "%d", clState->preferred_vwidth);
  387. strcat(binaryfilename, numbuf);
  388. strcat(binaryfilename, "w");
  389. sprintf(numbuf, "%d", (int)clState->work_size);
  390. strcat(binaryfilename, numbuf);
  391. strcat(binaryfilename, "long");
  392. sprintf(numbuf, "%d", (int)sizeof(long));
  393. strcat(binaryfilename, numbuf);
  394. strcat(binaryfilename, ".bin");
  395. binaryfile = fopen(binaryfilename, "rb");
  396. if (!binaryfile) {
  397. if (opt_debug)
  398. applog(LOG_DEBUG, "No binary found, generating from source");
  399. } else {
  400. struct stat binary_stat;
  401. if (unlikely(stat(binaryfilename, &binary_stat))) {
  402. if (opt_debug)
  403. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  404. fclose(binaryfile);
  405. goto build;
  406. }
  407. binary_sizes[gpu] = binary_stat.st_size;
  408. binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
  409. if (unlikely(!binaries[gpu])) {
  410. applog(LOG_ERR, "Unable to malloc binaries");
  411. fclose(binaryfile);
  412. return NULL;
  413. }
  414. if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
  415. applog(LOG_ERR, "Unable to fread binaries[gpu]");
  416. fclose(binaryfile);
  417. goto build;
  418. }
  419. fclose(binaryfile);
  420. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  421. if (status != CL_SUCCESS)
  422. {
  423. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  424. return NULL;
  425. }
  426. if (opt_debug)
  427. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  428. free(binaries[gpu]);
  429. goto built;
  430. }
  431. /////////////////////////////////////////////////////////////////
  432. // Load CL file, build CL program object, create CL kernel object
  433. /////////////////////////////////////////////////////////////////
  434. build:
  435. memcpy(source, rawsource, pl);
  436. /* Patch the source file with the preferred_vwidth */
  437. if (clState->preferred_vwidth > 1) {
  438. char *find = strstr(source, "VECTORSX");
  439. if (unlikely(!find)) {
  440. applog(LOG_ERR, "Unable to find VECTORSX in source");
  441. return NULL;
  442. }
  443. find += 7; // "VECTORS"
  444. if (clState->preferred_vwidth == 2)
  445. strncpy(find, "2", 1);
  446. else
  447. strncpy(find, "4", 1);
  448. if (opt_debug)
  449. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  450. }
  451. /* Patch the source file defining BITALIGN */
  452. if (clState->hasBitAlign) {
  453. char *find = strstr(source, "BITALIGNX");
  454. if (unlikely(!find)) {
  455. applog(LOG_ERR, "Unable to find BITALIGNX in source");
  456. return NULL;
  457. }
  458. find += 8; // "BITALIGN"
  459. strncpy(find, " ", 1);
  460. if (opt_debug)
  461. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BITALIGN");
  462. } else if (opt_debug)
  463. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BITALIGN patch");
  464. if (patchbfi) {
  465. char *find = strstr(source, "BFI_INTX");
  466. if (unlikely(!find)) {
  467. applog(LOG_ERR, "Unable to find BFI_INTX in source");
  468. return NULL;
  469. }
  470. find += 7; // "BFI_INT"
  471. strncpy(find, " ", 1);
  472. if (opt_debug)
  473. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BFI_INT");
  474. } else if (opt_debug)
  475. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BFI_INT patch");
  476. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  477. if (status != CL_SUCCESS)
  478. {
  479. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  480. return NULL;
  481. }
  482. /* create a cl program executable for all the devices specified */
  483. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  484. if (status != CL_SUCCESS)
  485. {
  486. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  487. size_t logSize;
  488. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  489. char *log = malloc(logSize);
  490. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  491. applog(LOG_INFO, "%s", log);
  492. return NULL;
  493. }
  494. /* Create the command queue just so we can flush it to try and avoid
  495. * zero sized binaries */
  496. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0, &status);
  497. if (status != CL_SUCCESS)
  498. {
  499. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  500. return NULL;
  501. }
  502. status = clFinish(clState->commandQueue);
  503. if (status != CL_SUCCESS)
  504. {
  505. applog(LOG_ERR, "Finishing command queue. (clFinish)");
  506. return NULL;
  507. }
  508. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*nDevices, binary_sizes, NULL );
  509. if (unlikely(status != CL_SUCCESS))
  510. {
  511. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetPlatformInfo)");
  512. return NULL;
  513. }
  514. /* copy over all of the generated binaries. */
  515. if (opt_debug)
  516. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  517. if (!binary_sizes[gpu]) {
  518. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, may need to reboot!");
  519. return NULL;
  520. }
  521. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu]);
  522. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*nDevices, binaries, NULL );
  523. if (unlikely(status != CL_SUCCESS))
  524. {
  525. applog(LOG_ERR, "Error: Getting program info. (clGetPlatformInfo)");
  526. return NULL;
  527. }
  528. clReleaseCommandQueue(clState->commandQueue);
  529. /* Patch the kernel if the hardware supports BFI_INT */
  530. if (patchbfi) {
  531. unsigned remaining = binary_sizes[gpu];
  532. char *w = binaries[gpu];
  533. unsigned int start, length;
  534. /* Find 2nd incidence of .text, and copy the program's
  535. * position and length at a fixed offset from that. Then go
  536. * back and find the 2nd incidence of \x7ELF (rewind by one
  537. * from ELF) and then patch the opcocdes */
  538. if (!advance(&w, &remaining, ".text"))
  539. {patchbfi = 0; goto build;}
  540. w++; remaining--;
  541. if (!advance(&w, &remaining, ".text")) {
  542. /* 32 bit builds only one ELF */
  543. w--; remaining++;
  544. }
  545. memcpy(&start, w + 285, 4);
  546. memcpy(&length, w + 289, 4);
  547. w = binaries[gpu]; remaining = binary_sizes[gpu];
  548. if (!advance(&w, &remaining, "ELF"))
  549. {patchbfi = 0; goto build;}
  550. w++; remaining--;
  551. if (!advance(&w, &remaining, "ELF")) {
  552. /* 32 bit builds only one ELF */
  553. w--; remaining++;
  554. }
  555. w--; remaining++;
  556. w += start; remaining -= start;
  557. if (opt_debug)
  558. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  559. w, remaining);
  560. patch_opcodes(w, length);
  561. status = clReleaseProgram(clState->program);
  562. if (status != CL_SUCCESS)
  563. {
  564. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  565. return NULL;
  566. }
  567. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  568. if (status != CL_SUCCESS)
  569. {
  570. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  571. return NULL;
  572. }
  573. }
  574. free(source);
  575. free(rawsource);
  576. /* Save the binary to be loaded next time */
  577. binaryfile = fopen(binaryfilename, "wb");
  578. if (!binaryfile) {
  579. /* Not a fatal problem, just means we build it again next time */
  580. if (opt_debug)
  581. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  582. } else {
  583. if (unlikely(fwrite(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu])) {
  584. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  585. return NULL;
  586. }
  587. fclose(binaryfile);
  588. }
  589. if (binaries[gpu])
  590. free(binaries[gpu]);
  591. built:
  592. free(binaries);
  593. free(binary_sizes);
  594. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT patching, %d vectors and worksize %d",
  595. filename, patchbfi ? "" : "out", clState->preferred_vwidth, clState->work_size);
  596. return initCQ(clState, gpu);
  597. }
  598. #endif /* HAVE_OPENCL */