update gradle project, parallel for-loop based on std::thread on android, fix mipmaps on TextureManager
This commit is contained in:
55
src/util.cpp
55
src/util.cpp
@@ -362,3 +362,58 @@ size_t curl_data_write(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
||||
size_t written = fwrite(ptr, size, nmemb, stream);
|
||||
return written;
|
||||
}
|
||||
|
||||
/// @param[in] nb_elements : size of your for loop
|
||||
/// @param[in] functor(start, end) :
|
||||
/// your function processing a sub chunk of the for loop.
|
||||
/// "start" is the first index to process (included) until the index "end"
|
||||
/// (excluded)
|
||||
/// @code
|
||||
/// for(int i = start; i < end; ++i)
|
||||
/// computation(i);
|
||||
/// @endcode
|
||||
/// @param use_threads : enable / disable threads.
|
||||
///
|
||||
///
|
||||
void parallel_for(unsigned nb_elements, std::function<void(int i)> functor, bool use_threads)
|
||||
{
|
||||
// -------
|
||||
unsigned nb_threads_hint = std::thread::hardware_concurrency();
|
||||
unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint);
|
||||
|
||||
unsigned batch_size = nb_elements / nb_threads;
|
||||
unsigned batch_remainder = nb_elements % nb_threads;
|
||||
|
||||
std::vector< std::thread > my_threads(nb_threads);
|
||||
|
||||
if (use_threads)
|
||||
{
|
||||
// Multithread execution
|
||||
for (unsigned i = 0; i < nb_threads; ++i)
|
||||
{
|
||||
int start = i * batch_size;
|
||||
my_threads[i] = std::thread([functor, start, batch_size]() {
|
||||
for (int j = start; j < start + batch_size; j++)
|
||||
functor(j);
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Single thread execution (for easy debugging)
|
||||
for (unsigned i = 0; i < nb_threads; ++i) {
|
||||
int start = i * batch_size;
|
||||
for (int j = start; j < start + batch_size; j++)
|
||||
functor(j);
|
||||
}
|
||||
}
|
||||
|
||||
// Deform the elements left
|
||||
int start = nb_threads * batch_size;
|
||||
for (int j = start; j < start + batch_remainder; j++)
|
||||
functor(j);
|
||||
|
||||
// Wait for the other thread to finish their task
|
||||
if (use_threads)
|
||||
std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join));
|
||||
}
|
||||
Reference in New Issue
Block a user