-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecm.py
More file actions
427 lines (338 loc) · 13.2 KB
/
ecm.py
File metadata and controls
427 lines (338 loc) · 13.2 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import os
import shutil
import subprocess
import traceback
import time
from threading import Thread
from config import SIEVER_MODE, YAFU_PATH, YAFU_THREADS, DEFAULT_WORKDIR, DEFAULT_YAFU_WORKDIR, DEFAULT_CUDAECM_WORKDIR
from config import ECM_PATH, CUDA_ECM_PARAMS, CUDAECM_PATH, HAS_AVX512, YAFU_INI_PATH
from candidate import Candidate
from api import submitSolutionToSisMargaret
from ecmTask import ECMTask
if SIEVER_MODE == 1:
import nvidia_smi
nvidia_smi.nvmlInit()
def popenPiped(args, cwd=DEFAULT_WORKDIR):
return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)
def resetWorkdir(workdir, yafu=True):
if os.path.exists(workdir):
shutil.rmtree(workdir)
os.makedirs(workdir)
if yafu:
# Copy yafu.ini to workdir
shutil.copy(YAFU_INI_PATH, workdir)
def stopYAFU():
print("stopYAFU: Stopping YAFU and ecm")
os.system("pkill -f " + YAFU_PATH)
os.system("pkill -f " + ECM_PATH)
def stopCUDAECM():
print("stopCUDAECM: Stopping CUDA ECM")
os.system("pkill -f " + CUDAECM_PATH)
def factorCandidateViaYAFU(candidate: Candidate, workdir=DEFAULT_YAFU_WORKDIR, threads=YAFU_THREADS, one=True):
resetWorkdir(workdir)
yafuArgs = [YAFU_PATH, "-threads", str(threads)]
if one:
yafuArgs.append("-one")
yafuArgs.append(f"factor({candidate.N})")
proc = popenPiped(yafuArgs, cwd=workdir)
startYAFUFactors = False
factors = []
for line in proc.stdout:
if not candidate.active:
stopYAFU()
return []
line = line.decode("utf8")
print(line, end="")
if "***factors found***" in line:
startYAFUFactors = True
try:
if startYAFUFactors and line[0] in ["C", "P"] and " = " in line:
factor = int(line.split()[-1])
if factor > 1 and candidate.N % factor == 0:
factors.append(factor)
except Exception:
traceback.print_exc()
factors.sort()
return factors
def performECMViaYAFU(task: ECMTask, workdir=DEFAULT_YAFU_WORKDIR, threads=YAFU_THREADS, one=True):
# Only one candidate per task is supported right now
assert len(task.Ns) == 1
N = task.Ns[0]
resetWorkdir(workdir)
yafuArgs = [YAFU_PATH, "-threads", str(threads),
"-B1ecm", str(task.B1), "-B2ecm", str(task.B2),
"-ecm_path", ECM_PATH, "-ext_ecm", "1000000000" if HAS_AVX512 else "0"]
if one:
yafuArgs.append("-one")
yafuArgs.append(f"ecm({N}, {task.curvesPerCandidate})")
task.startedAt = time.time()
task.ongoing = True
proc = popenPiped(yafuArgs, cwd=workdir)
startYAFUFactors = False
factors = []
for line in proc.stdout:
if not task.active:
stopYAFU()
task.taskRuntime = time.time() - task.startedAt
task.ongoing = False
return []
line = line.decode("utf8")
print(line, end="")
if "***factors found***" in line:
startYAFUFactors = True
if line.startswith("ecm: ") and "curves on" in line:
task.curvesRan = max(task.curvesRan, int(line.split("/")[0].split()[-1]))
try:
if startYAFUFactors and line[0] in ["C", "P"] and " = " in line:
factor = int(line.split()[-1])
if factor > 1 and N % factor == 0:
factors.append(factor)
except Exception:
traceback.print_exc()
factors.sort()
task.taskRuntime = time.time() - task.startedAt
task.ongoing = False
return [factors]
# Based on https://github.com/FACT0RN/GPUDispersion/blob/9cb46b4bb0104575fea695a369b4d256206470c4/blockchain.py#L32
def createConfigFile(b1, curvesPerCandidate, gpuID, filename, workdir=DEFAULT_CUDAECM_WORKDIR):
config = f"""[general]
; server or file
mode = file
; Logfile location
logfile = /dev/null
; Output file of abandoned, i.e. unsolved tasks.
; Format is the same as the output format, without listing factors
;
; Example line:
; 44800523911798220433379600867; # effort 112
output_abandoned = /dev/null
; Log level
;
; 1: "VERBOSE",
; 2: "DEBUG",
; 3: "INFO",
; 4: "WARNING",
; 5: "ERROR",
; 6: "FATAL",
; 7: "NONE"
; Default is set at compile time.
loglevel = 3
; Use a random seed for the random number generator used to generate points and
; curves. If set to 'false', each run of the program will behave the same
; provided the same input data.
; Default: true
random = true
[server]
port = 11111
[file]
; Input file.
; The input file should contain a single number to be factored per line. Lines
; starting with anything but a digit are skipped.
;
; Example line:
; 44800523911798220433379600867
input = {workdir}/input{gpuID}.txt
; Output file.
; Each fully factored input number is appended to the output on its own line in
; the format
; (input number);(factor),(factor),(factor), # effort: (number of curves)
;
; Example line:
; 44800523911798220433379600867;224536506062699,199524454608233, # effort: 12
output = {workdir}/output{gpuID}.txt
[cuda]
; Number of concurrent cuda streams to issue to GPU
; Default: 2
streams = 1
; Number of threads per block for cuda kernel launches.
; Set to auto to determine setting for maximum parallel resident blocks per SM at runtime.
; Note: The settings determined by 'auto' are not always automatically the optimal setting for maximum throughput.
; Default: auto
threads_per_block = auto
; Constant memory is used for (smaller) scalars during point multiplication.
; When the scalar is too large to fit into constant memory or this option is set
; to 'false', global device memory is used.
; Default: true
use_const_memory = true
[ecm]
; Redo ECM until numbers are fully factored.
; If set to false, only the first factor is returned.
; Default: false
find_all_factors = false
; Set the computation of the scalar s for point multiplication. With
; 'powersmooth' set to 'true', then s = lcm(2, ..., b1). If set to false,
; s = primorial(2, ..., b1), i.e. the product of all primes less than or equal
; to b1.
; Default: true
powersmooth = true
b1 = {b1}
b2 = 100000
; Maximum effort per input number.
; With each curve, the already spent effort is incremented. Thus, with effort
; set to 100, ecm stage1 (and stage2) will be executed on 100 curves per input
; number.
; Default: 10
effort = {curvesPerCandidate}
; Set the curve generator function.
; Use 2 under normal circumstances.
; 0: "Naive"
; 1: "GKL2016_j1"
; 2: "GKL2016_j4"
; Default: 2
curve_gen = 2
; Use only points for finding factors that are off curve.
; After point multiplication, use all resulting points to find factors. If set
; to 'false' coordinates of points will be checked that do not fulfill the curve
; equation.
; Settings for stage1 and stage2 respectively.
; Default: true
stage1.check_all = false
stage2.check_all = true
; Enable/Disable stage 2.
; If set to 'false', only stage 1 of ECM is performed.
; Default: true
stage2.enabled = false
; Set the window size for stage 2
; Default: 2310
;stage2.window_size = 2310
"""
with open(filename, "w") as f:
f.write(config)
f.close()
def factorCandidatesViaCUDAECM(manager, candidates: list[Candidate], baseWorkdir=DEFAULT_CUDAECM_WORKDIR):
if len(candidates) == 0:
return
print(f"factorCandidatesViaCUDAECM: Working on {len(candidates)} candidates")
height = manager.height
deviceCount = nvidia_smi.nvmlDeviceGetCount()
resetWorkdir(baseWorkdir)
levels = len(CUDA_ECM_PARAMS["b1"])
for level in range(levels):
for i in range(deviceCount):
configName = f"gpu_config_{level}_{i}.txt"
createConfigFile(CUDA_ECM_PARAMS["b1"][level], CUDA_ECM_PARAMS["curves"][level],
i, DEFAULT_CUDAECM_WORKDIR + configName)
for level in range(levels):
totalCands = len(candidates)
if totalCands == 0:
print(f"factorCandidatesViaCUDAECM: Out of candidates to run on GPU {level}. Stopping")
return
startCandId = 0
procs = []
for i in range(deviceCount):
candsToFetch = totalCands // deviceCount + (1 if i < totalCands % deviceCount else 0)
configName = f"gpu_config_{level}_{i}.txt"
with open(baseWorkdir + f"input{i}.txt", "w") as f:
for j in range(startCandId, startCandId + candsToFetch):
f.write(f"{j} {candidates[j].N}\n")
f.close()
startCandId += candsToFetch
procs.append(popenPiped(["env", f"CUDA_VISIBLE_DEVICES={i}", "unbuffer", CUDAECM_PATH, "-c", baseWorkdir + configName]))
print(f"factorCandidatesViaCUDAECM: GPU {i} started")
if manager.height > height:
for proc in procs:
proc.kill()
return
for i, proc in enumerate(procs):
print(f"factorCandidatesViaCUDAECM: Waiting for GPU {i} to finish")
for line in proc.stdout:
line = line.decode("utf8")
print(line, end="")
procs[i].wait()
print(f"factorCandidatesViaCUDAECM: GPU {i} done")
if manager.height > height:
return
for i in range(deviceCount):
for line in open(baseWorkdir + f"output{i}.txt").read().split("\n"):
try:
line = line.strip()
if line == "" or line == "DONE":
continue
index, factor = map(int, line.split())
if factor == 1 or not candidates[index].active:
continue
N = candidates[index].N
if N % factor != 0:
continue
candidates[index].active = False
factor2 = N // factor
print(f"factorCandidatesViaCUDAECM: Submitting {N} = {factor} * {factor2}")
Thread(target=submitSolutionToSisMargaret, args=(manager.mqtt, candidates[index].id, candidates[index].N,
factor, factor2), daemon=True).start()
except Exception:
traceback.print_exc()
continue
try:
os.remove(baseWorkdir + f"output{i}.txt")
except Exception:
pass
if manager.height > height:
return
origCands = len(candidates)
candidates = [c for c in candidates if c.active]
print(f"factorCandidatesViaCUDAECM: Level {level} done. Reduced {origCands} -> {len(candidates)} candidates")
def performECMViaCUDAECM(task: ECMTask, baseWorkdir=DEFAULT_CUDAECM_WORKDIR):
totalCands = len(task.Ns)
if totalCands == 0:
return
assert task.B2 == 0
print(f"performECMViaCUDAECM: Working on {totalCands} candidates")
task.startedAt = time.time()
task.ongoing = True
deviceCount = nvidia_smi.nvmlDeviceGetCount()
resetWorkdir(baseWorkdir)
startCandId = 0
procs = []
for i in range(deviceCount):
configName = f"performECMViaCUDAECM_{i}.txt"
createConfigFile(task.B1, task.curvesPerCandidate, i, baseWorkdir + configName)
candsToFetch = totalCands // deviceCount + (1 if i < totalCands % deviceCount else 0)
with open(baseWorkdir + f"input{i}.txt", "w") as f:
for j in range(startCandId, startCandId + candsToFetch):
f.write(f"{j} {task.Ns[j]}\n")
f.close()
startCandId += candsToFetch
procs.append(popenPiped(["env", f"CUDA_VISIBLE_DEVICES={i}", "unbuffer", CUDAECM_PATH, "-c", baseWorkdir + configName]))
print(f"performECMViaCUDAECM: GPU {i} started")
if not task.active:
for proc in procs:
proc.kill()
return
for i, proc in enumerate(procs):
print(f"performECMViaCUDAECM: Waiting for GPU {i} to finish")
for line in proc.stdout:
line = line.decode("utf8")
print(line, end="")
procs[i].wait()
print(f"performECMViaCUDAECM: GPU {i} done")
if not task.active:
return
factorsList = [[] for _ in range(totalCands)]
for i in range(deviceCount):
for line in open(baseWorkdir + f"output{i}.txt").read().split("\n"):
try:
line = line.strip()
if line == "" or line == "DONE":
continue
index, factor = map(int, line.split())
if factor == 1:
continue
N = task.Ns[index]
if N % factor != 0:
continue
factor2 = N // factor
print(f"performECMViaCUDAECM: Found {N} = {factor} * {factor2}")
factorsList[index] = [factor, factor2]
except Exception:
traceback.print_exc()
continue
try:
os.remove(baseWorkdir + f"output{i}.txt")
except Exception:
pass
if not task.active:
return
print(f"performECMViaCUDAECM: ECM finished. Reduced {totalCands} -> {sum(len(factors) < 2 for factors in factorsList)} candidates")
task.taskRuntime = time.time() - task.startedAt
task.ongoing = False
return factorsList