Hello everyone!
I am working with CuArray
s of complex numbers. After some previous processing, I would like to calculate the variance of only the real part of the array along the first dimension. In order to avoid copying I use
complex_array = CUDA.rand(ComplexF32, 100, 100000) # Example array
array_reinterpreted = reinterpret(reshape, Float32, complex_array)
array_real = selectdim(array_reinterpreted, 1, 1)
variances = var(array_real, dims=1)
However, this seems to be calling the CPU implementation in Statistics.jl which throws an error because that function tries to access the array with first(A)
.
From MD with Subarrays and CuArrays - #6 by maleadt, I tried to implement a version of Statistics functions on GPU? - #2 by maleadt replacing CuArray
with AnyCuArray
or StridedCuArray
but in both cases the result ended up being slower than computing the variance of both, real and imaginary parts and selecting only the real section of the reinterpreted array:
using Statistics
using CUDA
using Benchmarktools
complex_array = CUDA.rand(Float32, 100, 100000)
array_reinterpreted = reinterpret(reshape, Float32, complex_array)
@btime begin
variances = selectdim(var(array_reinterpreted, dims=2), 1, 1)
end
gave 15.163 μs (86 allocations: 2.39 KiB)
whereas using
using Statistics
using CUDA
using Benchmarktools
# File with implementation for AnyCuArray or Strided CuArray
include("src/statistics.jl")
complex_array = CUDA.rand(Float32, 100, 100000)
array_reinterpreted = reinterpret(reshape, Float32, complex_array)
@btime begin
variances = var(selectdim(array_reinterpreted, 1, 1), dims=1)
end
gave 58.351 μs (370 allocations: 11.39 KiB)
.
I was wondering if there is a faster way of calculating these variances by avoiding copying the real part of the complex array or computing unnecessarily the variance of the imaginary part of the array.