ocl.c 31 KB

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