ocl.c 18 KB

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