diff options
Diffstat (limited to 'expt_0516_2.cu')
| -rw-r--r-- | expt_0516_2.cu | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/expt_0516_2.cu b/expt_0516_2.cu index 0661e47..860e146 100644 --- a/expt_0516_2.cu +++ b/expt_0516_2.cu @@ -11,37 +11,47 @@ class CustomSort { public: explicit CustomSort(size_t length) : LENGTH(length) {} const size_t LENGTH; - static size_t fast_log(size_t a) { - float t = a; - return (((*(int *)&t) >> 23) + 1) & 127; + + __device__ __host__ static size_t fast_log(size_t a) { +#ifdef __CUDA_ARCH__ + return (size_t)log2((double)a); +#else + return (size_t)std::log2(a); +#endif } - size_t calculate_index(size_t rank) const { + __device__ __host__ size_t calculate_index(size_t rank) const { size_t bit_low = (LENGTH + 1) >> fast_log(++rank) >> 1; return (((rank << 1) | 1) * bit_low - LENGTH - 1); } - size_t calculate_rank(size_t index) const { + __device__ __host__ size_t calculate_rank(size_t index) const { index++; size_t low_bit = index & (-index); return ((LENGTH + index) / low_bit) >> 1; } - __attribute__((unused)) void testCalculation() const { + __device__ __host__ void testCalculation() const { 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) { + /*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); } } static void testSelf(size_t length) { CustomSort(length).testCalculation(); } }; + +__global__ void testCustomCalculationWithPrint(size_t length) { + CustomSort(length).testCalculation(); + printf("%lu pass\n", length); +} + #endif long pow_for_sample(long n) { |
