ocl.c 37 KB

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