[Bf-blender-cvs] [1cfa83d8603] soc-2020-soft-body: discregrid using std thread

mattoverby noreply at git.blender.org
Thu Aug 13 06:53:55 CEST 2020


Commit: 1cfa83d86030ea38780a34077db64130f1fa7c29
Author: mattoverby
Date:   Wed Aug 12 23:53:51 2020 -0500
Branches: soc-2020-soft-body
https://developer.blender.org/rB1cfa83d86030ea38780a34077db64130f1fa7c29

discregrid using std thread

===================================================================

M	extern/discregrid/discregrid/include/Discregrid/cubic_lagrange_discrete_grid.hpp
M	extern/discregrid/discregrid/include/Discregrid/discrete_grid.hpp
M	extern/discregrid/discregrid/include/Discregrid/geometry/mesh_distance.hpp
M	extern/discregrid/discregrid/src/cubic_lagrange_discrete_grid.cpp
M	extern/discregrid/discregrid/src/geometry/mesh_distance.cpp
M	extern/softbody/src/admmpd_collision.cpp
M	extern/softbody/src/admmpd_mesh.cpp
M	extern/softbody/src/admmpd_solver.cpp

===================================================================

diff --git a/extern/discregrid/discregrid/include/Discregrid/cubic_lagrange_discrete_grid.hpp b/extern/discregrid/discregrid/include/Discregrid/cubic_lagrange_discrete_grid.hpp
index 6682875604b..d6d87d3a791 100755
--- a/extern/discregrid/discregrid/include/Discregrid/cubic_lagrange_discrete_grid.hpp
+++ b/extern/discregrid/discregrid/include/Discregrid/cubic_lagrange_discrete_grid.hpp
@@ -17,7 +17,10 @@ public:
 	void save(std::string const& filename) const override;
 	void load(std::string const& filename) override;
 
-	unsigned int addFunction(ContinuousFunction const& func, bool verbose = false,
+	unsigned int addFunction(
+		ContinuousFunction const& func,
+		std::vector<std::thread::id> *thread_map,
+		bool verbose = false,
 		SamplePredicate const& pred = nullptr) override;
 
 
diff --git a/extern/discregrid/discregrid/include/Discregrid/discrete_grid.hpp b/extern/discregrid/discregrid/include/Discregrid/discrete_grid.hpp
index b1461651c24..871d355f9b5 100755
--- a/extern/discregrid/discregrid/include/Discregrid/discrete_grid.hpp
+++ b/extern/discregrid/discregrid/include/Discregrid/discrete_grid.hpp
@@ -4,6 +4,8 @@
 #include <fstream>
 #include <array>
 #include <Eigen/Dense>
+#include <thread>
+#include <map>
 
 namespace Discregrid
 {
@@ -32,7 +34,10 @@ public:
 	virtual void save(std::string const& filename) const = 0;
 	virtual void load(std::string const& filename) = 0;
 
-	virtual unsigned int addFunction(ContinuousFunction const& func, bool verbose = false,
+	virtual unsigned int addFunction(
+		ContinuousFunction const& func,
+		std::vector<std::thread::id> *thread_map,
+		bool verbose = false,
 		SamplePredicate const& pred = nullptr) = 0;
 
 	double interpolate(Eigen::Vector3d const& xi, Eigen::Vector3d* gradient = nullptr) const
diff --git a/extern/discregrid/discregrid/include/Discregrid/geometry/mesh_distance.hpp b/extern/discregrid/discregrid/include/Discregrid/geometry/mesh_distance.hpp
index 6c44229b569..2272403f23e 100755
--- a/extern/discregrid/discregrid/include/Discregrid/geometry/mesh_distance.hpp
+++ b/extern/discregrid/discregrid/include/Discregrid/geometry/mesh_distance.hpp
@@ -41,6 +41,7 @@ namespace std {
 #include <set>
 #include <map>
 #include <unordered_map>
+#include <thread>
 
 #include <Eigen/Dense>
 
@@ -78,6 +79,8 @@ public:
 	double unsignedDistance(Eigen::Vector3d const& x) const;
 	double unsignedDistanceCached(Eigen::Vector3d const& x) const;
 
+	void set_thread_map(std::vector<std::thread::id> *thread_map_) { thread_map = thread_map_; }
+
 private:
 
 	Eigen::Vector3d vertex_normal(unsigned int v) const;
@@ -91,10 +94,13 @@ private:
 	bool predicate(unsigned int node_index, TriangleMeshBSH const& bsh, 
 		Eigen::Vector3d const& x, double& dist) const;
 
+	int get_thread_num() const;
+
 private:
 
-	TriangleMesh const& m_mesh;
 	TriangleMeshBSH m_bsh;
+	TriangleMesh const& m_mesh;
+	mutable std::vector<std::thread::id> *thread_map;
 
 	using FunctionValueCache = LRUCache<Eigen::Vector3d, double>;
 	mutable std::vector<TriangleMeshBSH::TraversalQueue> m_queues;
diff --git a/extern/discregrid/discregrid/src/cubic_lagrange_discrete_grid.cpp b/extern/discregrid/discregrid/src/cubic_lagrange_discrete_grid.cpp
index a6396a77536..57149247170 100755
--- a/extern/discregrid/discregrid/src/cubic_lagrange_discrete_grid.cpp
+++ b/extern/discregrid/discregrid/src/cubic_lagrange_discrete_grid.cpp
@@ -11,6 +11,7 @@
 #include <set>
 #include <chrono>
 #include <future>
+#include <thread>
 
 using namespace Eigen;
 
@@ -611,8 +612,8 @@ CubicLagrangeDiscreteGrid::indexToNodePosition(unsigned int l) const
 	auto nv = (n[0] + 1) * (n[1] + 1) * (n[2] + 1);
 	auto ne_x = (n[0] + 0) * (n[1] + 1) * (n[2] + 1);
 	auto ne_y = (n[0] + 1) * (n[1] + 0) * (n[2] + 1);
-	auto ne_z = (n[0] + 1) * (n[1] + 1) * (n[2] + 0);
-	auto ne = ne_x + ne_y + ne_z;
+	//auto ne_z = (n[0] + 1) * (n[1] + 1) * (n[2] + 0);
+	//auto ne = ne_x + ne_y + ne_z;
 
 	auto ijk = Matrix<unsigned int, 3, 1>{};
 	if (l < nv)
@@ -778,8 +779,11 @@ void CubicLagrangeDiscreteGrid::load(std::string const &filename)
 }
 
 unsigned int
-CubicLagrangeDiscreteGrid::addFunction(ContinuousFunction const &func, bool verbose,
-									   SamplePredicate const &pred)
+CubicLagrangeDiscreteGrid::addFunction(
+	ContinuousFunction const &func,
+	std::vector<std::thread::id> *thread_map,
+	bool verbose,
+	SamplePredicate const &pred)
 {
 	using namespace std::chrono;
 
@@ -793,7 +797,7 @@ CubicLagrangeDiscreteGrid::addFunction(ContinuousFunction const &func, bool verb
 	auto ne_z = (n[0] + 1) * (n[1] + 1) * (n[2] + 0);
 	auto ne = ne_x + ne_y + ne_z;
 
-	auto n_nodes = nv + 2 * ne;
+	int n_nodes = nv + 2 * ne;
 
 	m_nodes.push_back({});
 	auto &coeffs = m_nodes.back();
@@ -801,35 +805,83 @@ CubicLagrangeDiscreteGrid::addFunction(ContinuousFunction const &func, bool verb
 
 	std::atomic_uint counter(0u);
 	SpinLock mutex;
-	auto t0 = high_resolution_clock::now();
+	//auto t0 = high_resolution_clock::now();
 
-#pragma omp parallel default(shared)
-	{
-#pragma omp for schedule(static) nowait
-		for (int l = 0; l < static_cast<int>(n_nodes); ++l)
-		{
-			auto x = indexToNodePosition(l);
-			auto &c = coeffs[l];
+	if (!thread_map) {
+		printf("**CubicLagrangeDiscreteGrid::addFunction Error: no thread map.");
+		return 0;
+	}
+
+	auto thread_function = [&](int i1, int i2, int t) {
+		thread_map->at(t) = std::this_thread::get_id();
+		for (int i=i1; i<i2; ++i) {
+			auto x = indexToNodePosition(i);
+			auto &c = coeffs[i];
 
-			if (!pred || pred(x))
+			if (!pred || pred(x)) {
 				c = func(x);
-			else
+			}
+			else {
 				c = std::numeric_limits<double>::max();
-
-			if (verbose && (++counter == n_nodes || duration_cast<milliseconds>(high_resolution_clock::now() - t0).count() > 1000u))
-			{
-				std::async(std::launch::async, [&]() {
-					mutex.lock();
-					t0 = high_resolution_clock::now();
-					std::cout << "\r"
-							  << "Construction " << std::setw(20)
-							  << 100.0 * static_cast<double>(counter) / static_cast<double>(n_nodes) << "%";
-					mutex.unlock();
-				});
 			}
 		}
+	};
+
+	// Launch threads
+	thread_map->clear();
+	std::vector<std::thread> pool;
+	int max_threads = std::min(n_nodes, std::max(1,(int)std::thread::hardware_concurrency()-1));
+	thread_map->resize(max_threads);
+	int slice = std::max((int)std::round((n_nodes+1)/float(max_threads)),1);
+    int i1 = 0;
+    int i2 = std::min(slice, n_nodes);
+	{
+		int t=0;
+		for (; t+1 < max_threads && i1 < n_nodes; ++t) {
+			pool.emplace_back(thread_function, i1, i2, t);
+			i1 = i2;
+			i2 = std::min(i2 + slice, n_nodes);
+		}
+		if (i1 < n_nodes) {
+			pool.emplace_back(thread_function, i1, n_nodes, t);
+		}
+	}
+
+	int nt = pool.size();
+	for (int i=0; i<nt; ++i) {
+		if (pool[i].joinable()) {
+			pool[i].join();
+		}
 	}
 
+	thread_map->clear();
+
+/*
+#pragma omp parallel for
+	for (int l = 0; l < static_cast<int>(n_nodes); ++l)
+	{
+		auto x = indexToNodePosition(l);
+		auto &c = coeffs[l];
+
+		if (!pred || pred(x))
+			c = func(x);
+		else
+			c = std::numeric_limits<double>::max();
+
+		if (verbose && (++counter == n_nodes || duration_cast<milliseconds>(high_resolution_clock::now() - t0).count() > 1000u))
+		{
+			std::async(std::launch::async, [&]() {
+				mutex.lock();
+				t0 = high_resolution_clock::now();
+				std::cout << "\r"
+							<< "Construction " << std::setw(20)
+							<< 100.0 * static_cast<double>(counter) / static_cast<double>(n_nodes) << "%";
+				mutex.unlock();
+			});
+		}
+	}
+*/
+
 	m_cells.push_back({});
 	auto &cells = m_cells.back();
 	cells.resize(m_n_cells);
@@ -920,7 +972,7 @@ CubicLagrangeDiscreteGrid::determineShapeFunctions(unsigned int field_id, Eigen:
 
 	auto sd = subdomain(i);
 	i = i_;
-	auto d = sd.diagonal().eval();
+	//auto d = sd.diagonal().eval();
 
 	auto denom = (sd.max() - sd.min()).eval();
 	c0 = Vector3d::Constant(2.0).cwiseQuotient(denom).eval();
@@ -995,7 +1047,7 @@ CubicLagrangeDiscreteGrid::interpolate(unsigned int field_id, Vector3d const &x,
 
 	auto sd = subdomain(i);
 	i = i_;
-	auto d = sd.diagonal().eval();
+	//auto d = sd.diagonal().eval();
 
 	auto denom = (sd.max() - sd.min()).eval();
 	auto c0 = Vector3d::Constant(2.0).cwiseQuotient(denom).eval();
@@ -1097,16 +1149,16 @@ void CubicLagrangeDiscreteGrid::reduceField(unsigned int field_id, Predicate pre
 			cell_map[i] = std::numeric_limits<unsigned int>::max();
 	}
 
-	auto n = Matrix<unsigned int, 3, 1>::Map(m_resolution.data());
+	//auto n = Matrix<unsigned int, 3, 1>::Map(m_resolution.data());
 
-	auto nv = (n[0] + 1) * (n[1] + 1) * (n[2] + 1);
-	auto ne_x = (n[0] + 0) * (n[1] + 1) * (n[2] + 1);
-	auto ne_y = (n[0] + 1) * (n[1] + 0) * (n[2] + 1);
-	auto ne_z = (n[0] + 1) * (n[1] + 1) * (n[2] + 0);
-	auto ne = ne_x + ne_y + ne_z;
+	//auto nv = (n[0] + 1) * (n[1] + 1) * (n[2] + 1);
+	//auto ne_x = (n[0] + 0) * (n[1] + 1) * (n[2] + 1);
+	//auto ne_y = (n[0] + 1) * (n[1] + 0) * (n[2] + 1);
+	//auto ne_z = (n[0] + 1) * (n[1] + 1) * (n[2] + 0);
+	//auto ne = ne_x + ne_y + ne_z;
 
 	// Reduce vertices.
-	auto xi = Vector3d{};
+	//auto xi = Vector3d{};
 	auto z_values = std::vector<uint64_t>(coeffs.size());
 	for (auto l = 0u; l < coeffs.size(); ++l)
 	{
diff --git a/extern/discregrid/discregrid/src/geometry/mesh_distance.cpp b/extern/discregrid/discregrid/src/geometry/mesh_distance.cpp
index 70feeff95bb..e54488371e3 100755
--- a/extern/discregrid/discregrid/src/geometry/mesh_distance.cpp
+++ b/extern/discregrid/discregrid/src/geometry/mesh_distance.cpp
@@ -5,7 +5,7 @@
 
 #include <limits>
 #include <functional>
-#include <omp.h>
+//#include <omp.h>
 
 using namespace Eigen;
 
@@ -13,7 +13,7 @@ namespace Discregrid
 {
 
 MeshDistance::MeshDistance(TriangleMesh const& mesh, bool precompute_normals)
-	: m_bsh(mesh.vertex_data(), mesh.face_data()), m_mesh(mesh)
+	: m_bsh(mesh.vertex_data(), mesh.face_data()), m_mesh(mesh), thread_map(nullptr)
 	, m_precomputed_normals(precompute_normals)
 {
 	auto max_threads = omp_get_max_threads();
@@ -56,6 +56,25 @@ MeshDistance::MeshDistance(TriangleMesh const& mesh, bool precompute_normals)
 	}
 }
 
+int MeshDistance::get_thread_num() const {
+	//return omp_get_thread_num();
+	if (thread_map == nullptr) {
+		throw std::runtime_error("**Discregride::MeshD

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list