summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunoiSayami <[email protected]>2022-08-07 02:36:03 +0800
committerKunoiSayami <[email protected]>2022-08-07 02:36:03 +0800
commitd889a91c7665b13c3b12b5ae96ae60cb79276d7b (patch)
treea45c49704d279048eab019a6f480073f804c4b3e
parent4d657d308ccc01701afefed6723d7610f0c5ce1b (diff)
feat: Implement expt_0802 binary search
Signed-off-by: KunoiSayami <[email protected]>
-rw-r--r--CMakeLists.txt11
-rw-r--r--expt_0802.cpp74
-rw-r--r--expt_0804.cu215
-rw-r--r--normal_distribution.cpp38
4 files changed, 316 insertions, 22 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 62d52de..7bc1eac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -91,10 +91,19 @@ target_link_libraries(expt_0729 m stdc++)
set_target_properties(expt_0729 PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(expt_0729 PROPERTIES CUDA_ARCHITECTURES "75")
-set_target_properties(expt_0726 PROPERTIES LINKER_LANGUAGE CUDA)
+set_target_properties(expt_0729 PROPERTIES LINKER_LANGUAGE CUDA)
add_executable(expt_0802 expt_0802.cpp)
#set_target_properties(expt_0729 PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(expt_0802 m stdc++)
+
+add_executable(expt_0804 expt_0804.cu)
+#set_target_properties(expt_0804 PROPERTIES LINKER_LANGUAGE CXX)
+target_link_libraries(expt_0804 m stdc++)
+
+set_target_properties(expt_0804 PROPERTIES
+ CUDA_SEPARABLE_COMPILATION ON)
+set_target_properties(expt_0804 PROPERTIES CUDA_ARCHITECTURES "75")
+set_target_properties(expt_0804 PROPERTIES LINKER_LANGUAGE CUDA)
diff --git a/expt_0802.cpp b/expt_0802.cpp
index 32727fb..fbd6c23 100644
--- a/expt_0802.cpp
+++ b/expt_0802.cpp
@@ -4,11 +4,9 @@
#include <iostream>
#include <vector>
-using namespace std;
-
-vector<vector<int>> a;
-vector<int> result;
-constexpr size_t LENGTH = 2048;
+std::vector<std::vector<int>> a;
+std::vector<int> result;
+constexpr size_t LENGTH = 1024;
void calculate(int l, int r, int dep) {
if (l > r)
@@ -20,20 +18,48 @@ void calculate(int l, int r, int dep) {
}
size_t calculate_location(size_t index) {
- // auto value = result[index + 1];
- auto real_index = ++index;
- int step = 0;
- while (index > 0) {
- index /= 2;
- step++;
- }
- auto end = (int)std::pow(2, step - 1);
- auto location = LENGTH / end / 2 * (real_index % end * 2 + 1);
- if (location == 0) {
- location = LENGTH;
+ int step = (int)std::log2(++index) + 1;
+ auto location =
+ LENGTH / (1 << step) * ((index & ((1 << (step - 1)) - 1)) * 2 + 1);
+ return location ? location : LENGTH;
+}
+typedef int key_type;
+
+inline double safeStep(double step) { return step < 0 ? 0 : step; }
+
+int get_son_from_step(int point, bool negative) {
+ return negative ? point * 2 : point * 2 + 1;
+}
+
+const key_type *cudaBinarySearch(key_type *const start, const key_type *end,
+ const key_type val) {
+ int step_limit = (int)std::log2(LENGTH) + 1;
+ const auto length = (end - start);
+ key_type *last_known_point = start;
+ auto son = 0;
+
+ for (int i = 0; i < step_limit; i++) {
+ const auto next_level_start = start + (1 << (i + 1)) - 1;
+ /*const auto current_time = (1.0 / (1 << (i + 1)));
+ printf("%d ", *level_start);
+ printf("%d %d\n", *last_known_point, (1 << i));
+ if (*last_known_point == val) {
+ puts("find");
+ return last_known_point;
+ }
+ sum = safeStep(sum +
+ ((*last_known_point > val) ? -current_time : current_time));
+ last_known_point = level_start + (int)(sum * (double)(1 << i));*/
+ if (*last_known_point == val) {
+ puts("found");
+ return last_known_point;
+ }
+ son = get_son_from_step(son, (*last_known_point > val));
+ last_known_point = next_level_start + son;
+ printf("%d\n", *last_known_point);
}
- // printf("%d %d %d %lu %lu\n", step, end, value, real_index % end, location);
- return location;
+ puts("");
+ return last_known_point;
}
int main() {
@@ -50,10 +76,18 @@ int main() {
result.push_back(j);
}
}
- cout << endl;
- for (int i = 0; i < LENGTH; i++) {
+ for (int i = 0; i < 32; i++) {
+ printf("%3d ", i);
+ }
+
+ puts("");
+
+ for (int i = 0; i < 32; i++) {
+ printf("%3d ", result[i]);
assert(calculate_location(i) == result[i]);
}
+ puts("");
+ cudaBinarySearch(&result[0], &*result.end(), 1024);
return 0;
}
diff --git a/expt_0804.cu b/expt_0804.cu
new file mode 100644
index 0000000..47c269e
--- /dev/null
+++ b/expt_0804.cu
@@ -0,0 +1,215 @@
+#include <algorithm>
+#include <cassert>
+#include <cstdio>
+#include <cstring>
+#include <iostream>
+#include <vector>
+
+constexpr size_t length = 2097152;
+std::vector<unsigned long long> population_vector, sample_vector,
+ sample_vector_into_cuda;
+
+unsigned long long max_value = 0, min_value = 0xfffffffff;
+
+inline void store_into_vector(unsigned long long value) {
+ if (max_value < value) {
+ max_value = value;
+ }
+ if (min_value > value) {
+ min_value = value;
+ }
+ population_vector.push_back(value);
+}
+
+typedef unsigned long long key_type;
+
+typedef unsigned long long *key_type_ptr;
+
+constexpr long SAMPLE_LENGTH = 1024;
+
+constexpr long TEST_SIZE = 1024;
+
+__device__ key_type *SampleItem, *PopulationItem;
+__device__ bool *cdf_result;
+__device__ unsigned insert_value;
+#ifdef TEST_BOUNDS
+__device__ unsigned int index_max, index_min;
+#endif
+
+__device__ const key_type *cudaBinarySearch(key_type *start, key_type *end,
+ const key_type val) {
+ auto begin = start;
+ key_type *last_known_point = nullptr;
+ while (begin < end) {
+ auto mid = (end - begin) / 2;
+ auto mid_val = *(start + mid);
+ if (val == mid_val) {
+ return start + mid;
+ } else if (val > mid_val) {
+ begin = begin + mid + 1;
+ } else {
+ end = end - mid - 1;
+ }
+ last_known_point = begin;
+ }
+ return last_known_point;
+}
+
+__device__ __host__ size_t calculate_location(size_t index) {
+ int step = (int)std::log2(++index) + 1;
+ auto location =
+ SAMPLE_LENGTH / (1 << step) * ((index & ((1 << (step - 1)) - 1)) * 2 + 1);
+ return location ? location : SAMPLE_LENGTH;
+}
+
+__device__ double sample_cdf(double x) {
+ auto it = cudaBinarySearch(SampleItem, SampleItem + SAMPLE_LENGTH + 2, x);
+ if (it == SampleItem + SAMPLE_LENGTH) {
+ return 1;
+ }
+ if (it == SampleItem) {
+ return 0;
+ }
+ auto it_prev = it - 1;
+ return (double(it_prev - SampleItem) +
+ (x - (double)*it_prev) / (double)(*it - *it_prev)) /
+ double(SAMPLE_LENGTH - 1);
+}
+
+__global__ void initStorage(unsigned scale, unsigned test_size) {
+ cudaFree(cdf_result);
+ cudaMalloc(&cdf_result, scale * test_size * sizeof(bool));
+ memset(cdf_result, 0, scale * test_size * sizeof(bool));
+ insert_value = 0;
+ // printf("initCuda storage\n");
+#ifdef TEST_BOUNDS
+ index_max = 0;
+ index_min = 0x7fffffff;
+#endif
+}
+
+__global__ void initCuda(key_type *sample_item, key_type *population_item) {
+ SampleItem = sample_item;
+ PopulationItem = population_item;
+ // cudaMalloc(&SampleItem, (SAMPLE_LENGTH + 2) * sizeof(unsigned long long));
+ // cudaMalloc(&Storage, TEST_SIZE * sizeof(key_type));
+ cdf_result = nullptr;
+}
+
+__global__ void kernel(unsigned long step, const double slice_size,
+ const long split_size) {
+ for (int i = 0; i < step; i++) {
+ auto tid = step * gridDim.x * blockDim.x + blockIdx.x * blockDim.x +
+ threadIdx.x + i;
+
+ // printf("%d\n", tid);
+ auto index = (int)(sample_cdf(PopulationItem[tid]) / slice_size);
+ if (cdf_result[index]) {
+ while (cdf_result[++index]) {
+ assert(index < split_size);
+ }
+ }
+ cdf_result[index] = true;
+ // atomicAdd(&insert_value, 1);
+
+#ifdef TEST_BOUNDS
+ while (true) {
+ unsigned tmp = index_min;
+ if (tmp < tid) {
+ break;
+ }
+ if (atomicCAS(&index_min, tmp, tid) == tmp) {
+ break;
+ }
+ }
+ while (true) {
+ unsigned tmp = index_max;
+ if (tmp > tid) {
+ break;
+ }
+ if (atomicCAS(&index_max, tmp, tid) == tmp) {
+ break;
+ }
+ }
+#endif
+ }
+}
+
+__global__ void print_function() {
+ // printf("%u\n", insert_value);
+#ifdef TEST_BOUNDS
+ printf("%u %u\n", index_min, index_max);
+#endif
+}
+
+int main(int argc, char const *argv[]) {
+ size_t test_size;
+ if (argc == 1) {
+ test_size = 1048576;
+ } else {
+ try {
+ test_size = std::stol(argv[1]);
+ } catch (...) {
+ return 1;
+ }
+ }
+
+ FILE *file = fopen("normal_distribution.txt", "r");
+ assert(file);
+ for (long long i; fscanf(file, "%lld ", &i) != EOF; store_into_vector(i))
+ ;
+ fclose(file);
+
+ assert(population_vector.size() == length);
+
+ sample_vector = std::vector<unsigned long long>(
+ population_vector.begin(), population_vector.begin() + SAMPLE_LENGTH - 2);
+ sample_vector.push_back(min_value);
+ sample_vector.push_back(max_value);
+
+ std::sort(sample_vector.begin(), sample_vector.end());
+
+ key_type *cudaSample = nullptr, *cudaPopulation = nullptr;
+ cudaMalloc(&cudaSample, sizeof(key_type) * (SAMPLE_LENGTH));
+ cudaMalloc(&cudaPopulation, sizeof(key_type) * TEST_SIZE);
+ cudaMemcpy(cudaSample, &sample_vector[0],
+ sizeof(key_type) * sample_vector.size(), cudaMemcpyHostToDevice);
+ cudaMemcpy(cudaPopulation, &population_vector[SAMPLE_LENGTH],
+ sizeof(key_type) * TEST_SIZE, cudaMemcpyHostToDevice);
+ // memcpy(sample_heap, sample_vector.cbegin(),sizeof(key_type) *(SAMPLE_LENGTH
+ // + 2));
+
+ initCuda<<<1, 1>>>(cudaSample, cudaPopulation);
+ cudaDeviceSynchronize();
+
+ dim3 grid_dim = 2, block_dim = 512;
+
+ unsigned long step = test_size / (grid_dim.x * block_dim.x);
+ printf("step: %lu\n", step);
+ assert(!(test_size % (grid_dim.x * block_dim.x)));
+
+ for (int scale = 2; scale <= 8; scale++) {
+ const long split_size = test_size * scale;
+ const auto slice_size = 1.0 / (double)split_size;
+ printf("scale: %i ", scale);
+ initStorage<<<1, 1>>>(scale, test_size);
+ cudaDeviceSynchronize();
+ cudaEvent_t start, stop;
+ cudaEventCreate(&start);
+ cudaEventCreate(&stop);
+ cudaEventRecord(start, nullptr);
+ kernel<<<grid_dim, block_dim>>>(step, slice_size, split_size);
+ cudaDeviceSynchronize();
+ cudaEventRecord(stop, nullptr);
+ cudaEventSynchronize(stop);
+ float time;
+ cudaEventElapsedTime(&time, start, stop);
+ cudaEventDestroy(start);
+ cudaEventDestroy(stop);
+
+ printf("time: %lf\n", time);
+ print_function<<<1, 1>>>();
+ cudaDeviceSynchronize();
+ }
+ return 0;
+} \ No newline at end of file
diff --git a/normal_distribution.cpp b/normal_distribution.cpp
index 72f914c..e9c646f 100644
--- a/normal_distribution.cpp
+++ b/normal_distribution.cpp
@@ -5,8 +5,45 @@
#include <set>
#include <vector>
+inline void
+store_into_vector(unsigned long long value, unsigned long long &max_value,
+ unsigned long long &min_value,
+ std::vector<unsigned long long> &population_vector) {
+ if (max_value < value) {
+ max_value = value;
+ }
+ if (min_value > value) {
+ min_value = value;
+ }
+ population_vector.push_back(value);
+}
+
+void checkFileValid(const std::string &name, size_t length) {
+
+ std::vector<unsigned long long> vector;
+ unsigned long long max_value = 0, min_value = 0xfffffffffffff;
+ FILE *file = fopen(name.c_str(), "r");
+ assert(file);
+ for (long long i; fscanf(file, "%lld ", &i) != EOF;
+ store_into_vector(i, max_value, min_value, vector))
+ ;
+ ;
+ fclose(file);
+
+ vector.resize(length);
+ for (auto element : vector) {
+ assert(element != max_value);
+ assert(element != min_value);
+ }
+}
+
int main(int argc, char const *argv[]) {
+ if (argc == 3) {
+ checkFileValid(argv[1], std::stol(argv[2]));
+ return 0;
+ }
+
std::random_device randomDevice;
std::mt19937 randomEngine(randomDevice());
std::normal_distribution<long double> normalDistribution(2147483648,
@@ -35,7 +72,6 @@ int main(int argc, char const *argv[]) {
auto ret = set.insert(element);
if (ret.second) {
vector.push_back(element);
- // TODO: add offset
// printf("%lld\n", element);
}
}