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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4.  
  5. #if defined(_OPENMP)
  6. // compile with: /openmp
  7. #include <omp.h>
  8. auto const epoch = omp_get_wtime();
  9. double now() {
  10. return omp_get_wtime() - epoch;
  11. };
  12. #else
  13. #include <chrono>
  14. auto const epoch = std::chrono::steady_clock::now();
  15. double now() {
  16. return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - epoch).count() / 1000.0;
  17. };
  18. #endif
  19.  
  20. template<typename FN>
  21. double bench(const FN &fn) {
  22. auto took = -now();
  23. return (fn(), took + now());
  24. }
  25.  
  26. #include <functional>
  27.  
  28. #if defined(_OPENMP)
  29. # include <omp.h>
  30. #else
  31. #include <thread>
  32.  
  33. #include <vector>
  34. #endif
  35.  
  36. #ifdef _OPENMP
  37. static int processorCount = static_cast<int>(omp_get_num_procs());
  38. #else
  39. static int processorCount = static_cast<int>(std::thread::hardware_concurrency());
  40. #endif
  41.  
  42. static void ParallelFor(int inclusiveFrom, int exclusiveTo, std::function<void(size_t)> func)
  43. {
  44. #if defined(_OPENMP)
  45. #pragma omp parallel for num_threads(processorCount)
  46. for (int i = inclusiveFrom; i < exclusiveTo; ++i)
  47. {
  48. func(i);
  49. }
  50. return;
  51. #else
  52. if (inclusiveFrom >= exclusiveTo)
  53. return;
  54.  
  55. static size_t thread_cnt = ;
  56. if (thread_cnt == )
  57. {
  58. thread_cnt = std::thread::hardware_concurrency();
  59. }
  60. size_t entry_per_thread = (exclusiveTo - inclusiveFrom) / thread_cnt;
  61.  
  62. if (entry_per_thread < )
  63. {
  64. for (int i = inclusiveFrom; i < exclusiveTo; ++i)
  65. {
  66. func(i);
  67. }
  68. return;
  69. }
  70. std::vector<std::thread> threads;
  71. int start_idx, end_idx;
  72.  
  73. for (start_idx = inclusiveFrom; start_idx < exclusiveTo; start_idx += entry_per_thread)
  74. {
  75. end_idx = start_idx + entry_per_thread;
  76. if (end_idx > exclusiveTo)
  77. end_idx = exclusiveTo;
  78.  
  79. threads.emplace_back([&](size_t from, size_t to)
  80. {
  81. for (size_t entry_idx = from; entry_idx < to; ++entry_idx)
  82. func(entry_idx);
  83. }, start_idx, end_idx);
  84. }
  85.  
  86. for (auto& t : threads)
  87. {
  88. t.join();
  89. }
  90. #endif
  91. }
  92.  
  93. void test_scale(int i, double* a, double* b) {
  94. a[i] = * b[i];
  95. }
  96.  
  97. int main()
  98. {
  99. int N = ;
  100. double* a2 = (double*)calloc(N, sizeof(double));
  101. double* a1 = (double*)calloc(N, sizeof(double));
  102. double* b = (double*)calloc(N, sizeof(double));
  103. if (a1 == NULL || a2 == NULL || b == NULL)
  104. {
  105. if (a1)
  106. {
  107. free(a1);
  108. }if (a2)
  109. {
  110. free(a2);
  111. }if (b)
  112. {
  113. free(b);
  114. }
  115. return -;
  116. }
  117. for (int i = ; i < N; i++)
  118. {
  119. a1[i] = i;
  120. a2[i] = i;
  121. b[i] = i;
  122. }
  123. double beforeTime = bench([&] {
  124. for (int i = ; i < N; i++)
  125. {
  126. test_scale(i, a1, b);
  127. }
  128. });
  129.  
  130. std::cout << " \nbefore: " << int(beforeTime * ) << "ms" << std::endl;
  131. double afterTime = bench([&] {
  132. ParallelFor(, N, [a2, b](size_t i)
  133. {
  134. test_scale(i, a2, b);
  135. });
  136. });
  137. std::cout << " \nafter: " << int(afterTime * ) << "ms" << std::endl;
  138.  
  139. for (int i = ; i < N; i++)
  140. {
  141. if (a1[i] != a2[i]) {
  142. printf("error %f : %f \t", a1[i], a2[i]);
  143. getchar();
  144. }
  145. }
  146. free(a1);
  147. free(a2);
  148. free(b);
  149. getchar();
  150. return ;
  151. }

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

建议c++11编译。

示例代码比较简单。

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

  1. #pragma omp parallel for
  2. for (int q=; q<channels; q++)
  3. {
  4. const Mat m = src.channel(q);
  5. Mat borderm = dst.channel(q);
  6.  
  7. copy_make_border_image(m, borderm, top, left, type, v);
  8. }

替换为:

  1. ParallelFor(, channels, [&](int q) {
  2. {
  3. const Mat m = src.channel(q);
  4. Mat borderm = dst.channel(q);
  5.  
  6. copy_make_border_image(m, borderm, top, left, type, v);
  7. }});

采用std::thread 替换 openmp的更多相关文章

  1. 用std::thread替换实现boost::thread_group

    thread_group是boost库中的线程池类,内部使用的是boost::thread. 随着C++ 11标准的制定和各大编译器的新版本的推出(其实主要是VS2012的推出啦……),本着能用标准库 ...

  2. Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的

    下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...

  3. Microsoft C++ 异常: std::system_error std::thread

    第一次使用std::thread,把之前项目里面的Windows的thread进行了替换,程序退出的然后发生了std::system_error. 经过调试,发现std::thread ,join了两 ...

  4. std::thread(2)

    个线程都有一个唯一的 ID 以识别不同的线程,std:thread 类有一个 get_id() 方法返回对应线程的唯一编号,你可以通过 std::this_thread 来访问当前线程实例,下面的例子 ...

  5. C++ std::thread 多线程中的异常处理

    环境: VS2019 包含头文件: #include <iostream>#include<thread>#include<exception> 线程函数采用try ...

  6. std::thread

    std::shared_ptr<std::thread> m_spThread; m_spThread.reset(new std::thread(std::bind(&GameS ...

  7. C++11 并发指南------std::thread 详解

    参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...

  8. C++ 11 笔记 (五) : std::thread

    这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...

  9. Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)

    昨天练车时有一MM与我交替着练,聊了几句话就多了起来,我对她说:"看到前面那俩教练没?老色鬼两枚!整天调戏女学员."她说:"还好啦,这毕竟是他们的乐趣所在,你不认为教练每 ...

随机推荐

  1. 为啥Android手机总会越用越慢?

    转自:http://www.androidchina.net/818.html 根据第三方的调研数据显示,有77%的Android手机用户承认自己曾遭遇过手机变慢的影响,百度搜索“Android+卡慢 ...

  2. ISP图像质量调节介绍

    ISP(Image Signal Processor),即图像处理,主要作用是对前端图像传感器输出的信号做后期处理,主要功能有线性纠正.噪声去除.坏点去除.内插.白平衡.自己主动曝光控制等.依赖于IS ...

  3. Python sql注入 过滤字符串的非法字符

    #coding:utf8 #在开发过程中,要对前端传过来的数据进行验证,防止sql注入攻击,其中的一个方案就是过滤用户传过来的非法的字符 def sql_filter(sql, max_length= ...

  4. Linux下从视频提取音频的方法

    Linux下可以利用mencoder将视频里的音频提取出来.方法如下: 1.首先安装mencoder.对于Ubuntu来说,软件仓库里就有mencoder,可直接输入如下命令安装 sudo apt-g ...

  5. Ubuntu 16.04下搭建kubernetes集群环境

    简介 目前Kubernetes为Ubuntu提供的kube-up脚本,不支持15.10以及16.04这两个使用systemd作为init系统的版本. 这里详细介绍一下如何以非Docker方式在Ubun ...

  6. Eclipse中导入JDK类库的源代码以及添加指定的API

    一.在Eclipse中导入JDK类库的源代码 操作步骤: 打开eclipse->“window”-> “Preferences” -> “Java” -> “Installed ...

  7. Windows剪贴板操作简单小例

    1.复制文字到剪贴板 CString strText = L"须要拷贝到剪贴板的文字"; if ( ::OpenClipboard(m_hWnd) ) { if ( ::Empty ...

  8. CSplashScene类

    #ifndef __TRANSITIONSCENE_H__ #define __TRANSITIONSCENE_H__ #include "GameFrameHead.h" cla ...

  9. cocos2d-x发生undefined reference to `XX'异常 一劳永逸解决办法

    cocos2d-x发生undefined reference to `XX'错误 一劳永逸解决方法 参考文章: http://blog.csdn.net/kafeidev/article/detail ...

  10. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...