GPUArrays.jl icon indicating copy to clipboard operation
GPUArrays.jl copied to clipboard

Scalar indexing for `Base.hash`

Open nHackel opened this issue 7 months ago • 1 comments

Hello, I've recently noticed that GPUArrays don't define a Base.hash implementation and fallback to the default one. This requires one to @allowscalar which is slow and also means one has to differentiate between CPU and GPU arrays when calling hash.

MWE:

using GPUArrays, CUDA

A = cu(rand(1024, 1024))

hash(A) # errors

@allowscalar Base.hash(A) # slow

I'm not sure what a good implementation for GPU arrays would be. An inefficient GPU default could be:

Base.hash(arr::T, h) where T <: AbstractGPUArray = mapreduce(hash, hash, arr; init = hash(T, h))

That of course touches every element and works with UInt64 values, but it would be faster than the normal default.

From what I can tell the default Base.hash for arrays is accessing O(log n) elements. I'm not sure how to neatly map such a pattern onto GPUs, if someone has any pointers I'd be happy to implement it

nHackel avatar Mar 01 '25 08:03 nHackel