Skip to content

Commit

Permalink
fix #5965
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed May 11, 2018
1 parent 5f1a69e commit e70d8fa
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 82 deletions.
49 changes: 33 additions & 16 deletions rts/Sim/Path/Default/IPathFinder.cpp
Expand Up @@ -11,34 +11,51 @@ static std::vector<PathNodeStateBuffer> nodeStateBuffers;
static std::vector<IPathFinder*> pathFinderInstances;


IPathFinder::IPathFinder(unsigned int _BLOCK_SIZE)
: BLOCK_SIZE(_BLOCK_SIZE)
, BLOCK_PIXEL_SIZE(BLOCK_SIZE * SQUARE_SIZE)
, nbrOfBlocks(mapDims.mapx / BLOCK_SIZE, mapDims.mapy / BLOCK_SIZE)
, mStartBlockIdx(0)
, mGoalBlockIdx(0)
, mGoalHeuristic(0.0f)
, maxBlocksToBeSearched(0)
, testedBlocks(0)
, instanceIndex(pathFinderInstances.size())
void IPathFinder::InitStatic() { pathFinderInstances.reserve(8); }
void IPathFinder::KillStatic() { pathFinderInstances.clear ( ); }


void IPathFinder::Init(unsigned int _BLOCK_SIZE)
{
pathFinderInstances.push_back(this);
{
BLOCK_SIZE = _BLOCK_SIZE;
BLOCK_PIXEL_SIZE = BLOCK_SIZE * SQUARE_SIZE;

nbrOfBlocks.x = mapDims.mapx / BLOCK_SIZE;
nbrOfBlocks.y = mapDims.mapy / BLOCK_SIZE;

mStartBlockIdx = 0;
mGoalBlockIdx = 0;

mGoalHeuristic = 0.0f;

maxBlocksToBeSearched = 0;
testedBlocks = 0;

instanceIndex = pathFinderInstances.size();
}
{
openBlockBuffer.Clear();
// handled via AllocStateBuffer
// blockStates.Clear();
openBlocks.Clear();
dirtyBlocks.clear();
}
{
pathFinderInstances.push_back(this);
}

AllocStateBuffer();
ResetSearch();
}

IPathFinder::~IPathFinder()
void IPathFinder::Kill()
{
// allow our PNSB to be reused across reloads
nodeStateBuffers[instanceIndex] = std::move(blockStates);
}


void IPathFinder::InitStatic() { pathFinderInstances.reserve(8); }
void IPathFinder::KillStatic() { pathFinderInstances.clear ( ); }


void IPathFinder::AllocStateBuffer()
{
if (instanceIndex >= nodeStateBuffers.size())
Expand Down
24 changes: 12 additions & 12 deletions rts/Sim/Path/Default/IPathFinder.h
Expand Up @@ -3,8 +3,6 @@
#ifndef IPATH_FINDER_H
#define IPATH_FINDER_H

#include <list>
#include <queue>
#include <cstdlib>

#include "IPath.h"
Expand All @@ -19,8 +17,10 @@ class CSolidObject;

class IPathFinder {
public:
IPathFinder(unsigned int BLOCK_SIZE);
virtual ~IPathFinder();
virtual ~IPathFinder() {}

void Init(unsigned int BLOCK_SIZE);
void Kill();

static void InitStatic();
static void KillStatic();
Expand Down Expand Up @@ -124,22 +124,22 @@ class IPathFinder {

public:
// if larger than 1, this IPF is an estimator
const unsigned int BLOCK_SIZE;
const unsigned int BLOCK_PIXEL_SIZE;
unsigned int BLOCK_SIZE = 0;
unsigned int BLOCK_PIXEL_SIZE = 0;

int2 nbrOfBlocks;
int2 mStartBlock;

unsigned int mStartBlockIdx;
unsigned int mGoalBlockIdx; // set during each search as the square closest to the goal
unsigned int mStartBlockIdx = 0;
unsigned int mGoalBlockIdx = 0; // set during each search as the square closest to the goal

// heuristic value of goalSquareIdx
float mGoalHeuristic;
float mGoalHeuristic = 0.0f;

unsigned int maxBlocksToBeSearched;
unsigned int testedBlocks;
unsigned int maxBlocksToBeSearched = 0;
unsigned int testedBlocks = 0;

unsigned int instanceIndex;
unsigned int instanceIndex = 0;

PathNodeBuffer openBlockBuffer;
PathNodeStateBuffer blockStates;
Expand Down
12 changes: 10 additions & 2 deletions rts/Sim/Path/Default/PathDataTypes.h
Expand Up @@ -42,7 +42,15 @@ struct lessCost: public std::binary_function<PathNode*, PathNode*, bool> {

struct PathNodeBuffer {
public:
PathNodeBuffer(): idx(0) {}
PathNodeBuffer() { Clear(); }

void Clear() {
for (unsigned int i = 0; i < MAX_SEARCHED_NODES; i++) {
buffer[i] = {};
}

SetSize(0);
}

void SetSize(unsigned int i) { idx = i; }
unsigned int GetSize() const { return idx; }
Expand All @@ -52,7 +60,7 @@ struct PathNodeBuffer {

private:
/// index of the most recently added node
unsigned int idx;
unsigned int idx = 0;

PathNode buffer[MAX_SEARCHED_NODES];
};
Expand Down
56 changes: 37 additions & 19 deletions rts/Sim/Path/Default/PathEstimator.cpp
Expand Up @@ -52,21 +52,36 @@ static size_t GetNumThreads() {



CPathEstimator::CPathEstimator(IPathFinder* pf, unsigned int BLOCK_SIZE, const std::string& cacheFileName, const std::string& mapFileName)
: IPathFinder(BLOCK_SIZE)
, BLOCKS_TO_UPDATE(SQUARES_TO_UPDATE / (BLOCK_SIZE * BLOCK_SIZE) + 1)
, nextOffsetMessageIdx(0)
, nextCostMessageIdx(0)
, pathChecksum(0)
, fileHashCode(CalcHash(__func__))
, offsetBlockNum(nbrOfBlocks.x * nbrOfBlocks.y)
, costBlockNum(nbrOfBlocks.x * nbrOfBlocks.y)
, parentPathFinder(pf)
, nextPathEstimator(nullptr)
, blockUpdatePenalty(0)
void CPathEstimator::Init(IPathFinder* pf, unsigned int BLOCK_SIZE, const std::string& cacheFileName, const std::string& mapFileName)
{
vertexCosts.resize(moveDefHandler.GetNumMoveDefs() * blockStates.GetSize() * PATH_DIRECTION_VERTICES, PATHCOST_INFINITY);
maxSpeedMods.resize(moveDefHandler.GetNumMoveDefs(), 0.001f);
IPathFinder::Init(BLOCK_SIZE);

{
BLOCKS_TO_UPDATE = SQUARES_TO_UPDATE / (BLOCK_SIZE * BLOCK_SIZE) + 1;

blockUpdatePenalty = 0;
nextOffsetMessageIdx = 0;
nextCostMessageIdx = 0;

pathChecksum = 0;
fileHashCode = CalcHash(__func__);

offsetBlockNum = {nbrOfBlocks.x * nbrOfBlocks.y};
costBlockNum = {nbrOfBlocks.x * nbrOfBlocks.y};

parentPathFinder = pf;
nextPathEstimator = nullptr;
}
{
vertexCosts.clear();
vertexCosts.resize(moveDefHandler.GetNumMoveDefs() * blockStates.GetSize() * PATH_DIRECTION_VERTICES, PATHCOST_INFINITY);
maxSpeedMods.clear();
maxSpeedMods.resize(moveDefHandler.GetNumMoveDefs(), 0.001f);

updatedBlocks.clear();
consumedBlocks.clear();
offsetBlocksSortedByCost.clear();
}

CPathEstimator* childPE = this;
CPathEstimator* parentPE = dynamic_cast<CPathEstimator*>(pf);
Expand Down Expand Up @@ -117,7 +132,7 @@ CPathEstimator::CPathEstimator(IPathFinder* pf, unsigned int BLOCK_SIZE, const s
}


CPathEstimator::~CPathEstimator()
void CPathEstimator::Kill()
{
pcMemPool.free(pathCache[0]);
pcMemPool.free(pathCache[1]);
Expand All @@ -129,12 +144,15 @@ void CPathEstimator::InitEstimator(const std::string& cacheFileName, const std::
const unsigned int numThreads = GetNumThreads();

if (threads.size() != numThreads) {
threads.clear();
threads.resize(numThreads);
pathFinders.clear();
pathFinders.resize(numThreads);
}

// always use PF for initialization, later PE maybe used
pathFinders[0] = pfMemPool.alloc<CPathFinder>();
// TODO: pooling these will not help much, need to reuse
pathFinders[0] = pfMemPool.alloc<CPathFinder>(true);

// Not much point in multithreading these...
InitBlocks();
Expand All @@ -151,8 +169,8 @@ void CPathEstimator::InitEstimator(const std::string& cacheFileName, const std::
const char* fmtStrs[4] = {
"[%s] creating PE%u cache with %u PF threads (%u MB)",
"[%s] creating PE%u cache with %u PF thread (%u MB)",
"[%s] writing PE%u %s-cache to file",
"[%s] written PE%u %s-cache to file",
"[%s] writing PE%u cache to file %s",
"[%s] written PE%u cache to file %s",
};

{
Expand All @@ -165,7 +183,7 @@ void CPathEstimator::InitEstimator(const std::string& cacheFileName, const std::
spring::barrier pathBarrier(numExtraThreads + 1);

for (unsigned int i = 1; i <= numExtraThreads; i++) {
pathFinders[i] = pfMemPool.alloc<CPathFinder>();
pathFinders[i] = pfMemPool.alloc<CPathFinder>(true);
threads[i] = std::move(spring::thread(&CPathEstimator::CalcOffsetsAndPathCosts, this, i, &pathBarrier));
}

Expand Down
22 changes: 11 additions & 11 deletions rts/Sim/Path/Default/PathEstimator.h
Expand Up @@ -40,8 +40,8 @@ class CPathEstimator: public IPathFinder {
* name of the corresponding map.
* Ex. PE-name "pe" + Mapname "Desert" => "Desert.pe"
*/
CPathEstimator(IPathFinder*, unsigned int BSIZE, const std::string& cacheFileName, const std::string& mapFileName);
~CPathEstimator();
void Init(IPathFinder*, unsigned int BSIZE, const std::string& cacheFileName, const std::string& mapFileName);
void Kill();


/**
Expand Down Expand Up @@ -122,16 +122,18 @@ class CPathEstimator: public IPathFinder {
friend class CPathManager;
friend class CDefaultPathDrawer;

const unsigned int BLOCKS_TO_UPDATE;
unsigned int BLOCKS_TO_UPDATE = 0;

unsigned int nextOffsetMessageIdx;
unsigned int nextCostMessageIdx;
unsigned int nextOffsetMessageIdx = 0;
unsigned int nextCostMessageIdx = 0;

std::uint32_t pathChecksum;
std::uint32_t fileHashCode;
int blockUpdatePenalty = 0;

std::atomic<std::int64_t> offsetBlockNum;
std::atomic<std::int64_t> costBlockNum;
std::uint32_t pathChecksum = 0;
std::uint32_t fileHashCode = 0;

std::atomic<std::int64_t> offsetBlockNum = {0};
std::atomic<std::int64_t> costBlockNum = {0};

IPathFinder* parentPathFinder; // parent (PF if BLOCK_SIZE is 16, PE[16] if 32)
CPathEstimator* nextPathEstimator; // next lower-resolution estimator
Expand All @@ -145,8 +147,6 @@ class CPathEstimator: public IPathFinder {
/// blocks that may need an update due to map changes
std::deque<int2> updatedBlocks;

int blockUpdatePenalty;

struct SOffsetBlock {
float cost;
int2 offset;
Expand Down
15 changes: 8 additions & 7 deletions rts/Sim/Path/Default/PathFinder.cpp
Expand Up @@ -73,13 +73,6 @@ static constexpr float3 PF_DIRECTION_VECTORS_3D[] = {
};


CPathFinder::CPathFinder(bool threadSafe): IPathFinder(1)
{
blockCheckFunc = blockCheckFuncs[threadSafe];
dummyCacheItem = CPathCache::CacheItem{IPath::Error, {}, {-1, -1}, {-1, -1}, -1.0f, -1};
}


void CPathFinder::InitStatic() {
static_assert(PF_DIRECTION_COSTS[PATHOPT_LEFT ] == 1.0f, "");
static_assert(PF_DIRECTION_COSTS[PATHOPT_RIGHT ] == 1.0f, "");
Expand Down Expand Up @@ -117,7 +110,15 @@ void CPathFinder::InitStatic() {
temp.SafeNormalize();
assert(temp == PF_DIRECTION_VECTORS_3D[i]);
}
}


void CPathFinder::Init(bool threadSafe)
{
IPathFinder::Init(1);

blockCheckFunc = blockCheckFuncs[threadSafe];
dummyCacheItem = CPathCache::CacheItem{IPath::Error, {}, {-1, -1}, {-1, -1}, -1.0f, -1};
}


Expand Down
9 changes: 6 additions & 3 deletions rts/Sim/Path/Default/PathFinder.h
Expand Up @@ -3,7 +3,6 @@
#ifndef PATH_FINDER_H
#define PATH_FINDER_H

#include <list>
#include <vector>
#include <deque>

Expand All @@ -19,10 +18,14 @@ class CPathFinderDef;

class CPathFinder: public IPathFinder {
public:
CPathFinder(bool threadSafe = true);

static void InitStatic();

CPathFinder() = default; // defer Init
CPathFinder(bool threadSafe) { Init(threadSafe); }

void Init(bool threadSafe);
void Kill() { IPathFinder::Kill(); }

typedef CMoveMath::BlockType (*BlockCheckFunc)(const MoveDef&, int, int, const CSolidObject*);

protected:
Expand Down

0 comments on commit e70d8fa

Please sign in to comment.