Fix check of kdtree loading from disk

This commit is contained in:
Guilhem Lavaux 2022-05-27 16:26:59 +03:00
parent e2a2c7287c
commit 59bb99e7ee

View File

@ -33,7 +33,7 @@ namespace CosmoTool {
template<int N, typename ValType, typename CType, typename CellSplitter> template<int N, typename ValType, typename CType, typename CellSplitter>
KDTree<N,ValType,CType,CellSplitter>::KDTree(Cell *cells, NodeIntType Ncells) KDTree<N,ValType,CType,CellSplitter>::KDTree(Cell *cells, NodeIntType Ncells)
{ {
periodic = false; periodic = false;
base_cell = cells; base_cell = cells;
numNodes = Ncells; numNodes = Ncells;
@ -41,7 +41,7 @@ namespace CosmoTool {
sortingHelper = new Cell *[Ncells]; sortingHelper = new Cell *[Ncells];
for (NodeIntType i = 0; i < Ncells; i++) for (NodeIntType i = 0; i < Ncells; i++)
sortingHelper[i] = &cells[i]; sortingHelper[i] = &cells[i];
optimize(); optimize();
} }
@ -73,14 +73,14 @@ namespace CosmoTool {
absoluteMax[k] = cell->coord[k]; absoluteMax[k] = cell->coord[k];
} }
} }
std::cout << " rebuilding the tree..." << std::endl; std::cout << " rebuilding the tree..." << std::endl;
root = buildTree(sortingHelper, activeCells, 0, absoluteMin, absoluteMax); root = buildTree(sortingHelper, activeCells, 0, absoluteMin, absoluteMax);
std::cout << " done." << std::endl; std::cout << " done." << std::endl;
} }
template<int N, typename ValType, typename CType, typename CellSplitter> template<int N, typename ValType, typename CType, typename CellSplitter>
uint64_t KDTree<N,ValType,CType,CellSplitter>::getIntersection(const coords& x, CoordType r, uint64_t KDTree<N,ValType,CType,CellSplitter>::getIntersection(const coords& x, CoordType r,
KDTree<N,ValType,CType,CellSplitter>::Cell **cells, KDTree<N,ValType,CType,CellSplitter>::Cell **cells,
uint64_t numCells) uint64_t numCells)
{ {
@ -112,7 +112,7 @@ namespace CosmoTool {
} }
template<int N, typename ValType, typename CType, typename CellSplitter> template<int N, typename ValType, typename CType, typename CellSplitter>
uint64_t KDTree<N,ValType,CType,CellSplitter>::getIntersection(const coords& x, CoordType r, uint64_t KDTree<N,ValType,CType,CellSplitter>::getIntersection(const coords& x, CoordType r,
Cell **cells, Cell **cells,
CoordType *distances, CoordType *distances,
uint64_t numCells) uint64_t numCells)
@ -175,7 +175,7 @@ namespace CosmoTool {
template<int N, typename ValType, typename CType, typename CellSplitter> template<int N, typename ValType, typename CType, typename CellSplitter>
template<bool justCount> template<bool justCount>
void KDTree<N,ValType,CType,CellSplitter>::recursiveIntersectionCells(RecursionInfoCells<N,ValType,CType>& info, void KDTree<N,ValType,CType,CellSplitter>::recursiveIntersectionCells(RecursionInfoCells<N,ValType,CType>& info,
Node *node, Node *node,
int level) int level)
{ {
@ -183,7 +183,7 @@ namespace CosmoTool {
CoordType d2 = 0; CoordType d2 = 0;
#if __KD_TREE_ACTIVE_CELLS == 1 #if __KD_TREE_ACTIVE_CELLS == 1
if (node->value->active) if (node->value->active)
#endif #endif
{ {
for (int j = 0; j < 3; j++) for (int j = 0; j < 3; j++)
@ -250,9 +250,9 @@ namespace CosmoTool {
} }
template<int N, typename ValType, typename CType> template<int N, typename ValType, typename CType>
void KD_default_cell_splitter<N,ValType,CType>::operator()(KDCell<N,ValType,CType> **cells, NodeIntType Ncells, void KD_default_cell_splitter<N,ValType,CType>::operator()(KDCell<N,ValType,CType> **cells, NodeIntType Ncells,
NodeIntType& split_index, int axis, NodeIntType& split_index, int axis,
typename KDDef<N,CType>::KDCoordinates minBound, typename KDDef<N,CType>::KDCoordinates minBound,
typename KDDef<N,CType>::KDCoordinates maxBound) typename KDDef<N,CType>::KDCoordinates maxBound)
{ {
CellCompare<N,ValType,CType> compare(axis); CellCompare<N,ValType,CType> compare(axis);
@ -279,9 +279,9 @@ namespace CosmoTool {
//#pragma omp atomic capture //#pragma omp atomic capture
nodeId = (this->lastNode)++; nodeId = (this->lastNode)++;
node = &nodes[nodeId]; node = &nodes[nodeId];
// Isolate the environment // Isolate the environment
splitter(cell0, Ncells, mid, axis, minBound, maxBound); splitter(cell0, Ncells, mid, axis, minBound, maxBound);
@ -297,12 +297,12 @@ namespace CosmoTool {
{ {
node->children[0] = buildTree(cell0, mid, depth, minBound, tmpBound); node->children[0] = buildTree(cell0, mid, depth, minBound, tmpBound);
} }
memcpy(tmpBound, minBound, sizeof(coords)); memcpy(tmpBound, minBound, sizeof(coords));
tmpBound[axis] = node->value->coord[axis]; tmpBound[axis] = node->value->coord[axis];
#pragma omp task private(tmpBound) #pragma omp task private(tmpBound)
{ {
node->children[1] = buildTree(cell0+mid+1, Ncells-mid-1, depth, node->children[1] = buildTree(cell0+mid+1, Ncells-mid-1, depth,
tmpBound, maxBound); tmpBound, maxBound);
} }
@ -391,17 +391,17 @@ namespace CosmoTool {
} }
return; return;
} }
// Check if current node is not the nearest // Check if current node is not the nearest
CoordType thisR2 = CoordType thisR2 =
computeDistance(node->value, x); computeDistance(node->value, x);
if (thisR2 < R2) if (thisR2 < R2)
{ {
R2 = thisR2; R2 = thisR2;
best = node->value; best = node->value;
} }
// Now we found the best. We check whether the hypersphere // Now we found the best. We check whether the hypersphere
// intersect the hyperplane of the other branch // intersect the hyperplane of the other branch
@ -435,11 +435,11 @@ namespace CosmoTool {
{ {
coords x_new; coords x_new;
r.getPosition(x_new); r.getPosition(x_new);
recursiveNearest(root, 0, x_new, R2, best); recursiveNearest(root, 0, x_new, R2, best);
} }
while (r.next()); while (r.next());
} }
return best; return best;
} }
@ -474,15 +474,15 @@ namespace CosmoTool {
{ {
recursiveMultipleNearest(info, go, level+1); recursiveMultipleNearest(info, go, level+1);
} }
// Check if current node is not the nearest // Check if current node is not the nearest
CoordType thisR2 = CoordType thisR2 =
computeDistance(node->value, info.x); computeDistance(node->value, info.x);
info.queue.push(node->value, thisR2); info.queue.push(node->value, thisR2);
info.traversed++; info.traversed++;
// if (go == 0) // if (go == 0)
// return; // return;
// Now we found the best. We check whether the hypersphere // Now we found the best. We check whether the hypersphere
// intersect the hyperplane of the other branch // intersect the hyperplane of the other branch
@ -497,7 +497,7 @@ namespace CosmoTool {
{ {
recursiveMultipleNearest(info, other, level+1); recursiveMultipleNearest(info, other, level+1);
} }
} }
} }
template<int N, typename ValType, typename CType, typename CellSplitter> template<int N, typename ValType, typename CType, typename CellSplitter>
@ -505,7 +505,7 @@ namespace CosmoTool {
Cell **cells) Cell **cells)
{ {
RecursionMultipleInfo<N,ValType> info(x, cells, N2); RecursionMultipleInfo<N,ValType> info(x, cells, N2);
for (int i = 0; i < N2; i++) for (int i = 0; i < N2; i++)
cells[i] = 0; cells[i] = 0;
@ -532,7 +532,7 @@ namespace CosmoTool {
CoordType *distances) CoordType *distances)
{ {
RecursionMultipleInfo<N,ValType> info(x, cells, N2); RecursionMultipleInfo<N,ValType> info(x, cells, N2);
for (int i = 0; i < N2; i++) for (int i = 0; i < N2; i++)
cells[i] = 0; cells[i] = 0;
@ -555,14 +555,14 @@ namespace CosmoTool {
#ifdef __KD_TREE_SAVE_ON_DISK #ifdef __KD_TREE_SAVE_ON_DISK
#define KDTREE_DISK_SIGNATURE "KDTREE" #define KDTREE_DISK_SIGNATURE "KDTREE"
#define KDTREE_DISK_SIGNATURE_LEN 7 #define KDTREE_DISK_SIGNATURE_LEN 7
template<int N, typename CType> template<int N, typename CType>
struct KDTreeOnDisk struct KDTreeOnDisk
{ {
long cell_id; long cell_id;
long children_node[2]; long children_node[2];
typename KDDef<N, CType>::KDCoordinates minBound, maxBound; typename KDDef<N, CType>::KDCoordinates minBound, maxBound;
}; };
struct KDTreeHeader struct KDTreeHeader
{ {
@ -619,7 +619,7 @@ namespace CosmoTool {
{ {
std::cerr << "KDTree Signature invalid" << std::endl; std::cerr << "KDTree Signature invalid" << std::endl;
throw InvalidOnDiskKDTree(); throw InvalidOnDiskKDTree();
} }
if (h.numCells != Ncells || h.nodesUsed < 0) { if (h.numCells != Ncells || h.nodesUsed < 0) {
std::cerr << "The number of cells has changed (" << h.numCells << " != " << Ncells << ") or nodesUsed=" << h.nodesUsed << std::endl; std::cerr << "The number of cells has changed (" << h.numCells << " != " << Ncells << ") or nodesUsed=" << h.nodesUsed << std::endl;
@ -643,8 +643,8 @@ namespace CosmoTool {
throw InvalidOnDiskKDTree(); throw InvalidOnDiskKDTree();
} }
if (node_on_disk.cell_id > numNodes || node_on_disk.cell_id < 0 || if (node_on_disk.cell_id > numNodes || node_on_disk.cell_id < 0 ||
node_on_disk.children_node[0] > lastNode || node_on_disk.children_node[0] < -1 || (node_on_disk.children_node[0] >= 0 && node_on_disk.children_node[0] > lastNode) || node_on_disk.children_node[0] < -1 ||
node_on_disk.children_node[1] > lastNode || node_on_disk.children_node[1] < -1) (node_on_disk.children_node[1] >= 0 && node_on_disk.children_node[1] > lastNode) || node_on_disk.children_node[1] < -1)
{ {
delete[] nodes; delete[] nodes;
std::cerr << "Invalid cell id or children node id invalid" << std::endl; std::cerr << "Invalid cell id or children node id invalid" << std::endl;
@ -683,10 +683,10 @@ namespace CosmoTool {
} }
root = &nodes[h.rootId]; root = &nodes[h.rootId];
sortingHelper = new Cell *[Ncells]; sortingHelper = new Cell *[Ncells];
for (NodeIntType i = 0; i < Ncells; i++) for (NodeIntType i = 0; i < Ncells; i++)
sortingHelper[i] = &cells[i]; sortingHelper[i] = &cells[i];
} }
#endif #endif