ocl.c 13 KB

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