Hi there.
|
#include <cuda_runtime.h> |
|
|
|
__device__ unsigned int fnv1a_hash(int input) { |
|
const unsigned int FNV_PRIME = 16777619; |
|
const unsigned int OFFSET_BASIS = 2166136261; |
|
|
|
unsigned int hash = OFFSET_BASIS; |
|
|
|
for (int byte_pos = 0; byte_pos < 4; byte_pos++) { |
|
unsigned char byte = (input >> (byte_pos * 8)) & 0xFF; |
|
hash = (hash ^ byte) * FNV_PRIME; |
|
} |
|
|
|
return hash; |
|
} |
|
|
|
__global__ void fnv1a_hash_kernel(const int* input, unsigned int* output, int N, int R) {} |
|
|
|
// input, output are device pointers (i.e. pointers to memory on the GPU) |
|
extern "C" void solve(const int* input, unsigned int* output, int N, int R) { |
|
int threadsPerBlock = 256; |
|
int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock; |
|
|
|
fnv1a_hash_kernel<<<blocksPerGrid, threadsPerBlock>>>(input, output, N, R); |
|
cudaDeviceSynchronize(); |
|
} |
The input type is int, while the output type is unsigned int.
Since we need to iterate the fnv1a_hash $R$ times, there is a type mismatch.
It would be better to change the input type to unsigned int.
Thanks.
example answer.
#include <cuda_runtime.h>
__device__ unsigned int fnv1a_hash(int input) { // mismatch
const unsigned int FNV_PRIME = 16777619;
const unsigned int OFFSET_BASIS = 2166136261;
unsigned int hash = OFFSET_BASIS;
for (int byte_pos = 0; byte_pos < 4; byte_pos++) {
unsigned char byte = (input >> (byte_pos * 8)) & 0xFF;
hash = (hash ^ byte) * FNV_PRIME;
}
return hash;
}
__global__ void fnv1a_hash_kernel(const int* input, unsigned int* output, int N, int R) {
int idx = blockDim.x * blockIdx.x + threadIdx.x;
if (idx < N) {
output[idx] = input[idx]; // mismatch
for (int i = 0 ; i < R ; ++i) {
output[idx] = fnv1a_hash(output[idx]); // mismatch
}
}
}
// input, output are device pointers (i.e. pointers to memory on the GPU)
extern "C" void solve(const int* input, unsigned int* output, int N, int R) {
int threadsPerBlock = 256;
int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
fnv1a_hash_kernel<<<blocksPerGrid, threadsPerBlock>>>(input, output, N, R);
cudaDeviceSynchronize();
}
Hi there.
leetgpu-challenges/challenges/easy/24_rainbow_table/starter/starter.cu
Lines 1 to 26 in d9fbdf6
The$R$ times, there is a type mismatch.
inputtype isint, while theoutputtype isunsigned int.Since we need to iterate the
fnv1a_hashIt would be better to change the
inputtype tounsigned int.Thanks.
example answer.