ocl.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 <ctype.h>
  13. #include <signal.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  22. #include <pthread.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #define OMIT_OPENCL_API
  26. #include "deviceapi.h"
  27. #include "driver-opencl.h"
  28. #include "findnonce.h"
  29. #include "logging.h"
  30. #include "miner.h"
  31. #include "ocl.h"
  32. #include "sha2.h"
  33. #include "util.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. FILE *opencl_open_kernel(const char * const filename)
  189. {
  190. char *fullpath = alloca(PATH_MAX);
  191. FILE *f;
  192. /* Try in the optional kernel path or installed prefix first */
  193. f = open_bitstream("opencl", filename);
  194. if (!f) {
  195. /* Then try from the path BFGMiner was called */
  196. strcpy(fullpath, cgminer_path);
  197. strcat(fullpath, filename);
  198. f = fopen(fullpath, "rb");
  199. }
  200. /* Finally try opening it directly */
  201. if (!f)
  202. f = fopen(filename, "rb");
  203. return f;
  204. }
  205. char *file_contents(const char *filename, int *length)
  206. {
  207. void *buffer;
  208. FILE *f;
  209. f = opencl_open_kernel(filename);
  210. if (!f) {
  211. applog(LOG_ERR, "Unable to open %s for reading", filename);
  212. return NULL;
  213. }
  214. fseek(f, 0, SEEK_END);
  215. *length = ftell(f);
  216. fseek(f, 0, SEEK_SET);
  217. buffer = malloc(*length+1);
  218. *length = fread(buffer, 1, *length, f);
  219. fclose(f);
  220. ((char*)buffer)[*length] = '\0';
  221. return (char*)buffer;
  222. }
  223. extern int opt_g_threads;
  224. int clDevicesNum(void) {
  225. cl_int status;
  226. char pbuff[256];
  227. cl_uint numDevices;
  228. cl_uint numPlatforms;
  229. int most_devices = -1;
  230. cl_platform_id *platforms;
  231. cl_platform_id platform = NULL;
  232. unsigned int i, mdplatform = 0;
  233. bool mdmesa = false;
  234. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  235. /* If this fails, assume no GPUs. */
  236. if (status != CL_SUCCESS) {
  237. applog(LOG_ERR, "Error %d: clGetPlatformsIDs failed (no OpenCL SDK installed?)", status);
  238. return -1;
  239. }
  240. if (numPlatforms == 0) {
  241. applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
  242. return -1;
  243. }
  244. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  245. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  246. if (status != CL_SUCCESS) {
  247. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  248. return -1;
  249. }
  250. for (i = 0; i < numPlatforms; i++) {
  251. if (opt_platform_id >= 0 && (int)i != opt_platform_id)
  252. continue;
  253. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  254. if (status != CL_SUCCESS) {
  255. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  256. return -1;
  257. }
  258. platform = platforms[i];
  259. applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
  260. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  261. if (status == CL_SUCCESS)
  262. applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
  263. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  264. if (status == CL_SUCCESS)
  265. applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
  266. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  267. if (status != CL_SUCCESS) {
  268. applog(LOG_INFO, "Error %d: Getting Device IDs (num)", status);
  269. continue;
  270. }
  271. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  272. if ((int)numDevices > most_devices) {
  273. most_devices = numDevices;
  274. mdplatform = i;
  275. mdmesa = strstr(pbuff, "MESA");
  276. }
  277. if (numDevices) {
  278. unsigned int j;
  279. cl_device_id *devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  280. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  281. for (j = 0; j < numDevices; j++) {
  282. clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  283. applog(LOG_INFO, "\t%i\t%s", j, pbuff);
  284. }
  285. free(devices);
  286. }
  287. }
  288. if (opt_platform_id < 0)
  289. opt_platform_id = mdplatform;
  290. if (mdmesa && opt_g_threads == -1)
  291. opt_g_threads = 1;
  292. return most_devices;
  293. }
  294. static int advance(char **area, unsigned *remaining, const char *marker)
  295. {
  296. char *find = memmem(*area, *remaining, marker, strlen(marker));
  297. if (!find) {
  298. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  299. return 0;
  300. }
  301. *remaining -= find - *area;
  302. *area = find;
  303. return 1;
  304. }
  305. #define OP3_INST_BFE_UINT 4ULL
  306. #define OP3_INST_BFE_INT 5ULL
  307. #define OP3_INST_BFI_INT 6ULL
  308. #define OP3_INST_BIT_ALIGN_INT 12ULL
  309. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  310. void patch_opcodes(char *w, unsigned remaining)
  311. {
  312. uint64_t *opcode = (uint64_t *)w;
  313. int patched = 0;
  314. int count_bfe_int = 0;
  315. int count_bfe_uint = 0;
  316. int count_byte_align = 0;
  317. while (42) {
  318. int clamp = (*opcode >> (32 + 31)) & 0x1;
  319. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  320. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  321. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  322. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  323. int pred_sel = (*opcode >> 29) & 0x3;
  324. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  325. if (alu_inst == OP3_INST_BFE_INT) {
  326. count_bfe_int++;
  327. } else if (alu_inst == OP3_INST_BFE_UINT) {
  328. count_bfe_uint++;
  329. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  330. count_byte_align++;
  331. // patch this instruction to BFI_INT
  332. *opcode &= 0xfffc1fffffffffffULL;
  333. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  334. patched++;
  335. }
  336. }
  337. if (remaining <= 8)
  338. break;
  339. opcode++;
  340. remaining -= 8;
  341. }
  342. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  343. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  344. count_bfe_int, count_bfe_uint, count_byte_align);
  345. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  346. }
  347. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  348. {
  349. _clState *clState = calloc(1, sizeof(_clState));
  350. bool patchbfi = false, prog_built = false;
  351. bool ismesa = false;
  352. struct cgpu_info *cgpu = &gpus[gpu];
  353. struct opencl_device_data * const data = cgpu->device_data;
  354. cl_platform_id platform = NULL;
  355. char pbuff[256], vbuff[255];
  356. char *s, *q;
  357. cl_platform_id* platforms;
  358. cl_uint preferred_vwidth;
  359. cl_device_id *devices;
  360. cl_uint numPlatforms;
  361. cl_uint numDevices;
  362. cl_int status;
  363. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  364. if (status != CL_SUCCESS) {
  365. applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
  366. return NULL;
  367. }
  368. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  369. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  370. if (status != CL_SUCCESS) {
  371. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  372. return NULL;
  373. }
  374. if (opt_platform_id >= (int)numPlatforms) {
  375. applog(LOG_ERR, "Specified platform that does not exist");
  376. return NULL;
  377. }
  378. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  379. if (status != CL_SUCCESS) {
  380. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  381. return NULL;
  382. }
  383. platform = platforms[opt_platform_id];
  384. if (platform == NULL) {
  385. perror("NULL platform found!\n");
  386. return NULL;
  387. }
  388. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  389. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  390. if (status == CL_SUCCESS)
  391. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  392. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  393. if (status == CL_SUCCESS)
  394. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  395. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  396. if (status != CL_SUCCESS) {
  397. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  398. return NULL;
  399. }
  400. if (numDevices > 0 ) {
  401. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  402. /* Now, get the device list data */
  403. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  404. if (status != CL_SUCCESS) {
  405. applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
  406. return NULL;
  407. }
  408. applog(LOG_INFO, "List of devices:");
  409. unsigned int i;
  410. for (i = 0; i < numDevices; i++) {
  411. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  412. if (status != CL_SUCCESS) {
  413. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  414. return NULL;
  415. }
  416. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  417. }
  418. if (gpu < numDevices) {
  419. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  420. if (status != CL_SUCCESS) {
  421. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  422. return NULL;
  423. }
  424. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  425. strncpy(name, pbuff, nameSize);
  426. } else {
  427. applog(LOG_ERR, "Invalid GPU %i", gpu);
  428. return NULL;
  429. }
  430. } else return NULL;
  431. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  432. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  433. if (status != CL_SUCCESS) {
  434. applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
  435. return NULL;
  436. }
  437. /////////////////////////////////////////////////////////////////
  438. // Create an OpenCL command queue
  439. /////////////////////////////////////////////////////////////////
  440. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  441. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  442. if (status != CL_SUCCESS) /* Try again without OOE enable */
  443. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  444. if (status != CL_SUCCESS) {
  445. applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
  446. return NULL;
  447. }
  448. /* Check for BFI INT support. Hopefully people don't mix devices with
  449. * and without it! */
  450. char * extensions = malloc(1024);
  451. const char * camo = "cl_amd_media_ops";
  452. char *find;
  453. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  454. if (status != CL_SUCCESS) {
  455. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
  456. return NULL;
  457. }
  458. find = strstr(extensions, camo);
  459. if (find)
  460. clState->hasBitAlign = true;
  461. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  462. char * devoclver = malloc(1024);
  463. const char * ocl10 = "OpenCL 1.0";
  464. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  465. if (status != CL_SUCCESS) {
  466. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  467. return NULL;
  468. }
  469. find = strstr(devoclver, ocl10);
  470. if (!find)
  471. clState->hasOpenCL11plus = true;
  472. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  473. if (status != CL_SUCCESS) {
  474. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  475. return NULL;
  476. }
  477. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  478. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  479. if (status != CL_SUCCESS) {
  480. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  481. return NULL;
  482. }
  483. applog(LOG_DEBUG, "Max work group size reported %"PRId64, (int64_t)clState->max_work_size);
  484. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(clState->max_compute_units), (void *)&clState->max_compute_units, NULL);
  485. if (status != CL_SUCCESS) {
  486. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_COMPUTE_UNITS", status);
  487. return NULL;
  488. }
  489. if (data->_init_intensity)
  490. {
  491. data->oclthreads = 1; // Needed to ensure we don't just try to re-save the string (which would free before strduping and segfault anyway)
  492. opencl_set_intensity_from_str(cgpu, data->_init_intensity);
  493. }
  494. else
  495. data->oclthreads = intensity_to_oclthreads(MIN_INTENSITY, !opt_scrypt);
  496. applog(LOG_DEBUG, "Max compute units reported %u", (unsigned)clState->max_compute_units);
  497. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&data->max_alloc, NULL);
  498. if (status != CL_SUCCESS) {
  499. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  500. return NULL;
  501. }
  502. applog(LOG_DEBUG, "Max mem alloc size is %lu", (unsigned long)data->max_alloc);
  503. find = strstr(vbuff, "MESA");
  504. if (find)
  505. {
  506. long int major = strtol(&find[4], &s, 10), minor = 0;
  507. if (!major)
  508. {} // No version number at all
  509. else
  510. if (s[0] == '.')
  511. minor = strtol(&s[1], NULL, 10);
  512. if (major < 10 || (major == 10 && minor < 1))
  513. {
  514. if (data->opt_opencl_binaries == OBU_DEFAULT)
  515. {
  516. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld), disabling OpenCL kernel binaries and bitalign", major, minor);
  517. data->opt_opencl_binaries = OBU_NONE;
  518. }
  519. else
  520. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld), disabling bitalign", major, minor);
  521. clState->hasBitAlign = false;
  522. }
  523. else
  524. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld)", major, minor);
  525. ismesa = true;
  526. }
  527. if (data->opt_opencl_binaries == OBU_DEFAULT)
  528. {
  529. #ifdef __APPLE__
  530. // Apple OpenCL doesn't like using binaries this way
  531. data->opt_opencl_binaries = OBU_NONE;
  532. #else
  533. data->opt_opencl_binaries = OBU_LOADSAVE;
  534. #endif
  535. }
  536. /* Create binary filename based on parameters passed to opencl
  537. * compiler to ensure we only load a binary that matches what would
  538. * have otherwise created. The filename is:
  539. * kernelname + name +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + p + platform version + .bin
  540. * For scrypt the filename is:
  541. * kernelname + name + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + p + platform version + .bin
  542. */
  543. char binaryfilename[255];
  544. char filename[255];
  545. char numbuf[32];
  546. if (!data->kernel_file)
  547. {
  548. if (opt_scrypt) {
  549. applog(LOG_INFO, "Selecting scrypt kernel");
  550. clState->chosen_kernel = KL_SCRYPT;
  551. }
  552. else if (ismesa)
  553. {
  554. applog(LOG_INFO, "Selecting phatk kernel for Mesa");
  555. clState->chosen_kernel = KL_PHATK;
  556. } else if (!strstr(name, "Tahiti") &&
  557. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  558. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  559. strstr(vbuff, "851.4") || // Windows 64 bit ""
  560. strstr(vbuff, "831.4") ||
  561. strstr(vbuff, "898.1") || // 12.2 driver SDK
  562. strstr(vbuff, "923.1") || // 12.4
  563. strstr(vbuff, "938.2") || // SDK 2.7
  564. strstr(vbuff, "1113.2"))) {// SDK 2.8
  565. applog(LOG_INFO, "Selecting diablo kernel");
  566. clState->chosen_kernel = KL_DIABLO;
  567. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  568. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  569. applog(LOG_INFO, "Selecting poclbm kernel");
  570. clState->chosen_kernel = KL_POCLBM;
  571. /* Use phatk for the rest R5xxx R6xxx */
  572. } else {
  573. applog(LOG_INFO, "Selecting phatk kernel");
  574. clState->chosen_kernel = KL_PHATK;
  575. }
  576. data->kernel_file = strdup(opencl_get_kernel_interface_name(clState->chosen_kernel));
  577. }
  578. snprintf(filename, sizeof(filename), "%s.cl", data->kernel_file);
  579. snprintf(binaryfilename, sizeof(filename), "%s", data->kernel_file);
  580. int pl;
  581. char *source = file_contents(filename, &pl);
  582. if (!source)
  583. return NULL;
  584. {
  585. uint8_t hash[0x20];
  586. char hashhex[7];
  587. sha256((void*)source, pl, hash);
  588. bin2hex(hashhex, hash, 3);
  589. tailsprintf(binaryfilename, sizeof(binaryfilename), "-%s", hashhex);
  590. }
  591. s = strstr(source, "kernel-interface:");
  592. if (s)
  593. {
  594. for (s = &s[17]; s[0] && isspace(s[0]); ++s)
  595. if (s[0] == '\n' || s[0] == '\r')
  596. break;
  597. for (q = s; q[0] && !isspace(q[0]); ++q)
  598. {} // Find end of string
  599. const size_t kinamelen = q - s;
  600. char kiname[kinamelen + 1];
  601. memcpy(kiname, s, kinamelen);
  602. kiname[kinamelen] = '\0';
  603. clState->chosen_kernel = select_kernel(kiname);
  604. }
  605. else
  606. if (opt_scrypt)
  607. clState->chosen_kernel = KL_SCRYPT;
  608. switch (clState->chosen_kernel) {
  609. case KL_NONE:
  610. applog(LOG_ERR, "%s: Failed to identify kernel interface for %s",
  611. cgpu->dev_repr, data->kernel_file);
  612. free(source);
  613. return NULL;
  614. case KL_PHATK:
  615. if ((strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  616. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  617. strstr(vbuff, "923.1") || strstr(vbuff, "938.2") ||
  618. strstr(vbuff, "1113.2"))) {
  619. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  620. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  621. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  622. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  623. }
  624. default:
  625. ;
  626. }
  627. applog(LOG_DEBUG, "%s: Using kernel %s with interface %s",
  628. cgpu->dev_repr, data->kernel_file,
  629. opencl_get_kernel_interface_name(clState->chosen_kernel));
  630. /* For some reason 2 vectors is still better even if the card says
  631. * otherwise, and many cards lie about their max so use 256 as max
  632. * unless explicitly set on the command line. Tahiti prefers 1 */
  633. if (strstr(name, "Tahiti"))
  634. preferred_vwidth = 1;
  635. else if (preferred_vwidth > 2)
  636. preferred_vwidth = 2;
  637. if (data->vwidth)
  638. clState->vwidth = data->vwidth;
  639. else {
  640. clState->vwidth = preferred_vwidth;
  641. data->vwidth = preferred_vwidth;
  642. }
  643. if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  644. clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
  645. clState->goffset = true;
  646. if (data->work_size && data->work_size <= clState->max_work_size)
  647. clState->wsize = data->work_size;
  648. else if (opt_scrypt)
  649. clState->wsize = 256;
  650. else if (strstr(name, "Tahiti"))
  651. clState->wsize = 64;
  652. else
  653. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  654. data->work_size = clState->wsize;
  655. #ifdef USE_SCRYPT
  656. if (opt_scrypt) {
  657. if (!data->opt_lg) {
  658. applog(LOG_DEBUG, "GPU %d: selecting lookup gap of 2", gpu);
  659. data->lookup_gap = 2;
  660. } else
  661. data->lookup_gap = data->opt_lg;
  662. if (!data->opt_tc) {
  663. unsigned int sixtyfours;
  664. sixtyfours = data->max_alloc / 131072 / 64 - 1;
  665. data->thread_concurrency = sixtyfours * 64;
  666. if (data->shaders && data->thread_concurrency > data->shaders) {
  667. data->thread_concurrency -= data->thread_concurrency % data->shaders;
  668. if (data->thread_concurrency > data->shaders * 5)
  669. data->thread_concurrency = data->shaders * 5;
  670. }
  671. applog(LOG_DEBUG, "GPU %u: selecting thread concurrency of %lu", gpu, (unsigned long)data->thread_concurrency);
  672. } else
  673. data->thread_concurrency = data->opt_tc;
  674. }
  675. #endif
  676. FILE *binaryfile;
  677. size_t *binary_sizes;
  678. char **binaries;
  679. size_t sourceSize[] = {(size_t)pl};
  680. cl_uint slot, cpnd;
  681. slot = cpnd = 0;
  682. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  683. if (unlikely(!binary_sizes)) {
  684. applog(LOG_ERR, "Unable to calloc binary_sizes");
  685. return NULL;
  686. }
  687. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  688. if (unlikely(!binaries)) {
  689. applog(LOG_ERR, "Unable to calloc binaries");
  690. return NULL;
  691. }
  692. strcat(binaryfilename, name);
  693. if (clState->goffset)
  694. strcat(binaryfilename, "g");
  695. if (opt_scrypt) {
  696. #ifdef USE_SCRYPT
  697. sprintf(numbuf, "lg%utc%u", data->lookup_gap, (unsigned int)data->thread_concurrency);
  698. strcat(binaryfilename, numbuf);
  699. #endif
  700. } else {
  701. sprintf(numbuf, "v%d", clState->vwidth);
  702. strcat(binaryfilename, numbuf);
  703. }
  704. sprintf(numbuf, "w%d", (int)clState->wsize);
  705. strcat(binaryfilename, numbuf);
  706. sprintf(numbuf, "l%d", (int)sizeof(long));
  707. strcat(binaryfilename, numbuf);
  708. strcat(binaryfilename, "p");
  709. strcat(binaryfilename, vbuff);
  710. sanestr(binaryfilename, binaryfilename);
  711. applog(LOG_DEBUG, "OCL%2u: Configured OpenCL kernel name: %s", gpu, binaryfilename);
  712. strcat(binaryfilename, ".bin");
  713. if (!(data->opt_opencl_binaries & OBU_LOAD))
  714. goto build;
  715. binaryfile = fopen(binaryfilename, "rb");
  716. if (!binaryfile) {
  717. applog(LOG_DEBUG, "No binary found, generating from source");
  718. } else {
  719. struct stat binary_stat;
  720. if (unlikely(stat(binaryfilename, &binary_stat))) {
  721. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  722. fclose(binaryfile);
  723. goto build;
  724. }
  725. if (!binary_stat.st_size)
  726. goto build;
  727. binary_sizes[slot] = binary_stat.st_size;
  728. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  729. if (unlikely(!binaries[slot])) {
  730. applog(LOG_ERR, "Unable to calloc binaries");
  731. fclose(binaryfile);
  732. return NULL;
  733. }
  734. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  735. applog(LOG_ERR, "Unable to fread binaries");
  736. fclose(binaryfile);
  737. free(binaries[slot]);
  738. goto build;
  739. }
  740. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  741. if (status != CL_SUCCESS) {
  742. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  743. fclose(binaryfile);
  744. free(binaries[slot]);
  745. goto build;
  746. }
  747. fclose(binaryfile);
  748. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  749. goto built;
  750. }
  751. /////////////////////////////////////////////////////////////////
  752. // Load CL file, build CL program object, create CL kernel object
  753. /////////////////////////////////////////////////////////////////
  754. build:
  755. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  756. if (status != CL_SUCCESS) {
  757. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  758. return NULL;
  759. }
  760. /* create a cl program executable for all the devices specified */
  761. char *CompilerOptions = calloc(1, 256);
  762. #ifdef USE_SCRYPT
  763. if (opt_scrypt)
  764. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  765. data->lookup_gap, (unsigned int)data->thread_concurrency, (int)clState->wsize);
  766. else
  767. #endif
  768. {
  769. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  770. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  771. }
  772. applog(LOG_DEBUG, "Setting worksize to %"PRId64, (int64_t)clState->wsize);
  773. if (clState->vwidth > 1)
  774. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  775. if (clState->hasBitAlign) {
  776. strcat(CompilerOptions, " -D BITALIGN");
  777. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  778. if (strstr(name, "Cedar") ||
  779. strstr(name, "Redwood") ||
  780. strstr(name, "Juniper") ||
  781. strstr(name, "Cypress" ) ||
  782. strstr(name, "Hemlock" ) ||
  783. strstr(name, "Caicos" ) ||
  784. strstr(name, "Turks" ) ||
  785. strstr(name, "Barts" ) ||
  786. strstr(name, "Cayman" ) ||
  787. strstr(name, "Antilles" ) ||
  788. strstr(name, "Wrestler" ) ||
  789. strstr(name, "Zacate" ) ||
  790. strstr(name, "WinterPark" ))
  791. {
  792. // BFI_INT patching only works with AMD-APP up to 1084
  793. if (strstr(vbuff, "ATI-Stream"))
  794. patchbfi = true;
  795. else
  796. if ((s = strstr(vbuff, "AMD-APP")) && (s = strchr(s, '(')) && atoi(&s[1]) < 1085)
  797. patchbfi = true;
  798. }
  799. } else
  800. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  801. if (patchbfi) {
  802. if (data->opt_opencl_binaries == OBU_LOADSAVE)
  803. {
  804. strcat(CompilerOptions, " -D BFI_INT");
  805. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  806. }
  807. else
  808. {
  809. patchbfi = false;
  810. applog(LOG_WARNING, "BFI_INT patch requiring device found, but OpenCL binary usage disabled; cannot BFI_INT patch");
  811. }
  812. } else
  813. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  814. if (clState->goffset)
  815. strcat(CompilerOptions, " -D GOFFSET");
  816. if (!clState->hasOpenCL11plus)
  817. strcat(CompilerOptions, " -D OCL1");
  818. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  819. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  820. free(CompilerOptions);
  821. if (status != CL_SUCCESS) {
  822. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  823. size_t logSize;
  824. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  825. char *log = malloc(logSize);
  826. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  827. applog(LOG_ERR, "%s", log);
  828. return NULL;
  829. }
  830. prog_built = true;
  831. if (!(data->opt_opencl_binaries & OBU_SAVE))
  832. goto built;
  833. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  834. if (unlikely(status != CL_SUCCESS)) {
  835. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  836. return NULL;
  837. }
  838. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  839. if (unlikely(status != CL_SUCCESS)) {
  840. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  841. return NULL;
  842. }
  843. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  844. * to iterate over all the binary slots and find where the real program
  845. * is. What the heck is this!? */
  846. for (slot = 0; slot < cpnd; slot++)
  847. if (binary_sizes[slot])
  848. break;
  849. /* copy over all of the generated binaries. */
  850. applog(LOG_DEBUG, "Binary size for gpu %u found in binary slot %u: %"PRId64,
  851. gpu, (unsigned)slot, (int64_t)binary_sizes[slot]);
  852. if (!binary_sizes[slot]) {
  853. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  854. return NULL;
  855. }
  856. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  857. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  858. if (unlikely(status != CL_SUCCESS)) {
  859. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  860. return NULL;
  861. }
  862. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  863. * be hacked in */
  864. if (patchbfi) {
  865. unsigned remaining = binary_sizes[slot];
  866. char *w = binaries[slot];
  867. unsigned int start, length;
  868. /* Find 2nd incidence of .text, and copy the program's
  869. * position and length at a fixed offset from that. Then go
  870. * back and find the 2nd incidence of \x7ELF (rewind by one
  871. * from ELF) and then patch the opcocdes */
  872. if (!advance(&w, &remaining, ".text"))
  873. goto build;
  874. w++; remaining--;
  875. if (!advance(&w, &remaining, ".text")) {
  876. /* 32 bit builds only one ELF */
  877. w--; remaining++;
  878. }
  879. memcpy(&start, w + 285, 4);
  880. memcpy(&length, w + 289, 4);
  881. w = binaries[slot]; remaining = binary_sizes[slot];
  882. if (!advance(&w, &remaining, "ELF"))
  883. goto build;
  884. w++; remaining--;
  885. if (!advance(&w, &remaining, "ELF")) {
  886. /* 32 bit builds only one ELF */
  887. w--; remaining++;
  888. }
  889. w--; remaining++;
  890. w += start; remaining -= start;
  891. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  892. w, remaining);
  893. patch_opcodes(w, length);
  894. status = clReleaseProgram(clState->program);
  895. if (status != CL_SUCCESS) {
  896. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  897. return NULL;
  898. }
  899. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  900. if (status != CL_SUCCESS) {
  901. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  902. return NULL;
  903. }
  904. /* Program needs to be rebuilt */
  905. prog_built = false;
  906. }
  907. free(source);
  908. /* Save the binary to be loaded next time */
  909. binaryfile = fopen(binaryfilename, "wb");
  910. if (!binaryfile) {
  911. /* Not a fatal problem, just means we build it again next time */
  912. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  913. } else {
  914. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  915. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  916. return NULL;
  917. }
  918. fclose(binaryfile);
  919. }
  920. built:
  921. if (binaries[slot])
  922. free(binaries[slot]);
  923. free(binaries);
  924. free(binary_sizes);
  925. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %"PRId64" vectors and worksize %"PRIu64,
  926. filename, clState->hasBitAlign ? "" : "out", (int64_t)clState->vwidth, (uint64_t)clState->wsize);
  927. if (!prog_built) {
  928. /* create a cl program executable for all the devices specified */
  929. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  930. if (status != CL_SUCCESS) {
  931. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  932. size_t logSize;
  933. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  934. char *log = malloc(logSize);
  935. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  936. applog(LOG_ERR, "%s", log);
  937. return NULL;
  938. }
  939. }
  940. /* get a kernel object handle for a kernel with the given name */
  941. clState->kernel = clCreateKernel(clState->program, "search", &status);
  942. if (status != CL_SUCCESS) {
  943. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  944. return NULL;
  945. }
  946. #ifdef USE_SCRYPT
  947. if (opt_scrypt) {
  948. size_t ipt = (1024 / data->lookup_gap + (1024 % data->lookup_gap > 0));
  949. size_t bufsize = 128 * ipt * data->thread_concurrency;
  950. /* Use the max alloc value which has been rounded to a power of
  951. * 2 greater >= required amount earlier */
  952. if (bufsize > data->max_alloc) {
  953. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %lu", gpu, (unsigned long)data->max_alloc);
  954. applog(LOG_WARNING, "Your scrypt settings come to %lu", (unsigned long)bufsize);
  955. }
  956. applog(LOG_DEBUG, "Creating scrypt buffer sized %lu", (unsigned long)bufsize);
  957. clState->padbufsize = bufsize;
  958. /* This buffer is weird and might work to some degree even if
  959. * the create buffer call has apparently failed, so check if we
  960. * get anything back before we call it a failure. */
  961. clState->padbuffer8 = NULL;
  962. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  963. if (status != CL_SUCCESS && !clState->padbuffer8) {
  964. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
  965. return NULL;
  966. }
  967. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  968. if (status != CL_SUCCESS) {
  969. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  970. return NULL;
  971. }
  972. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, SCRYPT_BUFFERSIZE, NULL, &status);
  973. } else
  974. #endif
  975. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  976. if (status != CL_SUCCESS) {
  977. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  978. return NULL;
  979. }
  980. return clState;
  981. }
  982. #endif /* HAVE_OPENCL */