ocl.c 36 KB

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