- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个不相关的for
循环,一个是串行执行的,一个是使用 OpenMP 并行执行的。
我使用的 OpenMP 线程越多,下一个串行代码就越慢。
class Foo {
public:
Foo(size_t size) {
parallel_vector.resize(size, 0.0);
serial_vector.resize(size, 0.0);
}
void do_serial_work() {
std::mt19937 random_number_generator;
std::uniform_real_distribution<double> random_number_distribution{ 0.0, 1.0 };
for (size_t i = 0; i < serial_vector.size(); i++) {
serial_vector[i] = random_number_distribution(random_number_generator);
}
}
void do_parallel_work() {
#pragma omp parallel for
for (auto i = 0; i < parallel_vector.size(); ++i) {
for (auto integration_steps = 0; integration_steps < 30; integration_steps++) {
parallel_vector[i] += (0.05 - parallel_vector[i]) / 30.0;
}
}
}
private:
std::vector<double> parallel_vector;
std::vector<double> serial_vector;
};
void test_with_size(size_t size, int num_threads) {
std::cout << "Testing with " << num_threads << " and size: " << size << "\n";
omp_set_num_threads(num_threads);
Foo foo{ size };
long long total_dur_1 = 0;
long long total_dur_2 = 0;
for (auto i = 0; i < 500; i++) {
const auto tp_1 = std::chrono::high_resolution_clock::now();
foo.do_serial_work();
const auto tp_2 = std::chrono::high_resolution_clock::now();
foo.do_parallel_work();
const auto tp_3 = std::chrono::high_resolution_clock::now();
const auto dur_1 = std::chrono::duration_cast<std::chrono::microseconds>(tp_2 - tp_1).count();
const auto dur_2 = std::chrono::duration_cast<std::chrono::microseconds>(tp_3 - tp_2).count();
total_dur_1 += dur_1;
total_dur_2 += dur_2;
}
std::cout << total_dur_1 << "\t" << total_dur_2 << "\n";
}
int main(int argc, char** argv) {
test_with_size(100000, 1);
test_with_size(100000, 2);
test_with_size(100000, 4);
test_with_size(100000, 8);
return 0;
}
速度变慢发生在我的本地机器上,一台 Win10 笔记本电脑,配备 4 核和超线程的 Intel Core i7-7700,24 GB RAM。编译器是 VisualStudio 2019 中的最新版本。在 RelWithDebugMode 中编译(来自 CMake,包括
/O2
和
/openmp
)。
g++ --std=c++17 -O3 -fopenmp
.
Testing with 1 and size: 100000
3043846 10536315
Testing with 2 and size: 100000
3276611 5350204
Testing with 4 and size: 100000
3937311 2735655
Testing with 8 and size: 100000
5002727 1598775
在 CentOS 8、2x Xeon Platinum 9242 上:
Testing with 1 and size: 100000
727756 4111363
Testing with 2 and size: 100000
731649 2069257
Testing with 4 and size: 100000
734019 1056157
Testing with 8 and size: 100000
752584 544373
所以我最初的想法是“缓存压力太大”。但是,当我从并行部分中删除几乎所有内容但循环时,速度再次下降。
void do_parallel_work() {
#pragma omp parallel for
for (auto i = 0; i < 8; ++i) {
//for (auto integration_steps = 0; integration_steps < 30; integration_steps++) {
// parallel_vector[i] += (0.05 - parallel_vector[i]) / 30.0;
//}
}
}
Win10 上更新并行部分的时间:
Testing with 1 and size: 100000
3206293 636
Testing with 2 and size: 100000
3218667 2672
Testing with 4 and size: 100000
3928818 8689
Testing with 8 and size: 100000
5106605 10797
查看 OpenMP 2.0 标准(VS 仅支持 2.0)(在此处找到:
https://www.openmp.org/specifications/ ),它在 2.7.2.5 第 7,8 行中说:
In the absence of an explicit default clause, the default behavior is thesame as if the default(shared) were specified.
All threads within the team access the same storage area for shared variables.
Testing with 1 and size: 100000
833678 2752
Testing with 2 and size: 100000
762877 1863
Testing with 4 and size: 100000
816440 1860
Testing with 8 and size: 100000
991184 2350
老实说,我不确定为什么 Windows 可执行文件在与 linux 相同的机器上花费的时间要长得多(VC++ 的优化/O2 是最大的),但有趣的是,这里不会发生相同的工件。
最佳答案
Windows 上的 OpenMP 默认具有 200 毫秒的自旋锁。这意味着当您离开 omp 块时,所有 omp 工作线程都在旋转等待新工作。如果您有许多彼此相邻的 omp 块,它会有好处。在您的情况下,线程只消耗 CPU 功率。
要禁用/控制自旋锁,您有多种选择:
OMP_WAIT_POLICY
并将其设置为 PASSIVE
完全禁用自旋锁,KMP_BLOCKTIME
来完全控制自旋锁定时间。环境变量,KMP_BLOCKTIME
来控制自旋锁时间环境变量。 关于c++ - OpenMP 减慢不相关的串行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66206296/
我正在使用 UISnapBehavior,但它的捕捉速度太快了,我不喜欢。有没有办法让它慢下来?或者换句话说:有没有办法用它应该捕捉的点来调整物体的弹性? 最佳答案 我能够通过将 View 附加到 U
我想减慢 SWTBot 的执行速度。 我已经找到了这个 wiki: https://wiki.eclipse.org/SWTBot/FAQ#Can_I_slow_down_the_execution_
我的应用程序中有一个计时错误,只有在我使用 valgrind 时才会发生,因为 valgrind 会大大减慢进程的速度。 (它实际上是一个我无法本地化的 boost::weak_ptr-excepti
问题 我正在创建一个涉及躲避射弹的游戏。玩家控制着一艘船的图像,我不希望船完全一起移动,因为这看起来非常不现实。 问题 有没有办法控制图像移动的速度,如何减慢图像的移动速度? 代码 var game
我在我的 iOS 应用程序中使用了 NSTimer,但由于 SetNeedsDisplay,我没有得到我想要的结果。 我做了一些研究并找到了 CADisplayLink,它为我提供了我想要的动画结果。
我目前正在开发一个项目,当按下按钮时,该项目会将圆从一个空间移动到另一个空间。我的设计如下:当按下按钮时,它会在 for 循环中从 0 到 10 增加圆的坐标。 问题是,我想要的 for 循环运动没有
我想缓慢地制作一个三色渐变动画。 我有一个自定义UIView,如下所示: class MyView: UIView, CAAnimationDelegate { lazy var gradient
当 RAM 达到 x 内存量或调用 didReceiveMemoryWarning() 时,是否有办法减慢处理器速度? func didReceiveMemoryWarning() { sup
有没有办法减慢行插入/删除动画的速度? 在我的特殊情况下,我通过在我的单元格下方添加/删除行来扩展/折叠单元格,我想稍微放慢动画速度。 最佳答案 我正在使用以下技巧在我的项目中以动画方式插入/删除表格
我的 Logo 和页脚中有 scroll-top 属性,但我离页面顶部越远,它向上滚动的速度就越快!所以当我从页面底部滚动到顶部时,它就像火箭一样!我将如何放慢速度?我找不到足够具体的答案 可以看看l
我想放慢由我的 UIDynamicAnimator 生成的动画,以便我可以微调我的 UIDynamicBehaviors。 在 ios 模拟器中,调试菜单下有一个菜单选项,标签为“在最前面的应用程序中
在 OS X 上,可以按住 Shift 键使动画变慢。有什么方法可以通过远程调试器或 Instruments 将其应用于 iOS 吗? (或者,我可以在 QuickTime 中录制并逐帧回放,但我宁愿
我想在 .opacity CSS 属性中减慢动画时间。就像,我希望它延迟 0.2 毫秒或类似的东西。 为了获得更好的想法,将鼠标悬停在我网站上的精选帖子上时会添加不透明度:http://www.the
我希望我的 UIPageViewController 在用户的手指离开屏幕时缓慢滚动到下一页。比默认情况下慢。如果可能的话,对其减速曲线等进行更多控制。 我不想使用 SCPageViewControl
我发现了这个 javascript 自动滚动函数,并通过将其粘贴到 WordPress 站点的头文件中来使其工作。但是,我想减慢滚动速度,以便它不会立即捕捉到页面底部。 我是 javascript 的
我正在使用 UIScrollView 以编程方式为某些内容设置动画。 但是,我需要减慢 View 的滚动速度。 这是我用于滚动的代码: self.scrollView.setContentOffset
我一直在使用 jQuery 滚动来增强我的视差滚动页面。具体来说就是这个。 JQuery Scroll to Next Section 我对 jQuery 完全陌生(过去只使用过一些相当基本的 Jav
如何减慢 Windows 进程? 我知道我需要 Hook QueryPerformanceCounter 但接下来我需要做什么? 需要 Delphi 或 C++ 方面的帮助 最佳答案 我不确定我是否理
我想在我这边控制下载量/速度——在服务器端也一样(礼貌一点)。...不是“我自己的下载管理器”。 让我们想象一下:我允许我的儿子每天从 utube 下载最多 500Mb,但他仍然启动了一个 sessi
在我的网站上,我有多个 href's,我需要在点击它们和加载它们之间添加延迟。由于有数百个 hrefs,我不能为每个单独的 js 函数。 我研究过的两种方法是,将 href 的内容作为变量传递给 ja
我是一名优秀的程序员,十分优秀!