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