Closed LaurieGustavo closed 5 years ago
The problem is in the propagation
function, which is fully sequential (runs on the CPU). This is extra bad in Futhark, because when using the CUDA backend, all arrays are currently naively stored on the GPU, even if they are only used on the CPU. This adds orders of magnitude of overhead to the memory access latency.
I think the best way to rewrite propagation
is to perform the complicated indexing on f_out
, such that the result can be computed with a map
nest. However, I don't know Lattice-Boltzman methods well enough to say whether this is reasonable. If you do need complicated in-place updates on the result, then the only way is to use scatter
, which operates on a flat array:
let propagation [nx][ny] (f_out: [nx][ny][9]f64): [nx][ny][9]f64 =
let f_in = replicate (nx*ny*9) 0
let indices = tabulate_3d nx ny 9 (\x y i -> ((x+(i32.f64 c[i].1)+nx)%nx) * ny * 9 +
(((y+(i32.f64 c[i].2)+ny)%ny)*9) +
i)
|> flatten_3d
in scatter f_in indices (flatten_3d f_out) |> unflatten_3d nx ny 9
Notoe the nasty index computations to turn the (conceptually) multi-dimensional index into a flat row-major index. The above will be fully parallel.
Thank you very much, i'll give it a try
Here is an even better approach, without nasty flattening:
let propagation [nx][ny] (f_out: [nx][ny][9]f64): [nx][ny][9]f64 =
tabulate_3d nx ny 9 (\x y i -> f_out[(x-(i32.f64 c[i].1))%nx,
(y-(i32.f64 c[i].2))%ny,
i])
You may need an unsafe
to avoid bounds checking.
May the glory of your name be sung from here to the very gates of heaven and beyond (a.k.a thanks)
Does it work? Can we close this issue?
It does work. Now i'm basically trying to do some optimizations. I can't guarantee that i won't have any new questions about this specific code and Futhark until 11 July, when my work is due. Can we keep this issue opened until then ?
Sure, no problem.
Hello,
I'm implementing the Lattice-Boltzmann method (for fluid flows) in Futhark. My goal is to execute it on GPU using CUDA. It works but it's very slow, many times slower than the translated sequential C code.
So i'd like to ask if there is any obvious mistake or misuse of Futhark in my code that would lead to inefficient parallel code. Here's a few things i'm not sure of the impact :
I'm also looking with my university if i'm not simply misusing the gpu environment they gave me