ocl.c 31 KB

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