ocl.c 21 KB

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