ocl.c 39 KB

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