summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunoiSayami <[email protected]>2023-06-04 19:35:54 +0800
committerKunoiSayami <[email protected]>2023-06-04 19:35:54 +0800
commit155b3a832c590c55ea3d89dd0783713f1d0f19c1 (patch)
treedfeb770865c77f76cf1ab5448f628b7e21bec483
parent1fbb943005ad78335f0b921be516b3df8140406d (diff)
fix: Fix compile error
Signed-off-by: KunoiSayami <[email protected]>
-rw-r--r--CMakeLists.txt5
-rw-r--r--expt_0406.cu5
-rw-r--r--expt_0501.cu3
-rw-r--r--expt_0502.cu11
-rw-r--r--expt_0516.cu2
-rw-r--r--expt_0529.cu4
-rw-r--r--expt_0604.cpp58
-rw-r--r--expt_0604.cu73
-rw-r--r--expt_0830.cu5
-rw-r--r--read_helper.h34
10 files changed, 96 insertions, 104 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 02efadf..56323ee 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -173,6 +173,7 @@ set_cuda_target_base(expt_0528)
add_executable(expt_0529 expt_0529.cu read_helper.h)
set_cuda_target_base(expt_0529)
-add_executable(expt_0604 expt_0604.cu read_helper.h)
-set_cuda_target_base(expt_0604)
+add_executable(expt_0604 expt_0604.cpp)
+set_target_properties(expt_0604 PROPERTIES LINKER_LANGUAGE CXX)
+
diff --git a/expt_0406.cu b/expt_0406.cu
index 96533c9..30ee7c4 100644
--- a/expt_0406.cu
+++ b/expt_0406.cu
@@ -1,3 +1,4 @@
+#include "sortlib.cuh"
#include <algorithm>
#include <cassert>
#include <cstdio>
@@ -55,7 +56,9 @@ __global__ void kernel(unsigned long step, const double slice_size) {
auto tid = step * (blockIdx.x * blockDim.x + threadIdx.x) + i;
// printf("%d\n", tid);
- auto index = (int)(sample_cdf(cudaPopulationItem[tid]) / slice_size);
+ auto index = (int)(FactorySort::sample_cdf(cudaSampleItem, length,
+ cudaPopulationItem[tid]) /
+ slice_size);
cdf_result[index] = true;
}
diff --git a/expt_0501.cu b/expt_0501.cu
index dcd25f5..7e63ae6 100644
--- a/expt_0501.cu
+++ b/expt_0501.cu
@@ -60,8 +60,7 @@ __global__ void kernel(unsigned long step, const double slice_size,
// printf("%d\n", tid);
auto index = (int)(custom_sort.sample_cdf_custom_version(
- cudaSampleItem, cudaSampleItem + SAMPLE_LENGTH,
- cudaPopulationItem[tid]) /
+ cudaSampleItem, cudaPopulationItem[tid]) /
slice_size) -
1;
// printf("%llu %d\n", cudaPopulationItem[tid], index);
diff --git a/expt_0502.cu b/expt_0502.cu
index eb4ff5b..0c89535 100644
--- a/expt_0502.cu
+++ b/expt_0502.cu
@@ -1,5 +1,7 @@
// Experimental content: Test the correctness of binary search and
// special(single thread) search
+#define ENABLE_SORT_TEST
+#define SORT_FIRST_VERSION
#include "sortlib.cuh"
#include <algorithm>
@@ -58,8 +60,7 @@ __global__ void kernel(unsigned long step, const double slice_size) {
// printf("%d\n", tid);
auto index = (int)(custom_sort.sample_cdf_custom_version(
- cudaSampleItem, cudaSampleItem + SAMPLE_LENGTH,
- cudaPopulationItem[tid]) /
+ cudaSampleItem, cudaPopulationItem[tid]) /
slice_size) -
1;
@@ -90,9 +91,9 @@ __global__ void kernel2(unsigned long step, const double _slice_size) {
// printf("%d\n", tid);
auto index = custom_sort.calculate_index(
- custom_sort.binary_search(cudaSampleItem,
- cudaSampleItem + SAMPLE_LENGTH,
- cudaPopulationItem[tid]) -
+ custom_sort.original_binary_search(cudaSampleItem,
+ cudaSampleItem + SAMPLE_LENGTH,
+ cudaPopulationItem[tid]) -
cudaSampleItem);
auto index2 =
diff --git a/expt_0516.cu b/expt_0516.cu
index 48b3811..351a8c4 100644
--- a/expt_0516.cu
+++ b/expt_0516.cu
@@ -1,6 +1,6 @@
// Experimental content: Test branch performance (optimized memory and output)
// #define PRINT_READ_PROCESS
-#include "read_helper_p.h"
+#include "read_helper.h"
#include "sortlib.cuh"
#include <algorithm>
diff --git a/expt_0529.cu b/expt_0529.cu
index bd64ad0..3448469 100644
--- a/expt_0529.cu
+++ b/expt_0529.cu
@@ -2,7 +2,7 @@
#include "sortlib.cuh"
#define READ_NO_OUTPUT
-#include "read_helper.h"
+#include "read_helper_p.h"
long pow_for_sample(long n) {
auto x = 2;
@@ -45,7 +45,7 @@ int main(int argc, char const *argv[]) {
printf("sample length: %ld, population length: %ld\n", sample_length,
population_length);
- ReadHelper readHelper("normal_distribution.txt", sample_length, 0);
+ ReadHelper<key_type> readHelper("normal_distribution.txt", sample_length, 0);
readHelper.readFile();
std::vector<key_type> sample, population;
diff --git a/expt_0604.cpp b/expt_0604.cpp
new file mode 100644
index 0000000..9217f9e
--- /dev/null
+++ b/expt_0604.cpp
@@ -0,0 +1,58 @@
+// Experimental content: Test fstream vs stdio
+
+#include <cassert>
+#include <chrono>
+#include <cstdio>
+#include <fstream>
+#include <iomanip>
+#include <iostream>
+#include <string>
+
+constexpr auto max_limit = 0x7fffffff;
+auto limit = 0L;
+auto filename = "normal_distribution.txt";
+
+void stdio() {
+ auto file = std::fopen(filename, "r");
+ assert(file);
+ auto read_count = 0L;
+ for (unsigned long long l;
+ read_count < limit && fscanf(file, "%lld ", &l) != EOF;)
+ read_count++;
+ fclose(file);
+}
+
+void fstream() {
+ std::ifstream fin(filename);
+ auto read_count = 0L;
+ for (unsigned long long l; read_count < limit && !fin.eof(); fin >> l)
+ read_count++;
+ fin.close();
+}
+
+auto measure(const char *function_name, void (*test_function)()) {
+ auto start = std::chrono::high_resolution_clock::now();
+ test_function();
+ auto end = std::chrono::high_resolution_clock ::now();
+ auto duration = end - start;
+ std::cout << "Function: " << std::fixed << std::setprecision(3)
+ << function_name << " " << (double)duration.count() / 1000.0 << "ms"
+ << std::endl;
+ return duration.count();
+}
+
+int main(int argc, char const *argv[]) {
+
+ if (argc >= 2) {
+ filename = argv[1];
+ }
+ if (argc >= 3) {
+ limit = strtol(argv[2], nullptr, 10);
+ }
+ if (limit <= 0) {
+ limit = max_limit;
+ }
+ auto s = measure("stdio", stdio);
+ auto f = measure("fstream", fstream);
+ std::cout << (s > f ? "stdio" : "fstream") << "win";
+}
diff --git a/expt_0604.cu b/expt_0604.cu
deleted file mode 100644
index bd64ad0..0000000
--- a/expt_0604.cu
+++ /dev/null
@@ -1,73 +0,0 @@
-// Experimental content: Print level by cdf
-
-#include "sortlib.cuh"
-#define READ_NO_OUTPUT
-#include "read_helper.h"
-
-long pow_for_sample(long n) {
- auto x = 2;
- for (int i = 1; i < n; i++) {
- x *= 2;
- }
- return x - 1;
-}
-
-static unsigned trailing_zeroes(size_t index) {
- constexpr auto block_size = 2;
- unsigned bits = 0;
- auto x = index / block_size;
-
- if (x) {
- while (x % block_size == 0) {
- ++bits;
- x /= block_size;
- }
- }
- return bits;
-}
-
-inline double calcSliceSize(size_t insertion_size) {
- return 1.0 / (double)insertion_size;
-}
-
-constexpr auto RESULT_LENGTH = 35;
-
-int main(int argc, char const *argv[]) {
-
- if (argc != 3) {
- printf("Usage %s [sample(pow)] [population]\n", argv[0]);
- return 1;
- }
-
- auto sample_length = pow_for_sample(strtol(argv[1], nullptr, 10));
- auto population_length = strtol(argv[2], nullptr, 10);
-
- printf("sample length: %ld, population length: %ld\n", sample_length,
- population_length);
-
- ReadHelper readHelper("normal_distribution.txt", sample_length, 0);
- readHelper.readFile();
-
- std::vector<key_type> sample, population;
- std::vector<unsigned int> result(RESULT_LENGTH);
- readHelper.split_into(sample, population);
- rebuildSort(sample);
-
- auto scale_size = calcSliceSize(population_length);
-
- auto sort = CustomSort(sample_length, sizeof(key_type) * 8);
-
- for (auto element : readHelper.population_vector) {
- auto ret = sort.sample_cdf_custom_version(sample.data(), element);
- auto cdf_index = ret / scale_size;
- auto index = trailing_zeroes((size_t)cdf_index);
- result[index]++;
- }
-
- for (int i = 0; i < 32; i++) {
- if (!result[i]) {
- continue;
- }
- printf("%d: %d\n", i, result[i]);
- }
-}
diff --git a/expt_0830.cu b/expt_0830.cu
index 6f7e14a..fc05092 100644
--- a/expt_0830.cu
+++ b/expt_0830.cu
@@ -1,3 +1,5 @@
+#define ENABLE_SORT_TEST
+#define SORT_FIRST_VERSION
#include "sortlib.cuh"
#include <algorithm>
@@ -53,8 +55,7 @@ __global__ void kernel(unsigned long step, const double slice_size,
// printf("%d\n", tid);
auto index = (int)(custom_sort.sample_cdf_custom_version(
- cudaSampleItem, cudaSampleItem + SAMPLE_LENGTH,
- cudaPopulationItem[tid]) /
+ cudaSampleItem, cudaPopulationItem[tid]) /
slice_size) -
1;
// printf("%llu %d\n", cudaPopulationItem[tid], index);
diff --git a/read_helper.h b/read_helper.h
index 2256ef8..f4615d4 100644
--- a/read_helper.h
+++ b/read_helper.h
@@ -3,7 +3,7 @@
#define LOCKFREE_READ_HELPER_H
#include <cassert>
-#include <cstdio>
+#include <fstream>
#include <random>
#include <vector>
@@ -15,9 +15,8 @@
#endif
#endif
-// template <typename key_type>
-class ReadHelper {
- typedef unsigned long long key_type;
+template <typename key_type> class ReadHelper_ {
+ // typedef unsigned long long key_type;
key_type max_value = std::numeric_limits<key_type>::min(),
min_value = std::numeric_limits<key_type>::max();
@@ -46,8 +45,8 @@ class ReadHelper {
public:
const size_t sample_length, population_length, random_number;
- ReadHelper(char const *filename, size_t sample_length,
- size_t population_length, size_t total_row = 0)
+ ReadHelper_(char const *filename, size_t sample_length,
+ size_t population_length, size_t total_row = 0)
: filename(filename), sample_length(sample_length),
population_length(population_length),
random_number(genRandomRow(total_row)) {
@@ -68,24 +67,24 @@ public:
bool readFile() {
this->population_vector.clear();
auto read_number = 0UL;
- FILE *file = fopen(filename, "r");
+ std::ifstream fin(filename);
- if (file == nullptr) {
- fprintf(stderr, "Unable to open file %s\n", filename);
+ if (!fin.is_open()) {
return false;
}
P_ERR("Reading sample");
- for (key_type i;
- read_number < sample_length && fscanf(file, "%lld ", &i) != EOF;
- store_into_vector(i))
+ for (key_type i; read_number < sample_length && !fin.eof();
+ store_into_vector(i)) {
+ fin >> i;
read_number++;
+ }
if (random_number > 0) {
P_ERR("\rReading skip");
// fprintf(stderr, "Skip %lu\n", read_number);
read_number = random_number;
- for (key_type i; read_number > 0 && fscanf(file, "%lld ", &i) != EOF;)
+ for (key_type i; read_number > 0 && !fin.eof(); fin >> i)
read_number--;
}
@@ -93,10 +92,11 @@ public:
read_number = 0;
auto remain = population_length + REVERSED_BLOCK;
- for (key_type i; read_number < remain && fscanf(file, "%lld ", &i) != EOF;
- store_into_vector(i))
+ for (key_type i; read_number < remain && !fin.eof(); store_into_vector(i)) {
+ fin >> i;
read_number++;
- fclose(file);
+ }
+ fin.close();
P_ERR("\r");
return true;
}
@@ -127,4 +127,6 @@ public:
size_t size() const { return this->population_vector.size(); }
};
+typedef ReadHelper_<unsigned long long> ReadHelper;
+
#endif \ No newline at end of file