ocl.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * Copyright 2011-2013 Con Kolivas
  3. * Copyright 2012-2013 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #ifdef HAVE_OPENCL
  12. #include <signal.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include <time.h>
  20. #include <sys/time.h>
  21. #include <pthread.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #define OMIT_OPENCL_API
  25. #include "deviceapi.h"
  26. #include "driver-opencl.h"
  27. #include "findnonce.h"
  28. #include "logging.h"
  29. #include "ocl.h"
  30. /* Platform API */
  31. extern
  32. CL_API_ENTRY cl_int CL_API_CALL
  33. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  34. cl_platform_id * /* platforms */,
  35. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  36. extern
  37. CL_API_ENTRY cl_int CL_API_CALL
  38. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  39. cl_platform_info /* param_name */,
  40. size_t /* param_value_size */,
  41. void * /* param_value */,
  42. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  43. /* Device APIs */
  44. extern
  45. CL_API_ENTRY cl_int CL_API_CALL
  46. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  47. cl_device_type /* device_type */,
  48. cl_uint /* num_entries */,
  49. cl_device_id * /* devices */,
  50. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  51. extern
  52. CL_API_ENTRY cl_int CL_API_CALL
  53. (*clGetDeviceInfo)(cl_device_id /* device */,
  54. cl_device_info /* param_name */,
  55. size_t /* param_value_size */,
  56. void * /* param_value */,
  57. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  58. /* Context APIs */
  59. extern
  60. CL_API_ENTRY cl_context CL_API_CALL
  61. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  62. cl_device_type /* device_type */,
  63. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  64. void * /* user_data */,
  65. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  66. extern
  67. CL_API_ENTRY cl_int CL_API_CALL
  68. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  69. /* Command Queue APIs */
  70. extern
  71. CL_API_ENTRY cl_command_queue CL_API_CALL
  72. (*clCreateCommandQueue)(cl_context /* context */,
  73. cl_device_id /* device */,
  74. cl_command_queue_properties /* properties */,
  75. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  76. extern
  77. CL_API_ENTRY cl_int CL_API_CALL
  78. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  79. /* Memory Object APIs */
  80. extern
  81. CL_API_ENTRY cl_mem CL_API_CALL
  82. (*clCreateBuffer)(cl_context /* context */,
  83. cl_mem_flags /* flags */,
  84. size_t /* size */,
  85. void * /* host_ptr */,
  86. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  87. /* Program Object APIs */
  88. extern
  89. CL_API_ENTRY cl_program CL_API_CALL
  90. (*clCreateProgramWithSource)(cl_context /* context */,
  91. cl_uint /* count */,
  92. const char ** /* strings */,
  93. const size_t * /* lengths */,
  94. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  95. extern
  96. CL_API_ENTRY cl_program CL_API_CALL
  97. (*clCreateProgramWithBinary)(cl_context /* context */,
  98. cl_uint /* num_devices */,
  99. const cl_device_id * /* device_list */,
  100. const size_t * /* lengths */,
  101. const unsigned char ** /* binaries */,
  102. cl_int * /* binary_status */,
  103. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  104. extern
  105. CL_API_ENTRY cl_int CL_API_CALL
  106. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  107. extern
  108. CL_API_ENTRY cl_int CL_API_CALL
  109. (*clBuildProgram)(cl_program /* program */,
  110. cl_uint /* num_devices */,
  111. const cl_device_id * /* device_list */,
  112. const char * /* options */,
  113. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  114. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  115. extern
  116. CL_API_ENTRY cl_int CL_API_CALL
  117. (*clGetProgramInfo)(cl_program /* program */,
  118. cl_program_info /* param_name */,
  119. size_t /* param_value_size */,
  120. void * /* param_value */,
  121. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  122. extern
  123. CL_API_ENTRY cl_int CL_API_CALL
  124. (*clGetProgramBuildInfo)(cl_program /* program */,
  125. cl_device_id /* device */,
  126. cl_program_build_info /* param_name */,
  127. size_t /* param_value_size */,
  128. void * /* param_value */,
  129. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  130. /* Kernel Object APIs */
  131. extern
  132. CL_API_ENTRY cl_kernel CL_API_CALL
  133. (*clCreateKernel)(cl_program /* program */,
  134. const char * /* kernel_name */,
  135. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  136. extern
  137. CL_API_ENTRY cl_int CL_API_CALL
  138. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  139. extern
  140. CL_API_ENTRY cl_int CL_API_CALL
  141. (*clSetKernelArg)(cl_kernel /* kernel */,
  142. cl_uint /* arg_index */,
  143. size_t /* arg_size */,
  144. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  145. /* Flush and Finish APIs */
  146. extern
  147. CL_API_ENTRY cl_int CL_API_CALL
  148. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  149. /* Enqueued Commands APIs */
  150. extern
  151. CL_API_ENTRY cl_int CL_API_CALL
  152. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  153. cl_mem /* buffer */,
  154. cl_bool /* blocking_read */,
  155. size_t /* offset */,
  156. size_t /* size */,
  157. void * /* ptr */,
  158. cl_uint /* num_events_in_wait_list */,
  159. const cl_event * /* event_wait_list */,
  160. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  161. extern
  162. CL_API_ENTRY cl_int CL_API_CALL
  163. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  164. cl_mem /* buffer */,
  165. cl_bool /* blocking_write */,
  166. size_t /* offset */,
  167. size_t /* size */,
  168. const void * /* ptr */,
  169. cl_uint /* num_events_in_wait_list */,
  170. const cl_event * /* event_wait_list */,
  171. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  172. extern
  173. CL_API_ENTRY cl_int CL_API_CALL
  174. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  175. cl_kernel /* kernel */,
  176. cl_uint /* work_dim */,
  177. const size_t * /* global_work_offset */,
  178. const size_t * /* global_work_size */,
  179. const size_t * /* local_work_size */,
  180. cl_uint /* num_events_in_wait_list */,
  181. const cl_event * /* event_wait_list */,
  182. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  183. int opt_platform_id = -1;
  184. #ifdef __APPLE__
  185. // Apple OpenCL doesn't like using binaries this way
  186. bool opt_opencl_binaries;
  187. #else
  188. bool opt_opencl_binaries = true;
  189. #endif
  190. char *file_contents(const char *filename, int *length)
  191. {
  192. char *fullpath = alloca(PATH_MAX);
  193. void *buffer;
  194. FILE *f;
  195. /* Try in the optional kernel path or installed prefix first */
  196. f = open_bitstream("opencl", filename);
  197. if (!f) {
  198. /* Then try from the path BFGMiner was called */
  199. strcpy(fullpath, cgminer_path);
  200. strcat(fullpath, filename);
  201. f = fopen(fullpath, "rb");
  202. }
  203. /* Finally try opening it directly */
  204. if (!f)
  205. f = fopen(filename, "rb");
  206. if (!f) {
  207. applog(LOG_ERR, "Unable to open %s or %s for reading", filename, fullpath);
  208. return NULL;
  209. }
  210. fseek(f, 0, SEEK_END);
  211. *length = ftell(f);
  212. fseek(f, 0, SEEK_SET);
  213. buffer = malloc(*length+1);
  214. *length = fread(buffer, 1, *length, f);
  215. fclose(f);
  216. ((char*)buffer)[*length] = '\0';
  217. return (char*)buffer;
  218. }
  219. extern int opt_g_threads;
  220. int clDevicesNum(void) {
  221. cl_int status;
  222. char pbuff[256];
  223. cl_uint numDevices;
  224. cl_uint numPlatforms;
  225. int most_devices = -1;
  226. cl_platform_id *platforms;
  227. cl_platform_id platform = NULL;
  228. unsigned int i, mdplatform = 0;
  229. bool mdmesa = false;
  230. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  231. /* If this fails, assume no GPUs. */
  232. if (status != CL_SUCCESS) {
  233. applog(LOG_ERR, "Error %d: clGetPlatformsIDs failed (no OpenCL SDK installed?)", status);
  234. return -1;
  235. }
  236. if (numPlatforms == 0) {
  237. applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
  238. return -1;
  239. }
  240. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  241. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  242. if (status != CL_SUCCESS) {
  243. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  244. return -1;
  245. }
  246. for (i = 0; i < numPlatforms; i++) {
  247. if (opt_platform_id >= 0 && (int)i != opt_platform_id)
  248. continue;
  249. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  250. if (status != CL_SUCCESS) {
  251. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  252. return -1;
  253. }
  254. platform = platforms[i];
  255. applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
  256. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  257. if (status == CL_SUCCESS)
  258. applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
  259. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  260. if (status == CL_SUCCESS)
  261. applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
  262. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  263. if (status != CL_SUCCESS) {
  264. applog(LOG_INFO, "Error %d: Getting Device IDs (num)", status);
  265. continue;
  266. }
  267. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  268. if ((int)numDevices > most_devices) {
  269. most_devices = numDevices;
  270. mdplatform = i;
  271. mdmesa = strstr(pbuff, "MESA");
  272. }
  273. if (numDevices) {
  274. unsigned int j;
  275. cl_device_id *devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  276. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  277. for (j = 0; j < numDevices; j++) {
  278. clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  279. applog(LOG_INFO, "\t%i\t%s", j, pbuff);
  280. }
  281. free(devices);
  282. }
  283. }
  284. if (opt_platform_id < 0)
  285. opt_platform_id = mdplatform;;
  286. if (mdmesa && opt_g_threads == -1)
  287. opt_g_threads = 1;
  288. return most_devices;
  289. }
  290. static int advance(char **area, unsigned *remaining, const char *marker)
  291. {
  292. char *find = memmem(*area, *remaining, marker, strlen(marker));
  293. if (!find) {
  294. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  295. return 0;
  296. }
  297. *remaining -= find - *area;
  298. *area = find;
  299. return 1;
  300. }
  301. #define OP3_INST_BFE_UINT 4ULL
  302. #define OP3_INST_BFE_INT 5ULL
  303. #define OP3_INST_BFI_INT 6ULL
  304. #define OP3_INST_BIT_ALIGN_INT 12ULL
  305. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  306. void patch_opcodes(char *w, unsigned remaining)
  307. {
  308. uint64_t *opcode = (uint64_t *)w;
  309. int patched = 0;
  310. int count_bfe_int = 0;
  311. int count_bfe_uint = 0;
  312. int count_byte_align = 0;
  313. while (42) {
  314. int clamp = (*opcode >> (32 + 31)) & 0x1;
  315. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  316. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  317. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  318. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  319. int pred_sel = (*opcode >> 29) & 0x3;
  320. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  321. if (alu_inst == OP3_INST_BFE_INT) {
  322. count_bfe_int++;
  323. } else if (alu_inst == OP3_INST_BFE_UINT) {
  324. count_bfe_uint++;
  325. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  326. count_byte_align++;
  327. // patch this instruction to BFI_INT
  328. *opcode &= 0xfffc1fffffffffffULL;
  329. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  330. patched++;
  331. }
  332. }
  333. if (remaining <= 8)
  334. break;
  335. opcode++;
  336. remaining -= 8;
  337. }
  338. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  339. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  340. count_bfe_int, count_bfe_uint, count_byte_align);
  341. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  342. }
  343. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  344. {
  345. _clState *clState = calloc(1, sizeof(_clState));
  346. bool patchbfi = false, prog_built = false;
  347. bool usebinary = opt_opencl_binaries, ismesa = false;
  348. struct cgpu_info *cgpu = &gpus[gpu];
  349. struct opencl_device_data * const data = cgpu->device_data;
  350. cl_platform_id platform = NULL;
  351. char pbuff[256], vbuff[255];
  352. char *s;
  353. cl_platform_id* platforms;
  354. cl_uint preferred_vwidth;
  355. cl_device_id *devices;
  356. cl_uint numPlatforms;
  357. cl_uint numDevices;
  358. cl_int status;
  359. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  360. if (status != CL_SUCCESS) {
  361. applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
  362. return NULL;
  363. }
  364. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  365. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  366. if (status != CL_SUCCESS) {
  367. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  368. return NULL;
  369. }
  370. if (opt_platform_id >= (int)numPlatforms) {
  371. applog(LOG_ERR, "Specified platform that does not exist");
  372. return NULL;
  373. }
  374. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  375. if (status != CL_SUCCESS) {
  376. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  377. return NULL;
  378. }
  379. platform = platforms[opt_platform_id];
  380. if (platform == NULL) {
  381. perror("NULL platform found!\n");
  382. return NULL;
  383. }
  384. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  385. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  386. if (status == CL_SUCCESS)
  387. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  388. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  389. if (status == CL_SUCCESS)
  390. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  391. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  392. if (status != CL_SUCCESS) {
  393. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  394. return NULL;
  395. }
  396. if (numDevices > 0 ) {
  397. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  398. /* Now, get the device list data */
  399. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  400. if (status != CL_SUCCESS) {
  401. applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
  402. return NULL;
  403. }
  404. applog(LOG_INFO, "List of devices:");
  405. unsigned int i;
  406. for (i = 0; i < numDevices; i++) {
  407. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  408. if (status != CL_SUCCESS) {
  409. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  410. return NULL;
  411. }
  412. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  413. }
  414. if (gpu < numDevices) {
  415. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  416. if (status != CL_SUCCESS) {
  417. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  418. return NULL;
  419. }
  420. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  421. strncpy(name, pbuff, nameSize);
  422. } else {
  423. applog(LOG_ERR, "Invalid GPU %i", gpu);
  424. return NULL;
  425. }
  426. } else return NULL;
  427. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  428. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  429. if (status != CL_SUCCESS) {
  430. applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
  431. return NULL;
  432. }
  433. /////////////////////////////////////////////////////////////////
  434. // Create an OpenCL command queue
  435. /////////////////////////////////////////////////////////////////
  436. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  437. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  438. if (status != CL_SUCCESS) /* Try again without OOE enable */
  439. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  440. if (status != CL_SUCCESS) {
  441. applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
  442. return NULL;
  443. }
  444. /* Check for BFI INT support. Hopefully people don't mix devices with
  445. * and without it! */
  446. char * extensions = malloc(1024);
  447. const char * camo = "cl_amd_media_ops";
  448. char *find;
  449. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  450. if (status != CL_SUCCESS) {
  451. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
  452. return NULL;
  453. }
  454. find = strstr(extensions, camo);
  455. if (find)
  456. clState->hasBitAlign = true;
  457. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  458. char * devoclver = malloc(1024);
  459. const char * ocl10 = "OpenCL 1.0";
  460. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  461. if (status != CL_SUCCESS) {
  462. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  463. return NULL;
  464. }
  465. find = strstr(devoclver, ocl10);
  466. if (!find)
  467. clState->hasOpenCL11plus = true;
  468. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  469. if (status != CL_SUCCESS) {
  470. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  471. return NULL;
  472. }
  473. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  474. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  475. if (status != CL_SUCCESS) {
  476. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  477. return NULL;
  478. }
  479. applog(LOG_DEBUG, "Max work group size reported %"PRId64, (int64_t)clState->max_work_size);
  480. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(clState->max_compute_units), (void *)&clState->max_compute_units, NULL);
  481. if (status != CL_SUCCESS) {
  482. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_COMPUTE_UNITS", status);
  483. return NULL;
  484. }
  485. if (data->_init_intensity)
  486. {
  487. data->oclthreads = 1; // Needed to ensure we don't just try to re-save the string (which would free before strduping and segfault anyway)
  488. opencl_set_intensity_from_str(cgpu, data->_init_intensity);
  489. }
  490. else
  491. data->oclthreads = intensity_to_oclthreads(MIN_INTENSITY, !opt_scrypt);
  492. applog(LOG_DEBUG, "Max compute units reported %u", (unsigned)clState->max_compute_units);
  493. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&data->max_alloc, NULL);
  494. if (status != CL_SUCCESS) {
  495. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  496. return NULL;
  497. }
  498. applog(LOG_DEBUG, "Max mem alloc size is %lu", (unsigned long)data->max_alloc);
  499. if (strstr(vbuff, "MESA"))
  500. {
  501. applog(LOG_DEBUG, "Mesa OpenCL platform detected, disabling OpenCL kernel binaries and bitalign");
  502. clState->hasBitAlign = false;
  503. usebinary = false;
  504. ismesa = true;
  505. }
  506. /* Create binary filename based on parameters passed to opencl
  507. * compiler to ensure we only load a binary that matches what would
  508. * have otherwise created. The filename is:
  509. * kernelname + name +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + p + platform version + .bin
  510. * For scrypt the filename is:
  511. * kernelname + name + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + p + platform version + .bin
  512. */
  513. char binaryfilename[255];
  514. char filename[255];
  515. char numbuf[32];
  516. if (data->kernel == KL_NONE) {
  517. if (opt_scrypt) {
  518. applog(LOG_INFO, "Selecting scrypt kernel");
  519. clState->chosen_kernel = KL_SCRYPT;
  520. }
  521. else if (ismesa)
  522. {
  523. applog(LOG_INFO, "Selecting phatk kernel for Mesa");
  524. clState->chosen_kernel = KL_PHATK;
  525. } else if (!strstr(name, "Tahiti") &&
  526. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  527. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  528. strstr(vbuff, "851.4") || // Windows 64 bit ""
  529. strstr(vbuff, "831.4") ||
  530. strstr(vbuff, "898.1") || // 12.2 driver SDK
  531. strstr(vbuff, "923.1") || // 12.4
  532. strstr(vbuff, "938.2") || // SDK 2.7
  533. strstr(vbuff, "1113.2"))) {// SDK 2.8
  534. applog(LOG_INFO, "Selecting diablo kernel");
  535. clState->chosen_kernel = KL_DIABLO;
  536. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  537. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  538. applog(LOG_INFO, "Selecting poclbm kernel");
  539. clState->chosen_kernel = KL_POCLBM;
  540. /* Use phatk for the rest R5xxx R6xxx */
  541. } else {
  542. applog(LOG_INFO, "Selecting phatk kernel");
  543. clState->chosen_kernel = KL_PHATK;
  544. }
  545. data->kernel = clState->chosen_kernel;
  546. } else {
  547. clState->chosen_kernel = data->kernel;
  548. if (clState->chosen_kernel == KL_PHATK &&
  549. (strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  550. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  551. strstr(vbuff, "923.1") || strstr(vbuff, "938.2") ||
  552. strstr(vbuff, "1113.2"))) {
  553. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  554. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  555. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  556. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  557. }
  558. }
  559. /* For some reason 2 vectors is still better even if the card says
  560. * otherwise, and many cards lie about their max so use 256 as max
  561. * unless explicitly set on the command line. Tahiti prefers 1 */
  562. if (strstr(name, "Tahiti"))
  563. preferred_vwidth = 1;
  564. else if (preferred_vwidth > 2)
  565. preferred_vwidth = 2;
  566. switch (clState->chosen_kernel) {
  567. case KL_POCLBM:
  568. strcpy(filename, POCLBM_KERNNAME".cl");
  569. strcpy(binaryfilename, POCLBM_KERNNAME);
  570. break;
  571. case KL_PHATK:
  572. strcpy(filename, PHATK_KERNNAME".cl");
  573. strcpy(binaryfilename, PHATK_KERNNAME);
  574. break;
  575. case KL_DIAKGCN:
  576. strcpy(filename, DIAKGCN_KERNNAME".cl");
  577. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  578. break;
  579. case KL_SCRYPT:
  580. strcpy(filename, SCRYPT_KERNNAME".cl");
  581. strcpy(binaryfilename, SCRYPT_KERNNAME);
  582. /* Scrypt only supports vector 1 */
  583. data->vwidth = 1;
  584. break;
  585. case KL_NONE: /* Shouldn't happen */
  586. case KL_DIABLO:
  587. strcpy(filename, DIABLO_KERNNAME".cl");
  588. strcpy(binaryfilename, DIABLO_KERNNAME);
  589. break;
  590. }
  591. if (data->vwidth)
  592. clState->vwidth = data->vwidth;
  593. else {
  594. clState->vwidth = preferred_vwidth;
  595. data->vwidth = preferred_vwidth;
  596. }
  597. if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  598. clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
  599. clState->goffset = true;
  600. if (data->work_size && data->work_size <= clState->max_work_size)
  601. clState->wsize = data->work_size;
  602. else if (opt_scrypt)
  603. clState->wsize = 256;
  604. else if (strstr(name, "Tahiti"))
  605. clState->wsize = 64;
  606. else
  607. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  608. data->work_size = clState->wsize;
  609. #ifdef USE_SCRYPT
  610. if (opt_scrypt) {
  611. if (!data->opt_lg) {
  612. applog(LOG_DEBUG, "GPU %d: selecting lookup gap of 2", gpu);
  613. data->lookup_gap = 2;
  614. } else
  615. data->lookup_gap = data->opt_lg;
  616. if (!data->opt_tc) {
  617. unsigned int sixtyfours;
  618. sixtyfours = data->max_alloc / 131072 / 64 - 1;
  619. data->thread_concurrency = sixtyfours * 64;
  620. if (data->shaders && data->thread_concurrency > data->shaders) {
  621. data->thread_concurrency -= data->thread_concurrency % data->shaders;
  622. if (data->thread_concurrency > data->shaders * 5)
  623. data->thread_concurrency = data->shaders * 5;
  624. }
  625. applog(LOG_DEBUG, "GPU %u: selecting thread concurrency of %lu", gpu, (unsigned long)data->thread_concurrency);
  626. } else
  627. data->thread_concurrency = data->opt_tc;
  628. }
  629. #endif
  630. FILE *binaryfile;
  631. size_t *binary_sizes;
  632. char **binaries;
  633. int pl;
  634. char *source = file_contents(filename, &pl);
  635. size_t sourceSize[] = {(size_t)pl};
  636. cl_uint slot, cpnd;
  637. slot = cpnd = 0;
  638. if (!source)
  639. return NULL;
  640. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  641. if (unlikely(!binary_sizes)) {
  642. applog(LOG_ERR, "Unable to calloc binary_sizes");
  643. return NULL;
  644. }
  645. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  646. if (unlikely(!binaries)) {
  647. applog(LOG_ERR, "Unable to calloc binaries");
  648. return NULL;
  649. }
  650. strcat(binaryfilename, name);
  651. if (clState->goffset)
  652. strcat(binaryfilename, "g");
  653. if (opt_scrypt) {
  654. #ifdef USE_SCRYPT
  655. sprintf(numbuf, "lg%utc%u", data->lookup_gap, (unsigned int)data->thread_concurrency);
  656. strcat(binaryfilename, numbuf);
  657. #endif
  658. } else {
  659. sprintf(numbuf, "v%d", clState->vwidth);
  660. strcat(binaryfilename, numbuf);
  661. }
  662. sprintf(numbuf, "w%d", (int)clState->wsize);
  663. strcat(binaryfilename, numbuf);
  664. sprintf(numbuf, "l%d", (int)sizeof(long));
  665. strcat(binaryfilename, numbuf);
  666. strcat(binaryfilename, "p");
  667. strcat(binaryfilename, vbuff);
  668. sanestr(binaryfilename, binaryfilename);
  669. applog(LOG_DEBUG, "OCL%2u: Configured OpenCL kernel name: %s", gpu, binaryfilename);
  670. strcat(binaryfilename, ".bin");
  671. if (!usebinary)
  672. goto build;
  673. binaryfile = fopen(binaryfilename, "rb");
  674. if (!binaryfile) {
  675. applog(LOG_DEBUG, "No binary found, generating from source");
  676. } else {
  677. struct stat binary_stat;
  678. if (unlikely(stat(binaryfilename, &binary_stat))) {
  679. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  680. fclose(binaryfile);
  681. goto build;
  682. }
  683. if (!binary_stat.st_size)
  684. goto build;
  685. binary_sizes[slot] = binary_stat.st_size;
  686. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  687. if (unlikely(!binaries[slot])) {
  688. applog(LOG_ERR, "Unable to calloc binaries");
  689. fclose(binaryfile);
  690. return NULL;
  691. }
  692. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  693. applog(LOG_ERR, "Unable to fread binaries");
  694. fclose(binaryfile);
  695. free(binaries[slot]);
  696. goto build;
  697. }
  698. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  699. if (status != CL_SUCCESS) {
  700. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  701. fclose(binaryfile);
  702. free(binaries[slot]);
  703. goto build;
  704. }
  705. fclose(binaryfile);
  706. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  707. goto built;
  708. }
  709. /////////////////////////////////////////////////////////////////
  710. // Load CL file, build CL program object, create CL kernel object
  711. /////////////////////////////////////////////////////////////////
  712. build:
  713. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  714. if (status != CL_SUCCESS) {
  715. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  716. return NULL;
  717. }
  718. /* create a cl program executable for all the devices specified */
  719. char *CompilerOptions = calloc(1, 256);
  720. #ifdef USE_SCRYPT
  721. if (opt_scrypt)
  722. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  723. data->lookup_gap, (unsigned int)data->thread_concurrency, (int)clState->wsize);
  724. else
  725. #endif
  726. {
  727. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  728. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  729. }
  730. applog(LOG_DEBUG, "Setting worksize to %"PRId64, (int64_t)clState->wsize);
  731. if (clState->vwidth > 1)
  732. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  733. if (clState->hasBitAlign) {
  734. strcat(CompilerOptions, " -D BITALIGN");
  735. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  736. if (strstr(name, "Cedar") ||
  737. strstr(name, "Redwood") ||
  738. strstr(name, "Juniper") ||
  739. strstr(name, "Cypress" ) ||
  740. strstr(name, "Hemlock" ) ||
  741. strstr(name, "Caicos" ) ||
  742. strstr(name, "Turks" ) ||
  743. strstr(name, "Barts" ) ||
  744. strstr(name, "Cayman" ) ||
  745. strstr(name, "Antilles" ) ||
  746. strstr(name, "Wrestler" ) ||
  747. strstr(name, "Zacate" ) ||
  748. strstr(name, "WinterPark" ))
  749. {
  750. // BFI_INT patching only works with AMD-APP up to 1084
  751. if (strstr(vbuff, "ATI-Stream"))
  752. patchbfi = true;
  753. else
  754. if ((s = strstr(vbuff, "AMD-APP")) && (s = strchr(s, '(')) && atoi(&s[1]) < 1085)
  755. patchbfi = true;
  756. }
  757. } else
  758. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  759. if (patchbfi) {
  760. strcat(CompilerOptions, " -D BFI_INT");
  761. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  762. } else
  763. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  764. if (clState->goffset)
  765. strcat(CompilerOptions, " -D GOFFSET");
  766. if (!clState->hasOpenCL11plus)
  767. strcat(CompilerOptions, " -D OCL1");
  768. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  769. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  770. free(CompilerOptions);
  771. if (status != CL_SUCCESS) {
  772. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  773. size_t logSize;
  774. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  775. char *log = malloc(logSize);
  776. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  777. applog(LOG_ERR, "%s", log);
  778. return NULL;
  779. }
  780. prog_built = true;
  781. if (!usebinary)
  782. goto built;
  783. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  784. if (unlikely(status != CL_SUCCESS)) {
  785. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  786. return NULL;
  787. }
  788. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  789. if (unlikely(status != CL_SUCCESS)) {
  790. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  791. return NULL;
  792. }
  793. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  794. * to iterate over all the binary slots and find where the real program
  795. * is. What the heck is this!? */
  796. for (slot = 0; slot < cpnd; slot++)
  797. if (binary_sizes[slot])
  798. break;
  799. /* copy over all of the generated binaries. */
  800. applog(LOG_DEBUG, "Binary size for gpu %u found in binary slot %u: %"PRId64,
  801. gpu, (unsigned)slot, (int64_t)binary_sizes[slot]);
  802. if (!binary_sizes[slot]) {
  803. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  804. return NULL;
  805. }
  806. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  807. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  808. if (unlikely(status != CL_SUCCESS)) {
  809. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  810. return NULL;
  811. }
  812. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  813. * be hacked in */
  814. if (patchbfi) {
  815. unsigned remaining = binary_sizes[slot];
  816. char *w = binaries[slot];
  817. unsigned int start, length;
  818. /* Find 2nd incidence of .text, and copy the program's
  819. * position and length at a fixed offset from that. Then go
  820. * back and find the 2nd incidence of \x7ELF (rewind by one
  821. * from ELF) and then patch the opcocdes */
  822. if (!advance(&w, &remaining, ".text"))
  823. goto build;
  824. w++; remaining--;
  825. if (!advance(&w, &remaining, ".text")) {
  826. /* 32 bit builds only one ELF */
  827. w--; remaining++;
  828. }
  829. memcpy(&start, w + 285, 4);
  830. memcpy(&length, w + 289, 4);
  831. w = binaries[slot]; remaining = binary_sizes[slot];
  832. if (!advance(&w, &remaining, "ELF"))
  833. goto build;
  834. w++; remaining--;
  835. if (!advance(&w, &remaining, "ELF")) {
  836. /* 32 bit builds only one ELF */
  837. w--; remaining++;
  838. }
  839. w--; remaining++;
  840. w += start; remaining -= start;
  841. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  842. w, remaining);
  843. patch_opcodes(w, length);
  844. status = clReleaseProgram(clState->program);
  845. if (status != CL_SUCCESS) {
  846. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  847. return NULL;
  848. }
  849. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  850. if (status != CL_SUCCESS) {
  851. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  852. return NULL;
  853. }
  854. /* Program needs to be rebuilt */
  855. prog_built = false;
  856. }
  857. free(source);
  858. /* Save the binary to be loaded next time */
  859. binaryfile = fopen(binaryfilename, "wb");
  860. if (!binaryfile) {
  861. /* Not a fatal problem, just means we build it again next time */
  862. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  863. } else {
  864. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  865. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  866. return NULL;
  867. }
  868. fclose(binaryfile);
  869. }
  870. built:
  871. if (binaries[slot])
  872. free(binaries[slot]);
  873. free(binaries);
  874. free(binary_sizes);
  875. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %"PRId64" vectors and worksize %"PRIu64,
  876. filename, clState->hasBitAlign ? "" : "out", (int64_t)clState->vwidth, (uint64_t)clState->wsize);
  877. if (!prog_built) {
  878. /* create a cl program executable for all the devices specified */
  879. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  880. if (status != CL_SUCCESS) {
  881. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  882. size_t logSize;
  883. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  884. char *log = malloc(logSize);
  885. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  886. applog(LOG_ERR, "%s", log);
  887. return NULL;
  888. }
  889. }
  890. /* get a kernel object handle for a kernel with the given name */
  891. clState->kernel = clCreateKernel(clState->program, "search", &status);
  892. if (status != CL_SUCCESS) {
  893. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  894. return NULL;
  895. }
  896. #ifdef USE_SCRYPT
  897. if (opt_scrypt) {
  898. size_t ipt = (1024 / data->lookup_gap + (1024 % data->lookup_gap > 0));
  899. size_t bufsize = 128 * ipt * data->thread_concurrency;
  900. /* Use the max alloc value which has been rounded to a power of
  901. * 2 greater >= required amount earlier */
  902. if (bufsize > data->max_alloc) {
  903. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %lu", gpu, (unsigned long)data->max_alloc);
  904. applog(LOG_WARNING, "Your scrypt settings come to %lu", (unsigned long)bufsize);
  905. }
  906. applog(LOG_DEBUG, "Creating scrypt buffer sized %lu", (unsigned long)bufsize);
  907. clState->padbufsize = bufsize;
  908. /* This buffer is weird and might work to some degree even if
  909. * the create buffer call has apparently failed, so check if we
  910. * get anything back before we call it a failure. */
  911. clState->padbuffer8 = NULL;
  912. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  913. if (status != CL_SUCCESS && !clState->padbuffer8) {
  914. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
  915. return NULL;
  916. }
  917. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  918. if (status != CL_SUCCESS) {
  919. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  920. return NULL;
  921. }
  922. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, SCRYPT_BUFFERSIZE, NULL, &status);
  923. } else
  924. #endif
  925. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  926. if (status != CL_SUCCESS) {
  927. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  928. return NULL;
  929. }
  930. return clState;
  931. }
  932. #endif /* HAVE_OPENCL */