summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunoiSayami <[email protected]>2022-06-21 07:55:51 +0800
committerKunoiSayami <[email protected]>2022-06-21 07:55:51 +0800
commit985dfaf9f1b541b51e020561ccc9b91d9c982510 (patch)
tree2a70518d24b9754d7e6fb086ee81dd00baaacbe7
parent85296957a8d755cda80c5dc00dcef4555ff14ebc (diff)
feat(script): Optimize normal distribution generator
* feat(script): Finish experiment 1 and 2 Signed-off-by: KunoiSayami <[email protected]>
-rwxr-xr-xex_normal_distribution.py25
-rw-r--r--main.cu7
-rw-r--r--normal_distribution.cpp9
3 files changed, 25 insertions, 16 deletions
diff --git a/ex_normal_distribution.py b/ex_normal_distribution.py
index f82df93..272b9b6 100755
--- a/ex_normal_distribution.py
+++ b/ex_normal_distribution.py
@@ -1,24 +1,35 @@
#!/usr/bin/env python3
+import numpy as np
import matplotlib.pyplot as plt
+from matplotlib.ticker import PercentFormatter
def function_pdf(element: list[int]):
- plt.hist(element, bins=500)
+ plt.hist(element, bins=500, histtype='step')
plt.show()
def function_cdf(element: list[int]):
- plt.hist(element, bins=500, cumulative=True)
+ plt.hist(element, bins=500, cumulative=True, histtype='step')
plt.show()
-def normal_distribution():
+def function_1000_cdf(element: list[int]):
+ plt.hist(element, bins=250, histtype='step', label='x', cumulative=True,
+ weights=np.ones(len(element)) / len(element))
+ plt.hist(element[:1000], bins=100, histtype='step', label='y', cumulative=True, weights=np.ones(1000) / 1000)
+ plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
+ plt.show()
+
+
+def normal_distribution() -> list[int]:
with open('normal_distribution.txt') as fin:
context = fin.read()
- elements = list(map(int, context.splitlines()))
- function_pdf(elements)
- function_cdf(elements)
+ return list(map(int, context.splitlines()))
if __name__ == '__main__':
- normal_distribution()
+ elements = normal_distribution()
+ function_pdf(elements)
+ function_cdf(elements)
+ function_1000_cdf(elements)
diff --git a/main.cu b/main.cu
index bc42b11..c34add0 100644
--- a/main.cu
+++ b/main.cu
@@ -92,7 +92,6 @@ typedef unsigned int LL;
#endif
#ifdef MEASURE_TIME
-#include <cuda/atomic>
#undef BUILD_SIZE
#define BUILD_SIZE 1024
#endif
@@ -578,12 +577,10 @@ __global__ void kernel(LL *items, LL *op, LL *result) {
result[tid] = l->Search(item);
#ifdef MEASURE_TIME
unsigned long long end_time = clock64() - start_time;
- // SpendTime[tid] = (int)end_time;
if (l->spend_time[tid]) {
printf("%d\n", tid);
}
l->spend_time[tid] = (int)end_time;
- // printf("%d %d\n", SpendTime[tid], tid);
#endif
}
}
@@ -833,7 +830,7 @@ int main(int argc, char **argv) {
cudaEventCreate(&stop);
cudaEventRecord(start, nullptr);
#ifdef MEASURE_TIME
- kernel<<<NUM_THREADS, 1>>>(Citems, Cop2, Cresult);
+ kernel<<<NUM_ITEMS, 1>>>(Citems, Cop2, Cresult);
#else
kernel<<<blocks, NUM_THREADS>>>(Citems, Cop2, Cresult);
#endif
@@ -891,7 +888,7 @@ int main(int argc, char **argv) {
break;
fprintf(file, "%d\n", tmp[i]);
}
- // printf("%d\n", i);
+ printf("%d\n", i);
delete[] tmp;
fclose(file);
}
diff --git a/normal_distribution.cpp b/normal_distribution.cpp
index fd52ac1..c320ce4 100644
--- a/normal_distribution.cpp
+++ b/normal_distribution.cpp
@@ -1,4 +1,5 @@
#include <algorithm>
+#include <cassert>
#include <cstdio>
#include <random>
#include <set>
@@ -22,13 +23,13 @@ int main() {
while (set.size() != length) {
auto element = (long long)std::round(normalDistribution(randomEngine));
auto ret = set.insert(element);
- if (ret.second)
+ if (ret.second) {
vector.push_back(element);
+ printf("%lld\n", element);
+ }
}
- for (long long element : vector) {
- printf("%lld\n", element);
- }
+ assert(vector.size() == length);
return 0;
} \ No newline at end of file