ocl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. 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. /* create a cl program executable for the device specified */
  240. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  241. if (status != CL_SUCCESS)
  242. {
  243. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  244. size_t logSize;
  245. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  246. char *log = malloc(logSize);
  247. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  248. applog(LOG_INFO, "%s", log);
  249. return NULL;
  250. }
  251. /* get a kernel object handle for a kernel with the given name */
  252. clState->kernel = clCreateKernel(clState->program, "search", &status);
  253. if (status != CL_SUCCESS)
  254. {
  255. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  256. return NULL;
  257. }
  258. /////////////////////////////////////////////////////////////////
  259. // Create an OpenCL command queue
  260. /////////////////////////////////////////////////////////////////
  261. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  262. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  263. if (status != CL_SUCCESS) /* Try again without OOE enable */
  264. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  265. if (status != CL_SUCCESS)
  266. {
  267. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  268. return NULL;
  269. }
  270. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, BUFFERSIZE, NULL, &status);
  271. if (status != CL_SUCCESS) {
  272. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  273. return NULL;
  274. }
  275. return clState;
  276. }
  277. _clState *initCl(struct cgpu_info *cgpu, char *name, size_t nameSize)
  278. {
  279. unsigned int gpu = cgpu->cpu_gpu;
  280. int patchbfi = 0;
  281. cl_int status = 0;
  282. size_t nDevices;
  283. _clState *clState = calloc(1, sizeof(_clState));
  284. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  285. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  286. if (status != CL_SUCCESS)
  287. {
  288. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  289. return NULL;
  290. }
  291. if (gpu < numDevices) {
  292. char pbuff[100];
  293. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, &nDevices);
  294. if (status != CL_SUCCESS)
  295. {
  296. applog(LOG_ERR, "Error: Getting Device Info");
  297. return NULL;
  298. }
  299. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  300. strncpy(name, pbuff, nameSize);
  301. } else {
  302. applog(LOG_ERR, "Invalid GPU %i", gpu);
  303. return NULL;
  304. }
  305. /* Check for BFI INT support. Hopefully people don't mix devices with
  306. * and without it! */
  307. char * extensions = malloc(1024);
  308. const char * camo = "cl_amd_media_ops";
  309. char *find;
  310. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  311. if (status != CL_SUCCESS) {
  312. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  313. return NULL;
  314. }
  315. find = strstr(extensions, camo);
  316. if (find)
  317. cgpu->hasBitAlign = patchbfi = 1;
  318. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  319. if (status != CL_SUCCESS) {
  320. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  321. return NULL;
  322. }
  323. if (opt_debug)
  324. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  325. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&cgpu->max_work_size, NULL);
  326. if (status != CL_SUCCESS) {
  327. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  328. return NULL;
  329. }
  330. if (opt_debug)
  331. applog(LOG_DEBUG, "Max work group size reported %d", cgpu->max_work_size);
  332. /* For some reason 2 vectors is still better even if the card says
  333. * otherwise, and many cards lie about their max so use 256 as max
  334. * unless explicitly set on the command line */
  335. cgpu->vwidth = clState->preferred_vwidth;
  336. if (clState->preferred_vwidth > 1)
  337. cgpu->vwidth = 2;
  338. if (opt_vectors)
  339. cgpu->vwidth = opt_vectors;
  340. if (opt_worksize && opt_worksize <= cgpu->max_work_size)
  341. cgpu->work_size = opt_worksize;
  342. else
  343. cgpu->work_size = (cgpu->max_work_size <= 256 ? cgpu->max_work_size : 256) /
  344. cgpu->vwidth;
  345. /* Create binary filename based on parameters passed to opencl
  346. * compiler to ensure we only load a binary that matches what would
  347. * have otherwise created. The filename is:
  348. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  349. */
  350. char binaryfilename[255];
  351. char numbuf[10];
  352. char filename[16];
  353. if (chosen_kernel == KL_NONE) {
  354. if (cgpu->hasBitAlign)
  355. chosen_kernel = KL_PHATK;
  356. else
  357. chosen_kernel = KL_POCLBM;
  358. }
  359. switch (chosen_kernel) {
  360. case KL_POCLBM:
  361. strcpy(filename, "poclbm110717.cl");
  362. strcpy(binaryfilename, "poclbm110717");
  363. break;
  364. case KL_NONE: /* Shouldn't happen */
  365. case KL_PHATK:
  366. strcpy(filename, "phatk110722.cl");
  367. strcpy(binaryfilename, "phatk110722");
  368. break;
  369. }
  370. FILE *binaryfile;
  371. size_t *binary_sizes;
  372. char **binaries;
  373. int pl;
  374. char *source, *rawsource = file_contents(filename, &pl);
  375. size_t sourceSize[] = {(size_t)pl};
  376. source = malloc(pl);
  377. if (!source) {
  378. applog(LOG_ERR, "Unable to malloc source");
  379. return NULL;
  380. }
  381. binary_sizes = (size_t *)malloc(sizeof(size_t)*nDevices);
  382. if (unlikely(!binary_sizes)) {
  383. applog(LOG_ERR, "Unable to malloc binary_sizes");
  384. return NULL;
  385. }
  386. binaries = (char **)malloc(sizeof(char *)*nDevices);
  387. if (unlikely(!binaries)) {
  388. applog(LOG_ERR, "Unable to malloc binaries");
  389. return NULL;
  390. }
  391. strcat(binaryfilename, name);
  392. if (cgpu->hasBitAlign)
  393. strcat(binaryfilename, "bitalign");
  394. strcat(binaryfilename, "v");
  395. sprintf(numbuf, "%d", cgpu->vwidth);
  396. strcat(binaryfilename, numbuf);
  397. strcat(binaryfilename, "w");
  398. sprintf(numbuf, "%d", (int)cgpu->work_size);
  399. strcat(binaryfilename, numbuf);
  400. strcat(binaryfilename, "long");
  401. sprintf(numbuf, "%d", (int)sizeof(long));
  402. strcat(binaryfilename, numbuf);
  403. strcat(binaryfilename, ".bin");
  404. binaryfile = fopen(binaryfilename, "rb");
  405. if (!binaryfile) {
  406. if (opt_debug)
  407. applog(LOG_DEBUG, "No binary found, generating from source");
  408. } else {
  409. struct stat binary_stat;
  410. if (unlikely(stat(binaryfilename, &binary_stat))) {
  411. if (opt_debug)
  412. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  413. fclose(binaryfile);
  414. goto build;
  415. }
  416. binary_sizes[gpu] = binary_stat.st_size;
  417. binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
  418. if (unlikely(!binaries[gpu])) {
  419. applog(LOG_ERR, "Unable to malloc binaries");
  420. fclose(binaryfile);
  421. return NULL;
  422. }
  423. if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
  424. applog(LOG_ERR, "Unable to fread binaries[gpu]");
  425. fclose(binaryfile);
  426. goto build;
  427. }
  428. fclose(binaryfile);
  429. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  430. if (status != CL_SUCCESS)
  431. {
  432. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  433. return NULL;
  434. }
  435. if (opt_debug)
  436. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  437. free(binaries[gpu]);
  438. goto built;
  439. }
  440. /////////////////////////////////////////////////////////////////
  441. // Load CL file, build CL program object, create CL kernel object
  442. /////////////////////////////////////////////////////////////////
  443. build:
  444. memcpy(source, rawsource, pl);
  445. /* Patch the source file with the preferred_vwidth */
  446. if (cgpu->vwidth > 1) {
  447. char *find = strstr(source, "VECTORSX");
  448. if (unlikely(!find)) {
  449. applog(LOG_ERR, "Unable to find VECTORSX in source");
  450. return NULL;
  451. }
  452. find += 7; // "VECTORS"
  453. if (cgpu->vwidth == 2)
  454. strncpy(find, "2", 1);
  455. else
  456. strncpy(find, "4", 1);
  457. if (opt_debug)
  458. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  459. }
  460. /* Patch the source file defining BITALIGN */
  461. if (cgpu->hasBitAlign) {
  462. char *find = strstr(source, "BITALIGNX");
  463. if (unlikely(!find)) {
  464. applog(LOG_ERR, "Unable to find BITALIGNX in source");
  465. return NULL;
  466. }
  467. find += 8; // "BITALIGN"
  468. strncpy(find, " ", 1);
  469. if (opt_debug)
  470. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BITALIGN");
  471. } else if (opt_debug)
  472. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BITALIGN patch");
  473. if (patchbfi) {
  474. char *find = strstr(source, "BFI_INTX");
  475. if (unlikely(!find)) {
  476. applog(LOG_ERR, "Unable to find BFI_INTX in source");
  477. return NULL;
  478. }
  479. find += 7; // "BFI_INT"
  480. strncpy(find, " ", 1);
  481. if (opt_debug)
  482. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BFI_INT");
  483. } else if (opt_debug)
  484. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BFI_INT patch");
  485. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  486. if (status != CL_SUCCESS)
  487. {
  488. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  489. return NULL;
  490. }
  491. /* create a cl program executable for all the devices specified */
  492. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  493. if (status != CL_SUCCESS)
  494. {
  495. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  496. size_t logSize;
  497. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  498. char *log = malloc(logSize);
  499. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  500. applog(LOG_INFO, "%s", log);
  501. return NULL;
  502. }
  503. /* Create the command queue just so we can flush it to try and avoid
  504. * zero sized binaries */
  505. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0, &status);
  506. if (status != CL_SUCCESS)
  507. {
  508. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  509. return NULL;
  510. }
  511. status = clFinish(clState->commandQueue);
  512. if (status != CL_SUCCESS)
  513. {
  514. applog(LOG_ERR, "Finishing command queue. (clFinish)");
  515. return NULL;
  516. }
  517. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*nDevices, binary_sizes, NULL );
  518. if (unlikely(status != CL_SUCCESS))
  519. {
  520. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetPlatformInfo)");
  521. return NULL;
  522. }
  523. /* copy over all of the generated binaries. */
  524. if (opt_debug)
  525. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  526. if (!binary_sizes[gpu]) {
  527. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, may need to reboot!");
  528. return NULL;
  529. }
  530. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu]);
  531. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*nDevices, binaries, NULL );
  532. if (unlikely(status != CL_SUCCESS))
  533. {
  534. applog(LOG_ERR, "Error: Getting program info. (clGetPlatformInfo)");
  535. return NULL;
  536. }
  537. clReleaseCommandQueue(clState->commandQueue);
  538. /* Patch the kernel if the hardware supports BFI_INT */
  539. if (patchbfi) {
  540. unsigned remaining = binary_sizes[gpu];
  541. char *w = binaries[gpu];
  542. unsigned int start, length;
  543. /* Find 2nd incidence of .text, and copy the program's
  544. * position and length at a fixed offset from that. Then go
  545. * back and find the 2nd incidence of \x7ELF (rewind by one
  546. * from ELF) and then patch the opcocdes */
  547. if (!advance(&w, &remaining, ".text"))
  548. {patchbfi = 0; goto build;}
  549. w++; remaining--;
  550. if (!advance(&w, &remaining, ".text")) {
  551. /* 32 bit builds only one ELF */
  552. w--; remaining++;
  553. }
  554. memcpy(&start, w + 285, 4);
  555. memcpy(&length, w + 289, 4);
  556. w = binaries[gpu]; remaining = binary_sizes[gpu];
  557. if (!advance(&w, &remaining, "ELF"))
  558. {patchbfi = 0; goto build;}
  559. w++; remaining--;
  560. if (!advance(&w, &remaining, "ELF")) {
  561. /* 32 bit builds only one ELF */
  562. w--; remaining++;
  563. }
  564. w--; remaining++;
  565. w += start; remaining -= start;
  566. if (opt_debug)
  567. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  568. w, remaining);
  569. patch_opcodes(w, length);
  570. status = clReleaseProgram(clState->program);
  571. if (status != CL_SUCCESS)
  572. {
  573. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  574. return NULL;
  575. }
  576. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  577. if (status != CL_SUCCESS)
  578. {
  579. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  580. return NULL;
  581. }
  582. }
  583. free(source);
  584. free(rawsource);
  585. /* Save the binary to be loaded next time */
  586. binaryfile = fopen(binaryfilename, "wb");
  587. if (!binaryfile) {
  588. /* Not a fatal problem, just means we build it again next time */
  589. if (opt_debug)
  590. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  591. } else {
  592. if (unlikely(fwrite(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu])) {
  593. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  594. return NULL;
  595. }
  596. fclose(binaryfile);
  597. }
  598. if (binaries[gpu])
  599. free(binaries[gpu]);
  600. built:
  601. free(binaries);
  602. free(binary_sizes);
  603. /* We throw everything out now and create the real context we're using in initCQ */
  604. clReleaseContext(clState->context);
  605. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT patching, %d vectors and worksize %d",
  606. filename, patchbfi ? "" : "out", cgpu->vwidth, cgpu->work_size);
  607. return initCQ(clState, gpu);
  608. }
  609. #endif /* HAVE_OPENCL */