-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cc
executable file
·216 lines (181 loc) · 5.61 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <cstring>
#include <inttypes.h>
#include "k2.h"
#ifdef _WIN64
#include "boinc_win.h"
#else
#ifdef _WIN32
#include "boinc_win.h"
#endif
#endif
#include "boinc_api.h"
#define MAX_SEED_BUFFER_SIZE (0x10000)
double time()
{
using namespace std::chrono;
auto now = high_resolution_clock::now().time_since_epoch();
return (double)duration_cast<milliseconds>(now).count() / 1000;
}
int main(int argc, char **argv)
{
BOINC_OPTIONS options;
boinc_options_defaults(options);
options.normal_thread_priority = true;
boinc_init_options(&options);
int threads = 0;
unsigned long long start = 0;
unsigned long long end = 0;
unsigned long long chunkSeed = 0;
int neighbor1 = 0;
int neighbor2 = 0;
int neighbor3 = 0;
int diagonalIndex = 0;
int height = 0;
struct checkpoint_vars
{
unsigned long long offset;
unsigned long long start;
int block;
double elapsed_chkpoint;
int total_seed_count;
};
if (argc % 2 != 1)
{
fprintf(stderr, "Failed to parse arguments\n");
exit(EXIT_FAILURE);
}
for (int i = 1; i < argc; i += 2)
{
const char *param = argv[i];
if (strcmp(param, "-t") == 0 || strcmp(param, "--threads") == 0)
{
threads = atoi(argv[i + 1]);
}
else if (strcmp(param, "-s") == 0 || strcmp(param, "--start") == 0)
{
sscanf(argv[i + 1], "%llu", &start);
}
else if (strcmp(param, "-e") == 0 || strcmp(param, "--end") == 0)
{
sscanf(argv[i + 1], "%llu", &end);
}
else if (strcmp(param, "-cs") == 0 || strcmp(param, "--chunkseed") == 0)
{
sscanf(argv[i + 1], "%llu", &chunkSeed);
}
else if (strcmp(param, "-n1") == 0 || strcmp(param, "--neighbor1") == 0)
{
neighbor1 = atoi(argv[i + 1]);
}
else if (strcmp(param, "-n2") == 0 || strcmp(param, "--neighbor2") == 0)
{
neighbor2 = atoi(argv[i + 1]);
}
else if (strcmp(param, "-n3") == 0 || strcmp(param, "--neighbor3") == 0)
{
neighbor3 = atoi(argv[i + 1]);
}
else if (strcmp(param, "-di") == 0 || strcmp(param, "--diagonalindex") == 0)
{
diagonalIndex = atoi(argv[i + 1]);
}
else if (strcmp(param, "-ch") == 0 || strcmp(param, "--cactusheight") == 0)
{
height = atoi(argv[i + 1]);
}
else
{
fprintf(stderr, "Unknown parameter: %s\n", param);
}
}
fprintf(stderr, "Received work unit: %llu\n", chunkSeed);
fprintf(stderr, "Data: n1: %d, n2: %d, n3: %d, di: %d, ch: %d\n",
neighbor1,
neighbor2,
neighbor3,
diagonalIndex,
height);
static unsigned long long found_seeds[MAX_SEED_BUFFER_SIZE];
static int neighbors[] = {neighbor1, neighbor2, neighbor3};
k2_params(chunkSeed, neighbors, diagonalIndex, height);
unsigned long long total = end - start;
unsigned long long total_seed_count = 0;
unsigned long long offset = start;
unsigned long long block = 0;
int progcheck = 0;
FILE *checkpoint_data = boinc_fopen("kaktpoint.txt", "rb");
unsigned long long elapsed_chkpoint = 0;
if (!checkpoint_data)
{
fprintf(stderr, "No checkpoint to load\n");
}
else
{
boinc_begin_critical_section();
struct checkpoint_vars data_store;
fread(&data_store, sizeof(data_store), 1, checkpoint_data);
offset = data_store.offset;
start = data_store.start;
block = data_store.block;
elapsed_chkpoint = data_store.elapsed_chkpoint;
total_seed_count = data_store.total_seed_count;
fread(found_seeds, sizeof(cl_ulong), total_seed_count, checkpoint_data);
fprintf(stderr, "Checkpoint loaded, task time %llu s \n", elapsed_chkpoint);
fclose(checkpoint_data);
boinc_end_critical_section();
}
double start_time = time();
for (; offset < end;)
{
unsigned long long count = k2_start_block(threads, &offset, end, found_seeds + total_seed_count);
progcheck++;
block++;
for (unsigned long long j = 0; j < count; j++)
{
fprintf(stderr," Found seed: %llu, %llu, height: %d\n",
found_seeds[total_seed_count],
found_seeds[total_seed_count] & ((1ULL << 48ULL) - 1ULL),
(int)(found_seeds[total_seed_count] >> 58ULL));
fprintf(stderr, "%llu\n", found_seeds[total_seed_count]);
total_seed_count++;
}
if (progcheck > 125 || boinc_time_to_checkpoint())
{ // 200 for 0.2bil seeds before checkpoint
boinc_begin_critical_section(); // Boinc should not interrupt this
double frac = (double(offset - start) / (total));
boinc_fraction_done(frac);
progcheck = 0;
boinc_delete_file("kaktpoint.txt");
FILE *checkpoint_data = boinc_fopen("kaktpoint.txt", "wb");
struct checkpoint_vars data_store;
data_store.offset = offset;
data_store.start = start;
data_store.block = block;
data_store.elapsed_chkpoint = (elapsed_chkpoint + (double)(time() - start_time));
data_store.total_seed_count = total_seed_count;
fwrite(&data_store, sizeof(data_store), 1, checkpoint_data);
fwrite(found_seeds, sizeof(cl_ulong), total_seed_count, checkpoint_data);
fclose(checkpoint_data);
double fraction_done = ((offset - start) / (total));
boinc_fraction_done(fraction_done);
boinc_end_critical_section();
boinc_checkpoint_completed(); // Checkpointing completed
}
}
fflush(stderr);
double end_time = time();
double elapsed = elapsed_chkpoint + end_time - start_time;
double per_sec = total / elapsed;
fprintf(stderr, "\nSpeed (%.2lfm/s * %i t): %.2lfm/s\n", ((per_sec / 1000000.0) / threads), threads, per_sec / 1000000.0 );
fprintf(stderr, "Done\n");
fprintf(stderr, "Processed: %llu seeds in %.2lfs seconds\n", total, elapsed);
fprintf(stderr, "Found seeds: \n");
for (unsigned long i = 0; i < total_seed_count; i++) {
fprintf(stderr, " %llu\n", found_seeds[i]);
}
fflush(stderr);
boinc_finish(0);
}