ocl.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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;
  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;
  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. if (numDevices) {
  265. unsigned int j;
  266. char pbuff[256];
  267. cl_device_id *devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  268. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  269. for (j = 0; j < numDevices; j++) {
  270. clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  271. applog(LOG_INFO, "\t%i\t%s", j, pbuff);
  272. }
  273. free(devices);
  274. }
  275. }
  276. return most_devices;
  277. }
  278. static int advance(char **area, unsigned *remaining, const char *marker)
  279. {
  280. char *find = memmem(*area, *remaining, marker, strlen(marker));
  281. if (!find) {
  282. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  283. return 0;
  284. }
  285. *remaining -= find - *area;
  286. *area = find;
  287. return 1;
  288. }
  289. #define OP3_INST_BFE_UINT 4ULL
  290. #define OP3_INST_BFE_INT 5ULL
  291. #define OP3_INST_BFI_INT 6ULL
  292. #define OP3_INST_BIT_ALIGN_INT 12ULL
  293. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  294. void patch_opcodes(char *w, unsigned remaining)
  295. {
  296. uint64_t *opcode = (uint64_t *)w;
  297. int patched = 0;
  298. int count_bfe_int = 0;
  299. int count_bfe_uint = 0;
  300. int count_byte_align = 0;
  301. while (42) {
  302. int clamp = (*opcode >> (32 + 31)) & 0x1;
  303. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  304. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  305. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  306. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  307. int pred_sel = (*opcode >> 29) & 0x3;
  308. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  309. if (alu_inst == OP3_INST_BFE_INT) {
  310. count_bfe_int++;
  311. } else if (alu_inst == OP3_INST_BFE_UINT) {
  312. count_bfe_uint++;
  313. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  314. count_byte_align++;
  315. // patch this instruction to BFI_INT
  316. *opcode &= 0xfffc1fffffffffffULL;
  317. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  318. patched++;
  319. }
  320. }
  321. if (remaining <= 8)
  322. break;
  323. opcode++;
  324. remaining -= 8;
  325. }
  326. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  327. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  328. count_bfe_int, count_bfe_uint, count_byte_align);
  329. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  330. }
  331. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  332. {
  333. _clState *clState = calloc(1, sizeof(_clState));
  334. bool patchbfi = false, prog_built = false;
  335. cl_platform_id platform = NULL;
  336. char pbuff[256], vbuff[255];
  337. cl_platform_id* platforms;
  338. cl_uint preferred_vwidth;
  339. cl_device_id *devices;
  340. cl_uint numPlatforms;
  341. cl_uint numDevices;
  342. cl_int status;
  343. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  344. if (status != CL_SUCCESS) {
  345. applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
  346. return NULL;
  347. }
  348. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  349. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  350. if (status != CL_SUCCESS) {
  351. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  352. return NULL;
  353. }
  354. if (opt_platform_id >= (int)numPlatforms) {
  355. applog(LOG_ERR, "Specified platform that does not exist");
  356. return NULL;
  357. }
  358. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  359. if (status != CL_SUCCESS) {
  360. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  361. return NULL;
  362. }
  363. platform = platforms[opt_platform_id];
  364. if (platform == NULL) {
  365. perror("NULL platform found!\n");
  366. return NULL;
  367. }
  368. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  369. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  370. if (status == CL_SUCCESS)
  371. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  372. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  373. if (status == CL_SUCCESS)
  374. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  375. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  376. if (status != CL_SUCCESS) {
  377. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  378. return NULL;
  379. }
  380. if (numDevices > 0 ) {
  381. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  382. /* Now, get the device list data */
  383. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  384. if (status != CL_SUCCESS) {
  385. applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
  386. return NULL;
  387. }
  388. applog(LOG_INFO, "List of devices:");
  389. unsigned int i;
  390. for (i = 0; i < numDevices; i++) {
  391. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  392. if (status != CL_SUCCESS) {
  393. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  394. return NULL;
  395. }
  396. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  397. }
  398. if (gpu < numDevices) {
  399. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  400. if (status != CL_SUCCESS) {
  401. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  402. return NULL;
  403. }
  404. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  405. strncpy(name, pbuff, nameSize);
  406. } else {
  407. applog(LOG_ERR, "Invalid GPU %i", gpu);
  408. return NULL;
  409. }
  410. } else return NULL;
  411. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  412. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  413. if (status != CL_SUCCESS) {
  414. applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
  415. return NULL;
  416. }
  417. /* Check for BFI INT support. Hopefully people don't mix devices with
  418. * and without it! */
  419. char * extensions = malloc(1024);
  420. const char * camo = "cl_amd_media_ops";
  421. char *find;
  422. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  423. if (status != CL_SUCCESS) {
  424. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
  425. return NULL;
  426. }
  427. find = strstr(extensions, camo);
  428. if (find)
  429. clState->hasBitAlign = true;
  430. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  431. char * devoclver = malloc(1024);
  432. const char * ocl10 = "OpenCL 1.0";
  433. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  434. if (status != CL_SUCCESS) {
  435. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  436. return NULL;
  437. }
  438. find = strstr(devoclver, ocl10);
  439. if (!find)
  440. clState->hasOpenCL11plus = true;
  441. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  442. if (status != CL_SUCCESS) {
  443. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  444. return NULL;
  445. }
  446. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  447. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  448. if (status != CL_SUCCESS) {
  449. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  450. return NULL;
  451. }
  452. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  453. /* Create binary filename based on parameters passed to opencl
  454. * compiler to ensure we only load a binary that matches what would
  455. * have otherwise created. The filename is:
  456. * name + kernelname +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + .bin
  457. */
  458. char binaryfilename[255];
  459. char filename[255];
  460. char numbuf[10];
  461. if (gpus[gpu].kernel == KL_NONE) {
  462. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  463. if (!strstr(name, "Tahiti") &&
  464. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  465. strstr(vbuff, "851.4") || // Windows 64 bit ""
  466. strstr(vbuff, "831.4") ||
  467. strstr(vbuff, "898.1") || // 12.2 driver SDK
  468. strstr(vbuff, "923.1"))) { // 12.4 driver SDK
  469. applog(LOG_INFO, "Selecting diablo kernel");
  470. clState->chosen_kernel = KL_DIABLO;
  471. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  472. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  473. applog(LOG_INFO, "Selecting poclbm kernel");
  474. clState->chosen_kernel = KL_POCLBM;
  475. /* Use phatk for the rest R5xxx R6xxx */
  476. } else {
  477. applog(LOG_INFO, "Selecting phatk kernel");
  478. clState->chosen_kernel = KL_PHATK;
  479. }
  480. gpus[gpu].kernel = clState->chosen_kernel;
  481. } else {
  482. clState->chosen_kernel = gpus[gpu].kernel;
  483. if (clState->chosen_kernel == KL_PHATK &&
  484. (strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  485. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  486. strstr(vbuff, "923.1"))) {
  487. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  488. applog(LOG_WARNING, "You are running SDK 2.6 which performs poorly with this kernel.");
  489. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  490. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  491. }
  492. }
  493. /* For some reason 2 vectors is still better even if the card says
  494. * otherwise, and many cards lie about their max so use 256 as max
  495. * unless explicitly set on the command line. Tahiti prefers 1 */
  496. if (strstr(name, "Tahiti"))
  497. preferred_vwidth = 1;
  498. else if (preferred_vwidth > 2)
  499. preferred_vwidth = 2;
  500. switch (clState->chosen_kernel) {
  501. case KL_POCLBM:
  502. strcpy(filename, POCLBM_KERNNAME".cl");
  503. strcpy(binaryfilename, POCLBM_KERNNAME);
  504. break;
  505. case KL_PHATK:
  506. strcpy(filename, PHATK_KERNNAME".cl");
  507. strcpy(binaryfilename, PHATK_KERNNAME);
  508. break;
  509. case KL_DIAKGCN:
  510. strcpy(filename, DIAKGCN_KERNNAME".cl");
  511. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  512. break;
  513. case KL_NONE: /* Shouldn't happen */
  514. case KL_DIABLO:
  515. strcpy(filename, DIABLO_KERNNAME".cl");
  516. strcpy(binaryfilename, DIABLO_KERNNAME);
  517. break;
  518. }
  519. if (gpus[gpu].vwidth)
  520. clState->vwidth = gpus[gpu].vwidth;
  521. else {
  522. clState->vwidth = preferred_vwidth;
  523. gpus[gpu].vwidth = preferred_vwidth;
  524. }
  525. if ((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  526. clState->vwidth == 1 && clState->hasOpenCL11plus)
  527. clState->goffset = true;
  528. if (gpus[gpu].work_size && gpus[gpu].work_size <= clState->max_work_size)
  529. clState->wsize = gpus[gpu].work_size;
  530. else if (strstr(name, "Tahiti"))
  531. clState->wsize = 64;
  532. else
  533. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  534. gpus[gpu].work_size = clState->wsize;
  535. FILE *binaryfile;
  536. size_t *binary_sizes;
  537. char **binaries;
  538. int pl;
  539. char *source = file_contents(filename, &pl);
  540. size_t sourceSize[] = {(size_t)pl};
  541. cl_uint slot, cpnd;
  542. slot = cpnd = 0;
  543. if (!source)
  544. return NULL;
  545. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  546. if (unlikely(!binary_sizes)) {
  547. applog(LOG_ERR, "Unable to calloc binary_sizes");
  548. return NULL;
  549. }
  550. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  551. if (unlikely(!binaries)) {
  552. applog(LOG_ERR, "Unable to calloc binaries");
  553. return NULL;
  554. }
  555. strcat(binaryfilename, name);
  556. if (clState->goffset)
  557. strcat(binaryfilename, "g");
  558. strcat(binaryfilename, "v");
  559. sprintf(numbuf, "%d", clState->vwidth);
  560. strcat(binaryfilename, numbuf);
  561. strcat(binaryfilename, "w");
  562. sprintf(numbuf, "%d", (int)clState->wsize);
  563. strcat(binaryfilename, numbuf);
  564. strcat(binaryfilename, "l");
  565. sprintf(numbuf, "%d", (int)sizeof(long));
  566. strcat(binaryfilename, numbuf);
  567. strcat(binaryfilename, ".bin");
  568. binaryfile = fopen(binaryfilename, "rb");
  569. if (!binaryfile) {
  570. applog(LOG_DEBUG, "No binary found, generating from source");
  571. } else {
  572. struct stat binary_stat;
  573. if (unlikely(stat(binaryfilename, &binary_stat))) {
  574. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  575. fclose(binaryfile);
  576. goto build;
  577. }
  578. if (!binary_stat.st_size)
  579. goto build;
  580. binary_sizes[slot] = binary_stat.st_size;
  581. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  582. if (unlikely(!binaries[slot])) {
  583. applog(LOG_ERR, "Unable to calloc binaries");
  584. fclose(binaryfile);
  585. return NULL;
  586. }
  587. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  588. applog(LOG_ERR, "Unable to fread binaries");
  589. fclose(binaryfile);
  590. free(binaries[slot]);
  591. goto build;
  592. }
  593. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  594. if (status != CL_SUCCESS) {
  595. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  596. fclose(binaryfile);
  597. free(binaries[slot]);
  598. goto build;
  599. }
  600. fclose(binaryfile);
  601. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  602. goto built;
  603. }
  604. /////////////////////////////////////////////////////////////////
  605. // Load CL file, build CL program object, create CL kernel object
  606. /////////////////////////////////////////////////////////////////
  607. build:
  608. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  609. if (status != CL_SUCCESS) {
  610. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  611. return NULL;
  612. }
  613. /* create a cl program executable for all the devices specified */
  614. char *CompilerOptions = calloc(1, 256);
  615. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  616. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  617. applog(LOG_DEBUG, "Setting worksize to %d", clState->wsize);
  618. if (clState->vwidth > 1)
  619. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  620. if (clState->hasBitAlign) {
  621. strcat(CompilerOptions, " -D BITALIGN");
  622. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  623. if (strstr(name, "Cedar") ||
  624. strstr(name, "Redwood") ||
  625. strstr(name, "Juniper") ||
  626. strstr(name, "Cypress" ) ||
  627. strstr(name, "Hemlock" ) ||
  628. strstr(name, "Caicos" ) ||
  629. strstr(name, "Turks" ) ||
  630. strstr(name, "Barts" ) ||
  631. strstr(name, "Cayman" ) ||
  632. strstr(name, "Antilles" ) ||
  633. strstr(name, "Wrestler" ) ||
  634. strstr(name, "Zacate" ) ||
  635. strstr(name, "WinterPark" ) ||
  636. strstr(name, "BeaverCreek" ))
  637. patchbfi = true;
  638. } else
  639. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  640. if (patchbfi) {
  641. strcat(CompilerOptions, " -D BFI_INT");
  642. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  643. } else
  644. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  645. if (clState->goffset)
  646. strcat(CompilerOptions, " -D GOFFSET");
  647. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  648. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  649. free(CompilerOptions);
  650. if (status != CL_SUCCESS) {
  651. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  652. size_t logSize;
  653. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  654. char *log = malloc(logSize);
  655. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  656. applog(LOG_ERR, "%s", log);
  657. return NULL;
  658. }
  659. prog_built = true;
  660. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  661. if (unlikely(status != CL_SUCCESS)) {
  662. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  663. return NULL;
  664. }
  665. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  666. if (unlikely(status != CL_SUCCESS)) {
  667. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  668. return NULL;
  669. }
  670. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  671. * to iterate over all the binary slots and find where the real program
  672. * is. What the heck is this!? */
  673. for (slot = 0; slot < cpnd; slot++)
  674. if (binary_sizes[slot])
  675. break;
  676. /* copy over all of the generated binaries. */
  677. applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
  678. if (!binary_sizes[slot]) {
  679. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  680. return NULL;
  681. }
  682. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  683. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  684. if (unlikely(status != CL_SUCCESS)) {
  685. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  686. return NULL;
  687. }
  688. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  689. * be hacked in */
  690. if (patchbfi) {
  691. unsigned remaining = binary_sizes[slot];
  692. char *w = binaries[slot];
  693. unsigned int start, length;
  694. /* Find 2nd incidence of .text, and copy the program's
  695. * position and length at a fixed offset from that. Then go
  696. * back and find the 2nd incidence of \x7ELF (rewind by one
  697. * from ELF) and then patch the opcocdes */
  698. if (!advance(&w, &remaining, ".text"))
  699. goto build;
  700. w++; remaining--;
  701. if (!advance(&w, &remaining, ".text")) {
  702. /* 32 bit builds only one ELF */
  703. w--; remaining++;
  704. }
  705. memcpy(&start, w + 285, 4);
  706. memcpy(&length, w + 289, 4);
  707. w = binaries[slot]; remaining = binary_sizes[slot];
  708. if (!advance(&w, &remaining, "ELF"))
  709. goto build;
  710. w++; remaining--;
  711. if (!advance(&w, &remaining, "ELF")) {
  712. /* 32 bit builds only one ELF */
  713. w--; remaining++;
  714. }
  715. w--; remaining++;
  716. w += start; remaining -= start;
  717. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  718. w, remaining);
  719. patch_opcodes(w, length);
  720. status = clReleaseProgram(clState->program);
  721. if (status != CL_SUCCESS) {
  722. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  723. return NULL;
  724. }
  725. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  726. if (status != CL_SUCCESS) {
  727. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  728. return NULL;
  729. }
  730. /* Program needs to be rebuilt */
  731. prog_built = false;
  732. }
  733. free(source);
  734. /* Save the binary to be loaded next time */
  735. binaryfile = fopen(binaryfilename, "wb");
  736. if (!binaryfile) {
  737. /* Not a fatal problem, just means we build it again next time */
  738. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  739. } else {
  740. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  741. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  742. return NULL;
  743. }
  744. fclose(binaryfile);
  745. }
  746. built:
  747. if (binaries[slot])
  748. free(binaries[slot]);
  749. free(binaries);
  750. free(binary_sizes);
  751. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  752. filename, clState->hasBitAlign ? "" : "out", clState->vwidth, clState->wsize);
  753. if (!prog_built) {
  754. /* create a cl program executable for all the devices specified */
  755. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  756. if (status != CL_SUCCESS) {
  757. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  758. size_t logSize;
  759. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  760. char *log = malloc(logSize);
  761. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  762. applog(LOG_ERR, "%s", log);
  763. return NULL;
  764. }
  765. }
  766. /* get a kernel object handle for a kernel with the given name */
  767. clState->kernel = clCreateKernel(clState->program, "search", &status);
  768. if (status != CL_SUCCESS) {
  769. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  770. return NULL;
  771. }
  772. /////////////////////////////////////////////////////////////////
  773. // Create an OpenCL command queue
  774. /////////////////////////////////////////////////////////////////
  775. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  776. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  777. if (status != CL_SUCCESS) /* Try again without OOE enable */
  778. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  779. if (status != CL_SUCCESS) {
  780. applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
  781. return NULL;
  782. }
  783. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  784. if (status != CL_SUCCESS) {
  785. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  786. return NULL;
  787. }
  788. return clState;
  789. }
  790. #endif /* HAVE_OPENCL */