Skip to content

Commit

Permalink
fix memory leak for vec (#532)
Browse files Browse the repository at this point in the history
* fix memory leak for vec

* removing reminiscence of CUDA

* formatting
  • Loading branch information
pca006132 authored Aug 16, 2023
1 parent b4f583f commit 111350f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 73 deletions.
19 changes: 9 additions & 10 deletions src/manifold/src/csg_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Manifold::Impl CsgLeafNode::Compose(
if (nodes.size() > 1 && policy == ExecutionPolicy::Par)
policy = ExecutionPolicy::Seq;

for_each_n_host(
for_each_n(
nodes.size() > 1 ? ExecutionPolicy::Par : ExecutionPolicy::Seq,
countAt(0), nodes.size(),
[&nodes, &vertIndices, &edgeIndices, &triIndices, &propVertIndices,
Expand Down Expand Up @@ -557,7 +557,7 @@ void CsgOpNode::BatchUnion() const {
// compose each set of disjoint children
std::vector<std::shared_ptr<const Manifold::Impl>> impls(
disjointSets.size());
for_each_n_host(
for_each_n(
disjointSets.size() > 1 ? ExecutionPolicy::Par : ExecutionPolicy::Seq,
countAt(0), disjointSets.size(),
[children_, &impls, &disjointSets, start](int i) {
Expand Down Expand Up @@ -599,14 +599,13 @@ std::vector<std::shared_ptr<CsgNode>> &CsgOpNode::GetChildren(

if (forceToLeafNodes && !impl->forcedToLeafNodes_) {
impl->forcedToLeafNodes_ = true;
for_each_host(impl->children_.size() > 1 ? ExecutionPolicy::Par
: ExecutionPolicy::Seq,
impl->children_.begin(), impl->children_.end(),
[](auto &child) {
if (child->GetNodeType() != CsgNodeType::Leaf) {
child = child->ToLeafNode();
}
});
for_each(impl->children_.size() > 1 ? ExecutionPolicy::Par
: ExecutionPolicy::Seq,
impl->children_.begin(), impl->children_.end(), [](auto &child) {
if (child->GetNodeType() != CsgNodeType::Leaf) {
child = child->ToLeafNode();
}
});
}
return impl->children_;
}
Expand Down
29 changes: 14 additions & 15 deletions src/manifold/src/face_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,18 @@ void Manifold::Impl::Face2Tri(const Vec<int>& faceEdge,
triCount.back() = 0;
// precompute number of triangles per face, and launch async tasks to
// triangulate complex faces
for_each_host(autoPolicy(faceEdge.size()), countAt(0),
countAt(faceEdge.size() - 1), [&](int face) {
triCount[face] = faceEdge[face + 1] - faceEdge[face] - 2;
ASSERT(triCount[face] >= 1, topologyErr,
"face has less than three edges.");
if (triCount[face] > 2)
group.run([&, face] {
std::vector<glm::ivec3> newTris =
generalTriangulation(face);
triCount[face] = newTris.size();
results[face] = std::move(newTris);
});
});
for_each(autoPolicy(faceEdge.size()), countAt(0),
countAt(faceEdge.size() - 1), [&](int face) {
triCount[face] = faceEdge[face + 1] - faceEdge[face] - 2;
ASSERT(triCount[face] >= 1, topologyErr,
"face has less than three edges.");
if (triCount[face] > 2)
group.run([&, face] {
std::vector<glm::ivec3> newTris = generalTriangulation(face);
triCount[face] = newTris.size();
results[face] = std::move(newTris);
});
});
group.wait();
// prefix sum computation (assign unique index to each face) and preallocation
exclusive_scan(autoPolicy(triCount.size()), triCount.begin(), triCount.end(),
Expand All @@ -168,8 +167,8 @@ void Manifold::Impl::Face2Tri(const Vec<int>& faceEdge,
},
std::placeholders::_1);
// set triangles in parallel
for_each_host(autoPolicy(faceEdge.size()), countAt(0),
countAt(faceEdge.size() - 1), processFace2);
for_each(autoPolicy(faceEdge.size()), countAt(0),
countAt(faceEdge.size() - 1), processFace2);
#else
triVerts.reserve(faceEdge.size());
triNormal.reserve(faceEdge.size());
Expand Down
16 changes: 0 additions & 16 deletions src/utilities/include/par.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,6 @@ inline constexpr ExecutionPolicy autoPolicy(int size) {
return thrust::NAME(thrust::cpp::par, args...); \
}

#define THRUST_DYNAMIC_BACKEND_HOST_VOID(NAME) \
template <typename... Args> \
void NAME##_host(ExecutionPolicy policy, Args... args) { \
switch (policy) { \
case ExecutionPolicy::Par: \
thrust::NAME(thrust::MANIFOLD_PAR_NS::par, args...); \
break; \
case ExecutionPolicy::Seq: \
thrust::NAME(thrust::cpp::par, args...); \
break; \
} \
}

#if MANIFOLD_PAR == 'T' && __has_include(<pstl/glue_execution_defs.h>)
// sometimes stl variant is faster
#define STL_DYNAMIC_BACKEND(NAME, RET) \
Expand Down Expand Up @@ -158,9 +145,6 @@ THRUST_DYNAMIC_BACKEND_VOID(exclusive_scan)
THRUST_DYNAMIC_BACKEND(copy_if, void)
#endif

THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each)
THRUST_DYNAMIC_BACKEND_HOST_VOID(for_each_n)

THRUST_DYNAMIC_BACKEND_VOID(gather)
THRUST_DYNAMIC_BACKEND_VOID(scatter)
THRUST_DYNAMIC_BACKEND_VOID(for_each)
Expand Down
49 changes: 21 additions & 28 deletions src/utilities/include/public.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ namespace manifold {

constexpr float kTolerance = 1e-5;

#ifdef __CUDACC__
#define HOST_DEVICE __host__ __device__
#else
#define HOST_DEVICE
#endif

/** @defgroup Connections
* @brief Move data in and out of the Manifold class.
* @{
Expand All @@ -50,7 +44,7 @@ constexpr float kTolerance = 1e-5;
*
* @param x Angle in degrees.
*/
inline HOST_DEVICE float sind(float x) {
inline float sind(float x) {
if (!std::isfinite(x)) return sin(x);
if (x < 0.0f) return -sind(-x);
int quo;
Expand All @@ -73,15 +67,15 @@ inline HOST_DEVICE float sind(float x) {
*
* @param x Angle in degrees.
*/
inline HOST_DEVICE float cosd(float x) { return sind(x + 90.0f); }
inline float cosd(float x) { return sind(x + 90.0f); }

/**
* This 4x3 matrix can be used as an input to Manifold.Transform() to turn an
* object. Turns along the shortest path from given up-vector to (0, 0, 1).
*
* @param up The vector to be turned to point upwards. Length does not matter.
*/
inline HOST_DEVICE glm::mat4x3 RotateUp(glm::vec3 up) {
inline glm::mat4x3 RotateUp(glm::vec3 up) {
up = glm::normalize(up);
glm::vec3 axis = glm::cross(up, {0, 0, 1});
float angle = glm::asin(glm::length(axis));
Expand All @@ -100,8 +94,7 @@ inline HOST_DEVICE glm::mat4x3 RotateUp(glm::vec3 up) {
* @return int, like Signum, this returns 1 for CCW, -1 for CW, and 0 if within
* tol of colinear.
*/
inline HOST_DEVICE int CCW(glm::vec2 p0, glm::vec2 p1, glm::vec2 p2,
float tol) {
inline int CCW(glm::vec2 p0, glm::vec2 p1, glm::vec2 p2, float tol) {
glm::vec2 v1 = p1 - p0;
glm::vec2 v2 = p2 - p0;
float area = v1.x * v2.y - v1.y * v2.x;
Expand Down Expand Up @@ -179,63 +172,63 @@ struct Box {
/**
* Default constructor is an infinite box that contains all space.
*/
HOST_DEVICE Box() {}
Box() {}

/**
* Creates a box that contains the two given points.
*/
HOST_DEVICE Box(const glm::vec3 p1, const glm::vec3 p2) {
Box(const glm::vec3 p1, const glm::vec3 p2) {
min = glm::min(p1, p2);
max = glm::max(p1, p2);
}

/**
* Returns the dimensions of the Box.
*/
HOST_DEVICE glm::vec3 Size() const { return max - min; }
glm::vec3 Size() const { return max - min; }

/**
* Returns the center point of the Box.
*/
HOST_DEVICE glm::vec3 Center() const { return 0.5f * (max + min); }
glm::vec3 Center() const { return 0.5f * (max + min); }

/**
* Returns the absolute-largest coordinate value of any contained
* point.
*/
HOST_DEVICE float Scale() const {
float Scale() const {
glm::vec3 absMax = glm::max(glm::abs(min), glm::abs(max));
return glm::max(absMax.x, glm::max(absMax.y, absMax.z));
}

/**
* Does this box contain (includes equal) the given point?
*/
HOST_DEVICE bool Contains(const glm::vec3& p) const {
bool Contains(const glm::vec3& p) const {
return glm::all(glm::greaterThanEqual(p, min)) &&
glm::all(glm::greaterThanEqual(max, p));
}

/**
* Does this box contain (includes equal) the given box?
*/
HOST_DEVICE bool Contains(const Box& box) const {
bool Contains(const Box& box) const {
return glm::all(glm::greaterThanEqual(box.min, min)) &&
glm::all(glm::greaterThanEqual(max, box.max));
}

/**
* Expand this box to include the given point.
*/
HOST_DEVICE void Union(const glm::vec3 p) {
void Union(const glm::vec3 p) {
min = glm::min(min, p);
max = glm::max(max, p);
}

/**
* Expand this box to include the given box.
*/
HOST_DEVICE Box Union(const Box& box) const {
Box Union(const Box& box) const {
Box out;
out.min = glm::min(min, box.min);
out.max = glm::max(max, box.max);
Expand All @@ -249,7 +242,7 @@ struct Box {
* multiples of 90 degrees), or else the resulting bounding box will no longer
* bound properly.
*/
HOST_DEVICE Box Transform(const glm::mat4x3& transform) const {
Box Transform(const glm::mat4x3& transform) const {
Box out;
glm::vec3 minT = transform * glm::vec4(min, 1.0f);
glm::vec3 maxT = transform * glm::vec4(max, 1.0f);
Expand All @@ -261,7 +254,7 @@ struct Box {
/**
* Shift this box by the given vector.
*/
HOST_DEVICE Box operator+(glm::vec3 shift) const {
Box operator+(glm::vec3 shift) const {
Box out;
out.min = min + shift;
out.max = max + shift;
Expand All @@ -271,7 +264,7 @@ struct Box {
/**
* Shift this box in-place by the given vector.
*/
HOST_DEVICE Box& operator+=(glm::vec3 shift) {
Box& operator+=(glm::vec3 shift) {
min += shift;
max += shift;
return *this;
Expand All @@ -280,7 +273,7 @@ struct Box {
/**
* Scale this box by the given vector.
*/
HOST_DEVICE Box operator*(glm::vec3 scale) const {
Box operator*(glm::vec3 scale) const {
Box out;
out.min = min * scale;
out.max = max * scale;
Expand All @@ -290,7 +283,7 @@ struct Box {
/**
* Scale this box in-place by the given vector.
*/
HOST_DEVICE Box& operator*=(glm::vec3 scale) {
Box& operator*=(glm::vec3 scale) {
min *= scale;
max *= scale;
return *this;
Expand All @@ -299,7 +292,7 @@ struct Box {
/**
* Does this box overlap the one given (including equality)?
*/
HOST_DEVICE bool DoesOverlap(const Box& box) const {
bool DoesOverlap(const Box& box) const {
return min.x <= box.max.x && min.y <= box.max.y && min.z <= box.max.z &&
max.x >= box.min.x && max.y >= box.min.y && max.z >= box.min.z;
}
Expand All @@ -308,14 +301,14 @@ struct Box {
* Does the given point project within the XY extent of this box
* (including equality)?
*/
HOST_DEVICE bool DoesOverlap(glm::vec3 p) const { // projected in z
bool DoesOverlap(glm::vec3 p) const { // projected in z
return p.x <= max.x && p.x >= min.x && p.y <= max.y && p.y >= min.y;
}

/**
* Does this box have finite bounds?
*/
HOST_DEVICE bool IsFinite() const {
bool IsFinite() const {
return glm::all(glm::isfinite(min)) && glm::all(glm::isfinite(max));
}
};
Expand Down
8 changes: 4 additions & 4 deletions src/utilities/include/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ class Vec : public VecView<T> {
TracyAllocS(newBuffer, size_ * sizeof(T), 3);
uninitialized_copy(autoPolicy(this->size_), this->ptr_,
this->ptr_ + this->size_, newBuffer);
if (this->ptr_ != nullptr) {
TracyFreeS(ptr_, 3);
free(this->ptr_);
}
}
if (this->ptr_ != nullptr) {
TracyFreeS(ptr_, 3);
free(this->ptr_);
}
this->ptr_ = newBuffer;
capacity_ = this->size_;
Expand Down

0 comments on commit 111350f

Please sign in to comment.