ocl.c 36 KB

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