ocl.c 18 KB

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