main-intel.c 12 KB

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