parallel is slower than serial

Yura yuriy.min at gmail.com
Tue Oct 18 11:56:30 UTC 2022


Dear All,

I am trying to make a simple code run in parallel. The parallel 
version works, and gives the same number as serial albeit slower.

First, the parallel features I am using:

import core.thread: Thread;
import std.range;
import std.parallelism:parallel;
import std.parallelism:taskPool;
import std.parallelism:totalCPUs;

// Then, I have an array of structures

shared Sphere [] dot;

// Each Sphere is

struct Sphere {
   string El;
   double x;
   double y;
   double z;
   double S;
   double Z;
   double V;
}

// Then for each Sphere, i.e. dot[i]
// I need to do some arithmetics with itself and other dots
// I have only parallelized the inner loop, i is fixed.

// parallel loop
auto I = std.range.iota(0,dot.length);
shared double [] Ai;
Ai.length = dot.length;
foreach (j;parallel(I)) {
   Ai[j] = GETAij (i, j, dot[i], dot[j]);
}

for (auto j=0;j<Ai.length;j++) {
   A = A ~ Ai[j];
}

// the function GETAij

// this is the function to calculate Aij cells
// in parallel

double GETAij (ulong i, ulong j, Sphere dot_i, Sphere dot_j) {
   double Aij;
   if (i == j) {
     Aij = 1.0694*pow((4*pi/dot_i.S),0.5);
   }
   else {
     Aij = 1/(distDD(dot_i,dot_j));
   }
   return Aij;
}

double distDD (Sphere A, Sphere B) {
   double dx2 = (A.x-B.x)*(A.x-B.x);
   double dy2 = (A.y-B.y)*(A.y-B.y);
   double dz2 = (A.z-B.z)*(A.z-B.z);
   double d = pow((dx2 + dy2 + dz2),0.5);
   return d;
}

What I am doing wrong? Any advanced options for the ldc2 
compiler? Many thanks in advance!



More information about the Digitalmars-d-learn mailing list