ocl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. if (strstr(pbuff, "844.4") /* Linux 64 bit ATI 2.6 SDK */ ||
  98. strstr(pbuff, "851.4") /* Windows 64 bit "" */ ||
  99. strstr(pbuff, "831.4") /* Windows & Linux 32 bit "" */ ) {
  100. applog(LOG_WARNING, "AMD OpenCL SDK 2.6 installed giving POOR PERFORMANCE on R5xxx and R6xxx.");
  101. applog(LOG_WARNING, "Downgrade SDK, delete .bin files and restart cgminer to fix this.");
  102. }
  103. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  104. if (status != CL_SUCCESS) {
  105. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  106. return -1;
  107. }
  108. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  109. if (numDevices > most_devices)
  110. most_devices = numDevices;
  111. }
  112. return most_devices;
  113. }
  114. static int advance(char **area, unsigned *remaining, const char *marker)
  115. {
  116. char *find = memmem(*area, *remaining, marker, strlen(marker));
  117. if (!find) {
  118. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  119. return 0;
  120. }
  121. *remaining -= find - *area;
  122. *area = find;
  123. return 1;
  124. }
  125. #define OP3_INST_BFE_UINT 4ULL
  126. #define OP3_INST_BFE_INT 5ULL
  127. #define OP3_INST_BFI_INT 6ULL
  128. #define OP3_INST_BIT_ALIGN_INT 12ULL
  129. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  130. void patch_opcodes(char *w, unsigned remaining)
  131. {
  132. uint64_t *opcode = (uint64_t *)w;
  133. int patched = 0;
  134. int count_bfe_int = 0;
  135. int count_bfe_uint = 0;
  136. int count_byte_align = 0;
  137. while (42) {
  138. int clamp = (*opcode >> (32 + 31)) & 0x1;
  139. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  140. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  141. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  142. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  143. int pred_sel = (*opcode >> 29) & 0x3;
  144. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  145. if (alu_inst == OP3_INST_BFE_INT) {
  146. count_bfe_int++;
  147. } else if (alu_inst == OP3_INST_BFE_UINT) {
  148. count_bfe_uint++;
  149. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  150. count_byte_align++;
  151. // patch this instruction to BFI_INT
  152. *opcode &= 0xfffc1fffffffffffULL;
  153. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  154. patched++;
  155. }
  156. }
  157. if (remaining <= 8)
  158. break;
  159. opcode++;
  160. remaining -= 8;
  161. }
  162. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  163. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  164. count_bfe_int, count_bfe_uint, count_byte_align);
  165. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  166. }
  167. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  168. {
  169. _clState *clState = calloc(1, sizeof(_clState));
  170. bool patchbfi = false, prog_built = false;
  171. cl_platform_id platform = NULL;
  172. char pbuff[256], vbuff[255];
  173. cl_platform_id* platforms;
  174. cl_device_id *devices;
  175. cl_uint numPlatforms;
  176. cl_uint numDevices;
  177. cl_int status;
  178. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  179. if (status != CL_SUCCESS) {
  180. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  181. return NULL;
  182. }
  183. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  184. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  185. if (status != CL_SUCCESS) {
  186. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  187. return NULL;
  188. }
  189. if (opt_platform_id >= (int)numPlatforms) {
  190. applog(LOG_ERR, "Specified platform that does not exist");
  191. return NULL;
  192. }
  193. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  194. if (status != CL_SUCCESS) {
  195. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  196. return NULL;
  197. }
  198. platform = platforms[opt_platform_id];
  199. if (platform == NULL) {
  200. perror("NULL platform found!\n");
  201. return NULL;
  202. }
  203. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  204. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  205. if (status == CL_SUCCESS)
  206. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  207. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  208. if (status == CL_SUCCESS)
  209. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  210. if (strstr(vbuff, "844.4") /* Linux 64 bit ATI 2.6 SDK */ ||
  211. strstr(vbuff, "851.4") /* Windows 64 bit "" */ ||
  212. strstr(vbuff, "831.4") /* Windows & Linux 32 bit "" */ ) {
  213. applog(LOG_WARNING, "AMD OpenCL SDK 2.6 installed giving POOR PERFORMANCE on R5xxx and R6xxx.");
  214. applog(LOG_WARNING, "Downgrade SDK, delete .bin files and restart cgminer to fix this.");
  215. }
  216. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  217. if (status != CL_SUCCESS) {
  218. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  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: Getting Device IDs (list)");
  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: Getting Device Info");
  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: Getting Device Info");
  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: Creating Context. (clCreateContextFromType)");
  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: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  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: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION");
  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: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  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: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  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
  309. clState->work_size = (clState->max_work_size <= 256 ? clState->max_work_size : 256) /
  310. clState->preferred_vwidth;
  311. /* Create binary filename based on parameters passed to opencl
  312. * compiler to ensure we only load a binary that matches what would
  313. * have otherwise created. The filename is:
  314. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  315. */
  316. char binaryfilename[255];
  317. char filename[255];
  318. char numbuf[10];
  319. if (chosen_kernel == KL_NONE) {
  320. /* If no binary is available, and we have a card that suffers with phatk
  321. * on SDK2.6, use the poclbm kernel instead if one has not been
  322. * selected. */
  323. if (strstr(name, "Tahiti") // GCN
  324. || !clState->hasBitAlign // Older Radeon & Nvidia
  325. || strstr(vbuff, "844.4") // Linux 64 bit ATI 2.6 SDK
  326. || strstr(vbuff, "851.4") // Windows 64 bit ""
  327. || strstr(vbuff, "831.4") // Windows & Linux 32 bit ""
  328. ) {
  329. applog(LOG_INFO, "Selecting poclbm kernel");
  330. clState->chosen_kernel = KL_POCLBM;
  331. } else {
  332. applog(LOG_INFO, "Selecting phatk kernel");
  333. clState->chosen_kernel = KL_PHATK;
  334. }
  335. } else
  336. clState->chosen_kernel = chosen_kernel;
  337. switch (clState->chosen_kernel) {
  338. case KL_POCLBM:
  339. strcpy(filename, POCLBM_KERNNAME".cl");
  340. strcpy(binaryfilename, POCLBM_KERNNAME);
  341. break;
  342. case KL_NONE: /* Shouldn't happen */
  343. case KL_PHATK:
  344. strcpy(filename, PHATK_KERNNAME".cl");
  345. strcpy(binaryfilename, PHATK_KERNNAME);
  346. break;
  347. case KL_DIAKGCN:
  348. strcpy(filename, DIAKGCN_KERNNAME".cl");
  349. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  350. break;
  351. case KL_DIABLO:
  352. strcpy(filename, DIABLO_KERNNAME".cl");
  353. strcpy(binaryfilename, DIABLO_KERNNAME);
  354. break;
  355. }
  356. FILE *binaryfile;
  357. size_t *binary_sizes;
  358. char **binaries;
  359. int pl;
  360. char *source = file_contents(filename, &pl);
  361. size_t sourceSize[] = {(size_t)pl};
  362. cl_uint slot, cpnd;
  363. slot = cpnd = 0;
  364. if (!source)
  365. return NULL;
  366. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  367. if (unlikely(!binary_sizes)) {
  368. applog(LOG_ERR, "Unable to calloc binary_sizes");
  369. return NULL;
  370. }
  371. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  372. if (unlikely(!binaries)) {
  373. applog(LOG_ERR, "Unable to calloc binaries");
  374. return NULL;
  375. }
  376. strcat(binaryfilename, name);
  377. if (clState->hasBitAlign)
  378. strcat(binaryfilename, "bitalign");
  379. strcat(binaryfilename, "v");
  380. sprintf(numbuf, "%d", clState->preferred_vwidth);
  381. strcat(binaryfilename, numbuf);
  382. strcat(binaryfilename, "w");
  383. sprintf(numbuf, "%d", (int)clState->work_size);
  384. strcat(binaryfilename, numbuf);
  385. strcat(binaryfilename, "long");
  386. sprintf(numbuf, "%d", (int)sizeof(long));
  387. strcat(binaryfilename, numbuf);
  388. strcat(binaryfilename, ".bin");
  389. binaryfile = fopen(binaryfilename, "rb");
  390. if (!binaryfile) {
  391. applog(LOG_DEBUG, "No binary found, generating from source");
  392. } else {
  393. struct stat binary_stat;
  394. if (unlikely(stat(binaryfilename, &binary_stat))) {
  395. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  396. fclose(binaryfile);
  397. goto build;
  398. }
  399. if (!binary_stat.st_size)
  400. goto build;
  401. binary_sizes[slot] = binary_stat.st_size;
  402. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  403. if (unlikely(!binaries[slot])) {
  404. applog(LOG_ERR, "Unable to calloc binaries");
  405. fclose(binaryfile);
  406. return NULL;
  407. }
  408. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  409. applog(LOG_ERR, "Unable to fread binaries");
  410. fclose(binaryfile);
  411. free(binaries[slot]);
  412. goto build;
  413. }
  414. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  415. if (status != CL_SUCCESS) {
  416. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  417. fclose(binaryfile);
  418. free(binaries[slot]);
  419. goto build;
  420. }
  421. clRetainProgram(clState->program);
  422. if (status != CL_SUCCESS) {
  423. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  424. return NULL;
  425. }
  426. fclose(binaryfile);
  427. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  428. goto built;
  429. }
  430. /////////////////////////////////////////////////////////////////
  431. // Load CL file, build CL program object, create CL kernel object
  432. /////////////////////////////////////////////////////////////////
  433. build:
  434. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  435. if (status != CL_SUCCESS) {
  436. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  437. return NULL;
  438. }
  439. clRetainProgram(clState->program);
  440. if (status != CL_SUCCESS) {
  441. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  442. return NULL;
  443. }
  444. /* create a cl program executable for all the devices specified */
  445. char *CompilerOptions = calloc(1, 256);
  446. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d",
  447. (int)clState->work_size, clState->preferred_vwidth);
  448. applog(LOG_DEBUG, "Setting worksize to %d", clState->work_size);
  449. if (clState->preferred_vwidth > 1)
  450. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  451. if (clState->hasBitAlign) {
  452. strcat(CompilerOptions, " -D BITALIGN");
  453. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  454. if (strstr(name, "Cedar") ||
  455. strstr(name, "Redwood") ||
  456. strstr(name, "Juniper") ||
  457. strstr(name, "Cypress" ) ||
  458. strstr(name, "Hemlock" ) ||
  459. strstr(name, "Caicos" ) ||
  460. strstr(name, "Turks" ) ||
  461. strstr(name, "Barts" ) ||
  462. strstr(name, "Cayman" ) ||
  463. strstr(name, "Antilles" ) ||
  464. strstr(name, "Wrestler" ) ||
  465. strstr(name, "Zacate" ) ||
  466. strstr(name, "WinterPark" ) ||
  467. strstr(name, "BeaverCreek" ))
  468. patchbfi = true;
  469. } else
  470. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  471. if (patchbfi) {
  472. strcat(CompilerOptions, " -D BFI_INT");
  473. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  474. } else
  475. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  476. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  477. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  478. free(CompilerOptions);
  479. if (status != CL_SUCCESS) {
  480. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  481. size_t logSize;
  482. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  483. char *log = malloc(logSize);
  484. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  485. applog(LOG_INFO, "%s", log);
  486. return NULL;
  487. }
  488. prog_built = true;
  489. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  490. if (unlikely(status != CL_SUCCESS)) {
  491. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)");
  492. return NULL;
  493. }
  494. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  495. if (unlikely(status != CL_SUCCESS)) {
  496. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)");
  497. return NULL;
  498. }
  499. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  500. * to iterate over all the binary slots and find where the real program
  501. * is. What the heck is this!? */
  502. for (slot = 0; slot < cpnd; slot++)
  503. if (binary_sizes[slot])
  504. break;
  505. /* copy over all of the generated binaries. */
  506. applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
  507. if (!binary_sizes[slot]) {
  508. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  509. return NULL;
  510. }
  511. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  512. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  513. if (unlikely(status != CL_SUCCESS)) {
  514. applog(LOG_ERR, "Error: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)");
  515. return NULL;
  516. }
  517. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  518. * be hacked in */
  519. if (patchbfi) {
  520. unsigned remaining = binary_sizes[slot];
  521. char *w = binaries[slot];
  522. unsigned int start, length;
  523. /* Find 2nd incidence of .text, and copy the program's
  524. * position and length at a fixed offset from that. Then go
  525. * back and find the 2nd incidence of \x7ELF (rewind by one
  526. * from ELF) and then patch the opcocdes */
  527. if (!advance(&w, &remaining, ".text"))
  528. goto build;
  529. w++; remaining--;
  530. if (!advance(&w, &remaining, ".text")) {
  531. /* 32 bit builds only one ELF */
  532. w--; remaining++;
  533. }
  534. memcpy(&start, w + 285, 4);
  535. memcpy(&length, w + 289, 4);
  536. w = binaries[slot]; remaining = binary_sizes[slot];
  537. if (!advance(&w, &remaining, "ELF"))
  538. goto build;
  539. w++; remaining--;
  540. if (!advance(&w, &remaining, "ELF")) {
  541. /* 32 bit builds only one ELF */
  542. w--; remaining++;
  543. }
  544. w--; remaining++;
  545. w += start; remaining -= start;
  546. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  547. w, remaining);
  548. patch_opcodes(w, length);
  549. status = clReleaseProgram(clState->program);
  550. if (status != CL_SUCCESS) {
  551. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  552. return NULL;
  553. }
  554. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  555. if (status != CL_SUCCESS) {
  556. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  557. return NULL;
  558. }
  559. clRetainProgram(clState->program);
  560. if (status != CL_SUCCESS) {
  561. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  562. return NULL;
  563. }
  564. /* Program needs to be rebuilt */
  565. prog_built = false;
  566. }
  567. free(source);
  568. /* Save the binary to be loaded next time */
  569. binaryfile = fopen(binaryfilename, "wb");
  570. if (!binaryfile) {
  571. /* Not a fatal problem, just means we build it again next time */
  572. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  573. } else {
  574. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  575. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  576. return NULL;
  577. }
  578. fclose(binaryfile);
  579. }
  580. built:
  581. if (binaries[slot])
  582. free(binaries[slot]);
  583. free(binaries);
  584. free(binary_sizes);
  585. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  586. filename, clState->hasBitAlign ? "" : "out", clState->preferred_vwidth, clState->work_size);
  587. if (!prog_built) {
  588. /* create a cl program executable for all the devices specified */
  589. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  590. if (status != CL_SUCCESS) {
  591. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  592. size_t logSize;
  593. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  594. char *log = malloc(logSize);
  595. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  596. applog(LOG_INFO, "%s", log);
  597. return NULL;
  598. }
  599. clRetainProgram(clState->program);
  600. if (status != CL_SUCCESS) {
  601. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  602. return NULL;
  603. }
  604. }
  605. /* get a kernel object handle for a kernel with the given name */
  606. clState->kernel = clCreateKernel(clState->program, "search", &status);
  607. if (status != CL_SUCCESS) {
  608. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  609. return NULL;
  610. }
  611. /////////////////////////////////////////////////////////////////
  612. // Create an OpenCL command queue
  613. /////////////////////////////////////////////////////////////////
  614. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  615. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  616. if (status != CL_SUCCESS) /* Try again without OOE enable */
  617. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  618. if (status != CL_SUCCESS) {
  619. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  620. return NULL;
  621. }
  622. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  623. if (status != CL_SUCCESS) {
  624. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  625. return NULL;
  626. }
  627. return clState;
  628. }
  629. #endif /* HAVE_OPENCL */