Skip to content

Commit

Permalink
improve gpu naive impl
Browse files Browse the repository at this point in the history
  • Loading branch information
unicoooorn committed Jan 8, 2025
1 parent 69938ac commit 7c950c6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/cl/nbody.cl
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ __kernel void nbody_calculate_force_global(

float dx = x1 - x0;
float dy = y1 - y0;
float dr2 = max(100.f, dx * dx + dy * dy);

float r2 = max(100.0f, pow(dx, 2) + pow(dy, 2));
float r = sqrt(r2);
float dr2_inv = 1.f / dr2;
float dr_inv = sqrt(dr2_inv);

float f = m1 / r2;
float ex = dx * dr_inv;
float ey = dy * dr_inv;

dvx[i] += f * dx / r;
dvy[i] += f * dy / r;
}
float fx = ex * dr2_inv * GRAVITATIONAL_FORCE;
float fy = ey * dr2_inv * GRAVITATIONAL_FORCE;

dvx[i] *= GRAVITATIONAL_FORCE;
dvy[i] *= GRAVITATIONAL_FORCE;
dvx[i] += m1 * fx;
dvy[i] += m1 * fy;
}
}

__kernel void nbody_integrate(
Expand Down

0 comments on commit 7c950c6

Please sign in to comment.