ocl.c 23 KB

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