I suspect the overhead of using channels and starting goroutines is bigger than the speedup from the parallelization.
I attached the whole code to this email but the interesting parts are the following:
func (body Body) computeForce(body2 Body, dt float64, c chan Result, i,j int){
res := new(Result);
res.i = i;
res.j = j;
dx := body.x - body2.x;
dy := body.y - body2.y;
dz := body.z - body2.z;
dSquared := dx*dx + dy*dy + dz*dz;
distance := math.Sqrt(dSquared);
mag := dt / (dSquared * distance);
res.vx = dx * body2.mass * mag;
res.vy = dy * body2.mass * mag;
res.vz = dz * body2.mass * mag;
res.vx2 = dx * body.mass * mag;
res.vy2 = dy * body.mass * mag;
res.vz2 = dz * body.mass * mag;
c <- *res;
}
func (sys System) advance(dt float64) {
N := ((len(sys)*(len(sys)-1))/2);
c := make(chan Result, N); // Buffering optional but sensible.
for i, body := range sys {
for j := i+1; j < len(sys); j++ {
go body.computeForce(*sys[j], dt, c, i, j);
}
}
// Drain the channel.
for i := 0; i < N; i++ {
res := <-c;
sys[res.i].vx -= res.vx;
sys[res.i].vy -= res.vy;
sys[res.i].vz -= res.vz;
sys[res.j].vx += res.vx2;
sys[res.j].vy += res.vy2;
sys[res.j].vz += res.vz2;
}
// All done.
for _, body := range sys {
body.x += dt * body.vx;
body.y += dt * body.vy;
body.z += dt * body.vz;
}
}
Is my assumption right or am I doing something wrong in my code that slows it down?
greets
Sebastian