ocl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. || strstr(vbuff, "844.4") // Linux 64 bit ATI 2.6 SDK
  311. || strstr(vbuff, "851.4") // Windows 64 bit ""
  312. || strstr(vbuff, "831.4") // Windows & Linux 32 bit ""
  313. )
  314. clState->chosen_kernel = KL_POCLBM;
  315. else
  316. clState->chosen_kernel = KL_PHATK;
  317. } else
  318. clState->chosen_kernel = chosen_kernel;
  319. switch (clState->chosen_kernel) {
  320. case KL_POCLBM:
  321. strcpy(filename, POCLBM_KERNNAME".cl");
  322. strcpy(binaryfilename, POCLBM_KERNNAME);
  323. break;
  324. case KL_NONE: /* Shouldn't happen */
  325. case KL_PHATK:
  326. strcpy(filename, PHATK_KERNNAME".cl");
  327. strcpy(binaryfilename, PHATK_KERNNAME);
  328. break;
  329. case KL_DIAKGCN:
  330. strcpy(filename, DIAKGCN_KERNNAME".cl");
  331. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  332. break;
  333. case KL_DIABLO:
  334. strcpy(filename, DIABLO_KERNNAME".cl");
  335. strcpy(binaryfilename, DIABLO_KERNNAME);
  336. break;
  337. }
  338. FILE *binaryfile;
  339. size_t *binary_sizes;
  340. char **binaries;
  341. int pl;
  342. char *source = file_contents(filename, &pl);
  343. size_t sourceSize[] = {(size_t)pl};
  344. cl_uint slot, cpnd;
  345. slot = cpnd = 0;
  346. if (!source)
  347. return NULL;
  348. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  349. if (unlikely(!binary_sizes)) {
  350. applog(LOG_ERR, "Unable to calloc binary_sizes");
  351. return NULL;
  352. }
  353. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  354. if (unlikely(!binaries)) {
  355. applog(LOG_ERR, "Unable to calloc binaries");
  356. return NULL;
  357. }
  358. strcat(binaryfilename, name);
  359. if (clState->hasBitAlign)
  360. strcat(binaryfilename, "bitalign");
  361. strcat(binaryfilename, "v");
  362. sprintf(numbuf, "%d", clState->preferred_vwidth);
  363. strcat(binaryfilename, numbuf);
  364. strcat(binaryfilename, "w");
  365. sprintf(numbuf, "%d", (int)clState->work_size);
  366. strcat(binaryfilename, numbuf);
  367. strcat(binaryfilename, "long");
  368. sprintf(numbuf, "%d", (int)sizeof(long));
  369. strcat(binaryfilename, numbuf);
  370. strcat(binaryfilename, ".bin");
  371. loadbin:
  372. binaryfile = fopen(binaryfilename, "rb");
  373. if (!binaryfile) {
  374. applog(LOG_DEBUG, "No binary found, generating from source");
  375. } else {
  376. struct stat binary_stat;
  377. if (unlikely(stat(binaryfilename, &binary_stat))) {
  378. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  379. fclose(binaryfile);
  380. goto build;
  381. }
  382. if (!binary_stat.st_size)
  383. goto build;
  384. binary_sizes[slot] = binary_stat.st_size;
  385. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  386. if (unlikely(!binaries[slot])) {
  387. applog(LOG_ERR, "Unable to calloc binaries");
  388. fclose(binaryfile);
  389. return NULL;
  390. }
  391. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  392. applog(LOG_ERR, "Unable to fread binaries");
  393. fclose(binaryfile);
  394. free(binaries[slot]);
  395. goto build;
  396. }
  397. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  398. if (status != CL_SUCCESS) {
  399. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  400. fclose(binaryfile);
  401. free(binaries[slot]);
  402. goto build;
  403. }
  404. clRetainProgram(clState->program);
  405. if (status != CL_SUCCESS) {
  406. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  407. return NULL;
  408. }
  409. fclose(binaryfile);
  410. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  411. goto built;
  412. }
  413. /////////////////////////////////////////////////////////////////
  414. // Load CL file, build CL program object, create CL kernel object
  415. /////////////////////////////////////////////////////////////////
  416. build:
  417. /* If no binary is available, and we have a card that suffers with phatk
  418. * on SDK2.6, use the poclbm kernel instead if one has not been
  419. * selected. */
  420. if (clState->chosen_kernel != KL_POCLBM && chosen_kernel == KL_NONE &&
  421. !strstr(name, "Tahiti") && clState->hasBitAlign &&
  422. (strstr(vbuff, "844.4") /* Linux 64 bit ATI 2.6 SDK */ ||
  423. strstr(vbuff, "851.4") /* Windows 64 bit "" */ ||
  424. strstr(vbuff, "831.4") /* Windows & Linux 32 bit "" */ )) {
  425. applog(LOG_WARNING, "SDK 2.6 detected, using poclbm kernel");
  426. clState->chosen_kernel = KL_POCLBM;
  427. strcpy(filename, POCLBM_KERNNAME".cl");
  428. strcpy(binaryfilename, POCLBM_KERNNAME);
  429. strcat(binaryfilename, name);
  430. strcat(binaryfilename, "bitalign");
  431. strcat(binaryfilename, "v");
  432. sprintf(numbuf, "%d", clState->preferred_vwidth);
  433. strcat(binaryfilename, numbuf);
  434. strcat(binaryfilename, "w");
  435. sprintf(numbuf, "%d", (int)clState->work_size);
  436. strcat(binaryfilename, numbuf);
  437. strcat(binaryfilename, "long");
  438. sprintf(numbuf, "%d", (int)sizeof(long));
  439. strcat(binaryfilename, numbuf);
  440. strcat(binaryfilename, ".bin");
  441. goto loadbin;
  442. }
  443. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  444. if (status != CL_SUCCESS) {
  445. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  446. return NULL;
  447. }
  448. clRetainProgram(clState->program);
  449. if (status != CL_SUCCESS) {
  450. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  451. return NULL;
  452. }
  453. /* create a cl program executable for all the devices specified */
  454. char *CompilerOptions = calloc(1, 256);
  455. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d",
  456. (int)clState->work_size, clState->preferred_vwidth);
  457. applog(LOG_DEBUG, "Setting worksize to %d", clState->work_size);
  458. if (clState->preferred_vwidth > 1)
  459. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  460. if (clState->hasBitAlign) {
  461. strcat(CompilerOptions, " -D BITALIGN");
  462. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  463. if (strstr(name, "Cedar") ||
  464. strstr(name, "Redwood") ||
  465. strstr(name, "Juniper") ||
  466. strstr(name, "Cypress" ) ||
  467. strstr(name, "Hemlock" ) ||
  468. strstr(name, "Caicos" ) ||
  469. strstr(name, "Turks" ) ||
  470. strstr(name, "Barts" ) ||
  471. strstr(name, "Cayman" ) ||
  472. strstr(name, "Antilles" ) ||
  473. strstr(name, "Wrestler" ) ||
  474. strstr(name, "Zacate" ) ||
  475. strstr(name, "WinterPark" ) ||
  476. strstr(name, "BeaverCreek" ))
  477. patchbfi = true;
  478. } else
  479. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  480. if (patchbfi) {
  481. strcat(CompilerOptions, " -D BFI_INT");
  482. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  483. } else
  484. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  485. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  486. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  487. free(CompilerOptions);
  488. if (status != CL_SUCCESS) {
  489. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  490. size_t logSize;
  491. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  492. char *log = malloc(logSize);
  493. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  494. applog(LOG_INFO, "%s", log);
  495. return NULL;
  496. }
  497. prog_built = true;
  498. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  499. if (unlikely(status != CL_SUCCESS)) {
  500. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)");
  501. return NULL;
  502. }
  503. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  504. if (unlikely(status != CL_SUCCESS)) {
  505. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)");
  506. return NULL;
  507. }
  508. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  509. * to iterate over all the binary slots and find where the real program
  510. * is. What the heck is this!? */
  511. for (slot = 0; slot < cpnd; slot++)
  512. if (binary_sizes[slot])
  513. break;
  514. /* copy over all of the generated binaries. */
  515. applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
  516. if (!binary_sizes[slot]) {
  517. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  518. return NULL;
  519. }
  520. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  521. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  522. if (unlikely(status != CL_SUCCESS)) {
  523. applog(LOG_ERR, "Error: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)");
  524. return NULL;
  525. }
  526. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  527. * be hacked in */
  528. if (patchbfi) {
  529. unsigned remaining = binary_sizes[slot];
  530. char *w = binaries[slot];
  531. unsigned int start, length;
  532. /* Find 2nd incidence of .text, and copy the program's
  533. * position and length at a fixed offset from that. Then go
  534. * back and find the 2nd incidence of \x7ELF (rewind by one
  535. * from ELF) and then patch the opcocdes */
  536. if (!advance(&w, &remaining, ".text"))
  537. goto build;
  538. w++; remaining--;
  539. if (!advance(&w, &remaining, ".text")) {
  540. /* 32 bit builds only one ELF */
  541. w--; remaining++;
  542. }
  543. memcpy(&start, w + 285, 4);
  544. memcpy(&length, w + 289, 4);
  545. w = binaries[slot]; remaining = binary_sizes[slot];
  546. if (!advance(&w, &remaining, "ELF"))
  547. goto build;
  548. w++; remaining--;
  549. if (!advance(&w, &remaining, "ELF")) {
  550. /* 32 bit builds only one ELF */
  551. w--; remaining++;
  552. }
  553. w--; remaining++;
  554. w += start; remaining -= start;
  555. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  556. w, remaining);
  557. patch_opcodes(w, length);
  558. status = clReleaseProgram(clState->program);
  559. if (status != CL_SUCCESS) {
  560. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  561. return NULL;
  562. }
  563. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  564. if (status != CL_SUCCESS) {
  565. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  566. return NULL;
  567. }
  568. clRetainProgram(clState->program);
  569. if (status != CL_SUCCESS) {
  570. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  571. return NULL;
  572. }
  573. /* Program needs to be rebuilt */
  574. prog_built = false;
  575. }
  576. free(source);
  577. /* Save the binary to be loaded next time */
  578. binaryfile = fopen(binaryfilename, "wb");
  579. if (!binaryfile) {
  580. /* Not a fatal problem, just means we build it again next time */
  581. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  582. } else {
  583. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  584. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  585. return NULL;
  586. }
  587. fclose(binaryfile);
  588. }
  589. built:
  590. if (binaries[slot])
  591. free(binaries[slot]);
  592. free(binaries);
  593. free(binary_sizes);
  594. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  595. filename, clState->hasBitAlign ? "" : "out", clState->preferred_vwidth, clState->work_size);
  596. if (!prog_built) {
  597. /* create a cl program executable for all the devices specified */
  598. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  599. if (status != CL_SUCCESS) {
  600. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  601. size_t logSize;
  602. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  603. char *log = malloc(logSize);
  604. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  605. applog(LOG_INFO, "%s", log);
  606. return NULL;
  607. }
  608. clRetainProgram(clState->program);
  609. if (status != CL_SUCCESS) {
  610. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  611. return NULL;
  612. }
  613. }
  614. /* get a kernel object handle for a kernel with the given name */
  615. clState->kernel = clCreateKernel(clState->program, "search", &status);
  616. if (status != CL_SUCCESS) {
  617. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  618. return NULL;
  619. }
  620. /////////////////////////////////////////////////////////////////
  621. // Create an OpenCL command queue
  622. /////////////////////////////////////////////////////////////////
  623. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  624. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  625. if (status != CL_SUCCESS) /* Try again without OOE enable */
  626. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  627. if (status != CL_SUCCESS) {
  628. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  629. return NULL;
  630. }
  631. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  632. if (status != CL_SUCCESS) {
  633. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  634. return NULL;
  635. }
  636. return clState;
  637. }
  638. #endif /* HAVE_OPENCL */