ocl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. */
  4. #include "config.h"
  5. #ifdef HAVE_OPENCL
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #ifdef WIN32
  12. #include <winsock2.h>
  13. #else
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #endif
  18. #include <time.h>
  19. #include <sys/time.h>
  20. #include <pthread.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include "findnonce.h"
  24. #include "ocl.h"
  25. extern int opt_vectors;
  26. extern int opt_worksize;
  27. int opt_platform_id;
  28. char *file_contents(const char *filename, int *length)
  29. {
  30. char *fullpath = alloca(PATH_MAX);
  31. void *buffer;
  32. FILE *f;
  33. strcpy(fullpath, opt_kernel_path);
  34. strcat(fullpath, filename);
  35. /* Try in the optional kernel path or installed prefix first */
  36. f = fopen(fullpath, "rb");
  37. if (!f) {
  38. /* Then try from the path cgminer was called */
  39. strcpy(fullpath, cgminer_path);
  40. strcat(fullpath, filename);
  41. f = fopen(fullpath, "rb");
  42. }
  43. /* Finally try opening it directly */
  44. if (!f)
  45. f = fopen(filename, "rb");
  46. if (!f) {
  47. applog(LOG_ERR, "Unable to open %s or %s for reading", filename, fullpath);
  48. return NULL;
  49. }
  50. fseek(f, 0, SEEK_END);
  51. *length = ftell(f);
  52. fseek(f, 0, SEEK_SET);
  53. buffer = malloc(*length+1);
  54. *length = fread(buffer, 1, *length, f);
  55. fclose(f);
  56. ((char*)buffer)[*length] = '\0';
  57. return (char*)buffer;
  58. }
  59. int clDevicesNum(void) {
  60. cl_int status;
  61. char pbuff[256];
  62. cl_uint numDevices;
  63. cl_uint numPlatforms;
  64. cl_platform_id *platforms;
  65. cl_platform_id platform = NULL;
  66. unsigned int most_devices = 0, i;
  67. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  68. /* If this fails, assume no GPUs. */
  69. if (status != CL_SUCCESS) {
  70. applog(LOG_ERR, "clGetPlatformsIDs failed (no OpenCL SDK installed?)");
  71. return -1;
  72. }
  73. if (numPlatforms == 0) {
  74. applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
  75. return -1;
  76. }
  77. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  78. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  79. if (status != CL_SUCCESS) {
  80. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  81. return -1;
  82. }
  83. for (i = 0; i < numPlatforms; i++) {
  84. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  85. if (status != CL_SUCCESS) {
  86. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  87. return -1;
  88. }
  89. platform = platforms[i];
  90. applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
  91. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  92. if (status == CL_SUCCESS)
  93. applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
  94. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  95. if (status == CL_SUCCESS)
  96. applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
  97. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  98. if (status != CL_SUCCESS) {
  99. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  100. return -1;
  101. }
  102. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  103. if (numDevices > most_devices)
  104. most_devices = numDevices;
  105. }
  106. return most_devices;
  107. }
  108. static int advance(char **area, unsigned *remaining, const char *marker)
  109. {
  110. char *find = memmem(*area, *remaining, marker, strlen(marker));
  111. if (!find) {
  112. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  113. return 0;
  114. }
  115. *remaining -= find - *area;
  116. *area = find;
  117. return 1;
  118. }
  119. #define OP3_INST_BFE_UINT 4ULL
  120. #define OP3_INST_BFE_INT 5ULL
  121. #define OP3_INST_BFI_INT 6ULL
  122. #define OP3_INST_BIT_ALIGN_INT 12ULL
  123. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  124. void patch_opcodes(char *w, unsigned remaining)
  125. {
  126. uint64_t *opcode = (uint64_t *)w;
  127. int patched = 0;
  128. int count_bfe_int = 0;
  129. int count_bfe_uint = 0;
  130. int count_byte_align = 0;
  131. while (42) {
  132. int clamp = (*opcode >> (32 + 31)) & 0x1;
  133. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  134. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  135. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  136. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  137. int pred_sel = (*opcode >> 29) & 0x3;
  138. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  139. if (alu_inst == OP3_INST_BFE_INT) {
  140. count_bfe_int++;
  141. } else if (alu_inst == OP3_INST_BFE_UINT) {
  142. count_bfe_uint++;
  143. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  144. count_byte_align++;
  145. // patch this instruction to BFI_INT
  146. *opcode &= 0xfffc1fffffffffffULL;
  147. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  148. patched++;
  149. }
  150. }
  151. if (remaining <= 8)
  152. break;
  153. opcode++;
  154. remaining -= 8;
  155. }
  156. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  157. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  158. count_bfe_int, count_bfe_uint, count_byte_align);
  159. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  160. }
  161. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  162. {
  163. _clState *clState = calloc(1, sizeof(_clState));
  164. bool patchbfi = false, prog_built = false;
  165. cl_platform_id platform = NULL;
  166. char pbuff[256], vbuff[255];
  167. cl_platform_id* platforms;
  168. cl_device_id *devices;
  169. cl_uint numPlatforms;
  170. cl_uint numDevices;
  171. cl_int status;
  172. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  173. if (status != CL_SUCCESS) {
  174. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  175. return NULL;
  176. }
  177. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  178. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  179. if (status != CL_SUCCESS) {
  180. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  181. return NULL;
  182. }
  183. if (opt_platform_id >= (int)numPlatforms) {
  184. applog(LOG_ERR, "Specified platform that does not exist");
  185. return NULL;
  186. }
  187. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  188. if (status != CL_SUCCESS) {
  189. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  190. return NULL;
  191. }
  192. platform = platforms[opt_platform_id];
  193. if (platform == NULL) {
  194. perror("NULL platform found!\n");
  195. return NULL;
  196. }
  197. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  198. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  199. if (status == CL_SUCCESS)
  200. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  201. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  202. if (status == CL_SUCCESS)
  203. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  204. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  205. if (status != CL_SUCCESS) {
  206. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  207. return NULL;
  208. }
  209. if (numDevices > 0 ) {
  210. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  211. /* Now, get the device list data */
  212. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  213. if (status != CL_SUCCESS) {
  214. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  215. return NULL;
  216. }
  217. applog(LOG_INFO, "List of devices:");
  218. unsigned int i;
  219. for (i = 0; i < numDevices; i++) {
  220. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  221. if (status != CL_SUCCESS) {
  222. applog(LOG_ERR, "Error: Getting Device Info");
  223. return NULL;
  224. }
  225. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  226. }
  227. if (gpu < numDevices) {
  228. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  229. if (status != CL_SUCCESS) {
  230. applog(LOG_ERR, "Error: Getting Device Info");
  231. return NULL;
  232. }
  233. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  234. strncpy(name, pbuff, nameSize);
  235. } else {
  236. applog(LOG_ERR, "Invalid GPU %i", gpu);
  237. return NULL;
  238. }
  239. } else return NULL;
  240. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  241. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  242. if (status != CL_SUCCESS) {
  243. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  244. return NULL;
  245. }
  246. /* Check for BFI INT support. Hopefully people don't mix devices with
  247. * and without it! */
  248. char * extensions = malloc(1024);
  249. const char * camo = "cl_amd_media_ops";
  250. char *find;
  251. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  252. if (status != CL_SUCCESS) {
  253. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  254. return NULL;
  255. }
  256. find = strstr(extensions, camo);
  257. if (find)
  258. clState->hasBitAlign = true;
  259. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  260. char * devoclver = malloc(1024);
  261. const char * ocl10 = "OpenCL 1.0";
  262. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  263. if (status != CL_SUCCESS) {
  264. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION");
  265. return NULL;
  266. }
  267. find = strstr(devoclver, ocl10);
  268. if (!find)
  269. clState->hasOpenCL11plus = true;
  270. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  271. if (status != CL_SUCCESS) {
  272. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  273. return NULL;
  274. }
  275. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  276. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  277. if (status != CL_SUCCESS) {
  278. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  279. return NULL;
  280. }
  281. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  282. /* For some reason 2 vectors is still better even if the card says
  283. * otherwise, and many cards lie about their max so use 256 as max
  284. * unless explicitly set on the command line. 79x0 cards perform
  285. * better without vectors */
  286. if (clState->preferred_vwidth > 1) {
  287. if (strstr(name, "Tahiti"))
  288. clState->preferred_vwidth = 1;
  289. else
  290. clState->preferred_vwidth = 2;
  291. }
  292. if (opt_vectors)
  293. clState->preferred_vwidth = opt_vectors;
  294. if (opt_worksize && opt_worksize <= (int)clState->max_work_size)
  295. clState->work_size = opt_worksize;
  296. else
  297. clState->work_size = (clState->max_work_size <= 256 ? clState->max_work_size : 256) /
  298. clState->preferred_vwidth;
  299. /* Create binary filename based on parameters passed to opencl
  300. * compiler to ensure we only load a binary that matches what would
  301. * have otherwise created. The filename is:
  302. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  303. */
  304. char binaryfilename[255];
  305. char filename[255];
  306. char numbuf[10];
  307. if (chosen_kernel == KL_NONE) {
  308. if (strstr(name, "Tahiti") // GCN
  309. || !clState->hasBitAlign) // Older Radeon & Nvidia
  310. clState->chosen_kernel = KL_POCLBM;
  311. else
  312. clState->chosen_kernel = KL_PHATK;
  313. } else
  314. clState->chosen_kernel = chosen_kernel;
  315. switch (clState->chosen_kernel) {
  316. case KL_POCLBM:
  317. strcpy(filename, POCLBM_KERNNAME".cl");
  318. strcpy(binaryfilename, POCLBM_KERNNAME);
  319. break;
  320. case KL_NONE: /* Shouldn't happen */
  321. case KL_PHATK:
  322. strcpy(filename, PHATK_KERNNAME".cl");
  323. strcpy(binaryfilename, PHATK_KERNNAME);
  324. break;
  325. case KL_DIAKGCN:
  326. strcpy(filename, DIAKGCN_KERNNAME".cl");
  327. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  328. break;
  329. case KL_DIABLO:
  330. strcpy(filename, DIABLO_KERNNAME".cl");
  331. strcpy(binaryfilename, DIABLO_KERNNAME);
  332. break;
  333. }
  334. FILE *binaryfile;
  335. size_t *binary_sizes;
  336. char **binaries;
  337. int pl;
  338. char *source = file_contents(filename, &pl);
  339. size_t sourceSize[] = {(size_t)pl};
  340. cl_uint slot, cpnd;
  341. slot = cpnd = 0;
  342. if (!source)
  343. return NULL;
  344. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  345. if (unlikely(!binary_sizes)) {
  346. applog(LOG_ERR, "Unable to calloc binary_sizes");
  347. return NULL;
  348. }
  349. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  350. if (unlikely(!binaries)) {
  351. applog(LOG_ERR, "Unable to calloc binaries");
  352. return NULL;
  353. }
  354. strcat(binaryfilename, name);
  355. if (clState->hasBitAlign)
  356. strcat(binaryfilename, "bitalign");
  357. strcat(binaryfilename, "v");
  358. sprintf(numbuf, "%d", clState->preferred_vwidth);
  359. strcat(binaryfilename, numbuf);
  360. strcat(binaryfilename, "w");
  361. sprintf(numbuf, "%d", (int)clState->work_size);
  362. strcat(binaryfilename, numbuf);
  363. strcat(binaryfilename, "long");
  364. sprintf(numbuf, "%d", (int)sizeof(long));
  365. strcat(binaryfilename, numbuf);
  366. strcat(binaryfilename, ".bin");
  367. loadbin:
  368. binaryfile = fopen(binaryfilename, "rb");
  369. if (!binaryfile) {
  370. applog(LOG_DEBUG, "No binary found, generating from source");
  371. } else {
  372. struct stat binary_stat;
  373. if (unlikely(stat(binaryfilename, &binary_stat))) {
  374. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  375. fclose(binaryfile);
  376. goto build;
  377. }
  378. if (!binary_stat.st_size)
  379. goto build;
  380. binary_sizes[slot] = binary_stat.st_size;
  381. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  382. if (unlikely(!binaries[slot])) {
  383. applog(LOG_ERR, "Unable to calloc binaries");
  384. fclose(binaryfile);
  385. return NULL;
  386. }
  387. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  388. applog(LOG_ERR, "Unable to fread binaries");
  389. fclose(binaryfile);
  390. free(binaries[slot]);
  391. goto build;
  392. }
  393. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  394. if (status != CL_SUCCESS) {
  395. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  396. fclose(binaryfile);
  397. free(binaries[slot]);
  398. goto build;
  399. }
  400. clRetainProgram(clState->program);
  401. if (status != CL_SUCCESS) {
  402. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  403. return NULL;
  404. }
  405. fclose(binaryfile);
  406. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  407. goto built;
  408. }
  409. /////////////////////////////////////////////////////////////////
  410. // Load CL file, build CL program object, create CL kernel object
  411. /////////////////////////////////////////////////////////////////
  412. build:
  413. /* If no binary is available, and we have a card that suffers with phatk
  414. * on SDK2.6, use the poclbm kernel instead if one has not been
  415. * selected. */
  416. if (clState->chosen_kernel != KL_POCLBM && chosen_kernel == KL_NONE &&
  417. !strstr(name, "Tahiti") && clState->hasBitAlign &&
  418. (strstr(vbuff, "844.4") /* Linux 64 bit ATI 2.6 SDK */ ||
  419. strstr(vbuff, "851.4") /* Windows 64 bit "" */ ||
  420. strstr(vbuff, "831.4") /* Windows & Linux 32 bit "" */ )) {
  421. applog(LOG_WARNING, "AMD OpenCL SDK 2.6 detected, using poclbm kernel");
  422. applog(LOG_WARNING, "This SDK will give POOR PERFORMANCE on this device");
  423. applog(LOG_WARNING, "To get best performance, remove any .bin files generated by cgminer,");
  424. applog(LOG_WARNING, "downgrade your SDK to 2.1-2.5 and restart cgminer.");
  425. clState->chosen_kernel = KL_POCLBM;
  426. strcpy(filename, POCLBM_KERNNAME".cl");
  427. strcpy(binaryfilename, POCLBM_KERNNAME);
  428. strcat(binaryfilename, name);
  429. strcat(binaryfilename, "bitalign");
  430. strcat(binaryfilename, "v");
  431. sprintf(numbuf, "%d", clState->preferred_vwidth);
  432. strcat(binaryfilename, numbuf);
  433. strcat(binaryfilename, "w");
  434. sprintf(numbuf, "%d", (int)clState->work_size);
  435. strcat(binaryfilename, numbuf);
  436. strcat(binaryfilename, "long");
  437. sprintf(numbuf, "%d", (int)sizeof(long));
  438. strcat(binaryfilename, numbuf);
  439. strcat(binaryfilename, ".bin");
  440. goto loadbin;
  441. }
  442. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  443. if (status != CL_SUCCESS) {
  444. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  445. return NULL;
  446. }
  447. clRetainProgram(clState->program);
  448. if (status != CL_SUCCESS) {
  449. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  450. return NULL;
  451. }
  452. /* create a cl program executable for all the devices specified */
  453. char *CompilerOptions = calloc(1, 256);
  454. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d",
  455. (int)clState->work_size, clState->preferred_vwidth);
  456. applog(LOG_DEBUG, "Setting worksize to %d", clState->work_size);
  457. if (clState->preferred_vwidth > 1)
  458. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  459. if (clState->hasBitAlign) {
  460. strcat(CompilerOptions, " -D BITALIGN");
  461. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  462. if (strstr(name, "Cedar") ||
  463. strstr(name, "Redwood") ||
  464. strstr(name, "Juniper") ||
  465. strstr(name, "Cypress" ) ||
  466. strstr(name, "Hemlock" ) ||
  467. strstr(name, "Caicos" ) ||
  468. strstr(name, "Turks" ) ||
  469. strstr(name, "Barts" ) ||
  470. strstr(name, "Cayman" ) ||
  471. strstr(name, "Antilles" ) ||
  472. strstr(name, "Wrestler" ) ||
  473. strstr(name, "Zacate" ) ||
  474. strstr(name, "WinterPark" ) ||
  475. strstr(name, "BeaverCreek" ))
  476. patchbfi = true;
  477. } else
  478. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  479. if (patchbfi) {
  480. strcat(CompilerOptions, " -D BFI_INT");
  481. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  482. } else
  483. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  484. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  485. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  486. free(CompilerOptions);
  487. if (status != CL_SUCCESS) {
  488. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  489. size_t logSize;
  490. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  491. char *log = malloc(logSize);
  492. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  493. applog(LOG_INFO, "%s", log);
  494. return NULL;
  495. }
  496. prog_built = true;
  497. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  498. if (unlikely(status != CL_SUCCESS)) {
  499. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)");
  500. return NULL;
  501. }
  502. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  503. if (unlikely(status != CL_SUCCESS)) {
  504. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)");
  505. return NULL;
  506. }
  507. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  508. * to iterate over all the binary slots and find where the real program
  509. * is. What the heck is this!? */
  510. for (slot = 0; slot < cpnd; slot++)
  511. if (binary_sizes[slot])
  512. break;
  513. /* copy over all of the generated binaries. */
  514. applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
  515. if (!binary_sizes[slot]) {
  516. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  517. return NULL;
  518. }
  519. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  520. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  521. if (unlikely(status != CL_SUCCESS)) {
  522. applog(LOG_ERR, "Error: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)");
  523. return NULL;
  524. }
  525. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  526. * be hacked in */
  527. if (patchbfi) {
  528. unsigned remaining = binary_sizes[slot];
  529. char *w = binaries[slot];
  530. unsigned int start, length;
  531. /* Find 2nd incidence of .text, and copy the program's
  532. * position and length at a fixed offset from that. Then go
  533. * back and find the 2nd incidence of \x7ELF (rewind by one
  534. * from ELF) and then patch the opcocdes */
  535. if (!advance(&w, &remaining, ".text"))
  536. goto build;
  537. w++; remaining--;
  538. if (!advance(&w, &remaining, ".text")) {
  539. /* 32 bit builds only one ELF */
  540. w--; remaining++;
  541. }
  542. memcpy(&start, w + 285, 4);
  543. memcpy(&length, w + 289, 4);
  544. w = binaries[slot]; remaining = binary_sizes[slot];
  545. if (!advance(&w, &remaining, "ELF"))
  546. goto build;
  547. w++; remaining--;
  548. if (!advance(&w, &remaining, "ELF")) {
  549. /* 32 bit builds only one ELF */
  550. w--; remaining++;
  551. }
  552. w--; remaining++;
  553. w += start; remaining -= start;
  554. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  555. w, remaining);
  556. patch_opcodes(w, length);
  557. status = clReleaseProgram(clState->program);
  558. if (status != CL_SUCCESS) {
  559. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  560. return NULL;
  561. }
  562. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  563. if (status != CL_SUCCESS) {
  564. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  565. return NULL;
  566. }
  567. clRetainProgram(clState->program);
  568. if (status != CL_SUCCESS) {
  569. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  570. return NULL;
  571. }
  572. /* Program needs to be rebuilt */
  573. prog_built = false;
  574. }
  575. free(source);
  576. /* Save the binary to be loaded next time */
  577. binaryfile = fopen(binaryfilename, "wb");
  578. if (!binaryfile) {
  579. /* Not a fatal problem, just means we build it again next time */
  580. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  581. } else {
  582. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  583. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  584. return NULL;
  585. }
  586. fclose(binaryfile);
  587. }
  588. built:
  589. if (binaries[slot])
  590. free(binaries[slot]);
  591. free(binaries);
  592. free(binary_sizes);
  593. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  594. filename, clState->hasBitAlign ? "" : "out", clState->preferred_vwidth, clState->work_size);
  595. if (!prog_built) {
  596. /* create a cl program executable for all the devices specified */
  597. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  598. if (status != CL_SUCCESS) {
  599. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  600. size_t logSize;
  601. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  602. char *log = malloc(logSize);
  603. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  604. applog(LOG_INFO, "%s", log);
  605. return NULL;
  606. }
  607. clRetainProgram(clState->program);
  608. if (status != CL_SUCCESS) {
  609. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  610. return NULL;
  611. }
  612. }
  613. /* get a kernel object handle for a kernel with the given name */
  614. clState->kernel = clCreateKernel(clState->program, "search", &status);
  615. if (status != CL_SUCCESS) {
  616. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  617. return NULL;
  618. }
  619. /////////////////////////////////////////////////////////////////
  620. // Create an OpenCL command queue
  621. /////////////////////////////////////////////////////////////////
  622. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  623. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  624. if (status != CL_SUCCESS) /* Try again without OOE enable */
  625. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  626. if (status != CL_SUCCESS) {
  627. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  628. return NULL;
  629. }
  630. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  631. if (status != CL_SUCCESS) {
  632. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  633. return NULL;
  634. }
  635. return clState;
  636. }
  637. #endif /* HAVE_OPENCL */