ocl.c 19 KB

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