ocl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #define _GNU_SOURCE
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include <pthread.h>
  13. #include "findnonce.h"
  14. #include "ocl.h"
  15. extern int opt_vectors;
  16. extern int opt_worksize;
  17. char *file_contents(const char *filename, int *length)
  18. {
  19. FILE *f = fopen(filename, "r");
  20. void *buffer;
  21. if (!f) {
  22. applog(LOG_ERR, "Unable to open %s for reading", filename);
  23. return NULL;
  24. }
  25. fseek(f, 0, SEEK_END);
  26. *length = ftell(f);
  27. fseek(f, 0, SEEK_SET);
  28. buffer = malloc(*length+1);
  29. *length = fread(buffer, 1, *length, f);
  30. fclose(f);
  31. ((char*)buffer)[*length] = '\0';
  32. return (char*)buffer;
  33. }
  34. int clDevicesNum() {
  35. cl_int status = 0;
  36. cl_uint numPlatforms;
  37. cl_platform_id platform = NULL;
  38. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  39. if (status != CL_SUCCESS)
  40. {
  41. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  42. return -1;
  43. }
  44. if (numPlatforms > 0)
  45. {
  46. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  47. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  48. if (status != CL_SUCCESS)
  49. {
  50. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  51. return -1;
  52. }
  53. unsigned int i;
  54. for(i=0; i < numPlatforms; ++i)
  55. {
  56. char pbuff[100];
  57. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  58. if (status != CL_SUCCESS)
  59. {
  60. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  61. free(platforms);
  62. return -1;
  63. }
  64. platform = platforms[i];
  65. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  66. {
  67. break;
  68. }
  69. }
  70. free(platforms);
  71. }
  72. if (platform == NULL) {
  73. perror("NULL platform found!\n");
  74. return -1;
  75. }
  76. cl_uint numDevices;
  77. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  78. if (status != CL_SUCCESS)
  79. {
  80. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  81. return -1;
  82. }
  83. return numDevices;
  84. }
  85. static int advance(char **area, unsigned *remaining, const char *marker)
  86. {
  87. char *find = memmem(*area, *remaining, marker, strlen(marker));
  88. if (!find) {
  89. if (opt_debug)
  90. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  91. return 0;
  92. }
  93. *remaining -= find - *area;
  94. *area = find;
  95. return 1;
  96. }
  97. #define OP3_INST_BFE_UINT 4ULL
  98. #define OP3_INST_BFE_INT 5ULL
  99. #define OP3_INST_BFI_INT 6ULL
  100. #define OP3_INST_BIT_ALIGN_INT 12ULL
  101. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  102. void patch_opcodes(char *w, unsigned remaining)
  103. {
  104. uint64_t *opcode = (uint64_t *)w;
  105. int patched = 0;
  106. int count_bfe_int = 0;
  107. int count_bfe_uint = 0;
  108. int count_byte_align = 0;
  109. while (42)
  110. {
  111. int clamp = (*opcode >> (32 + 31)) & 0x1;
  112. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  113. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  114. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  115. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  116. int pred_sel = (*opcode >> 29) & 0x3;
  117. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  118. if (alu_inst == OP3_INST_BFE_INT) {
  119. count_bfe_int++;
  120. } else if (alu_inst == OP3_INST_BFE_UINT) {
  121. count_bfe_uint++;
  122. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  123. count_byte_align++;
  124. // patch this instruction to BFI_INT
  125. *opcode &= 0xfffc1fffffffffffUL;
  126. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  127. patched++;
  128. }
  129. }
  130. if (remaining <= 8) {
  131. break;
  132. }
  133. opcode++;
  134. remaining -= 8;
  135. }
  136. if (opt_debug) {
  137. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  138. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  139. count_bfe_int, count_bfe_uint, count_byte_align);
  140. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  141. }
  142. }
  143. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  144. {
  145. int patchbfi = 0;
  146. cl_int status = 0;
  147. unsigned int i;
  148. _clState *clState = calloc(1, sizeof(_clState));
  149. cl_uint numPlatforms;
  150. cl_platform_id platform = NULL;
  151. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  152. if (status != CL_SUCCESS)
  153. {
  154. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  155. return NULL;
  156. }
  157. if (numPlatforms > 0)
  158. {
  159. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  160. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  161. if (status != CL_SUCCESS)
  162. {
  163. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  164. return NULL;
  165. }
  166. for(i = 0; i < numPlatforms; ++i)
  167. {
  168. char pbuff[100];
  169. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  170. if (status != CL_SUCCESS)
  171. {
  172. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  173. free(platforms);
  174. return NULL;
  175. }
  176. platform = platforms[i];
  177. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  178. {
  179. break;
  180. }
  181. }
  182. free(platforms);
  183. }
  184. if (platform == NULL) {
  185. perror("NULL platform found!\n");
  186. return NULL;
  187. }
  188. cl_uint numDevices;
  189. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  190. if (status != CL_SUCCESS)
  191. {
  192. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  193. return NULL;
  194. }
  195. cl_device_id *devices;
  196. if (numDevices > 0 ) {
  197. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  198. /* Now, get the device list data */
  199. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  200. if (status != CL_SUCCESS)
  201. {
  202. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  203. return NULL;
  204. }
  205. applog(LOG_INFO, "List of devices:");
  206. unsigned int i;
  207. for(i=0; i<numDevices; i++) {
  208. char pbuff[100];
  209. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  210. if (status != CL_SUCCESS)
  211. {
  212. applog(LOG_ERR, "Error: Getting Device Info");
  213. return NULL;
  214. }
  215. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  216. }
  217. if (gpu < numDevices) {
  218. char pbuff[100];
  219. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  220. if (status != CL_SUCCESS)
  221. {
  222. applog(LOG_ERR, "Error: Getting Device Info");
  223. return NULL;
  224. }
  225. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  226. strncpy(name, pbuff, nameSize);
  227. } else {
  228. applog(LOG_ERR, "Invalid GPU %i", gpu);
  229. return NULL;
  230. }
  231. } else return NULL;
  232. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  233. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  234. if (status != CL_SUCCESS)
  235. {
  236. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  237. return NULL;
  238. }
  239. /* Check for BFI INT support. Hopefully people don't mix devices with
  240. * and without it! */
  241. char * extensions = malloc(1024);
  242. const char * camo = "cl_amd_media_ops";
  243. char *find;
  244. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  245. if (status != CL_SUCCESS) {
  246. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  247. return NULL;
  248. }
  249. find = strstr(extensions, camo);
  250. if (find)
  251. clState->hasBitAlign = patchbfi = 1;
  252. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  253. if (status != CL_SUCCESS) {
  254. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  255. return NULL;
  256. }
  257. if (opt_debug)
  258. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  259. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  260. if (status != CL_SUCCESS) {
  261. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  262. return NULL;
  263. }
  264. if (opt_debug)
  265. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  266. /* Some nvidia cards report 1024 but fail when set larger than 512 !? */
  267. if (clState->max_work_size > 512)
  268. clState->max_work_size = 512;
  269. /////////////////////////////////////////////////////////////////
  270. // Load CL file, build CL program object, create CL kernel object
  271. /////////////////////////////////////////////////////////////////
  272. /* Load a different kernel depending on whether it supports
  273. * cl_amd_media_ops or not */
  274. char filename[10];
  275. if (clState->hasBitAlign)
  276. strcpy(filename, "phatk.cl");
  277. else
  278. strcpy(filename, "poclbm.cl");
  279. int pl;
  280. char *source, *rawsource = file_contents(filename, &pl);
  281. size_t sourceSize[] = {(size_t)pl};
  282. source = malloc(pl);
  283. retry:
  284. if (!source) {
  285. applog(LOG_ERR, "Unable to malloc source");
  286. return NULL;
  287. }
  288. memcpy(source, rawsource, pl);
  289. /* For some reason 2 vectors is still better even if the card says
  290. * otherwise */
  291. if (clState->preferred_vwidth > 1)
  292. clState->preferred_vwidth = 2;
  293. if (opt_vectors)
  294. clState->preferred_vwidth = opt_vectors;
  295. if (opt_worksize && opt_worksize <= clState->max_work_size)
  296. clState->work_size = opt_worksize;
  297. else
  298. clState->work_size = clState->max_work_size / clState->preferred_vwidth;
  299. /* Patch the source file with the preferred_vwidth */
  300. if (clState->preferred_vwidth > 1) {
  301. char *find = strstr(source, "VECTORSX");
  302. if (unlikely(!find)) {
  303. applog(LOG_ERR, "Unable to find VECTORSX in source");
  304. return NULL;
  305. }
  306. find += 7; // "VECTORS"
  307. if (clState->preferred_vwidth == 2)
  308. strncpy(find, "2", 1);
  309. else
  310. strncpy(find, "4", 1);
  311. if (opt_debug)
  312. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  313. }
  314. /* Patch the source file defining BITALIGN */
  315. if (clState->hasBitAlign) {
  316. char *find = strstr(source, "BITALIGNX");
  317. if (unlikely(!find)) {
  318. applog(LOG_ERR, "Unable to find BITALIGNX in source");
  319. return NULL;
  320. }
  321. find += 8; // "BITALIGN"
  322. strncpy(find, " ", 1);
  323. if (opt_debug)
  324. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BITALIGN");
  325. } else if (opt_debug)
  326. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BITALIGN patch");
  327. if (patchbfi) {
  328. char *find = strstr(source, "BFI_INTX");
  329. if (unlikely(!find)) {
  330. applog(LOG_ERR, "Unable to find BFI_INTX in source");
  331. return NULL;
  332. }
  333. find += 7; // "BFI_INT"
  334. strncpy(find, " ", 1);
  335. if (opt_debug)
  336. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BFI_INT");
  337. } else if (opt_debug)
  338. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BFI_INT patch");
  339. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  340. if (status != CL_SUCCESS)
  341. {
  342. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  343. return NULL;
  344. }
  345. /* create a cl program executable for all the devices specified */
  346. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  347. if (status != CL_SUCCESS)
  348. {
  349. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  350. size_t logSize;
  351. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  352. char *log = malloc(logSize);
  353. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  354. applog(LOG_INFO, "%s", log);
  355. return NULL;
  356. }
  357. /* Patch the kernel if the hardware supports BFI_INT */
  358. if (patchbfi) {
  359. size_t nDevices;
  360. size_t * binary_sizes;
  361. char ** binaries;
  362. int err;
  363. /* figure out number of devices and the sizes of the binary for each device. */
  364. err = clGetProgramInfo( clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(nDevices), &nDevices, NULL );
  365. binary_sizes = (size_t *)malloc( sizeof(size_t)*nDevices );
  366. err = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*nDevices, binary_sizes, NULL );
  367. /* copy over all of the generated binaries. */
  368. binaries = (char **)malloc( sizeof(char *)*nDevices );
  369. if (opt_debug)
  370. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  371. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu] );
  372. err = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*nDevices, binaries, NULL );
  373. unsigned remaining = binary_sizes[gpu];
  374. char *w = binaries[gpu];
  375. unsigned int start, length;
  376. /* Find 2nd incidence of .text, and copy the program's
  377. * position and length at a fixed offset from that. Then go
  378. * back and find the 2nd incidence of \x7ELF (rewind by one
  379. * from ELF) and then patch the opcocdes */
  380. if (!advance(&w, &remaining, ".text"))
  381. {patchbfi = 0; goto retry;}
  382. w++; remaining--;
  383. if (!advance(&w, &remaining, ".text")) {
  384. /* 32 bit builds only one ELF */
  385. w--; remaining++;
  386. }
  387. memcpy(&start, w + 285, 4);
  388. memcpy(&length, w + 289, 4);
  389. w = binaries[gpu]; remaining = binary_sizes[gpu];
  390. if (!advance(&w, &remaining, "ELF"))
  391. {patchbfi = 0; goto retry;}
  392. w++; remaining--;
  393. if (!advance(&w, &remaining, "ELF")) {
  394. /* 32 bit builds only one ELF */
  395. w--; remaining++;
  396. }
  397. w--; remaining++;
  398. w += start; remaining -= start;
  399. if (opt_debug)
  400. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  401. w, remaining);
  402. patch_opcodes(w, length);
  403. status = clReleaseProgram(clState->program);
  404. if (status != CL_SUCCESS)
  405. {
  406. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  407. return NULL;
  408. }
  409. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  410. if (status != CL_SUCCESS)
  411. {
  412. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  413. return NULL;
  414. }
  415. }
  416. free(source);
  417. free(rawsource);
  418. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT patching, %d vectors and worksize %d",
  419. filename, patchbfi ? "" : "out", clState->preferred_vwidth, clState->work_size);
  420. /* create a cl program executable for all the devices specified */
  421. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  422. if (status != CL_SUCCESS)
  423. {
  424. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  425. size_t logSize;
  426. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  427. char *log = malloc(logSize);
  428. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  429. applog(LOG_INFO, "%s", log);
  430. return NULL;
  431. }
  432. /* get a kernel object handle for a kernel with the given name */
  433. clState->kernel = clCreateKernel(clState->program, "search", &status);
  434. if (status != CL_SUCCESS)
  435. {
  436. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  437. return NULL;
  438. }
  439. /////////////////////////////////////////////////////////////////
  440. // Create an OpenCL command queue
  441. /////////////////////////////////////////////////////////////////
  442. clState->commandQueue = clCreateCommandQueue( clState->context, devices[gpu], 0, &status);
  443. if (status != CL_SUCCESS)
  444. {
  445. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  446. return NULL;
  447. }
  448. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, BUFFERSIZE, NULL, &status);
  449. if (status != CL_SUCCESS) {
  450. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  451. return NULL;
  452. }
  453. return clState;
  454. }