ocl.c 23 KB

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