-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
205 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ namespace Volt | |
} | ||
|
||
m_hasBeenInitialized = true; | ||
m_threadCount = threadCount; | ||
}); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "vtpch.h" | ||
#include "Algorithms.h" | ||
|
||
#include "Volt/Math/Math.h" | ||
#include "Volt/Core/Application.h" | ||
#include "Volt/Core/Threading/ThreadPool.h" | ||
|
||
namespace Volt::Algo | ||
{ | ||
std::vector<std::future<void>> ForEachParallelLockable(std::function<void(uint32_t)>&& func, uint32_t iterationCount) | ||
{ | ||
auto& threadPool = Application::GetThreadPool(); | ||
|
||
const uint32_t threadCount = std::min(iterationCount, threadPool.GetThreadCount()); | ||
const uint32_t perThreadIterationCount = Math::DivideRoundUp(iterationCount, threadCount); | ||
|
||
std::vector<std::future<void>> futures; | ||
futures.reserve(threadCount); | ||
|
||
uint32_t iterOffset = 0; | ||
for (uint32_t i = 0; i < threadCount; i++) | ||
{ | ||
uint32_t currThreadIterationCount = perThreadIterationCount; | ||
if (i == threadCount - 1) | ||
{ | ||
currThreadIterationCount = iterationCount - i * perThreadIterationCount; | ||
} | ||
|
||
futures.emplace_back(threadPool.SubmitTask([currThreadIterationCount, func, iterOffset]() | ||
{ | ||
for (uint32_t iter = 0; iter < currThreadIterationCount; iter++) | ||
{ | ||
func(iter + iterOffset); | ||
} | ||
})); | ||
|
||
iterOffset += currThreadIterationCount; | ||
} | ||
|
||
return futures; | ||
} | ||
|
||
void ForEachParallel(std::function<void(uint32_t)>&& func, uint32_t iterationCount) | ||
{ | ||
auto& threadPool = Application::GetThreadPool(); | ||
|
||
const uint32_t threadCount = std::min(iterationCount, threadPool.GetThreadCount()); | ||
const uint32_t perThreadIterationCount = Math::DivideRoundUp(iterationCount, threadCount); | ||
|
||
uint32_t iterOffset = 0; | ||
for (uint32_t i = 0; i < threadCount; i++) | ||
{ | ||
uint32_t currThreadIterationCount = perThreadIterationCount; | ||
if (i == threadCount - 1) | ||
{ | ||
currThreadIterationCount = iterationCount - i * perThreadIterationCount; | ||
} | ||
|
||
threadPool.SubmitTask([currThreadIterationCount, func, iterOffset]() | ||
{ | ||
for (uint32_t iter = 0; iter < currThreadIterationCount; iter++) | ||
{ | ||
func(iter + iterOffset); | ||
} | ||
}); | ||
|
||
iterOffset += currThreadIterationCount; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
|
||
|
||
namespace Volt::Algo | ||
{ | ||
extern [[nodiscard]] std::vector<std::future<void>> ForEachParallelLockable(std::function<void(uint32_t)>&& func, uint32_t iterationCount); | ||
extern void ForEachParallel(std::function<void(uint32_t)>&& func, uint32_t iterationCount); | ||
} |