ocl.c 21 KB

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