ocl.c 18 KB

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