ocl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 = 0;
  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. 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_GPU, 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. struct cgpu_info *cgpu = &gpus[gpu];
  186. cl_platform_id platform = NULL;
  187. char pbuff[256], vbuff[255];
  188. cl_platform_id* platforms;
  189. cl_uint preferred_vwidth;
  190. cl_device_id *devices;
  191. cl_uint numPlatforms;
  192. cl_uint numDevices;
  193. cl_int status;
  194. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  195. if (status != CL_SUCCESS) {
  196. applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
  197. return NULL;
  198. }
  199. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  200. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  201. if (status != CL_SUCCESS) {
  202. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  203. return NULL;
  204. }
  205. if (opt_platform_id >= (int)numPlatforms) {
  206. applog(LOG_ERR, "Specified platform that does not exist");
  207. return NULL;
  208. }
  209. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  210. if (status != CL_SUCCESS) {
  211. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  212. return NULL;
  213. }
  214. platform = platforms[opt_platform_id];
  215. if (platform == NULL) {
  216. perror("NULL platform found!\n");
  217. return NULL;
  218. }
  219. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  220. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  221. if (status == CL_SUCCESS)
  222. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  223. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  224. if (status == CL_SUCCESS)
  225. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  226. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  227. if (status != CL_SUCCESS) {
  228. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  229. return NULL;
  230. }
  231. if (numDevices > 0 ) {
  232. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  233. /* Now, get the device list data */
  234. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  235. if (status != CL_SUCCESS) {
  236. applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
  237. return NULL;
  238. }
  239. applog(LOG_INFO, "List of devices:");
  240. unsigned int i;
  241. for (i = 0; i < numDevices; i++) {
  242. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  243. if (status != CL_SUCCESS) {
  244. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  245. return NULL;
  246. }
  247. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  248. }
  249. if (gpu < numDevices) {
  250. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  251. if (status != CL_SUCCESS) {
  252. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  253. return NULL;
  254. }
  255. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  256. strncpy(name, pbuff, nameSize);
  257. } else {
  258. applog(LOG_ERR, "Invalid GPU %i", gpu);
  259. return NULL;
  260. }
  261. } else return NULL;
  262. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  263. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  264. if (status != CL_SUCCESS) {
  265. applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
  266. return NULL;
  267. }
  268. /////////////////////////////////////////////////////////////////
  269. // Create an OpenCL command queue
  270. /////////////////////////////////////////////////////////////////
  271. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  272. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  273. if (status != CL_SUCCESS) /* Try again without OOE enable */
  274. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  275. if (status != CL_SUCCESS) {
  276. applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
  277. return NULL;
  278. }
  279. /* Check for BFI INT support. Hopefully people don't mix devices with
  280. * and without it! */
  281. char * extensions = malloc(1024);
  282. const char * camo = "cl_amd_media_ops";
  283. char *find;
  284. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  285. if (status != CL_SUCCESS) {
  286. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
  287. return NULL;
  288. }
  289. find = strstr(extensions, camo);
  290. if (find)
  291. clState->hasBitAlign = true;
  292. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  293. char * devoclver = malloc(1024);
  294. const char * ocl10 = "OpenCL 1.0";
  295. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  296. if (status != CL_SUCCESS) {
  297. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  298. return NULL;
  299. }
  300. find = strstr(devoclver, ocl10);
  301. if (!find)
  302. clState->hasOpenCL11plus = true;
  303. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  304. if (status != CL_SUCCESS) {
  305. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  306. return NULL;
  307. }
  308. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  309. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  310. if (status != CL_SUCCESS) {
  311. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  312. return NULL;
  313. }
  314. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  315. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&cgpu->max_alloc, NULL);
  316. if (status != CL_SUCCESS) {
  317. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  318. return NULL;
  319. }
  320. applog(LOG_DEBUG, "Max mem alloc size is %u", cgpu->max_alloc);
  321. /* Create binary filename based on parameters passed to opencl
  322. * compiler to ensure we only load a binary that matches what would
  323. * have otherwise created. The filename is:
  324. * name + kernelname +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + .bin
  325. * For scrypt the filename is:
  326. * name + kernelname + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + .bin
  327. */
  328. char binaryfilename[255];
  329. char filename[255];
  330. char numbuf[10];
  331. if (cgpu->kernel == KL_NONE) {
  332. if (opt_scrypt) {
  333. applog(LOG_INFO, "Selecting scrypt kernel");
  334. clState->chosen_kernel = KL_SCRYPT;
  335. } else if (!strstr(name, "Tahiti") &&
  336. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  337. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  338. strstr(vbuff, "851.4") || // Windows 64 bit ""
  339. strstr(vbuff, "831.4") ||
  340. strstr(vbuff, "898.1") || // 12.2 driver SDK
  341. strstr(vbuff, "923.1"))) { // 12.4 driver SDK
  342. applog(LOG_INFO, "Selecting diablo kernel");
  343. clState->chosen_kernel = KL_DIABLO;
  344. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  345. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  346. applog(LOG_INFO, "Selecting poclbm kernel");
  347. clState->chosen_kernel = KL_POCLBM;
  348. /* Use phatk for the rest R5xxx R6xxx */
  349. } else {
  350. applog(LOG_INFO, "Selecting phatk kernel");
  351. clState->chosen_kernel = KL_PHATK;
  352. }
  353. cgpu->kernel = clState->chosen_kernel;
  354. } else {
  355. clState->chosen_kernel = cgpu->kernel;
  356. if (clState->chosen_kernel == KL_PHATK &&
  357. (strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  358. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  359. strstr(vbuff, "923.1"))) {
  360. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  361. applog(LOG_WARNING, "You are running SDK 2.6 which performs poorly with this kernel.");
  362. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  363. applog(LOG_WARNING, "Or allow cgminer to automatically choose a more suitable kernel.");
  364. }
  365. }
  366. /* For some reason 2 vectors is still better even if the card says
  367. * otherwise, and many cards lie about their max so use 256 as max
  368. * unless explicitly set on the command line. Tahiti prefers 1 */
  369. if (strstr(name, "Tahiti"))
  370. preferred_vwidth = 1;
  371. else if (preferred_vwidth > 2)
  372. preferred_vwidth = 2;
  373. switch (clState->chosen_kernel) {
  374. case KL_POCLBM:
  375. strcpy(filename, POCLBM_KERNNAME".cl");
  376. strcpy(binaryfilename, POCLBM_KERNNAME);
  377. break;
  378. case KL_PHATK:
  379. strcpy(filename, PHATK_KERNNAME".cl");
  380. strcpy(binaryfilename, PHATK_KERNNAME);
  381. break;
  382. case KL_DIAKGCN:
  383. strcpy(filename, DIAKGCN_KERNNAME".cl");
  384. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  385. break;
  386. case KL_SCRYPT:
  387. strcpy(filename, SCRYPT_KERNNAME".cl");
  388. strcpy(binaryfilename, SCRYPT_KERNNAME);
  389. /* Scrypt only supports vector 1 */
  390. cgpu->vwidth = 1;
  391. break;
  392. case KL_NONE: /* Shouldn't happen */
  393. case KL_DIABLO:
  394. strcpy(filename, DIABLO_KERNNAME".cl");
  395. strcpy(binaryfilename, DIABLO_KERNNAME);
  396. break;
  397. }
  398. if (cgpu->vwidth)
  399. clState->vwidth = cgpu->vwidth;
  400. else {
  401. clState->vwidth = preferred_vwidth;
  402. cgpu->vwidth = preferred_vwidth;
  403. }
  404. if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  405. clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
  406. clState->goffset = true;
  407. if (cgpu->work_size && cgpu->work_size <= clState->max_work_size)
  408. clState->wsize = cgpu->work_size;
  409. else if (strstr(name, "Tahiti"))
  410. clState->wsize = 64;
  411. else
  412. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  413. cgpu->work_size = clState->wsize;
  414. #ifdef USE_SCRYPT
  415. if (opt_scrypt) {
  416. cl_ulong ma = cgpu->max_alloc, mt;
  417. int pow2 = 0;
  418. if (!cgpu->opt_lg) {
  419. applog(LOG_DEBUG, "GPU %d: selecting lookup gap of 2", gpu);
  420. cgpu->lookup_gap = 2;
  421. } else
  422. cgpu->lookup_gap = cgpu->opt_lg;
  423. if (!cgpu->opt_tc) {
  424. cgpu->thread_concurrency = ma / 32768 / cgpu->lookup_gap;
  425. if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) {
  426. cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders;
  427. if (cgpu->thread_concurrency > cgpu->shaders * 5)
  428. cgpu->thread_concurrency = cgpu->shaders * 5;
  429. }
  430. applog(LOG_DEBUG, "GPU %d: selecting thread concurrency of %u",gpu, cgpu->thread_concurrency);
  431. } else
  432. cgpu->thread_concurrency = cgpu->opt_tc;
  433. /* If we have memory to spare, try to find a power of 2 value
  434. * >= required amount to map nicely to an intensity */
  435. mt = cgpu->thread_concurrency * 32768 * cgpu->lookup_gap;
  436. if (ma > mt) {
  437. while (ma >>= 1)
  438. pow2++;
  439. ma = 1;
  440. while (--pow2 && ma < mt)
  441. ma <<= 1;
  442. if (ma >= mt) {
  443. cgpu->max_alloc = ma;
  444. applog(LOG_DEBUG, "Max alloc decreased to %lu", cgpu->max_alloc);
  445. }
  446. }
  447. }
  448. #endif
  449. FILE *binaryfile;
  450. size_t *binary_sizes;
  451. char **binaries;
  452. int pl;
  453. char *source = file_contents(filename, &pl);
  454. size_t sourceSize[] = {(size_t)pl};
  455. cl_uint slot, cpnd;
  456. slot = cpnd = 0;
  457. if (!source)
  458. return NULL;
  459. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  460. if (unlikely(!binary_sizes)) {
  461. applog(LOG_ERR, "Unable to calloc binary_sizes");
  462. return NULL;
  463. }
  464. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  465. if (unlikely(!binaries)) {
  466. applog(LOG_ERR, "Unable to calloc binaries");
  467. return NULL;
  468. }
  469. strcat(binaryfilename, name);
  470. if (clState->goffset)
  471. strcat(binaryfilename, "g");
  472. if (opt_scrypt) {
  473. #ifdef USE_SCRYPT
  474. sprintf(numbuf, "lg%dtc%d", cgpu->lookup_gap, cgpu->thread_concurrency);
  475. strcat(binaryfilename, numbuf);
  476. #endif
  477. } else {
  478. sprintf(numbuf, "v%d", clState->vwidth);
  479. strcat(binaryfilename, numbuf);
  480. }
  481. sprintf(numbuf, "w%d", (int)clState->wsize);
  482. strcat(binaryfilename, numbuf);
  483. sprintf(numbuf, "l%d", (int)sizeof(long));
  484. strcat(binaryfilename, numbuf);
  485. strcat(binaryfilename, ".bin");
  486. binaryfile = fopen(binaryfilename, "rb");
  487. if (!binaryfile) {
  488. applog(LOG_DEBUG, "No binary found, generating from source");
  489. } else {
  490. struct stat binary_stat;
  491. if (unlikely(stat(binaryfilename, &binary_stat))) {
  492. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  493. fclose(binaryfile);
  494. goto build;
  495. }
  496. if (!binary_stat.st_size)
  497. goto build;
  498. binary_sizes[slot] = binary_stat.st_size;
  499. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  500. if (unlikely(!binaries[slot])) {
  501. applog(LOG_ERR, "Unable to calloc binaries");
  502. fclose(binaryfile);
  503. return NULL;
  504. }
  505. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  506. applog(LOG_ERR, "Unable to fread binaries");
  507. fclose(binaryfile);
  508. free(binaries[slot]);
  509. goto build;
  510. }
  511. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  512. if (status != CL_SUCCESS) {
  513. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  514. fclose(binaryfile);
  515. free(binaries[slot]);
  516. goto build;
  517. }
  518. fclose(binaryfile);
  519. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  520. goto built;
  521. }
  522. /////////////////////////////////////////////////////////////////
  523. // Load CL file, build CL program object, create CL kernel object
  524. /////////////////////////////////////////////////////////////////
  525. build:
  526. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  527. if (status != CL_SUCCESS) {
  528. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  529. return NULL;
  530. }
  531. /* create a cl program executable for all the devices specified */
  532. char *CompilerOptions = calloc(1, 256);
  533. #ifdef USE_SCRYPT
  534. if (opt_scrypt)
  535. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  536. cgpu->lookup_gap, cgpu->thread_concurrency, (int)clState->wsize);
  537. else
  538. #endif
  539. {
  540. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  541. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  542. }
  543. applog(LOG_DEBUG, "Setting worksize to %d", clState->wsize);
  544. if (clState->vwidth > 1)
  545. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  546. if (clState->hasBitAlign) {
  547. strcat(CompilerOptions, " -D BITALIGN");
  548. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  549. if (strstr(name, "Cedar") ||
  550. strstr(name, "Redwood") ||
  551. strstr(name, "Juniper") ||
  552. strstr(name, "Cypress" ) ||
  553. strstr(name, "Hemlock" ) ||
  554. strstr(name, "Caicos" ) ||
  555. strstr(name, "Turks" ) ||
  556. strstr(name, "Barts" ) ||
  557. strstr(name, "Cayman" ) ||
  558. strstr(name, "Antilles" ) ||
  559. strstr(name, "Wrestler" ) ||
  560. strstr(name, "Zacate" ) ||
  561. strstr(name, "WinterPark" ) ||
  562. strstr(name, "BeaverCreek" ))
  563. patchbfi = true;
  564. } else
  565. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  566. if (patchbfi) {
  567. strcat(CompilerOptions, " -D BFI_INT");
  568. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  569. } else
  570. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  571. if (clState->goffset)
  572. strcat(CompilerOptions, " -D GOFFSET");
  573. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  574. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  575. free(CompilerOptions);
  576. if (status != CL_SUCCESS) {
  577. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  578. size_t logSize;
  579. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  580. char *log = malloc(logSize);
  581. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  582. applog(LOG_ERR, "%s", log);
  583. return NULL;
  584. }
  585. prog_built = true;
  586. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  587. if (unlikely(status != CL_SUCCESS)) {
  588. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  589. return NULL;
  590. }
  591. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  592. if (unlikely(status != CL_SUCCESS)) {
  593. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  594. return NULL;
  595. }
  596. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  597. * to iterate over all the binary slots and find where the real program
  598. * is. What the heck is this!? */
  599. for (slot = 0; slot < cpnd; slot++)
  600. if (binary_sizes[slot])
  601. break;
  602. /* copy over all of the generated binaries. */
  603. applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
  604. if (!binary_sizes[slot]) {
  605. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  606. return NULL;
  607. }
  608. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  609. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  610. if (unlikely(status != CL_SUCCESS)) {
  611. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  612. return NULL;
  613. }
  614. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  615. * be hacked in */
  616. if (patchbfi) {
  617. unsigned remaining = binary_sizes[slot];
  618. char *w = binaries[slot];
  619. unsigned int start, length;
  620. /* Find 2nd incidence of .text, and copy the program's
  621. * position and length at a fixed offset from that. Then go
  622. * back and find the 2nd incidence of \x7ELF (rewind by one
  623. * from ELF) and then patch the opcocdes */
  624. if (!advance(&w, &remaining, ".text"))
  625. goto build;
  626. w++; remaining--;
  627. if (!advance(&w, &remaining, ".text")) {
  628. /* 32 bit builds only one ELF */
  629. w--; remaining++;
  630. }
  631. memcpy(&start, w + 285, 4);
  632. memcpy(&length, w + 289, 4);
  633. w = binaries[slot]; remaining = binary_sizes[slot];
  634. if (!advance(&w, &remaining, "ELF"))
  635. goto build;
  636. w++; remaining--;
  637. if (!advance(&w, &remaining, "ELF")) {
  638. /* 32 bit builds only one ELF */
  639. w--; remaining++;
  640. }
  641. w--; remaining++;
  642. w += start; remaining -= start;
  643. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  644. w, remaining);
  645. patch_opcodes(w, length);
  646. status = clReleaseProgram(clState->program);
  647. if (status != CL_SUCCESS) {
  648. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  649. return NULL;
  650. }
  651. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  652. if (status != CL_SUCCESS) {
  653. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  654. return NULL;
  655. }
  656. /* Program needs to be rebuilt */
  657. prog_built = false;
  658. }
  659. free(source);
  660. /* Save the binary to be loaded next time */
  661. binaryfile = fopen(binaryfilename, "wb");
  662. if (!binaryfile) {
  663. /* Not a fatal problem, just means we build it again next time */
  664. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  665. } else {
  666. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  667. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  668. return NULL;
  669. }
  670. fclose(binaryfile);
  671. }
  672. built:
  673. if (binaries[slot])
  674. free(binaries[slot]);
  675. free(binaries);
  676. free(binary_sizes);
  677. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  678. filename, clState->hasBitAlign ? "" : "out", clState->vwidth, clState->wsize);
  679. if (!prog_built) {
  680. /* create a cl program executable for all the devices specified */
  681. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  682. if (status != CL_SUCCESS) {
  683. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  684. size_t logSize;
  685. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  686. char *log = malloc(logSize);
  687. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  688. applog(LOG_ERR, "%s", log);
  689. return NULL;
  690. }
  691. }
  692. /* get a kernel object handle for a kernel with the given name */
  693. clState->kernel = clCreateKernel(clState->program, "search", &status);
  694. if (status != CL_SUCCESS) {
  695. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  696. return NULL;
  697. }
  698. #ifdef USE_SCRYPT
  699. if (opt_scrypt) {
  700. size_t ipt = (1024 / cgpu->lookup_gap + (1024 % cgpu->lookup_gap > 0));
  701. size_t bufsize = 128 * ipt * cgpu->thread_concurrency;
  702. /* Use the max alloc value which has been rounded to a power of
  703. * 2 greater >= required amount earlier */
  704. if (bufsize > cgpu->max_alloc) {
  705. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %u, your scrypt settings come to %u",
  706. gpu, cgpu->max_alloc, bufsize);
  707. } else
  708. bufsize = cgpu->max_alloc;
  709. applog(LOG_DEBUG, "Creating scrypt buffer sized %d", bufsize);
  710. clState->padbufsize = bufsize;
  711. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  712. if (status != CL_SUCCESS) {
  713. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease CT or increase LG", status);
  714. return NULL;
  715. }
  716. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  717. if (status != CL_SUCCESS) {
  718. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  719. return NULL;
  720. }
  721. }
  722. #endif
  723. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  724. if (status != CL_SUCCESS) {
  725. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  726. return NULL;
  727. }
  728. return clState;
  729. }
  730. #endif /* HAVE_OPENCL */