user3371024
user3371024

Reputation: 15

C++: Unsure if code is multithreadable

I'm working on a small piece of code which takes a very large amount of time to complete, so I was thinking of multithreading it either with pthread (which I hardly understand but think I can master a lot quicker) or with some GPGPU implementation (probably OpenCL as I have an AMD card at home and the PCs I use at my office have various NVIDIA cards)

   while(sDead < (unsigned long) nrPoints*nrPoints) {

    pPoint1 = distrib(*rng);
    pPoint2 = distrib(*rng);

    outAxel = -1;

    if(pPoint1 != pPoint2) {

      point1 = space->getPointRef(pPoint1);
      point2 = space->getPointRef(pPoint2);

      outAxel = point1->influencedBy(point2, distThres);

      if(outAxel == 0 || outAxel == 1)
    sDead++;
      else
    sDead = 0;

    }

    i++;
  }

Where distrib is a uniform_int_distribution with a = 0 and b = nrPoints-1. For clarity, here is the structure I'm working with:

class Space{
  vector<Point> points
  (more stuff)
}

class Point {
  vector<Coords> coordinates
  (more stuff)
}

struct Coords{
  char Range
  bool TypeOfCoord
  char Coord
}

The length of coordinates is the same for all Points and Point[x].Coord[y].Range == Point[z].Coord[y].Range for all x, y and z. The same goes for TypeOfCoord.

Some background: during each run of the while loop, two randomly drawn Points from space are tested for interaction. influencedBy() checks whether or not point1 and point2 are close enough to eachother (distance is dependent on some metric but it boils down to similarity in Coord. If the distance is smaller than distThres, interaction is possible) to interact. Interaction means that one of the Coord variables which doesn't equal the corresponding Coord in the other object is flipped to equal it. This decreases the distance between the Points but also changes the distance of the changed point to every other point in Space, hence my question of whether or not this is multithreadable. As I said, I'm a complete newbie to multithreading and I'm not sure if I can safely implement a function that chops this up, so I was looking for your input. Suggestions are also very welcome.

E: The influencedby() function (and the functions it in turn calls) can be found here. Functions that I did not include, such as getFeature() and getNrFeatures() are tiny and cannot possibly contribute much. Take note that I used generalised names for objects in this question but I might mess up or make it more confusing if I replace them in the other code, so I've left the original names there. For the record:

Space = CultSpace
Point = CultVec
Points = Points
Coordinates = Feats
Coords = Feature
TypeOfCoord = Nomin
Coord = Trait

Upvotes: 0

Views: 151

Answers (1)

laune
laune

Reputation: 31300

(Choosing "Answer" because the format permits better presentation. Not quite what your're asking for, but let's clarify this first.)

Later

How often is the loop executed until this condition becomes true?

while(sDead < (unsigned long) nrPoints*nrPoints) {

Probably not a big gain, but:

pPoint1 = distrib(*rng);
do {
   pPoint2 = distrib(*rng);
while( pPoint1 == pPoint2 );


outAxel = -1;

How costly is getPointRef? Linear search in Space?

point1 = space->getPointRef(pPoint1);
point2 = space->getPointRef(pPoint2);

outAxel = point1->influencedBy(point2, distThres);

Is it really necessary to recompute the "distance of the changed point to every other point in Space" immediately after a "flip"?

Upvotes: 1

Related Questions