ocl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. int most_devices = -1;
  68. cl_platform_id *platforms;
  69. cl_platform_id platform = NULL;
  70. unsigned int i, mdplatform = 0;
  71. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  72. /* If this fails, assume no GPUs. */
  73. if (status != CL_SUCCESS) {
  74. applog(LOG_ERR, "Error %d: clGetPlatformsIDs failed (no OpenCL SDK installed?)", status);
  75. return -1;
  76. }
  77. if (numPlatforms == 0) {
  78. applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
  79. return -1;
  80. }
  81. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  82. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  83. if (status != CL_SUCCESS) {
  84. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  85. return -1;
  86. }
  87. for (i = 0; i < numPlatforms; i++) {
  88. if (opt_platform_id >= 0 && (int)i != opt_platform_id)
  89. continue;
  90. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  91. if (status != CL_SUCCESS) {
  92. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  93. return -1;
  94. }
  95. platform = platforms[i];
  96. applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
  97. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  98. if (status == CL_SUCCESS)
  99. applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
  100. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  101. if (status == CL_SUCCESS)
  102. applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
  103. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  104. if (status != CL_SUCCESS) {
  105. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  106. continue;
  107. }
  108. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  109. if ((int)numDevices > most_devices) {
  110. most_devices = numDevices;
  111. mdplatform = i;
  112. }
  113. if (numDevices) {
  114. unsigned int j;
  115. char pbuff[256];
  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. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  297. if (status != CL_SUCCESS) {
  298. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  299. return NULL;
  300. }
  301. find = strstr(devoclver, ocl10);
  302. if (!find)
  303. clState->hasOpenCL11plus = true;
  304. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  305. if (status != CL_SUCCESS) {
  306. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  307. return NULL;
  308. }
  309. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  310. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  311. if (status != CL_SUCCESS) {
  312. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  313. return NULL;
  314. }
  315. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  316. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&cgpu->max_alloc, NULL);
  317. if (status != CL_SUCCESS) {
  318. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  319. return NULL;
  320. }
  321. applog(LOG_DEBUG, "Max mem alloc size is %u", cgpu->max_alloc);
  322. /* Create binary filename based on parameters passed to opencl
  323. * compiler to ensure we only load a binary that matches what would
  324. * have otherwise created. The filename is:
  325. * name + kernelname +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + .bin
  326. * For scrypt the filename is:
  327. * name + kernelname + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + .bin
  328. */
  329. char binaryfilename[255];
  330. char filename[255];
  331. char numbuf[16];
  332. if (cgpu->kernel == KL_NONE) {
  333. if (opt_scrypt) {
  334. applog(LOG_INFO, "Selecting scrypt kernel");
  335. clState->chosen_kernel = KL_SCRYPT;
  336. } else if (!strstr(name, "Tahiti") &&
  337. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  338. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  339. strstr(vbuff, "851.4") || // Windows 64 bit ""
  340. strstr(vbuff, "831.4") ||
  341. strstr(vbuff, "898.1") || // 12.2 driver SDK
  342. strstr(vbuff, "923.1") || // 12.4
  343. strstr(vbuff, "938.2") || // SDK 2.7
  344. strstr(vbuff, "1113.2"))) {// SDK 2.8
  345. applog(LOG_INFO, "Selecting diablo kernel");
  346. clState->chosen_kernel = KL_DIABLO;
  347. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  348. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  349. applog(LOG_INFO, "Selecting poclbm kernel");
  350. clState->chosen_kernel = KL_POCLBM;
  351. /* Use phatk for the rest R5xxx R6xxx */
  352. } else {
  353. applog(LOG_INFO, "Selecting phatk kernel");
  354. clState->chosen_kernel = KL_PHATK;
  355. }
  356. cgpu->kernel = clState->chosen_kernel;
  357. } else {
  358. clState->chosen_kernel = cgpu->kernel;
  359. if (clState->chosen_kernel == KL_PHATK &&
  360. (strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  361. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  362. strstr(vbuff, "923.1") || strstr(vbuff, "938.2") ||
  363. strstr(vbuff, "1113.2"))) {
  364. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  365. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  366. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  367. applog(LOG_WARNING, "Or allow cgminer to automatically choose a more suitable kernel.");
  368. }
  369. }
  370. /* For some reason 2 vectors is still better even if the card says
  371. * otherwise, and many cards lie about their max so use 256 as max
  372. * unless explicitly set on the command line. Tahiti prefers 1 */
  373. if (strstr(name, "Tahiti"))
  374. preferred_vwidth = 1;
  375. else if (preferred_vwidth > 2)
  376. preferred_vwidth = 2;
  377. switch (clState->chosen_kernel) {
  378. case KL_POCLBM:
  379. strcpy(filename, POCLBM_KERNNAME".cl");
  380. strcpy(binaryfilename, POCLBM_KERNNAME);
  381. break;
  382. case KL_PHATK:
  383. strcpy(filename, PHATK_KERNNAME".cl");
  384. strcpy(binaryfilename, PHATK_KERNNAME);
  385. break;
  386. case KL_DIAKGCN:
  387. strcpy(filename, DIAKGCN_KERNNAME".cl");
  388. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  389. break;
  390. case KL_SCRYPT:
  391. strcpy(filename, SCRYPT_KERNNAME".cl");
  392. strcpy(binaryfilename, SCRYPT_KERNNAME);
  393. /* Scrypt only supports vector 1 */
  394. cgpu->vwidth = 1;
  395. break;
  396. case KL_NONE: /* Shouldn't happen */
  397. case KL_DIABLO:
  398. strcpy(filename, DIABLO_KERNNAME".cl");
  399. strcpy(binaryfilename, DIABLO_KERNNAME);
  400. break;
  401. }
  402. if (cgpu->vwidth)
  403. clState->vwidth = cgpu->vwidth;
  404. else {
  405. clState->vwidth = preferred_vwidth;
  406. cgpu->vwidth = preferred_vwidth;
  407. }
  408. if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  409. clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
  410. clState->goffset = true;
  411. if (cgpu->work_size && cgpu->work_size <= clState->max_work_size)
  412. clState->wsize = cgpu->work_size;
  413. else if (strstr(name, "Tahiti"))
  414. clState->wsize = 64;
  415. else
  416. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  417. cgpu->work_size = clState->wsize;
  418. #ifdef USE_SCRYPT
  419. if (opt_scrypt) {
  420. if (!cgpu->opt_lg) {
  421. applog(LOG_DEBUG, "GPU %d: selecting lookup gap of 2", gpu);
  422. cgpu->lookup_gap = 2;
  423. } else
  424. cgpu->lookup_gap = cgpu->opt_lg;
  425. if (!cgpu->opt_tc) {
  426. unsigned int sixtyfours;
  427. sixtyfours = cgpu->max_alloc / 131072 / 64 - 1;
  428. cgpu->thread_concurrency = sixtyfours * 64;
  429. if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) {
  430. cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders;
  431. if (cgpu->thread_concurrency > cgpu->shaders * 5)
  432. cgpu->thread_concurrency = cgpu->shaders * 5;
  433. }
  434. applog(LOG_DEBUG, "GPU %d: selecting thread concurrency of %u",gpu, cgpu->thread_concurrency);
  435. } else
  436. cgpu->thread_concurrency = cgpu->opt_tc;
  437. }
  438. #endif
  439. FILE *binaryfile;
  440. size_t *binary_sizes;
  441. char **binaries;
  442. int pl;
  443. char *source = file_contents(filename, &pl);
  444. size_t sourceSize[] = {(size_t)pl};
  445. cl_uint slot, cpnd;
  446. slot = cpnd = 0;
  447. if (!source)
  448. return NULL;
  449. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  450. if (unlikely(!binary_sizes)) {
  451. applog(LOG_ERR, "Unable to calloc binary_sizes");
  452. return NULL;
  453. }
  454. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  455. if (unlikely(!binaries)) {
  456. applog(LOG_ERR, "Unable to calloc binaries");
  457. return NULL;
  458. }
  459. strcat(binaryfilename, name);
  460. if (clState->goffset)
  461. strcat(binaryfilename, "g");
  462. if (opt_scrypt) {
  463. #ifdef USE_SCRYPT
  464. sprintf(numbuf, "lg%utc%u", cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency);
  465. strcat(binaryfilename, numbuf);
  466. #endif
  467. } else {
  468. sprintf(numbuf, "v%d", clState->vwidth);
  469. strcat(binaryfilename, numbuf);
  470. }
  471. sprintf(numbuf, "w%d", (int)clState->wsize);
  472. strcat(binaryfilename, numbuf);
  473. sprintf(numbuf, "l%d", (int)sizeof(long));
  474. strcat(binaryfilename, numbuf);
  475. strcat(binaryfilename, ".bin");
  476. binaryfile = fopen(binaryfilename, "rb");
  477. if (!binaryfile) {
  478. applog(LOG_DEBUG, "No binary found, generating from source");
  479. } else {
  480. struct stat binary_stat;
  481. if (unlikely(stat(binaryfilename, &binary_stat))) {
  482. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  483. fclose(binaryfile);
  484. goto build;
  485. }
  486. if (!binary_stat.st_size)
  487. goto build;
  488. binary_sizes[slot] = binary_stat.st_size;
  489. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  490. if (unlikely(!binaries[slot])) {
  491. applog(LOG_ERR, "Unable to calloc binaries");
  492. fclose(binaryfile);
  493. return NULL;
  494. }
  495. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  496. applog(LOG_ERR, "Unable to fread binaries");
  497. fclose(binaryfile);
  498. free(binaries[slot]);
  499. goto build;
  500. }
  501. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  502. if (status != CL_SUCCESS) {
  503. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  504. fclose(binaryfile);
  505. free(binaries[slot]);
  506. goto build;
  507. }
  508. fclose(binaryfile);
  509. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  510. goto built;
  511. }
  512. /////////////////////////////////////////////////////////////////
  513. // Load CL file, build CL program object, create CL kernel object
  514. /////////////////////////////////////////////////////////////////
  515. build:
  516. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  517. if (status != CL_SUCCESS) {
  518. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  519. return NULL;
  520. }
  521. /* create a cl program executable for all the devices specified */
  522. char *CompilerOptions = calloc(1, 256);
  523. #ifdef USE_SCRYPT
  524. if (opt_scrypt)
  525. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  526. cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency, (int)clState->wsize);
  527. else
  528. #endif
  529. {
  530. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  531. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  532. }
  533. applog(LOG_DEBUG, "Setting worksize to %d", clState->wsize);
  534. if (clState->vwidth > 1)
  535. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  536. if (clState->hasBitAlign) {
  537. strcat(CompilerOptions, " -D BITALIGN");
  538. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  539. if (strstr(name, "Cedar") ||
  540. strstr(name, "Redwood") ||
  541. strstr(name, "Juniper") ||
  542. strstr(name, "Cypress" ) ||
  543. strstr(name, "Hemlock" ) ||
  544. strstr(name, "Caicos" ) ||
  545. strstr(name, "Turks" ) ||
  546. strstr(name, "Barts" ) ||
  547. strstr(name, "Cayman" ) ||
  548. strstr(name, "Antilles" ) ||
  549. strstr(name, "Wrestler" ) ||
  550. strstr(name, "Zacate" ) ||
  551. strstr(name, "WinterPark" ))
  552. patchbfi = true;
  553. } else
  554. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  555. if (patchbfi) {
  556. strcat(CompilerOptions, " -D BFI_INT");
  557. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  558. } else
  559. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  560. if (clState->goffset)
  561. strcat(CompilerOptions, " -D GOFFSET");
  562. if (!clState->hasOpenCL11plus)
  563. strcat(CompilerOptions, " -D OCL1");
  564. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  565. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  566. free(CompilerOptions);
  567. if (status != CL_SUCCESS) {
  568. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  569. size_t logSize;
  570. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  571. char *log = malloc(logSize);
  572. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  573. applog(LOG_ERR, "%s", log);
  574. return NULL;
  575. }
  576. prog_built = true;
  577. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  578. if (unlikely(status != CL_SUCCESS)) {
  579. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  580. return NULL;
  581. }
  582. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  583. if (unlikely(status != CL_SUCCESS)) {
  584. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  585. return NULL;
  586. }
  587. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  588. * to iterate over all the binary slots and find where the real program
  589. * is. What the heck is this!? */
  590. for (slot = 0; slot < cpnd; slot++)
  591. if (binary_sizes[slot])
  592. break;
  593. /* copy over all of the generated binaries. */
  594. applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
  595. if (!binary_sizes[slot]) {
  596. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  597. return NULL;
  598. }
  599. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  600. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  601. if (unlikely(status != CL_SUCCESS)) {
  602. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  603. return NULL;
  604. }
  605. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  606. * be hacked in */
  607. if (patchbfi) {
  608. unsigned remaining = binary_sizes[slot];
  609. char *w = binaries[slot];
  610. unsigned int start, length;
  611. /* Find 2nd incidence of .text, and copy the program's
  612. * position and length at a fixed offset from that. Then go
  613. * back and find the 2nd incidence of \x7ELF (rewind by one
  614. * from ELF) and then patch the opcocdes */
  615. if (!advance(&w, &remaining, ".text"))
  616. goto build;
  617. w++; remaining--;
  618. if (!advance(&w, &remaining, ".text")) {
  619. /* 32 bit builds only one ELF */
  620. w--; remaining++;
  621. }
  622. memcpy(&start, w + 285, 4);
  623. memcpy(&length, w + 289, 4);
  624. w = binaries[slot]; remaining = binary_sizes[slot];
  625. if (!advance(&w, &remaining, "ELF"))
  626. goto build;
  627. w++; remaining--;
  628. if (!advance(&w, &remaining, "ELF")) {
  629. /* 32 bit builds only one ELF */
  630. w--; remaining++;
  631. }
  632. w--; remaining++;
  633. w += start; remaining -= start;
  634. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  635. w, remaining);
  636. patch_opcodes(w, length);
  637. status = clReleaseProgram(clState->program);
  638. if (status != CL_SUCCESS) {
  639. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  640. return NULL;
  641. }
  642. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  643. if (status != CL_SUCCESS) {
  644. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  645. return NULL;
  646. }
  647. /* Program needs to be rebuilt */
  648. prog_built = false;
  649. }
  650. free(source);
  651. /* Save the binary to be loaded next time */
  652. binaryfile = fopen(binaryfilename, "wb");
  653. if (!binaryfile) {
  654. /* Not a fatal problem, just means we build it again next time */
  655. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  656. } else {
  657. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  658. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  659. return NULL;
  660. }
  661. fclose(binaryfile);
  662. }
  663. built:
  664. if (binaries[slot])
  665. free(binaries[slot]);
  666. free(binaries);
  667. free(binary_sizes);
  668. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  669. filename, clState->hasBitAlign ? "" : "out", clState->vwidth, clState->wsize);
  670. if (!prog_built) {
  671. /* create a cl program executable for all the devices specified */
  672. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  673. if (status != CL_SUCCESS) {
  674. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  675. size_t logSize;
  676. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  677. char *log = malloc(logSize);
  678. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  679. applog(LOG_ERR, "%s", log);
  680. return NULL;
  681. }
  682. }
  683. /* get a kernel object handle for a kernel with the given name */
  684. clState->kernel = clCreateKernel(clState->program, "search", &status);
  685. if (status != CL_SUCCESS) {
  686. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  687. return NULL;
  688. }
  689. #ifdef USE_SCRYPT
  690. if (opt_scrypt) {
  691. size_t ipt = (1024 / cgpu->lookup_gap + (1024 % cgpu->lookup_gap > 0));
  692. size_t bufsize = 128 * ipt * cgpu->thread_concurrency;
  693. /* Use the max alloc value which has been rounded to a power of
  694. * 2 greater >= required amount earlier */
  695. if (bufsize > cgpu->max_alloc) {
  696. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %u", gpu, cgpu->max_alloc);
  697. applog(LOG_WARNING, "Your scrypt settings come to %u", bufsize);
  698. }
  699. applog(LOG_DEBUG, "Creating scrypt buffer sized %u", bufsize);
  700. clState->padbufsize = bufsize;
  701. /* This buffer is weird and might work to some degree even if
  702. * the create buffer call has apparently failed, so check if we
  703. * get anything back before we call it a failure. */
  704. clState->padbuffer8 = NULL;
  705. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  706. if (status != CL_SUCCESS && !clState->padbuffer8) {
  707. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
  708. return NULL;
  709. }
  710. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  711. if (status != CL_SUCCESS) {
  712. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  713. return NULL;
  714. }
  715. }
  716. #endif
  717. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  718. if (status != CL_SUCCESS) {
  719. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  720. return NULL;
  721. }
  722. return clState;
  723. }
  724. #endif /* HAVE_OPENCL */