ocl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. enum cl_kernel this_kernel;
  310. char binaryfilename[255];
  311. char filename[255];
  312. char numbuf[10];
  313. if (chosen_kernel == KL_NONE) {
  314. if (strstr(name, "Tahiti"))
  315. this_kernel = KL_DIAKGCN;
  316. else if (!clState->hasBitAlign)
  317. this_kernel = KL_POCLBM;
  318. else
  319. this_kernel = KL_PHATK;
  320. } else
  321. this_kernel = chosen_kernel;
  322. switch (this_kernel) {
  323. case KL_DIAKGCN:
  324. strcpy(filename, DIAKGCN_KERNNAME".cl");
  325. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  326. break;
  327. case KL_POCLBM:
  328. strcpy(filename, POCLBM_KERNNAME".cl");
  329. strcpy(binaryfilename, POCLBM_KERNNAME);
  330. break;
  331. case KL_NONE: /* Shouldn't happen */
  332. case KL_PHATK:
  333. strcpy(filename, PHATK_KERNNAME".cl");
  334. strcpy(binaryfilename, PHATK_KERNNAME);
  335. break;
  336. }
  337. FILE *binaryfile;
  338. size_t *binary_sizes;
  339. char **binaries;
  340. int pl;
  341. char *source = file_contents(filename, &pl);
  342. size_t sourceSize[] = {(size_t)pl};
  343. if (!source)
  344. return NULL;
  345. binary_sizes = (size_t *)malloc(sizeof(size_t)*numDevices);
  346. if (unlikely(!binary_sizes)) {
  347. applog(LOG_ERR, "Unable to malloc binary_sizes");
  348. return NULL;
  349. }
  350. binaries = (char **)malloc(sizeof(char *)*numDevices);
  351. if (unlikely(!binaries)) {
  352. applog(LOG_ERR, "Unable to malloc binaries");
  353. return NULL;
  354. }
  355. strcat(binaryfilename, name);
  356. if (clState->hasBitAlign)
  357. strcat(binaryfilename, "bitalign");
  358. strcat(binaryfilename, "v");
  359. sprintf(numbuf, "%d", clState->preferred_vwidth);
  360. strcat(binaryfilename, numbuf);
  361. strcat(binaryfilename, "w");
  362. sprintf(numbuf, "%d", (int)clState->work_size);
  363. strcat(binaryfilename, numbuf);
  364. strcat(binaryfilename, "long");
  365. sprintf(numbuf, "%d", (int)sizeof(long));
  366. strcat(binaryfilename, numbuf);
  367. strcat(binaryfilename, ".bin");
  368. binaryfile = fopen(binaryfilename, "rb");
  369. if (!binaryfile) {
  370. if (opt_debug)
  371. applog(LOG_DEBUG, "No binary found, generating from source");
  372. } else {
  373. struct stat binary_stat;
  374. if (unlikely(stat(binaryfilename, &binary_stat))) {
  375. if (opt_debug)
  376. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  377. fclose(binaryfile);
  378. goto build;
  379. }
  380. if (!binary_stat.st_size)
  381. goto build;
  382. binary_sizes[gpu] = binary_stat.st_size;
  383. binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
  384. if (unlikely(!binaries[gpu])) {
  385. applog(LOG_ERR, "Unable to malloc binaries");
  386. fclose(binaryfile);
  387. return NULL;
  388. }
  389. if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
  390. applog(LOG_ERR, "Unable to fread binaries[gpu]");
  391. fclose(binaryfile);
  392. free(binaries[gpu]);
  393. goto build;
  394. }
  395. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  396. if (status != CL_SUCCESS) {
  397. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  398. fclose(binaryfile);
  399. free(binaries[gpu]);
  400. goto build;
  401. }
  402. fclose(binaryfile);
  403. if (opt_debug)
  404. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  405. /* We don't need to patch this already loaded image, but need to
  406. * set the flag for status later */
  407. if (clState->hasBitAlign)
  408. patchbfi = true;
  409. free(binaries[gpu]);
  410. goto built;
  411. }
  412. /////////////////////////////////////////////////////////////////
  413. // Load CL file, build CL program object, create CL kernel object
  414. /////////////////////////////////////////////////////////////////
  415. build:
  416. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  417. if (status != CL_SUCCESS) {
  418. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  419. return NULL;
  420. }
  421. clRetainProgram(clState->program);
  422. if (status != CL_SUCCESS) {
  423. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  424. return NULL;
  425. }
  426. /* create a cl program executable for all the devices specified */
  427. char *CompilerOptions = calloc(1, 256);
  428. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d",
  429. (int)clState->work_size, clState->preferred_vwidth);
  430. if (opt_debug)
  431. applog(LOG_DEBUG, "Setting worksize to %d", clState->work_size);
  432. if (clState->preferred_vwidth > 1 && opt_debug)
  433. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  434. if (clState->hasBitAlign) {
  435. strcat(CompilerOptions, " -D BITALIGN");
  436. if (opt_debug)
  437. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  438. if (strstr(name, "Cedar") ||
  439. strstr(name, "Redwood") ||
  440. strstr(name, "Juniper") ||
  441. strstr(name, "Cypress" ) ||
  442. strstr(name, "Hemlock" ) ||
  443. strstr(name, "Caicos" ) ||
  444. strstr(name, "Turks" ) ||
  445. strstr(name, "Barts" ) ||
  446. strstr(name, "Cayman" ) ||
  447. strstr(name, "Antilles" ) ||
  448. strstr(name, "Wrestler" ) ||
  449. strstr(name, "Zacate" ) ||
  450. strstr(name, "WinterPark" ) ||
  451. strstr(name, "BeaverCreek" ))
  452. patchbfi = true;
  453. } else if (opt_debug)
  454. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  455. if (patchbfi) {
  456. strcat(CompilerOptions, " -D BFI_INT");
  457. if (opt_debug)
  458. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  459. } else if (opt_debug)
  460. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  461. if (opt_debug)
  462. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  463. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  464. free(CompilerOptions);
  465. if (status != CL_SUCCESS) {
  466. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  467. size_t logSize;
  468. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  469. char *log = malloc(logSize);
  470. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  471. applog(LOG_INFO, "%s", log);
  472. return NULL;
  473. }
  474. prog_built = true;
  475. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*numDevices, binary_sizes, NULL );
  476. if (unlikely(status != CL_SUCCESS)) {
  477. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetPlatformInfo)");
  478. return NULL;
  479. }
  480. /* copy over all of the generated binaries. */
  481. if (opt_debug)
  482. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  483. if (!binary_sizes[gpu]) {
  484. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, may need to reboot!");
  485. return NULL;
  486. }
  487. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu]);
  488. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*numDevices, binaries, NULL );
  489. if (unlikely(status != CL_SUCCESS)) {
  490. applog(LOG_ERR, "Error: Getting program info. (clGetPlatformInfo)");
  491. return NULL;
  492. }
  493. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  494. * be hacked in */
  495. if (patchbfi) {
  496. unsigned remaining = binary_sizes[gpu];
  497. char *w = binaries[gpu];
  498. unsigned int start, length;
  499. /* Find 2nd incidence of .text, and copy the program's
  500. * position and length at a fixed offset from that. Then go
  501. * back and find the 2nd incidence of \x7ELF (rewind by one
  502. * from ELF) and then patch the opcocdes */
  503. if (!advance(&w, &remaining, ".text"))
  504. {patchbfi = 0; goto build;}
  505. w++; remaining--;
  506. if (!advance(&w, &remaining, ".text")) {
  507. /* 32 bit builds only one ELF */
  508. w--; remaining++;
  509. }
  510. memcpy(&start, w + 285, 4);
  511. memcpy(&length, w + 289, 4);
  512. w = binaries[gpu]; remaining = binary_sizes[gpu];
  513. if (!advance(&w, &remaining, "ELF"))
  514. {patchbfi = 0; goto build;}
  515. w++; remaining--;
  516. if (!advance(&w, &remaining, "ELF")) {
  517. /* 32 bit builds only one ELF */
  518. w--; remaining++;
  519. }
  520. w--; remaining++;
  521. w += start; remaining -= start;
  522. if (opt_debug)
  523. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  524. w, remaining);
  525. patch_opcodes(w, length);
  526. status = clReleaseProgram(clState->program);
  527. if (status != CL_SUCCESS) {
  528. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  529. return NULL;
  530. }
  531. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  532. if (status != CL_SUCCESS) {
  533. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  534. return NULL;
  535. }
  536. clRetainProgram(clState->program);
  537. if (status != CL_SUCCESS) {
  538. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  539. return NULL;
  540. }
  541. /* Program needs to be rebuilt */
  542. prog_built = false;
  543. }
  544. free(source);
  545. /* Save the binary to be loaded next time */
  546. binaryfile = fopen(binaryfilename, "wb");
  547. if (!binaryfile) {
  548. /* Not a fatal problem, just means we build it again next time */
  549. if (opt_debug)
  550. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  551. } else {
  552. if (unlikely(fwrite(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu])) {
  553. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  554. return NULL;
  555. }
  556. fclose(binaryfile);
  557. }
  558. if (binaries[gpu])
  559. free(binaries[gpu]);
  560. built:
  561. free(binaries);
  562. free(binary_sizes);
  563. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT, %d vectors and worksize %d",
  564. filename, patchbfi ? "" : "out", clState->preferred_vwidth, clState->work_size);
  565. if (!prog_built) {
  566. /* create a cl program executable for all the devices specified */
  567. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  568. if (status != CL_SUCCESS) {
  569. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  570. size_t logSize;
  571. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  572. char *log = malloc(logSize);
  573. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  574. applog(LOG_INFO, "%s", log);
  575. return NULL;
  576. }
  577. clRetainProgram(clState->program);
  578. if (status != CL_SUCCESS) {
  579. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  580. return NULL;
  581. }
  582. }
  583. /* get a kernel object handle for a kernel with the given name */
  584. clState->kernel = clCreateKernel(clState->program, "search", &status);
  585. if (status != CL_SUCCESS) {
  586. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  587. return NULL;
  588. }
  589. /////////////////////////////////////////////////////////////////
  590. // Create an OpenCL command queue
  591. /////////////////////////////////////////////////////////////////
  592. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  593. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  594. if (status != CL_SUCCESS) /* Try again without OOE enable */
  595. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  596. if (status != CL_SUCCESS) {
  597. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  598. return NULL;
  599. }
  600. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, BUFFERSIZE, NULL, &status);
  601. if (status != CL_SUCCESS) {
  602. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  603. return NULL;
  604. }
  605. return clState;
  606. }
  607. #endif /* HAVE_OPENCL */