ocl.c 19 KB

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