ocl.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. free(extensions);
  463. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  464. char * devoclver = malloc(1024);
  465. const char * ocl10 = "OpenCL 1.0";
  466. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  467. if (status != CL_SUCCESS) {
  468. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  469. return NULL;
  470. }
  471. find = strstr(devoclver, ocl10);
  472. if (!find)
  473. clState->hasOpenCL11plus = true;
  474. free(devoclver);
  475. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  476. if (status != CL_SUCCESS) {
  477. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  478. return NULL;
  479. }
  480. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  481. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  482. if (status != CL_SUCCESS) {
  483. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  484. return NULL;
  485. }
  486. applog(LOG_DEBUG, "Max work group size reported %"PRId64, (int64_t)clState->max_work_size);
  487. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&cgpu->max_alloc, NULL);
  488. if (status != CL_SUCCESS) {
  489. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  490. return NULL;
  491. }
  492. applog(LOG_DEBUG, "Max mem alloc size is %lu", (unsigned long)cgpu->max_alloc);
  493. if (strstr(vbuff, "MESA"))
  494. {
  495. applog(LOG_DEBUG, "Mesa OpenCL platform detected, disabling OpenCL kernel binaries and bitalign");
  496. clState->hasBitAlign = false;
  497. usebinary = false;
  498. ismesa = true;
  499. }
  500. /* Create binary filename based on parameters passed to opencl
  501. * compiler to ensure we only load a binary that matches what would
  502. * have otherwise created. The filename is:
  503. * kernelname + name +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + p + platform version + .bin
  504. * For scrypt the filename is:
  505. * kernelname + name + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + p + platform version + .bin
  506. */
  507. char binaryfilename[255];
  508. char filename[255];
  509. char numbuf[32];
  510. if (cgpu->kernel == KL_NONE) {
  511. if (opt_scrypt) {
  512. applog(LOG_INFO, "Selecting scrypt kernel");
  513. clState->chosen_kernel = KL_SCRYPT;
  514. }
  515. else if (ismesa)
  516. {
  517. applog(LOG_INFO, "Selecting phatk kernel for Mesa");
  518. clState->chosen_kernel = KL_PHATK;
  519. } else if (!strstr(name, "Tahiti") &&
  520. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  521. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  522. strstr(vbuff, "851.4") || // Windows 64 bit ""
  523. strstr(vbuff, "831.4") ||
  524. strstr(vbuff, "898.1") || // 12.2 driver SDK
  525. strstr(vbuff, "923.1") || // 12.4
  526. strstr(vbuff, "938.2") || // SDK 2.7
  527. strstr(vbuff, "1113.2"))) {// SDK 2.8
  528. applog(LOG_INFO, "Selecting diablo kernel");
  529. clState->chosen_kernel = KL_DIABLO;
  530. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  531. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  532. applog(LOG_INFO, "Selecting poclbm kernel");
  533. clState->chosen_kernel = KL_POCLBM;
  534. /* Use phatk for the rest R5xxx R6xxx */
  535. } else {
  536. applog(LOG_INFO, "Selecting phatk kernel");
  537. clState->chosen_kernel = KL_PHATK;
  538. }
  539. cgpu->kernel = clState->chosen_kernel;
  540. } else {
  541. clState->chosen_kernel = cgpu->kernel;
  542. if (clState->chosen_kernel == KL_PHATK &&
  543. (strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  544. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  545. strstr(vbuff, "923.1") || strstr(vbuff, "938.2") ||
  546. strstr(vbuff, "1113.2"))) {
  547. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  548. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  549. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  550. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  551. }
  552. }
  553. /* For some reason 2 vectors is still better even if the card says
  554. * otherwise, and many cards lie about their max so use 256 as max
  555. * unless explicitly set on the command line. Tahiti prefers 1 */
  556. if (strstr(name, "Tahiti"))
  557. preferred_vwidth = 1;
  558. else if (preferred_vwidth > 2)
  559. preferred_vwidth = 2;
  560. switch (clState->chosen_kernel) {
  561. case KL_POCLBM:
  562. strcpy(filename, POCLBM_KERNNAME".cl");
  563. strcpy(binaryfilename, POCLBM_KERNNAME);
  564. break;
  565. case KL_PHATK:
  566. strcpy(filename, PHATK_KERNNAME".cl");
  567. strcpy(binaryfilename, PHATK_KERNNAME);
  568. break;
  569. case KL_DIAKGCN:
  570. strcpy(filename, DIAKGCN_KERNNAME".cl");
  571. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  572. break;
  573. case KL_SCRYPT:
  574. strcpy(filename, SCRYPT_KERNNAME".cl");
  575. strcpy(binaryfilename, SCRYPT_KERNNAME);
  576. /* Scrypt only supports vector 1 */
  577. cgpu->vwidth = 1;
  578. break;
  579. case KL_NONE: /* Shouldn't happen */
  580. case KL_DIABLO:
  581. strcpy(filename, DIABLO_KERNNAME".cl");
  582. strcpy(binaryfilename, DIABLO_KERNNAME);
  583. break;
  584. }
  585. if (cgpu->vwidth)
  586. clState->vwidth = cgpu->vwidth;
  587. else {
  588. clState->vwidth = preferred_vwidth;
  589. cgpu->vwidth = preferred_vwidth;
  590. }
  591. if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  592. clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
  593. clState->goffset = true;
  594. if (cgpu->work_size && cgpu->work_size <= clState->max_work_size)
  595. clState->wsize = cgpu->work_size;
  596. else if (opt_scrypt)
  597. clState->wsize = 256;
  598. else if (strstr(name, "Tahiti"))
  599. clState->wsize = 64;
  600. else
  601. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  602. cgpu->work_size = clState->wsize;
  603. #ifdef USE_SCRYPT
  604. if (opt_scrypt) {
  605. if (!cgpu->opt_lg) {
  606. applog(LOG_DEBUG, "GPU %d: selecting lookup gap of 2", gpu);
  607. cgpu->lookup_gap = 2;
  608. } else
  609. cgpu->lookup_gap = cgpu->opt_lg;
  610. if (!cgpu->opt_tc) {
  611. unsigned int sixtyfours;
  612. sixtyfours = cgpu->max_alloc / 131072 / 64 - 1;
  613. cgpu->thread_concurrency = sixtyfours * 64;
  614. if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) {
  615. cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders;
  616. if (cgpu->thread_concurrency > cgpu->shaders * 5)
  617. cgpu->thread_concurrency = cgpu->shaders * 5;
  618. }
  619. applog(LOG_DEBUG, "GPU %u: selecting thread concurrency of %lu", gpu, (unsigned long)cgpu->thread_concurrency);
  620. } else
  621. cgpu->thread_concurrency = cgpu->opt_tc;
  622. }
  623. #endif
  624. FILE *binaryfile;
  625. size_t *binary_sizes;
  626. char **binaries;
  627. int pl;
  628. char *source = file_contents(filename, &pl);
  629. size_t sourceSize[] = {(size_t)pl};
  630. cl_uint slot, cpnd;
  631. slot = cpnd = 0;
  632. if (!source)
  633. return NULL;
  634. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  635. if (unlikely(!binary_sizes)) {
  636. applog(LOG_ERR, "Unable to calloc binary_sizes");
  637. return NULL;
  638. }
  639. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  640. if (unlikely(!binaries)) {
  641. applog(LOG_ERR, "Unable to calloc binaries");
  642. return NULL;
  643. }
  644. strcat(binaryfilename, name);
  645. if (clState->goffset)
  646. strcat(binaryfilename, "g");
  647. if (opt_scrypt) {
  648. #ifdef USE_SCRYPT
  649. sprintf(numbuf, "lg%utc%u", cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency);
  650. strcat(binaryfilename, numbuf);
  651. #endif
  652. } else {
  653. sprintf(numbuf, "v%d", clState->vwidth);
  654. strcat(binaryfilename, numbuf);
  655. }
  656. sprintf(numbuf, "w%d", (int)clState->wsize);
  657. strcat(binaryfilename, numbuf);
  658. sprintf(numbuf, "l%d", (int)sizeof(long));
  659. strcat(binaryfilename, numbuf);
  660. strcat(binaryfilename, "p");
  661. strcat(binaryfilename, vbuff);
  662. sanestr(binaryfilename, binaryfilename);
  663. applog(LOG_DEBUG, "OCL%2u: Configured OpenCL kernel name: %s", gpu, binaryfilename);
  664. strcat(binaryfilename, ".bin");
  665. if (!usebinary)
  666. goto build;
  667. binaryfile = fopen(binaryfilename, "rb");
  668. if (!binaryfile) {
  669. applog(LOG_DEBUG, "No binary found, generating from source");
  670. } else {
  671. struct stat binary_stat;
  672. if (unlikely(stat(binaryfilename, &binary_stat))) {
  673. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  674. fclose(binaryfile);
  675. goto build;
  676. }
  677. if (!binary_stat.st_size)
  678. goto build;
  679. binary_sizes[slot] = binary_stat.st_size;
  680. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  681. if (unlikely(!binaries[slot])) {
  682. applog(LOG_ERR, "Unable to calloc binaries");
  683. fclose(binaryfile);
  684. return NULL;
  685. }
  686. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  687. applog(LOG_ERR, "Unable to fread binaries");
  688. fclose(binaryfile);
  689. free(binaries[slot]);
  690. goto build;
  691. }
  692. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  693. if (status != CL_SUCCESS) {
  694. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  695. fclose(binaryfile);
  696. free(binaries[slot]);
  697. goto build;
  698. }
  699. fclose(binaryfile);
  700. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  701. goto built;
  702. }
  703. /////////////////////////////////////////////////////////////////
  704. // Load CL file, build CL program object, create CL kernel object
  705. /////////////////////////////////////////////////////////////////
  706. build:
  707. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  708. if (status != CL_SUCCESS) {
  709. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  710. return NULL;
  711. }
  712. /* create a cl program executable for all the devices specified */
  713. char *CompilerOptions = calloc(1, 256);
  714. #ifdef USE_SCRYPT
  715. if (opt_scrypt)
  716. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  717. cgpu->lookup_gap, (unsigned int)cgpu->thread_concurrency, (int)clState->wsize);
  718. else
  719. #endif
  720. {
  721. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  722. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  723. }
  724. applog(LOG_DEBUG, "Setting worksize to %"PRId64, (int64_t)clState->wsize);
  725. if (clState->vwidth > 1)
  726. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  727. if (clState->hasBitAlign) {
  728. strcat(CompilerOptions, " -D BITALIGN");
  729. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  730. if (strstr(name, "Cedar") ||
  731. strstr(name, "Redwood") ||
  732. strstr(name, "Juniper") ||
  733. strstr(name, "Cypress" ) ||
  734. strstr(name, "Hemlock" ) ||
  735. strstr(name, "Caicos" ) ||
  736. strstr(name, "Turks" ) ||
  737. strstr(name, "Barts" ) ||
  738. strstr(name, "Cayman" ) ||
  739. strstr(name, "Antilles" ) ||
  740. strstr(name, "Wrestler" ) ||
  741. strstr(name, "Zacate" ) ||
  742. strstr(name, "WinterPark" ))
  743. {
  744. // BFI_INT patching only works with AMD-APP up to 1084
  745. if (strstr(vbuff, "ATI-Stream"))
  746. patchbfi = true;
  747. else
  748. if ((s = strstr(vbuff, "AMD-APP")) && (s = strchr(s, '(')) && atoi(&s[1]) < 1085)
  749. patchbfi = true;
  750. }
  751. } else
  752. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  753. if (patchbfi) {
  754. strcat(CompilerOptions, " -D BFI_INT");
  755. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  756. } else
  757. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  758. if (clState->goffset)
  759. strcat(CompilerOptions, " -D GOFFSET");
  760. if (!clState->hasOpenCL11plus)
  761. strcat(CompilerOptions, " -D OCL1");
  762. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  763. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  764. free(CompilerOptions);
  765. if (status != CL_SUCCESS) {
  766. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  767. size_t logSize;
  768. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  769. char *log = malloc(logSize ?: 1);
  770. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  771. if (logSize > 0 && log[0])
  772. applog(LOG_ERR, "%s", log);
  773. free(log);
  774. return NULL;
  775. }
  776. prog_built = true;
  777. if (!usebinary)
  778. goto built;
  779. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  780. if (unlikely(status != CL_SUCCESS)) {
  781. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  782. return NULL;
  783. }
  784. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  785. if (unlikely(status != CL_SUCCESS)) {
  786. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  787. return NULL;
  788. }
  789. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  790. * to iterate over all the binary slots and find where the real program
  791. * is. What the heck is this!? */
  792. for (slot = 0; slot < cpnd; slot++)
  793. if (binary_sizes[slot])
  794. break;
  795. /* copy over all of the generated binaries. */
  796. applog(LOG_DEBUG, "Binary size for gpu %u found in binary slot %u: %"PRId64,
  797. gpu, (unsigned)slot, (int64_t)binary_sizes[slot]);
  798. if (!binary_sizes[slot]) {
  799. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  800. return NULL;
  801. }
  802. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  803. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  804. if (unlikely(status != CL_SUCCESS)) {
  805. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  806. return NULL;
  807. }
  808. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  809. * be hacked in */
  810. if (patchbfi) {
  811. unsigned remaining = binary_sizes[slot];
  812. char *w = binaries[slot];
  813. unsigned int start, length;
  814. /* Find 2nd incidence of .text, and copy the program's
  815. * position and length at a fixed offset from that. Then go
  816. * back and find the 2nd incidence of \x7ELF (rewind by one
  817. * from ELF) and then patch the opcocdes */
  818. if (!advance(&w, &remaining, ".text"))
  819. goto build;
  820. w++; remaining--;
  821. if (!advance(&w, &remaining, ".text")) {
  822. /* 32 bit builds only one ELF */
  823. w--; remaining++;
  824. }
  825. memcpy(&start, w + 285, 4);
  826. memcpy(&length, w + 289, 4);
  827. w = binaries[slot]; remaining = binary_sizes[slot];
  828. if (!advance(&w, &remaining, "ELF"))
  829. goto build;
  830. w++; remaining--;
  831. if (!advance(&w, &remaining, "ELF")) {
  832. /* 32 bit builds only one ELF */
  833. w--; remaining++;
  834. }
  835. w--; remaining++;
  836. w += start; remaining -= start;
  837. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  838. w, remaining);
  839. patch_opcodes(w, length);
  840. status = clReleaseProgram(clState->program);
  841. if (status != CL_SUCCESS) {
  842. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  843. return NULL;
  844. }
  845. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  846. if (status != CL_SUCCESS) {
  847. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  848. return NULL;
  849. }
  850. /* Program needs to be rebuilt */
  851. prog_built = false;
  852. }
  853. free(source);
  854. /* Save the binary to be loaded next time */
  855. binaryfile = fopen(binaryfilename, "wb");
  856. if (!binaryfile) {
  857. /* Not a fatal problem, just means we build it again next time */
  858. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  859. } else {
  860. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  861. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  862. return NULL;
  863. }
  864. fclose(binaryfile);
  865. }
  866. built:
  867. if (binaries[slot])
  868. free(binaries[slot]);
  869. free(binaries);
  870. free(binary_sizes);
  871. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %"PRId64" vectors and worksize %"PRIu64,
  872. filename, clState->hasBitAlign ? "" : "out", (int64_t)clState->vwidth, (uint64_t)clState->wsize);
  873. if (!prog_built) {
  874. /* create a cl program executable for all the devices specified */
  875. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  876. if (status != CL_SUCCESS) {
  877. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  878. size_t logSize;
  879. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  880. char *log = malloc(logSize ?: 1);
  881. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  882. if (logSize > 0 && log[0])
  883. applog(LOG_ERR, "%s", log);
  884. free(log);
  885. return NULL;
  886. }
  887. }
  888. /* get a kernel object handle for a kernel with the given name */
  889. clState->kernel = clCreateKernel(clState->program, "search", &status);
  890. if (status != CL_SUCCESS) {
  891. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  892. return NULL;
  893. }
  894. #ifdef USE_SCRYPT
  895. if (opt_scrypt) {
  896. size_t ipt = (1024 / cgpu->lookup_gap + (1024 % cgpu->lookup_gap > 0));
  897. size_t bufsize = 128 * ipt * cgpu->thread_concurrency;
  898. /* Use the max alloc value which has been rounded to a power of
  899. * 2 greater >= required amount earlier */
  900. if (bufsize > cgpu->max_alloc) {
  901. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %lu", gpu, (unsigned long)cgpu->max_alloc);
  902. applog(LOG_WARNING, "Your scrypt settings come to %lu", (unsigned long)bufsize);
  903. }
  904. applog(LOG_DEBUG, "Creating scrypt buffer sized %lu", (unsigned long)bufsize);
  905. clState->padbufsize = bufsize;
  906. /* This buffer is weird and might work to some degree even if
  907. * the create buffer call has apparently failed, so check if we
  908. * get anything back before we call it a failure. */
  909. clState->padbuffer8 = NULL;
  910. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  911. if (status != CL_SUCCESS && !clState->padbuffer8) {
  912. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
  913. return NULL;
  914. }
  915. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  916. if (status != CL_SUCCESS) {
  917. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  918. return NULL;
  919. }
  920. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, SCRYPT_BUFFERSIZE, NULL, &status);
  921. } else
  922. #endif
  923. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  924. if (status != CL_SUCCESS) {
  925. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  926. return NULL;
  927. }
  928. return clState;
  929. }
  930. #endif /* HAVE_OPENCL */