-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
175 lines (166 loc) · 4.66 KB
/
Copy pathtest.c
File metadata and controls
175 lines (166 loc) · 4.66 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
/*
* Copyright 2020-2022. Heekuck Oh, all rights reserved
* 이 프로그램은 한양대학교 ERICA 소프트웨어학부 재학생을 위한 교육용으로 제작되었다.
*/
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <omp.h>
#include <sys/time.h>
#include <unistd.h>
#include "miller_rabin.h"
int main(void)
{
uint64_t a, b, m, x;
int i;
atomic_int total;
struct timeval start, end;
double elapsed;
/*
* 모듈러 연산 검증
*/
a = 13053660249015046863ULL;
b = 14731404471217122002ULL;
m = 16520077267041420904ULL;
printf("a = %"PRIu64", b = %"PRIu64", m = %"PRIu64"\n", a, b, m);
printf("a+b mod m = %"PRIu64, mod_add(a, b, m));
if (mod_add(a, b, m) == 11264987453190747961ULL)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
printf("a-b mod m = %"PRIu64, mod_sub(a, b, m));
if (mod_sub(a, b, m) == 14842333044839345765ULL)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
printf("a*b mod m = %"PRIu64, mod_mul(a, b, m));
if (mod_mul(a, b, m) == 13008084103192797750ULL)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
printf("a^b mod m = %"PRIu64, mod_pow(a, b, m));
if (mod_pow(a, b, m) == 12523224429397597497ULL)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
a = 18446744073709551615ULL;
b = 72057594037927935ULL;
m = 65536ULL;
printf("a = %"PRIu64", b = %"PRIu64", m = %"PRIu64"\n", a, b, m);
printf("a+b mod m = %"PRIu64, mod_add(a, b, m));
if (mod_add(a, b, m) == 65534)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
printf("a-b mod m = %"PRIu64, mod_sub(a, b, m));
if (mod_sub(a, b, m) == 0)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
printf("a*b mod m = %"PRIu64, mod_mul(a, b, m));
if (mod_mul(a, b, m) == 1)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
printf("a^b mod m = %"PRIu64, mod_pow(a, b, m));
if (mod_pow(a, b, m) == 65535)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
/*
* 작은 모듈로 연산에서 피연산자 값이 큰 경우를 검증한다.
* 간단한 연산이 1초 이상 걸리면 코드에 큰 문제가 있으므로 알람 신호를 보내 종료한다.
*/
alarm(1); a = 18446744073709551613ULL; x = mod_pow(a,a,5); alarm(0);
printf("%"PRIu64"^%"PRIu64" mod 5 = %"PRIu64, a, a, x);
if (x == 3)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
/*
* 2부터 처음 100개의 소수를 출력한다.
*/
x = 2; i = 0;
while (true) {
if (miller_rabin(x)) {
++i;
printf("%"PRIu64" ", x);
if (i % 10 == 0)
printf("\n");
if (i == 100)
break;
}
++x;
}
/*
* x = 0x8000000000000000부터 처음 100개의 소수를 출력한다.
*/
x = 0x8000000000000000; i = 0;
while (true) {
if (miller_rabin(x)) {
++i;
printf("%"PRIu64" ", x);
if (i % 4 == 0)
printf("\n");
if (i == 100)
break;
}
++x;
}
/*
* x = 1부터 67108864까지 소수의 개수를 계산한다.
*/
printf("x = 1부터 67108864까지 소수를 세는 중"); fflush(stdout);
gettimeofday(&start, NULL); total = 0;
/*
* 코어의 개수만큼 스레드를 생성하여 병렬로 개수를 센다.
*/
#pragma omp parallel for
for (i = 0; i < 16; ++i) {
uint64_t x;
int count = 0;
for (x = 1+i*4194304; x < 1+(i+1)*4194304; x++) {
if (miller_rabin(x))
count++;
if (x % 1048576 == 0) {
printf(".");
fflush(stdout);
}
}
atomic_fetch_add(&total, count);
}
/*
* 병렬 계산이 여기서 종료된다.
*/
gettimeofday(&end, NULL);
elapsed = (double)(end.tv_sec - start.tv_sec)+(double)(end.tv_usec - start.tv_usec)*1e-6;
printf("소수 개수: %d개", total);
if (total == 3957809)
printf(".....PASSED\n");
else {
printf(".....FAILED\n");
return 1;
}
printf("계산 시간: %.4f초\n", elapsed);
return 0;
}