ocl.c 24 KB

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