ocl.c 21 KB

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