Skip to content

Commit 9026e37

Browse files
Merge pull request #11 from gmottajr/feature/add-cpu-core-selector
Introducing cpu core selector
2 parents 4fb93b5 + 1c23c7f commit 9026e37

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode/
2+
build/

inc/Util/CpuCoreSelector.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#pragma once
3+
#include <thread>
4+
5+
enum class CpuSelectionOption {
6+
All, Half, Third, Quarter, Specific
7+
};
8+
9+
10+
/// <summary>
11+
/// The CpuCoreSelector class takes a std::string in its constructor, which represents the setting for max_no_of_threads.
12+
/// It calculates the optimal number of threads in the updateThreadCount method based on this setting.
13+
/// getOptimalThreadCount method can be used to retrieve the calculated number of threads.
14+
/// The class encapsulates the logic for determining the number of threads, making it reusable and easy to integrate.
15+
/// </summary>
16+
class CpuCoreSelector
17+
{
18+
public:
19+
static unsigned int getOptimalCount(CpuSelectionOption option, unsigned int specificCount = 0);
20+
};
21+
22+
23+
24+

src/Util/CpuCoreSelector.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "CpuCoreSelector.hpp"
2+
#include <thread>
3+
#include <algorithm>
4+
5+
unsigned int CpuCoreSelector::getOptimalCount(CpuSelectionOption option, unsigned int specificCount) {
6+
unsigned int nCores = std::thread::hardware_concurrency();
7+
switch (option) {
8+
case CpuSelectionOption::All:
9+
return nCores;
10+
case CpuSelectionOption::Half:
11+
return nCores / 2;
12+
case CpuSelectionOption::Third:
13+
return nCores / 3;
14+
case CpuSelectionOption::Quarter:
15+
return nCores / 4;
16+
case CpuSelectionOption::Specific:
17+
return std::min(specificCount, nCores);
18+
default:
19+
return 1; // Default case for safety
20+
}
21+
}
22+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#define CATCH_CONFIG_MAIN
2+
#include <catch2/catch.hpp>
3+
#include "CpuCoreSelector.hpp"
4+
#include "CpuCoreSelector.cpp"
5+
#include <thread>
6+
7+
8+
TEST_CASE("CpuCoreSelector returns correct thread counts", "[CpuCoreSelector]") {
9+
SECTION("Return all cores for All option") {
10+
REQUIRE(CpuCoreSelector::getOptimalCount(CpuSelectionOption::All) == std::thread::hardware_concurrency());
11+
}
12+
13+
SECTION("Return half cores for Half option") {
14+
REQUIRE(CpuCoreSelector::getOptimalCount(CpuSelectionOption::Half) == std::thread::hardware_concurrency() / 2);
15+
}
16+
17+
SECTION("Return Third cores for Third option") {
18+
REQUIRE(CpuCoreSelector::getOptimalCount(CpuSelectionOption::Third) == std::thread::hardware_concurrency() / 3);
19+
}
20+
21+
SECTION("Return Quarter cores for Quarter option") {
22+
REQUIRE(CpuCoreSelector::getOptimalCount(CpuSelectionOption::Quarter) == std::thread::hardware_concurrency() / 4);
23+
}
24+
25+
}
26+
27+
TEST_CASE("CpuCoreSelector handles invalid cases", "[CpuCoreSelector]") {
28+
SECTION("Handle invalid specific count gracefully") {
29+
// You can test for an unusually high specific count, expecting it to return the max hardware concurrency
30+
REQUIRE(CpuCoreSelector::getOptimalCount(CpuSelectionOption::Specific, 10000) == 1);
31+
}
32+
33+
SECTION("Handle invalid specific count gracefully") {
34+
int threadsToUse = CpuCoreSelector::getOptimalCount(static_cast<CpuSelectionOption>(-1));
35+
REQUIRE(threadsToUse == 1);
36+
}
37+
}

0 commit comments

Comments
 (0)