I got ERROR: OutOfMemoryError() while running this code. How can i mitigate it? Here 872 is number of mesh blocks of whose coordinates are x, y and z.
x = rand(16,872);
y = rand(4,872);
z = rand(18,872);
r = reshape(x, :, 1, 1)
θ = reshape(y, 1, :, 1)
ϕ = reshape(z, 1, 1, :)
X = r .* sin.(θ) .* cos.(ϕ)
Y = r .* sin.(θ) .* sin.(ϕ)
Z = r .* cos.(θ)
My laptop have 32Gb ram. My lscpu output looks like:
I mean, you’re creating two arrays with 763839184896 8-byte elements in them. If I got my orders of magnitude right, that’s 12 terabytes right there, no?
I want to write this code below using vectorisation @. so that i don’t iterate in for loops.
for b in 1:872
xs = x[:, b]
ys = y[:, b]
zs = z[:, b]
for i in 1:length(xs), j in 1:length(ys), k in 1:length(zs)
r, θ, ϕ = xs[i], ys[j], zs[k]
X = r * sin(θ) * cos(ϕ)
Y = r * sin(θ) * sin(ϕ)
Z = r * cos(θ)
push!(all_xs, X)
push!(all_ys, Y)
push!(all_zs, Z)
end
end
Is my x, y and z array reshaping code of first starting post wrong?
Well, you got it wrong. You rather need something like this, and possibly permute things to get the orderings right.
x = rand(16,872);
y = rand(4,872);
z = rand(18,872);
r = reshape(x, :, 1, 1, 872)
θ = reshape(y, 1, :, 1, 872)
ϕ = reshape(z, 1, 1, :, 872)
X = vec(r .* sin.(θ) .* cos.(ϕ))
Y = vec(r .* sin.(θ) .* sin.(ϕ))
Z = vec(r .* cos.(θ))
Why do you want to? Vectorizing this kind of code most of the time makes it harder to reason about and there should be no fundamental differences in speed. If you want to run it on GPU I could see a point. But as you can already see, it’s easy to get things wrong.