ocl.c 36 KB

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