ocl.c 36 KB

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