ocl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  113. return 0;
  114. }
  115. *remaining -= find - *area;
  116. *area = find;
  117. return 1;
  118. }
  119. #define OP3_INST_BFE_UINT 4ULL
  120. #define OP3_INST_BFE_INT 5ULL
  121. #define OP3_INST_BFI_INT 6ULL
  122. #define OP3_INST_BIT_ALIGN_INT 12ULL
  123. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  124. void patch_opcodes(char *w, unsigned remaining)
  125. {
  126. uint64_t *opcode = (uint64_t *)w;
  127. int patched = 0;
  128. int count_bfe_int = 0;
  129. int count_bfe_uint = 0;
  130. int count_byte_align = 0;
  131. while (42) {
  132. int clamp = (*opcode >> (32 + 31)) & 0x1;
  133. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  134. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  135. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  136. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  137. int pred_sel = (*opcode >> 29) & 0x3;
  138. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  139. if (alu_inst == OP3_INST_BFE_INT) {
  140. count_bfe_int++;
  141. } else if (alu_inst == OP3_INST_BFE_UINT) {
  142. count_bfe_uint++;
  143. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  144. count_byte_align++;
  145. // patch this instruction to BFI_INT
  146. *opcode &= 0xfffc1fffffffffffULL;
  147. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  148. patched++;
  149. }
  150. }
  151. if (remaining <= 8)
  152. break;
  153. opcode++;
  154. remaining -= 8;
  155. }
  156. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  157. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  158. count_bfe_int, count_bfe_uint, count_byte_align);
  159. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  160. }
  161. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  162. {
  163. _clState *clState = calloc(1, sizeof(_clState));
  164. bool patchbfi = false, prog_built = false;
  165. cl_platform_id platform = NULL;
  166. cl_platform_id* platforms;
  167. cl_device_id *devices;
  168. cl_uint numPlatforms;
  169. cl_uint numDevices;
  170. char pbuff[256];
  171. cl_int status;
  172. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  173. if (status != CL_SUCCESS) {
  174. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  175. return NULL;
  176. }
  177. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  178. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  179. if (status != CL_SUCCESS) {
  180. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  181. return NULL;
  182. }
  183. if (opt_platform_id >= numPlatforms) {
  184. applog(LOG_ERR, "Specified platform that does not exist");
  185. return NULL;
  186. }
  187. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  188. if (status != CL_SUCCESS) {
  189. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  190. return NULL;
  191. }
  192. platform = platforms[opt_platform_id];
  193. if (platform == NULL) {
  194. perror("NULL platform found!\n");
  195. return NULL;
  196. }
  197. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  198. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  199. if (status == CL_SUCCESS)
  200. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  201. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  202. if (status == CL_SUCCESS)
  203. applog(LOG_INFO, "CL Platform version: %s", pbuff);
  204. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  205. if (status != CL_SUCCESS) {
  206. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  207. return NULL;
  208. }
  209. if (numDevices > 0 ) {
  210. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  211. /* Now, get the device list data */
  212. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  213. if (status != CL_SUCCESS) {
  214. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  215. return NULL;
  216. }
  217. applog(LOG_INFO, "List of devices:");
  218. unsigned int i;
  219. for (i = 0; i < numDevices; i++) {
  220. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  221. if (status != CL_SUCCESS) {
  222. applog(LOG_ERR, "Error: Getting Device Info");
  223. return NULL;
  224. }
  225. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  226. }
  227. if (gpu < numDevices) {
  228. status = clGetDeviceInfo(devices[gpu], 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, "Selected %i: %s", gpu, pbuff);
  234. strncpy(name, pbuff, nameSize);
  235. } else {
  236. applog(LOG_ERR, "Invalid GPU %i", gpu);
  237. return NULL;
  238. }
  239. } else return NULL;
  240. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  241. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  242. if (status != CL_SUCCESS) {
  243. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  244. return NULL;
  245. }
  246. /* Check for BFI INT support. Hopefully people don't mix devices with
  247. * and without it! */
  248. char * extensions = malloc(1024);
  249. const char * camo = "cl_amd_media_ops";
  250. char *find;
  251. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  252. if (status != CL_SUCCESS) {
  253. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  254. return NULL;
  255. }
  256. find = strstr(extensions, camo);
  257. if (find)
  258. clState->hasBitAlign = true;
  259. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  260. char * devoclver = malloc(1024);
  261. const char * ocl10 = "OpenCL 1.0";
  262. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  263. if (status != CL_SUCCESS) {
  264. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION");
  265. return NULL;
  266. }
  267. find = strstr(devoclver, ocl10);
  268. if (!find)
  269. clState->hasOpenCL11plus = true;
  270. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  271. if (status != CL_SUCCESS) {
  272. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  273. return NULL;
  274. }
  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. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  282. /* For some reason 2 vectors is still better even if the card says
  283. * otherwise, and many cards lie about their max so use 256 as max
  284. * unless explicitly set on the command line. 79x0 cards perform
  285. * better without vectors */
  286. if (clState->preferred_vwidth > 1) {
  287. if (strstr(name, "Tahiti"))
  288. clState->preferred_vwidth = 1;
  289. else
  290. clState->preferred_vwidth = 2;
  291. }
  292. if (opt_vectors)
  293. clState->preferred_vwidth = opt_vectors;
  294. if (opt_worksize && opt_worksize <= clState->max_work_size)
  295. clState->work_size = opt_worksize;
  296. else
  297. clState->work_size = (clState->max_work_size <= 256 ? clState->max_work_size : 256) /
  298. clState->preferred_vwidth;
  299. /* Create binary filename based on parameters passed to opencl
  300. * compiler to ensure we only load a binary that matches what would
  301. * have otherwise created. The filename is:
  302. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  303. */
  304. char binaryfilename[255];
  305. char filename[255];
  306. char numbuf[10];
  307. if (chosen_kernel == KL_NONE) {
  308. if (strstr(name, "Tahiti") || !clState->hasBitAlign)
  309. clState->chosen_kernel = KL_POCLBM;
  310. else
  311. clState->chosen_kernel = KL_PHATK;
  312. } else
  313. clState->chosen_kernel = chosen_kernel;
  314. switch (clState->chosen_kernel) {
  315. case KL_POCLBM:
  316. strcpy(filename, POCLBM_KERNNAME".cl");
  317. strcpy(binaryfilename, POCLBM_KERNNAME);
  318. break;
  319. case KL_NONE: /* Shouldn't happen */
  320. case KL_PHATK:
  321. strcpy(filename, PHATK_KERNNAME".cl");
  322. strcpy(binaryfilename, PHATK_KERNNAME);
  323. break;
  324. case KL_DIAKGCN:
  325. strcpy(filename, DIAKGCN_KERNNAME".cl");
  326. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  327. break;
  328. case KL_DIABLO:
  329. strcpy(filename, DIABLO_KERNNAME".cl");
  330. strcpy(binaryfilename, DIABLO_KERNNAME);
  331. break;
  332. }
  333. FILE *binaryfile;
  334. size_t *binary_sizes;
  335. char **binaries;
  336. int pl;
  337. char *source = file_contents(filename, &pl);
  338. size_t sourceSize[] = {(size_t)pl};
  339. if (!source)
  340. return NULL;
  341. binary_sizes = (size_t *)malloc(sizeof(size_t)*numDevices);
  342. if (unlikely(!binary_sizes)) {
  343. applog(LOG_ERR, "Unable to malloc binary_sizes");
  344. return NULL;
  345. }
  346. binaries = (char **)malloc(sizeof(char *)*numDevices);
  347. if (unlikely(!binaries)) {
  348. applog(LOG_ERR, "Unable to malloc binaries");
  349. return NULL;
  350. }
  351. strcat(binaryfilename, name);
  352. if (clState->hasBitAlign)
  353. strcat(binaryfilename, "bitalign");
  354. strcat(binaryfilename, "v");
  355. sprintf(numbuf, "%d", clState->preferred_vwidth);
  356. strcat(binaryfilename, numbuf);
  357. strcat(binaryfilename, "w");
  358. sprintf(numbuf, "%d", (int)clState->work_size);
  359. strcat(binaryfilename, numbuf);
  360. strcat(binaryfilename, "long");
  361. sprintf(numbuf, "%d", (int)sizeof(long));
  362. strcat(binaryfilename, numbuf);
  363. strcat(binaryfilename, ".bin");
  364. binaryfile = fopen(binaryfilename, "rb");
  365. if (!binaryfile) {
  366. applog(LOG_DEBUG, "No binary found, generating from source");
  367. } else {
  368. struct stat binary_stat;
  369. if (unlikely(stat(binaryfilename, &binary_stat))) {
  370. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  371. fclose(binaryfile);
  372. goto build;
  373. }
  374. if (!binary_stat.st_size)
  375. goto build;
  376. binary_sizes[gpu] = binary_stat.st_size;
  377. binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
  378. if (unlikely(!binaries[gpu])) {
  379. applog(LOG_ERR, "Unable to malloc binaries");
  380. fclose(binaryfile);
  381. return NULL;
  382. }
  383. if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
  384. applog(LOG_ERR, "Unable to fread binaries[gpu]");
  385. fclose(binaryfile);
  386. free(binaries[gpu]);
  387. goto build;
  388. }
  389. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  390. if (status != CL_SUCCESS) {
  391. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  392. fclose(binaryfile);
  393. free(binaries[gpu]);
  394. goto build;
  395. }
  396. clRetainProgram(clState->program);
  397. if (status != CL_SUCCESS) {
  398. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  399. return NULL;
  400. }
  401. fclose(binaryfile);
  402. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  403. goto built;
  404. }
  405. /////////////////////////////////////////////////////////////////
  406. // Load CL file, build CL program object, create CL kernel object
  407. /////////////////////////////////////////////////////////////////
  408. build:
  409. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  410. if (status != CL_SUCCESS) {
  411. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  412. return NULL;
  413. }
  414. clRetainProgram(clState->program);
  415. if (status != CL_SUCCESS) {
  416. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  417. return NULL;
  418. }
  419. /* create a cl program executable for all the devices specified */
  420. char *CompilerOptions = calloc(1, 256);
  421. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d",
  422. (int)clState->work_size, clState->preferred_vwidth);
  423. applog(LOG_DEBUG, "Setting worksize to %d", clState->work_size);
  424. if (clState->preferred_vwidth > 1)
  425. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  426. if (clState->hasBitAlign) {
  427. strcat(CompilerOptions, " -D BITALIGN");
  428. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  429. if (strstr(name, "Cedar") ||
  430. strstr(name, "Redwood") ||
  431. strstr(name, "Juniper") ||
  432. strstr(name, "Cypress" ) ||
  433. strstr(name, "Hemlock" ) ||
  434. strstr(name, "Caicos" ) ||
  435. strstr(name, "Turks" ) ||
  436. strstr(name, "Barts" ) ||
  437. strstr(name, "Cayman" ) ||
  438. strstr(name, "Antilles" ) ||
  439. strstr(name, "Wrestler" ) ||
  440. strstr(name, "Zacate" ) ||
  441. strstr(name, "WinterPark" ) ||
  442. strstr(name, "BeaverCreek" ))
  443. patchbfi = true;
  444. } else
  445. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  446. if (patchbfi) {
  447. strcat(CompilerOptions, " -D BFI_INT");
  448. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  449. } else
  450. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  451. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  452. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  453. free(CompilerOptions);
  454. if (status != CL_SUCCESS) {
  455. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  456. size_t logSize;
  457. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  458. char *log = malloc(logSize);
  459. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  460. applog(LOG_INFO, "%s", log);
  461. return NULL;
  462. }
  463. prog_built = true;
  464. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*numDevices, binary_sizes, NULL );
  465. if (unlikely(status != CL_SUCCESS)) {
  466. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetPlatformInfo)");
  467. return NULL;
  468. }
  469. /* copy over all of the generated binaries. */
  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. 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. 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. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  511. w, remaining);
  512. patch_opcodes(w, length);
  513. status = clReleaseProgram(clState->program);
  514. if (status != CL_SUCCESS) {
  515. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  516. return NULL;
  517. }
  518. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  519. if (status != CL_SUCCESS) {
  520. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  521. return NULL;
  522. }
  523. clRetainProgram(clState->program);
  524. if (status != CL_SUCCESS) {
  525. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  526. return NULL;
  527. }
  528. /* Program needs to be rebuilt */
  529. prog_built = false;
  530. }
  531. free(source);
  532. /* Save the binary to be loaded next time */
  533. binaryfile = fopen(binaryfilename, "wb");
  534. if (!binaryfile) {
  535. /* Not a fatal problem, just means we build it again next time */
  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. built:
  545. if (binaries[gpu])
  546. free(binaries[gpu]);
  547. free(binaries);
  548. free(binary_sizes);
  549. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  550. filename, clState->hasBitAlign ? "" : "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 */