main-intel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/time.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #define __STDC_FORMAT_MACROS
  8. #include <inttypes.h>
  9. #include <stdbool.h>
  10. #define CL_TARGET_OPENCL_VERSION 200
  11. #ifdef __APPLE__
  12. #include <OpenCL/opencl.h>
  13. #include <OpenCL/cl_platform.h>
  14. #else
  15. #include <CL/cl.h>
  16. #include <CL/cl_platform.h>
  17. #endif
  18. #include "clutil.h"
  19. #ifdef _WIN64
  20. #include "boinc_win.h"
  21. #else
  22. #ifdef _WIN32
  23. #include "boinc_win.h"
  24. #endif
  25. #endif
  26. #include "boinc_api.h"
  27. #include "boinc_opencl.h"
  28. #define KERNEL_BUFFER_SIZE (0x4000)
  29. #define MAX_SEED_BUFFER_SIZE (0x10000)
  30. int main(int argc, char * argv[]){
  31. BOINC_OPTIONS options;
  32. boinc_options_defaults(options);
  33. options.normal_thread_priority = true;
  34. boinc_init_options(&options);
  35. boinc_set_min_checkpoint_period(30);
  36. //boinc_init();
  37. int gpuIndex = 0; // Won't do anything for now
  38. cl_ulong start = 0;
  39. cl_ulong end = 0;
  40. cl_ulong chunkSeed = 0;
  41. int chunkSeedBottom4Bits = 0;
  42. int chunkSeedBit5 = 0;
  43. int neighbor1 = 0;
  44. int neighbor2 = 0;
  45. int neighbor3 = 0;
  46. int diagonalIndex = 0;
  47. int cactusHeight = 0;
  48. int retval = 0;
  49. int floor_level = 63;
  50. char *strend;
  51. size_t seedbuffer_size;
  52. struct checkpoint_vars {
  53. cl_ulong offset;
  54. cl_ulong start;
  55. int block;
  56. double elapsed_chkpoint;
  57. int total_seed_count;
  58. };
  59. if (argc % 2 != 1) {
  60. printf("Failed to parse arguments\n");
  61. exit(EXIT_FAILURE);
  62. }
  63. for (int i = 1; i < argc; i += 2) {
  64. const char *param = argv[i];
  65. if (strcmp(param, "-d") == 0 || strcmp(param, "--device") == 0) {
  66. gpuIndex = atoi(argv[i + 1]);
  67. } else if (strcmp(param, "-s") == 0 || strcmp(param, "--start") == 0) {
  68. sscanf(argv[i + 1], "%" SCNd64, &start);
  69. } else if (strcmp(param, "-e") == 0 || strcmp(param, "--end") == 0) {
  70. sscanf(argv[i + 1], "%" SCNd64, &end);
  71. } else if (strcmp(param, "-cs") == 0 || strcmp(param, "--chunkseed") == 0) {
  72. sscanf(argv[i + 1], "%" SCNd64, &chunkSeed);
  73. chunkSeedBottom4Bits = (int)(chunkSeed & 15U);
  74. chunkSeedBit5 = (int)((chunkSeed >> 4U) & 1U);
  75. } else if (strcmp(param, "-n1") == 0 || strcmp(param, "--neighbor1") == 0) {
  76. neighbor1 = atoi(argv[i + 1]);
  77. } else if (strcmp(param, "-n2") == 0 || strcmp(param, "--neighbor2") == 0) {
  78. neighbor2 = atoi(argv[i + 1]);
  79. } else if (strcmp(param, "-n3") == 0 || strcmp(param, "--neighbor3") == 0) {
  80. neighbor3 = atoi(argv[i + 1]);
  81. } else if (strcmp(param, "-di") == 0 || strcmp(param, "--diagonalindex") == 0) {
  82. diagonalIndex = atoi(argv[i + 1]);
  83. } else if (strcmp(param, "-ch") == 0 || strcmp(param, "--cactusheight") == 0) {
  84. cactusHeight = atoi(argv[i + 1]);
  85. } else if (strcmp(param, "-f") == 0 || strcmp(param, "--floorlevel") == 0){
  86. floor_level = atoi(argv[i + 1]);
  87. } else {
  88. printf("Unknown parameter: %s\n", param);
  89. }
  90. }
  91. fprintf(stderr,"Received work unit: %" SCNd64 "\n", chunkSeed);
  92. fprintf(stderr,"Data: n1: %d, n2: %d, n3: %d, di: %d, ch: %d, f: %d\n",
  93. neighbor1,
  94. neighbor2,
  95. neighbor3,
  96. diagonalIndex,
  97. cactusHeight,
  98. floor_level);
  99. int arguments[10] = {
  100. 0,
  101. 0,
  102. 0,
  103. neighbor1,
  104. neighbor2,
  105. neighbor3,
  106. diagonalIndex,
  107. cactusHeight,
  108. chunkSeedBottom4Bits,
  109. chunkSeedBit5
  110. };
  111. fflush(stderr);
  112. FILE *kernel_file = boinc_fopen("kaktwoos.cl", "r");
  113. if (!kernel_file) {
  114. printf("Failed to open kernel");
  115. exit(1);
  116. }
  117. char *kernel_src = (char *)malloc(KERNEL_BUFFER_SIZE);
  118. size_t kernel_length = fread(kernel_src, 1, KERNEL_BUFFER_SIZE, kernel_file);
  119. fclose(kernel_file);
  120. cl_platform_id platform_id = NULL;
  121. cl_device_id device_ids;
  122. cl_int err;
  123. cl_uint num_devices_standalone;
  124. num_devices_standalone = 1;
  125. cl_uint num_entries;
  126. num_entries = 1;
  127. // Third arg is 3 for Intel gpu
  128. retval = boinc_get_opencl_ids(argc, argv, 3, &device_ids, &platform_id);
  129. if (retval) {
  130. //Probably standalone mode
  131. fprintf(stderr, "Error: boinc_get_opencl_ids() failed with error %d\n", retval);
  132. retval = clGetPlatformIDs(num_entries, &platform_id, &num_devices_standalone);
  133. if (retval) {
  134. fprintf(stderr, "Error: clGetPlatformIDs() failed with error %d\n", retval);
  135. return 1;
  136. }
  137. retval = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, num_entries, &device_ids, &num_devices_standalone);
  138. if (retval) {
  139. fprintf(stderr, "Error: clGetDeviceIDs() failed with error %d\n", retval);
  140. return 1;
  141. }
  142. }
  143. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, 0};
  144. cl_context context = clCreateContext(cps, 1, &device_ids, NULL, NULL, &err);
  145. check(err, "clCreateContext ");
  146. cl_command_queue command_queue = clCreateCommandQueueWithProperties(context, device_ids, 0, &err);
  147. check(err, "clCreateCommandQueueWithProperties ");
  148. seedbuffer_size = 0x40 * sizeof(cl_ulong);
  149. // 16 Kb of memory for seeds
  150. cl_mem seeds = clCreateBuffer(context, CL_MEM_READ_WRITE, seedbuffer_size , NULL, &err);
  151. check(err, "clCreateBuffer (seeds) ");
  152. cl_mem data = clCreateBuffer(context, CL_MEM_READ_ONLY, 10 * sizeof(int), NULL, &err);
  153. check(err, "clCreateBuffer (data) ");
  154. cl_program program = clCreateProgramWithSource(
  155. context,
  156. 1,
  157. (const char **)&kernel_src,
  158. &kernel_length,
  159. &err);
  160. check(err, "clCreateProgramWithSource ");
  161. char* opt = (char*)malloc(20*sizeof(char));
  162. sprintf(opt, "-DFLOOR_LEVEL=%d", floor_level);
  163. printf("Test: %s if there's nothing before this then fucking hell", opt);
  164. err = clBuildProgram(program, 1, &device_ids, opt, NULL, NULL);
  165. if (err != CL_SUCCESS) {
  166. size_t len;
  167. clGetProgramBuildInfo(program, device_ids, CL_PROGRAM_BUILD_LOG, 0, NULL, &len);
  168. char *info = (char *)malloc(len);
  169. clGetProgramBuildInfo(program, device_ids, CL_PROGRAM_BUILD_LOG, len, info, NULL);
  170. printf("%s\n", info);
  171. free(info);
  172. }
  173. cl_kernel kernel = clCreateKernel(program, "crack", &err);
  174. check(err, "clCreateKernel ");
  175. check(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&data), "clSetKernelArg (0) ");
  176. check(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&seeds), "clSetKernelArg (1) ");
  177. size_t work_unit_size = 1048576;
  178. size_t block_size = 256;
  179. arguments[1] = work_unit_size;
  180. cl_ulong offset = start;
  181. int block = 0;
  182. int total_seed_count = 0;
  183. int chkpoint_ready = 0;
  184. double seedrange = (end - start);
  185. cl_ulong found_seeds[MAX_SEED_BUFFER_SIZE];
  186. clock_t start_time, end_time, elapsed_chkpoint;
  187. start_time = clock();
  188. FILE *checkpoint_data = boinc_fopen("kaktpoint.txt", "rb");
  189. if (!checkpoint_data) {
  190. fprintf(stderr,"No checkpoint to load\n");
  191. }
  192. else {
  193. boinc_begin_critical_section();
  194. struct checkpoint_vars data_store;
  195. fread(&data_store, sizeof(data_store), 1, checkpoint_data);
  196. offset = data_store.offset;
  197. start = data_store.start;
  198. block = data_store.block;
  199. elapsed_chkpoint = data_store.elapsed_chkpoint;
  200. total_seed_count = data_store.total_seed_count;
  201. fread(found_seeds, sizeof(cl_ulong), total_seed_count, checkpoint_data);
  202. fprintf(stderr,"Checkpoint loaded, task time %d s \n", elapsed_chkpoint);
  203. fclose(checkpoint_data);
  204. boinc_end_critical_section();
  205. }
  206. while (offset < end) {
  207. arguments[0] = block + start / work_unit_size;
  208. check(clEnqueueWriteBuffer(command_queue, data, CL_TRUE, 0, 10 * sizeof(int), arguments, 0, NULL, NULL), "clEnqueueWriteBuffer ");
  209. check(clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &work_unit_size, &block_size, 0, NULL, NULL), "clEnqueueNDRangeKernel ");
  210. int *data_out = (int *)malloc(sizeof(int) * 10);
  211. check(clEnqueueReadBuffer(command_queue, data, CL_TRUE, 0, sizeof(int) * 10, data_out, 0, NULL, NULL), "clEnqueueReadBuffer (data) ");
  212. int seed_count = data_out[2];
  213. seedbuffer_size = sizeof(cl_ulong) + sizeof(cl_ulong) * seed_count;
  214. cl_ulong *result = (cl_ulong *)malloc(sizeof(cl_ulong) + sizeof(cl_ulong) * seed_count);
  215. check(clEnqueueReadBuffer(command_queue, seeds, CL_TRUE, 0, seedbuffer_size, result, 0, NULL, NULL), "clEnqueueReadBuffer (seeds) ");
  216. end_time = clock();
  217. for (int i = 0; i < seed_count; i++) {
  218. fprintf(stderr," Found seed: %"SCNd64 ", %llu, height: %d\n",
  219. result[i],
  220. result[i] & ((1ULL << 48ULL) - 1ULL),
  221. (int)(result[i] >> 58ULL));
  222. fprintf(stderr, "%"SCNd64 "\n", (cl_ulong)result[i]);
  223. found_seeds[total_seed_count++] = result[i];
  224. }
  225. double elapsed = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  226. offset += work_unit_size;
  227. block++;
  228. chkpoint_ready++;
  229. if (chkpoint_ready >= 200 || boinc_time_to_checkpoint()){ // 200 for 0.2bil seeds before checkpoint
  230. boinc_begin_critical_section(); // Boinc should not interrupt this
  231. boinc_delete_file("kaktpoint.txt");
  232. FILE *checkpoint_data = boinc_fopen("kaktpoint.txt", "wb");
  233. struct checkpoint_vars data_store;
  234. data_store.offset = offset;
  235. data_store.start = start;
  236. data_store.block = block;
  237. data_store.elapsed_chkpoint = (elapsed_chkpoint + (double)(end_time - start_time) / CLOCKS_PER_SEC);
  238. data_store.total_seed_count = total_seed_count;
  239. fwrite(&data_store, sizeof(data_store), 1, checkpoint_data);
  240. fwrite(found_seeds, sizeof(cl_ulong), total_seed_count, checkpoint_data);
  241. chkpoint_ready = 0;
  242. fclose(checkpoint_data);
  243. double fraction_done = ((offset - start) / (seedrange));
  244. boinc_fraction_done(fraction_done);
  245. boinc_end_critical_section();
  246. boinc_checkpoint_completed(); // Checkpointing completed
  247. }
  248. free(result);
  249. free(data_out);
  250. } // End of seed feed and processing loop
  251. boinc_begin_critical_section();
  252. double elapsed = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  253. fprintf(stderr,"Speed: %.2fm/s \n", (offset - start) / (elapsed_chkpoint + elapsed) / 1000000);
  254. fprintf(stderr,"Done\n");
  255. fprintf(stderr,"Processed %"SCNd64 " seeds in %f seconds\n",
  256. end - start,
  257. elapsed_chkpoint + ((double)(end_time - start_time) / CLOCKS_PER_SEC));
  258. fprintf(stderr,"Found seeds: \n");
  259. for (int i = 0; i < total_seed_count; i++) {
  260. fprintf(stderr," %"SCNd64 "\n", found_seeds[i]);
  261. }
  262. boinc_delete_file("kaktpoint.txt");
  263. check(clFlush(command_queue), "clFlush ");
  264. check(clFinish(command_queue), "clFinish ");
  265. check(clReleaseKernel(kernel), "clReleaseKernel ");
  266. check(clReleaseProgram(program), "clReleaseProgram ");
  267. check(clReleaseMemObject(seeds), "clReleaseMemObject (seeds) ");
  268. check(clReleaseMemObject(data), "clReleaseMemObject (data) ");
  269. check(clReleaseCommandQueue(command_queue), "clReleaseCommandQueue ");
  270. check(clReleaseContext(context), "clReleaseContext ");
  271. fflush(stderr);
  272. boinc_end_critical_section();
  273. boinc_finish(0);
  274. }