C++程序无法扩展
C++ program doesn't scale
我有一个简单的小件代码,该代码在并行修改2D向量。我分别使用1和4个线程对其进行了测试。但是,我的加速速度不到2倍。我想不出任何原因不扩展。有人可以给我提示吗?谢谢!
#include<iostream>
#include<omp.h>
#include<vector>
#include<chrono>
#include<stdio.h>
using namespace std;
typedef std::chrono::milliseconds ms;
struct Dummy{
char dummy[70];
Dummy(){
for(int i=0;i<70;i++){
dummy[i]='a';
}
}
};
int main(){
int num = 5000000;
vector<vector<Dummy> >myvec(4, vector<Dummy>(num));
auto start = std::chrono::high_resolution_clock::now();
#pragma omp parallel for schedule(static)
for(int i=0;i<4;i++){ //modifies myvec in parallel
int tid = omp_get_thread_num();
printf("Thread %d is going to workn",tid);
for(int j=0;j<num;j++){
myvec[i][j].dummy[0]='b';
}
}
auto end = std::chrono::high_resolution_clock::now();
cout<<"Time used: "<< std::chrono::duration_cast<ms>(end - start).count()<<"ms"<<endl;
return 0;
}
在这种情况下,较差的缩放主要是由于您在平行部分中没有进行足够大的计算而引起的。在示例代码中,您的内存性能应该是最大的限制因素,并且在单个桌面/移动CPU上,内存子系统可能会由所有内核共享,因此您不应该期望良好的扩展。
相关文章:
- 我想通过带有C++和Python的插件创建一个可扩展的应用程序
- 谁能告诉我,程序中的错误是什么?该程序仅用于获取文件扩展名
- Windows10 上的 VS Code + WSL 应用程序上的 CMake 扩展
- 将源文件扩展名从 .m 更改为 .mm 在屏幕保护程序项目的 Xcode 9.3 中失败
- Eclipse CDT:传递具有相同文件扩展名的多个程序参数
- 对于我扩展此程序来计算最高10x10矩阵的倒数的最简单方法是什么
- 编写Chrome扩展程序以控制串行端口
- 如果C/C Python3扩展托管HTTP服务器并将Python代码称为请求处理程序,则如何处理并发
- dll 外壳扩展如何知道要执行的程序位置?
- 为新的指令集扩展程序进行了优化的代码的向后兼容性
- 如何使用C 扩展程序禁用虚假的Visual Studio Code Intellisense错误
- 如何调用 ShellExecute 以使用 C++ 打开具有特定程序的文件,而无需关联相同的文件扩展名
- 当我想扩展当前应用程序的功能时,出现DIAMOND情况
- 设计一个扩展良好的多线程应用程序
- 在没有关联的小部件插件的QT设计器中注册扩展程序
- C++程序无法扩展
- 在Windows Shell名称空间扩展程序下使用_WOPEN()
- 我的火狐扩展程序不起作用。使用C++XPCOM组件的Firefox扩展(XPI文件)的结构是什么?
- 正在创建脚本扩展程序
- 如何在c++中使用Detours扩展程序内函数而不进入无限循环