采用std::thread 替换 openmp

时间:2024-01-02 20:07:14

内容转换的,具体详见博客:https://cloud.tencent.com/developer/article/1094617 及对应的code:https://github.com/cpuimage/ParallelFor/blob/master/ParallelFor.cpp

#include <stdio.h>
#include <stdlib.h>
#include <iostream> #if defined(_OPENMP)
// compile with: /openmp
#include <omp.h>
auto const epoch = omp_get_wtime();
double now() {
return omp_get_wtime() - epoch;
};
#else
#include <chrono>
auto const epoch = std::chrono::steady_clock::now();
double now() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - epoch).count() / 1000.0;
};
#endif template<typename FN>
double bench(const FN &fn) {
auto took = -now();
return (fn(), took + now());
} #include <functional> #if defined(_OPENMP)
# include <omp.h>
#else
#include <thread> #include <vector>
#endif #ifdef _OPENMP
static int processorCount = static_cast<int>(omp_get_num_procs());
#else
static int processorCount = static_cast<int>(std::thread::hardware_concurrency());
#endif static void ParallelFor(int inclusiveFrom, int exclusiveTo, std::function<void(size_t)> func)
{
#if defined(_OPENMP)
#pragma omp parallel for num_threads(processorCount)
for (int i = inclusiveFrom; i < exclusiveTo; ++i)
{
func(i);
}
return;
#else
if (inclusiveFrom >= exclusiveTo)
return; static size_t thread_cnt = ;
if (thread_cnt == )
{
thread_cnt = std::thread::hardware_concurrency();
}
size_t entry_per_thread = (exclusiveTo - inclusiveFrom) / thread_cnt; if (entry_per_thread < )
{
for (int i = inclusiveFrom; i < exclusiveTo; ++i)
{
func(i);
}
return;
}
std::vector<std::thread> threads;
int start_idx, end_idx; for (start_idx = inclusiveFrom; start_idx < exclusiveTo; start_idx += entry_per_thread)
{
end_idx = start_idx + entry_per_thread;
if (end_idx > exclusiveTo)
end_idx = exclusiveTo; threads.emplace_back([&](size_t from, size_t to)
{
for (size_t entry_idx = from; entry_idx < to; ++entry_idx)
func(entry_idx);
}, start_idx, end_idx);
} for (auto& t : threads)
{
t.join();
}
#endif
} void test_scale(int i, double* a, double* b) {
a[i] = * b[i];
} int main()
{
int N = ;
double* a2 = (double*)calloc(N, sizeof(double));
double* a1 = (double*)calloc(N, sizeof(double));
double* b = (double*)calloc(N, sizeof(double));
if (a1 == NULL || a2 == NULL || b == NULL)
{
if (a1)
{
free(a1);
}if (a2)
{
free(a2);
}if (b)
{
free(b);
}
return -;
}
for (int i = ; i < N; i++)
{
a1[i] = i;
a2[i] = i;
b[i] = i;
}
double beforeTime = bench([&] {
for (int i = ; i < N; i++)
{
test_scale(i, a1, b);
}
}); std::cout << " \nbefore: " << int(beforeTime * ) << "ms" << std::endl;
double afterTime = bench([&] {
ParallelFor(, N, [a2, b](size_t i)
{
test_scale(i, a2, b);
});
});
std::cout << " \nafter: " << int(afterTime * ) << "ms" << std::endl; for (int i = ; i < N; i++)
{
if (a1[i] != a2[i]) {
printf("error %f : %f \t", a1[i], a2[i]);
getchar();
}
}
free(a1);
free(a2);
free(b);
getchar();
return ;
}

要使用OPENMP,加个编译选项/openmp  或者定义一下 _OPENMP 即可。

建议c++11编译。

示例代码比较简单。

这里举例的是ncnn代码修改例子,具体如下:

   #pragma omp parallel for
for (int q=; q<channels; q++)
{
const Mat m = src.channel(q);
Mat borderm = dst.channel(q); copy_make_border_image(m, borderm, top, left, type, v);
}

替换为:

    ParallelFor(, channels, [&](int  q) {
{
const Mat m = src.channel(q);
Mat borderm = dst.channel(q); copy_make_border_image(m, borderm, top, left, type, v);
}});