ocl.c 19 KB

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