并行计算例子

柔光的暖阳◎ 2023-06-28 11:29 97阅读 0赞
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <omp.h>
  5. #include <functional>
  6. using namespace std;
  7. typedef std::vector<int>CVectInt;
  8. typedef std::vector<CVectInt>CArrayInt;
  9. /*
  10. vs2008 首先要开始并行计算支持
  11. 1、工程 c++ -> 语言 -> openMp支持 ->是(/openmp)
  12. */
  13. void omp_test(CArrayInt inData, int nNumber)
  14. {
  15. CArrayInt vRet;
  16. //1、设置线程数
  17. omp_set_num_threads(omp_get_num_procs()* nNumber);
  18. //3、开始计算
  19. #pragma omp parallel for
  20. for (int i = 0; i < inData.size(); i++)
  21. {
  22. std::sort(inData[i].begin(),inData[i].end(),std::greater<int>());
  23. #pragma omp critical //对结果加锁
  24. {
  25. vRet.push_back(inData[i]);
  26. }
  27. }
  28. }
  29. int _tmain(int argc, _TCHAR* argv[])
  30. {
  31. CArrayInt inPut;
  32. for (int i = 0; i< 100; i++)
  33. {
  34. CVectInt tmp;
  35. for (int j = 0; j< 10; j++)
  36. {
  37. tmp.push_back(j);
  38. }
  39. inPut.push_back(tmp);
  40. }
  41. omp_test(inPut,2);
  42. return 0;
  43. }

发表评论

表情:
评论列表 (有 0 条评论,97人围观)

还没有评论,来说两句吧...

相关阅读

    相关 并行计算简介

    1 什么是并行计算? 串行计算: 传统的软件通常被设计成为串行计算模式,具有如下特点: 一个问题被分解成为一系列离散的指令; 这些指令被顺次执行;

    相关 CUDA 并行计算

    CUDA 并行计算 并行计算可以被定义为同时使用许多计算资源 (核心或计算机) 来执行并发计算,一个大的问题可以被分解成多个小问题,然后在不同的计算资源上并行处理这些小