ocl.c 22 KB

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