ocl.c 13 KB

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