ocl.c 38 KB

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