ocl.c 23 KB

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