Add logging

master
Max Ehrlicher-Schmidt 4 years ago
parent 4b5ae5b82b
commit ea3b090f6d

809619
primes.txt

File diff suppressed because it is too large Load Diff

@ -1,3 +1,4 @@
import math
from time import time
import pycuda.autoinit
@ -6,6 +7,8 @@ import numpy as np
from pycuda import gpuarray
from pycuda.compiler import SourceModule
from optparse import OptionParser
ker = SourceModule("""
__global__ void
check_prime(unsigned long long *input, bool *output)
@ -62,28 +65,66 @@ __global__ void check_prime2(const unsigned long *IN, bool *OUT) {
}
""")
block_size = 1024
grid_size = 50000
check_prime = ker2.get_function("check_prime2")
def calc_primes(start: int = 1, grid_size: int = 1000, block_size: int = 1024):
check_prime = ker2.get_function("check_prime2")
primes = []
if start < 2:
primes = [2]
start = 3
if start % 2 == 0:
start = start + 1
testvec = np.arange(start, block_size * grid_size * 2 + start, step=2).astype(np.uint)
testvec_gpu = gpuarray.to_gpu(testvec)
outvec_gpu = gpuarray.to_gpu(np.full(block_size * grid_size, False, dtype=bool))
t1 = time()
check_prime(testvec_gpu, outvec_gpu, block=(block_size, 1, 1), grid=(grid_size, 1, 1))
result = outvec_gpu.get()
t2 = time()
for idx, val in enumerate(result):
if val:
primes.append(testvec[idx])
print(len(primes))
print('checked ' + str(block_size * grid_size) + ' numbers')
print('last prime: ' + str(primes[-1]))
print('The GPU needed ' + str(t2 - t1) + ' seconds')
with open(options.timings_output, 'a') as file:
file.write(str(start) + "," + str((t2 - t1) * 1000) + "\n")
return primes
testvec = np.arange(1, block_size * grid_size * 2, step=2).astype(np.uint)
testvec_gpu = gpuarray.to_gpu(testvec)
outvec_gpu = gpuarray.to_gpu(np.full(block_size * grid_size, False, dtype=bool))
t1 = time()
check_prime(testvec_gpu, outvec_gpu, block=(block_size, 1, 1), grid=(grid_size, 1, 1))
result = outvec_gpu.get()
t2 = time()
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-e", "--end", dest="end",
help="numbers to check without even numbers", default="1000000000", type="int")
parser.add_option("--numbers-per-step", dest="numbers_per_step",
help="amount of uneven numbers checked in each step (even number are skipped)", default="8388608",
type="int")
parser.add_option("--output", dest="output",
help="name of the file, where the primes should be stored", default="primes.txt", type="string")
parser.add_option("--timings-output", dest="timings_output",
help="name of the csv file, where the timing is logged as csv", default="timings.csv",
type="string")
(options, args) = parser.parse_args()
primes = []
block_size = 1024
start = 1
grid_size = int(math.ceil(options.numbers_per_step / block_size)) # half of numbers per
resulting_numbers_per_step = block_size * grid_size
last_number_checked = start - 1
for idx, val in enumerate(result):
if val:
primes.append(idx)
with open(options.timings_output, 'w') as file:
file.write("offset, duration\n")
#print(primes)
print(len(primes))
print('checked the first ' + str(block_size * grid_size) + ' numbers')
print('last prime: ' + str(primes[-1]))
print('The GPU needed ' + str(t2 - t1) + ' seconds')
while last_number_checked < options.end:
calculated_primes = calc_primes(last_number_checked + 1, grid_size, block_size)
with open(options.output, 'w') as file:
file.write("\n".join([str(p) for p in calculated_primes]))
last_number_checked = last_number_checked + resulting_numbers_per_step * 2

@ -0,0 +1,61 @@
offset, duration
3,31.999588012695312
16777217,53.99918556213379
33554433,68.02868843078613
50331649,80.9941291809082
67108865,90.61336517333984
83886081,102.02455520629883
100663297,106.90450668334961
117440513,93.96934509277344
134217729,93.63293647766113
150994945,103.03258895874023
167772161,106.99653625488281
184549377,111.50145530700684
201326593,114.09521102905273
218103809,119.96984481811523
234881025,124.02963638305664
251658241,127.03633308410645
268435457,130.9680938720703
285212673,135.0102424621582
301989889,139.97721672058105
318767105,143.2936191558838
335544321,145.76005935668945
352321537,150.18820762634277
369098753,153.4435749053955
385875969,155.93981742858887
402653185,159.04545783996582
419430401,163.06209564208984
436207617,166.01324081420898
452984833,169.036865234375
469762049,172.0116138458252
486539265,176.08928680419922
503316481,177.0176887512207
520093697,181.7023754119873
536870913,183.00223350524902
553648129,186.00106239318848
570425345,189.3165111541748
587202561,190.9501552581787
603979777,194.99802589416504
620756993,196.00176811218262
637534209,196.03347778320312
654311425,198.0299949645996
671088641,201.9979953765869
687865857,210.02864837646484
704643073,211.0426425933838
721420289,214.00046348571777
738197505,218.0306911468506
754974721,217.00143814086914
771751937,220.0028896331787
788529153,221.9834327697754
805306369,222.0005989074707
822083585,225.00038146972656
838860801,231.0009002685547
855638017,231.0020923614502
872415233,232.00178146362305
889192449,236.97233200073242
905969665,236.02962493896484
922746881,240.9975528717041
939524097,239.99500274658203
956301313,250.0011920928955
973078529,243.97730827331543
989855745,246.0007667541504
1 offset duration
2 3 31.999588012695312
3 16777217 53.99918556213379
4 33554433 68.02868843078613
5 50331649 80.9941291809082
6 67108865 90.61336517333984
7 83886081 102.02455520629883
8 100663297 106.90450668334961
9 117440513 93.96934509277344
10 134217729 93.63293647766113
11 150994945 103.03258895874023
12 167772161 106.99653625488281
13 184549377 111.50145530700684
14 201326593 114.09521102905273
15 218103809 119.96984481811523
16 234881025 124.02963638305664
17 251658241 127.03633308410645
18 268435457 130.9680938720703
19 285212673 135.0102424621582
20 301989889 139.97721672058105
21 318767105 143.2936191558838
22 335544321 145.76005935668945
23 352321537 150.18820762634277
24 369098753 153.4435749053955
25 385875969 155.93981742858887
26 402653185 159.04545783996582
27 419430401 163.06209564208984
28 436207617 166.01324081420898
29 452984833 169.036865234375
30 469762049 172.0116138458252
31 486539265 176.08928680419922
32 503316481 177.0176887512207
33 520093697 181.7023754119873
34 536870913 183.00223350524902
35 553648129 186.00106239318848
36 570425345 189.3165111541748
37 587202561 190.9501552581787
38 603979777 194.99802589416504
39 620756993 196.00176811218262
40 637534209 196.03347778320312
41 654311425 198.0299949645996
42 671088641 201.9979953765869
43 687865857 210.02864837646484
44 704643073 211.0426425933838
45 721420289 214.00046348571777
46 738197505 218.0306911468506
47 754974721 217.00143814086914
48 771751937 220.0028896331787
49 788529153 221.9834327697754
50 805306369 222.0005989074707
51 822083585 225.00038146972656
52 838860801 231.0009002685547
53 855638017 231.0020923614502
54 872415233 232.00178146362305
55 889192449 236.97233200073242
56 905969665 236.02962493896484
57 922746881 240.9975528717041
58 939524097 239.99500274658203
59 956301313 250.0011920928955
60 973078529 243.97730827331543
61 989855745 246.0007667541504
Loading…
Cancel
Save