C++ 串行和并行openmp程序mac os的执行时间
我在理解程序的执行时间(串行和并行版本)时遇到问题 在这里,您可以找到我所说的主要功能的一部分:C++ 串行和并行openmp程序mac os的执行时间,c++,macos,performance,parallel-processing,C++,Macos,Performance,Parallel Processing,我在理解程序的执行时间(串行和并行版本)时遇到问题 在这里,您可以找到我所说的主要功能的一部分: stopwatch temp2; temp2.start(); #pragma omp parallel { #pragma omp for for (int i=0; i<100; i++){ int a=itemVectorTraining->at(mapScaledItem->at(5755)).timesIWatchedAfterC(ite
stopwatch temp2;
temp2.start();
#pragma omp parallel
{
#pragma omp for
for (int i=0; i<100; i++){
int a=itemVectorTraining->at(mapScaledItem->at(5755)).timesIWatchedAfterC(itemVectorTraining->at(mapScaledItem->at(236611)), deviceVectorTraining, 180);
}
}
temp2.stop();
cout<<"Parallel: "<<temp2.elapsed_ms()<<endl;
stopwatch temp1;
temp1.start();
for (int i=0; i<100; i++){
int a=itemVectorTraining->at(mapScaledItem->at(5755)).timesIWatchedAfterC(itemVectorTraining->at(mapScaledItem->at(236611)), deviceVectorTraining, 180);
}
temp1.stop();
cout<<"Serial: "<<temp1.elapsed_ms()<<endl;
我得到这个输出
Parallel: 140821125
Serial: 89847
添加“-fopenmp”时,即使用此命令行:
g++-4.9 -std=c++11 -o test -Iinclude main.cpp
g++-4.9 -fopenmp -std=c++11 -o testVale main.cpp
我得到:
Parallel: 39413
Serial: 2089786185294
这没有任何意义!此外,虽然程序在第一种情况下返回的并行值和在第二种情况下返回的串行值都很大,但实际上运行代码并不需要很长时间
我是从MAC OS X的终端进行编译的,通常我应该获得如下信息:
Parallel:38548
Serial 68007
有人知道这个程序的编译过程吗
多谢各位
秒表代码:
#ifndef CGLIFE_STOPWATCH_HPP
#define CGLIFE_STOPWATCH_HPP
#include <chrono>
class stopwatch {
private:
typedef std::chrono::high_resolution_clock clock;
bool running;
clock::time_point start_time;
clock::duration elapsed;
public:
stopwatch() {
running = false;
}
// Starts the stopwatch.
void start() {
if (!running) {
running = true;
start_time = clock::now();
}
}
// Stops the stopwatch.
void stop() {
if (running) {
running = false;
elapsed += clock::now() - start_time;
}
}
// Resets the elapsed time to 0.
void reset() {
elapsed = clock::duration();
}
// Returns the total elapsed time in milliseconds.
// If the stopwatch is running, the elapsed time
// includes the time elapsed in the current interval.
long long elapsed_ms() const {
clock::duration total;
if (!running) {
total = elapsed;
} else {
total = elapsed + (clock::now() - start_time);
}
return std::chrono::duration_cast<std::chrono::milliseconds>(total).count();
}
};
#endif
\ifndef CGLIFE\u秒表\u水电站
#定义CGLIFE\U秒表\U水电站
#包括
阶级秒表{
私人:
typedef std::chrono::高分辨率时钟;
布尔跑;
时钟:时间点开始时间;
时钟:经过的持续时间;
公众:
秒表{
运行=错误;
}
//启动秒表。
void start(){
如果(!正在运行){
运行=真;
开始时间=时钟::现在();
}
}
//停止秒表。
无效停止(){
如果(正在运行){
运行=错误;
已用时间+=时钟::现在()-开始时间;
}
}
//将经过的时间重置为0。
无效重置(){
已用=时钟::持续时间();
}
//返回以毫秒为单位的总运行时间。
//如果秒表正在运行,经过的时间
//包括当前间隔中经过的时间。
长时间运行的\u ms()常量{
时钟:总持续时间;
如果(!正在运行){
总计=经过的时间;
}否则{
总计=已用时间+(时钟::现在()-开始时间);
}
返回std::chrono::duration_cast(总计).count();
}
};
#恩迪夫
秒表::已用似乎未初始化。我不确定它是怎样的,因为它必须是类类型。
在stopwatch
构造函数中初始化它:
stopwatch() {
running = false;
elapsed = clock::duration();
}
或总是在启动前调用reset
:
stopwatch temp2;
temp2.reset();
temp2.start();
从stopwatch::appeased_ms()获取的数字似乎是随机的,就像某些未初始化成员的值一样。如果您需要帮助,请发布秒表课程代码。谢谢!我的问题中包含了秒表类的代码。无论如何,由于相同的程序(使用相同的秒表类)在Windows PC的虚拟机上工作,我想我的命令行中有一些错误。非常感谢你的帮助!