Skip to content

Commit

Permalink
Synchronize thread_pool
Browse files Browse the repository at this point in the history
  • Loading branch information
cbritopacheco committed Nov 14, 2023
1 parent 9808aca commit 525976a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
10 changes: 6 additions & 4 deletions examples/BoundaryOptimization/BoundaryHeatConductivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int main(int, char**)
return Integral(u).compute() + ell * Omega.getPerimeter(GammaD);
};

std::ofstream fObj("obj4.txt");
std::ofstream fObj("obj.txt");
size_t i = 0;
size_t prevRegionCount = 0;
while (i < maxIt)
Expand Down Expand Up @@ -197,6 +197,8 @@ int main(int, char**)
+ FaceIntegral(he * u, v).over({Gamma, GammaD})
- Integral(f, v);
state.solve(cg);
u.getSolution().save("u.gf");
Omega.save("miaow.mesh");

auto dj = -1.0 / Omega.getVolume();
Alert::Info() << "Solving adjoint equation..." << Alert::Raise;
Expand Down Expand Up @@ -301,9 +303,9 @@ int main(int, char**)
}

Alert::Info() << "Saving files..." << Alert::Raise;
Omega.save("Omega4.mesh", IO::FileFormat::MEDIT);
dOmega.save("out4/dOmega." + std::to_string(i) + ".mesh", IO::FileFormat::MEDIT);
dOmega.save("out4/dOmega.mfem." + std::to_string(i) + ".mesh", IO::FileFormat::MFEM);
Omega.save("Omega.mesh", IO::FileFormat::MEDIT);
dOmega.save("out/dOmega." + std::to_string(i) + ".mesh", IO::FileFormat::MEDIT);
dOmega.save("out/dOmega.mfem." + std::to_string(i) + ".mesh", IO::FileFormat::MFEM);

Alert::Success() << "Completed Iteration: " << i << '\n' << Alert::Raise;
i++;
Expand Down
7 changes: 1 addition & 6 deletions examples/PDEs/Poisson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int, char**)
{
// Build a mesh
Mesh mesh;
mesh = mesh.UniformGrid(Polytope::Type::Triangle, 16, 16);
mesh = mesh.UniformGrid(Polytope::Type::Triangle, 32, 32);
mesh.getConnectivity().compute(1, 2);

// Functions
Expand All @@ -37,11 +37,6 @@ int main(int, char**)
- Integral(f, v)
+ DirichletBC(u, g);

auto t0 = std::chrono::high_resolution_clock::now();
poisson.assemble();
auto t1 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() << std::endl;

// Solve the problem
Solver::SparseLU solver;
poisson.solve(solver);
Expand Down
27 changes: 22 additions & 5 deletions src/Rodin/Assembly/Multithreaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ namespace Rodin::Assembly
::execute(const BilinearAssemblyInput& input) const
{
using TripletVector = std::vector<Eigen::Triplet<Scalar>>;
BS::thread_pool threadPool(m_threadCount);
TripletVector res;
res.clear();
res.reserve(input.testFES.getSize() * std::log(input.trialFES.getSize()));
BS::thread_pool threadPool(m_threadCount);

Check warning on line 114 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L111-L114

Added lines #L111 - L114 were not covered by tests
for (auto& bfi : input.bfis)
{
const auto& attrs = bfi.getAttributes();
Expand All @@ -137,11 +138,14 @@ namespace Rodin::Assembly
}
}
m_mutex.lock();
res.insert(res.end(), tl_triplets.begin(), tl_triplets.end());
res.insert(res.end(),
std::make_move_iterator(tl_triplets.begin()),
std::make_move_iterator(tl_triplets.end()));
m_mutex.unlock();
tl_triplets.clear();
};
threadPool.push_loop(0, input.mesh.getCellCount(), loop);
threadPool.wait_for_tasks();
break;

Check warning on line 149 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L140-L149

Added lines #L140 - L149 were not covered by tests
}
case Variational::Integrator::Region::Faces:

Check warning on line 151 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L151

Added line #L151 was not covered by tests
Expand All @@ -165,11 +169,14 @@ namespace Rodin::Assembly
}
}
m_mutex.lock();
res.insert(res.end(), tl_triplets.begin(), tl_triplets.end());
res.insert(res.end(),
std::make_move_iterator(tl_triplets.begin()),
std::make_move_iterator(tl_triplets.end()));
m_mutex.unlock();
tl_triplets.clear();
};
threadPool.push_loop(0, input.mesh.getFaceCount(), loop);
threadPool.wait_for_tasks();
break;

Check warning on line 180 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L171-L180

Added lines #L171 - L180 were not covered by tests
}
case Variational::Integrator::Region::Boundary:

Check warning on line 182 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L182

Added line #L182 was not covered by tests
Expand All @@ -196,11 +203,14 @@ namespace Rodin::Assembly
}
}
m_mutex.lock();
res.insert(res.end(), tl_triplets.begin(), tl_triplets.end());
res.insert(res.end(),
std::make_move_iterator(tl_triplets.begin()),
std::make_move_iterator(tl_triplets.end()));
m_mutex.unlock();
tl_triplets.clear();
};
threadPool.push_loop(0, input.mesh.getFaceCount(), loop);
threadPool.wait_for_tasks();
break;

Check warning on line 214 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L205-L214

Added lines #L205 - L214 were not covered by tests
}
case Variational::Integrator::Region::Interface:

Check warning on line 216 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L216

Added line #L216 was not covered by tests
Expand All @@ -227,11 +237,14 @@ namespace Rodin::Assembly
}
}
m_mutex.lock();
res.insert(res.end(), tl_triplets.begin(), tl_triplets.end());
res.insert(res.end(),
std::make_move_iterator(tl_triplets.begin()),
std::make_move_iterator(tl_triplets.end()));
m_mutex.unlock();
tl_triplets.clear();
};
threadPool.push_loop(0, input.mesh.getFaceCount(), loop);
threadPool.wait_for_tasks();
break;

Check warning on line 248 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L239-L248

Added lines #L239 - L248 were not covered by tests
}
}
Expand Down Expand Up @@ -317,6 +330,7 @@ namespace Rodin::Assembly
m_mutex.unlock();
};
threadPool.push_loop(0, input.mesh.getCellCount(), loop);
threadPool.wait_for_tasks();
break;
}
case Variational::Integrator::Region::Faces:

Check warning on line 336 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L336

Added line #L336 was not covered by tests
Expand All @@ -343,6 +357,7 @@ namespace Rodin::Assembly
m_mutex.unlock();
};
threadPool.push_loop(0, input.mesh.getFaceCount(), loop);
threadPool.wait_for_tasks();
break;

Check warning on line 361 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L355-L361

Added lines #L355 - L361 were not covered by tests
}
case Variational::Integrator::Region::Boundary:

Check warning on line 363 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L363

Added line #L363 was not covered by tests
Expand Down Expand Up @@ -372,6 +387,7 @@ namespace Rodin::Assembly
m_mutex.unlock();
};
threadPool.push_loop(0, input.mesh.getFaceCount(), loop);
threadPool.wait_for_tasks();
break;

Check warning on line 391 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L385-L391

Added lines #L385 - L391 were not covered by tests
}
case Variational::Integrator::Region::Interface:

Check warning on line 393 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L393

Added line #L393 was not covered by tests
Expand Down Expand Up @@ -401,6 +417,7 @@ namespace Rodin::Assembly
m_mutex.unlock();
};
threadPool.push_loop(0, input.mesh.getFaceCount(), loop);
threadPool.wait_for_tasks();
break;

Check warning on line 421 in src/Rodin/Assembly/Multithreaded.cpp

View check run for this annotation

Codecov / codecov/patch

src/Rodin/Assembly/Multithreaded.cpp#L415-L421

Added lines #L415 - L421 were not covered by tests
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Rodin/Assembly/Multithreaded.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Rodin::Assembly
static thread_local std::unique_ptr<Variational::BilinearFormIntegratorBase> tl_bfi;

const size_t m_threadCount;

mutable Threads::Mutex m_mutex;
};

Expand Down
4 changes: 3 additions & 1 deletion src/Rodin/Variational/BilinearForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef RODIN_VARIATIONAL_BILINEARFORM_H
#define RODIN_VARIATIONAL_BILINEARFORM_H

#include "Rodin/Configure.h"

#include "Rodin/FormLanguage/List.h"
#include "Rodin/Math/SparseMatrix.h"
#include "Rodin/Assembly/ForwardDecls.h"
Expand Down Expand Up @@ -49,7 +51,7 @@ namespace Rodin::Variational
{}

BilinearFormBase(BilinearFormBase&& other)
: FormLanguage::Base(std::move(other)),
: FormLanguage::Base(std::move(other)),
m_assembly(std::move(other.m_assembly)),
m_bfis(std::move(other.m_bfis))
{}
Expand Down
6 changes: 4 additions & 2 deletions src/Rodin/Variational/LinearForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef RODIN_VARIATIONAL_LINEARFORM_H
#define RODIN_VARIATIONAL_LINEARFORM_H

#include "Rodin/Configure.h"

#include "Rodin/FormLanguage/List.h"
#include "Rodin/Assembly/ForwardDecls.h"
#include "Rodin/Assembly/Multithreaded.h"
Expand Down Expand Up @@ -36,12 +38,12 @@ namespace Rodin::Variational
}

LinearFormBase(const LinearFormBase& other)
: FormLanguage::Base(other),
: FormLanguage::Base(other),
m_lfis(other.m_lfis)
{}

LinearFormBase(LinearFormBase&& other)
: FormLanguage::Base(std::move(other)),
: FormLanguage::Base(std::move(other)),
m_lfis(std::move(other.m_lfis))
{}

Expand Down

3 comments on commit 525976a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'C++ Rodin Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 525976a Previous: 9808aca Ratio
P1Benchmark/2D_Square_GridFunction_Projection_Scalar_SumOfComponents 551.8234619955645 ns/iter 314.3091711892751 ns/iter 1.76
P1Benchmark/UniformTriangular16_GridFunction_Projection_Scalar_SumOfComponents 113872.82579618572 ns/iter 65186.3280171607 ns/iter 1.75
P1Benchmark/UniformTriangular32_GridFunction_Projection_Scalar_SumOfComponents 516591.4089595115 ns/iter 289589.9704312227 ns/iter 1.78
P1Benchmark/2D_Square_GridFunction_Projection_Vector_Components 1165.5639688395227 ns/iter 707.4607127333577 ns/iter 1.65
P1Benchmark/UniformTriangular16_GridFunction_Projection_Vector_Components 236309.5251256215 ns/iter 146357.0717563618 ns/iter 1.61
P1Benchmark/UniformTriangular32_GridFunction_Projection_Vector_Components 1001530.7603306466 ns/iter 635270.516651647 ns/iter 1.58
Poisson_UniformGrid_16x16/Assembly_ConstantCoefficient_ConstantSource 482935.15638145054 ns/iter 270605.2632597739 ns/iter 1.78
MeshIO/Load_MEDIT_2D_Square 23754.497731057018 ns/iter 14395.856734114655 ns/iter 1.65
MeshIO/Load_MEDIT_2D_UniformTriangular64 8960696.309523135 ns/iter 5265914.328358169 ns/iter 1.70
UniformGrid/Triangular_16x16 162035.30151548897 ns/iter 100357.29808517071 ns/iter 1.61
UniformGrid/Triangular_64x64 2599287.9188192347 ns/iter 1699549.570048338 ns/iter 1.53
UniformGrid/Triangular_128x128 10531149.402985742 ns/iter 6943180.524752724 ns/iter 1.52
UniformGrid/Triangular_256x256 59871401.800000966 ns/iter 35762144.8421032 ns/iter 1.67
UniformGrid/Triangular_512x512 258522846.5 ns/iter 148187397.50000986 ns/iter 1.74

This comment was automatically generated by workflow using github-action-benchmark.

@cbritopacheco
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be an anomaly. No common code affecting all of these tests was modified.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'C++ Rodin Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 525976a Previous: 9808aca Ratio
P1Benchmark/2D_Square_GridFunction_Projection_Scalar_SumOfComponents 557.8575820981669 ns/iter 314.3091711892751 ns/iter 1.77
P1Benchmark/UniformTriangular16_GridFunction_Projection_Scalar_SumOfComponents 107256.4376782601 ns/iter 65186.3280171607 ns/iter 1.65
P1Benchmark/UniformTriangular32_GridFunction_Projection_Scalar_SumOfComponents 466689.072655274 ns/iter 289589.9704312227 ns/iter 1.61
P1Benchmark/2D_Square_GridFunction_Projection_Vector_Components 1149.5451721290765 ns/iter 707.4607127333577 ns/iter 1.62
P1Benchmark/UniformTriangular16_GridFunction_Projection_Vector_Components 248267.75581397052 ns/iter 146357.0717563618 ns/iter 1.70
P1Benchmark/UniformTriangular32_GridFunction_Projection_Vector_Components 1015729.2706043544 ns/iter 635270.516651647 ns/iter 1.60
Poisson_UniformGrid_16x16/Assembly_ConstantCoefficient_ConstantSource 455555.8241279341 ns/iter 270605.2632597739 ns/iter 1.68
MeshIO/Load_MEDIT_2D_Square 23391.024218122944 ns/iter 14395.856734114655 ns/iter 1.62
MeshIO/Load_MEDIT_2D_UniformTriangular64 8143974.168830654 ns/iter 5265914.328358169 ns/iter 1.55
UniformGrid/Triangular_16x16 157598.1180620843 ns/iter 100357.29808517071 ns/iter 1.57
UniformGrid/Triangular_64x64 2607924.885057463 ns/iter 1699549.570048338 ns/iter 1.53
UniformGrid/Triangular_256x256 54959800.08333845 ns/iter 35762144.8421032 ns/iter 1.54
UniformGrid/Triangular_512x512 229249997.66667044 ns/iter 148187397.50000986 ns/iter 1.55

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.