ocl.c 34 KB

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