ocl.c 28 KB

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