ocl.c 25 KB

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