main.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 1 for Nvidia
  128. retval = boinc_get_opencl_ids(argc, argv, 1, &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. err = clBuildProgram(program, 1, &device_ids, opt, NULL, NULL);
  164. if (err != CL_SUCCESS) {
  165. size_t len;
  166. clGetProgramBuildInfo(program, device_ids, CL_PROGRAM_BUILD_LOG, 0, NULL, &len);
  167. char *info = (char *)malloc(len);
  168. clGetProgramBuildInfo(program, device_ids, CL_PROGRAM_BUILD_LOG, len, info, NULL);
  169. printf("%s\n", info);
  170. free(info);
  171. }
  172. cl_kernel kernel = clCreateKernel(program, "crack", &err);
  173. check(err, "clCreateKernel ");
  174. check(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&data), "clSetKernelArg (0) ");
  175. check(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&seeds), "clSetKernelArg (1) ");
  176. size_t work_unit_size = 1048576;
  177. size_t block_size = 256;
  178. arguments[1] = work_unit_size;
  179. cl_ulong offset = start;
  180. int block = 0;
  181. int total_seed_count = 0;
  182. int chkpoint_ready = 0;
  183. double seedrange = (end - start);
  184. cl_ulong found_seeds[MAX_SEED_BUFFER_SIZE];
  185. clock_t start_time, end_time, elapsed_chkpoint;
  186. start_time = clock();
  187. FILE *checkpoint_data = boinc_fopen("kaktpoint.txt", "rb");
  188. if (!checkpoint_data) {
  189. fprintf(stderr,"No checkpoint to load\n");
  190. }
  191. else {
  192. boinc_begin_critical_section();
  193. struct checkpoint_vars data_store;
  194. fread(&data_store, sizeof(data_store), 1, checkpoint_data);
  195. offset = data_store.offset;
  196. start = data_store.start;
  197. block = data_store.block;
  198. elapsed_chkpoint = data_store.elapsed_chkpoint;
  199. total_seed_count = data_store.total_seed_count;
  200. fread(found_seeds, sizeof(cl_ulong), total_seed_count, checkpoint_data);
  201. fprintf(stderr,"Checkpoint loaded, task time %d s \n", elapsed_chkpoint);
  202. fclose(checkpoint_data);
  203. boinc_end_critical_section();
  204. }
  205. while (offset < end) {
  206. arguments[0] = block + start / work_unit_size;
  207. check(clEnqueueWriteBuffer(command_queue, data, CL_TRUE, 0, 10 * sizeof(int), arguments, 0, NULL, NULL), "clEnqueueWriteBuffer ");
  208. check(clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &work_unit_size, &block_size, 0, NULL, NULL), "clEnqueueNDRangeKernel ");
  209. int *data_out = (int *)malloc(sizeof(int) * 10);
  210. check(clEnqueueReadBuffer(command_queue, data, CL_TRUE, 0, sizeof(int) * 10, data_out, 0, NULL, NULL), "clEnqueueReadBuffer (data) ");
  211. int seed_count = data_out[2];
  212. seedbuffer_size = sizeof(cl_ulong) + sizeof(cl_ulong) * seed_count;
  213. cl_ulong *result = (cl_ulong *)malloc(sizeof(cl_ulong) + sizeof(cl_ulong) * seed_count);
  214. check(clEnqueueReadBuffer(command_queue, seeds, CL_TRUE, 0, seedbuffer_size, result, 0, NULL, NULL), "clEnqueueReadBuffer (seeds) ");
  215. end_time = clock();
  216. for (int i = 0; i < seed_count; i++) {
  217. fprintf(stderr," Found seed: %"SCNd64 ", %llu, height: %d\n",
  218. result[i],
  219. result[i] & ((1ULL << 48ULL) - 1ULL),
  220. (int)(result[i] >> 58ULL));
  221. fprintf(stderr, "%"SCNd64 "\n", (cl_ulong)result[i]);
  222. found_seeds[total_seed_count++] = result[i];
  223. }
  224. double elapsed = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  225. offset += work_unit_size;
  226. block++;
  227. chkpoint_ready++;
  228. if (chkpoint_ready >= 200 || boinc_time_to_checkpoint() ){ // 200 for 0.2bil seeds before checkpoint
  229. boinc_begin_critical_section(); // Boinc should not interrupt this
  230. boinc_delete_file("kaktpoint.txt");
  231. FILE *checkpoint_data = boinc_fopen("kaktpoint.txt", "wb");
  232. struct checkpoint_vars data_store;
  233. data_store.offset = offset;
  234. data_store.start = start;
  235. data_store.block = block;
  236. data_store.elapsed_chkpoint = (elapsed_chkpoint + (double)(end_time - start_time) / CLOCKS_PER_SEC);
  237. data_store.total_seed_count = total_seed_count;
  238. fwrite(&data_store, sizeof(data_store), 1, checkpoint_data);
  239. fwrite(found_seeds, sizeof(cl_ulong), total_seed_count, checkpoint_data);
  240. chkpoint_ready = 0;
  241. fclose(checkpoint_data);
  242. double fraction_done = ((offset - start) / (seedrange));
  243. boinc_fraction_done(fraction_done);
  244. boinc_end_critical_section();
  245. boinc_checkpoint_completed(); // Checkpointing completed
  246. }
  247. free(result);
  248. free(data_out);
  249. } // End of seed feed and processing loop
  250. boinc_begin_critical_section();
  251. double elapsed = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  252. fprintf(stderr,"Speed: %.2fm/s \n", (offset - start) / (elapsed_chkpoint + elapsed) / 1000000);
  253. fprintf(stderr,"Done\n");
  254. fprintf(stderr,"Processed %"SCNd64 " seeds in %f seconds\n",
  255. end - start,
  256. elapsed_chkpoint + ((double)(end_time - start_time) / CLOCKS_PER_SEC));
  257. fprintf(stderr,"Found seeds: \n");
  258. for (int i = 0; i < total_seed_count; i++) {
  259. fprintf(stderr," %"SCNd64 "\n", found_seeds[i]);
  260. }
  261. boinc_delete_file("kaktpoint.txt");
  262. check(clFlush(command_queue), "clFlush ");
  263. check(clFinish(command_queue), "clFinish ");
  264. check(clReleaseKernel(kernel), "clReleaseKernel ");
  265. check(clReleaseProgram(program), "clReleaseProgram ");
  266. check(clReleaseMemObject(seeds), "clReleaseMemObject (seeds) ");
  267. check(clReleaseMemObject(data), "clReleaseMemObject (data) ");
  268. check(clReleaseCommandQueue(command_queue), "clReleaseCommandQueue ");
  269. check(clReleaseContext(context), "clReleaseContext ");
  270. fflush(stderr);
  271. boinc_end_critical_section();
  272. boinc_finish(0);
  273. }