ocl.c 18 KB

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