summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunoiSayami <[email protected]>2023-05-17 14:08:08 +0800
committerKunoiSayami <[email protected]>2023-05-17 14:08:08 +0800
commit1ad1e0ebf58da30ee7f65a3a1f2a8f62900c47d5 (patch)
tree7d5dfa70354deee42d436328cfb6474a38dbb1ef
parente503cc4de4f7f2e3387e55c77ab1690f709b7687 (diff)
fix: Fix fast_log
Signed-off-by: KunoiSayami <[email protected]>
-rw-r--r--expt_0516_2.cu19
-rw-r--r--sortlib.cuh19
2 files changed, 32 insertions, 6 deletions
diff --git a/expt_0516_2.cu b/expt_0516_2.cu
index f0eb69a..0661e47 100644
--- a/expt_0516_2.cu
+++ b/expt_0516_2.cu
@@ -1,4 +1,7 @@
// Experimental content: Test CustomSort Calculation
+#define DISABLE_TEST_WARNING
+#include "sortlib.cuh"
+
#include <algorithm>
#include <cstdio>
@@ -50,12 +53,24 @@ long pow_for_sample(long n) {
}
int main(int argc, char const *argv[]) {
- auto max = 30L;
+ auto max = 32L;
+ auto cuda_only = false;
+
if (argc > 1) {
max = strtol(argv[1], nullptr, 10);
}
- for (int i = 4; i < max; i++) {
+
+ if (argc > 2) {
+ cuda_only = strtol(argv[2], nullptr, 10);
+ }
+
+ for (int i = 4; i <= max && !cuda_only; i++) {
CustomSort::testSelf(pow_for_sample(i));
printf("%d pass\n", i);
}
+
+ for (int i = 4; i <= max; i++) {
+ testCustomCalculationWithPrint<<<1, 1>>>(pow_for_sample(i));
+ cudaDeviceSynchronize();
+ }
}
diff --git a/sortlib.cuh b/sortlib.cuh
index fa306b0..5d32e4b 100644
--- a/sortlib.cuh
+++ b/sortlib.cuh
@@ -17,10 +17,12 @@ public:
const int MOVE_OFFSET;
const unsigned int STEP_LIMIT;
- // IEEE 754
__device__ __host__ static size_t fast_log(size_t a) {
- float t = a;
- return (((*(int *)&t) >> 23) + 1) & 127;
+#ifdef __CUDA_ARCH__
+ return log2((double)a);
+#else
+ return std::log2(a);
+#endif
}
__device__ __host__ size_t calculate_index(size_t rank) const {
@@ -38,7 +40,11 @@ public:
for (size_t i = 0; i < LENGTH; i++) {
auto left = calculate_index(i);
auto right = calculate_rank(left - 1);
-
+ if (i + 1 == LENGTH || i + 1 != right) {
+ printf("%lu %lu %lu\n", i, left, right);
+ printf("%lu %lu %lu\n", LENGTH, fast_log(i + 1),
+ (LENGTH + 1) >> fast_log(i + 1) >> 1);
+ }
assert(i + 1 == right);
}
}
@@ -176,6 +182,11 @@ __global__ void testCustomCalculation(size_t length) {
CustomSort(length, sizeof(long) * 8).testCalculation();
}
+__global__ void testCustomCalculationWithPrint(size_t length) {
+ CustomSort(length, sizeof(long) * 8).testCalculation();
+ printf("%lu pass\n", length);
+}
+
class FactorySort {
public: