使用OpenMP并行执行比串行执行c ++需要更长的时间,我是否以正确的方式计算执行时间? [英] Parallel exection using OpenMP takes longer than serial execution c++, am i calculating execution time in the right way?

查看:630
本文介绍了使用OpenMP并行执行比串行执行c ++需要更长的时间,我是否以正确的方式计算执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不使用Open MP指令-串行执行-在此处查看屏幕截图

使用OpenMp指令-并行执行-在此处检查屏幕截图

#include "stdafx.h"
#include <omp.h>
#include <iostream>
#include <time.h>
using namespace std;

static long num_steps = 100000;
double step;
double pi;

int main()
{
clock_t tStart = clock();
int i;
double x, sum = 0.0;
step = 1.0 / (double)num_steps;

#pragma omp parallel for shared(sum)
for (i = 0; i < num_steps; i++)
{
    x = (i + 0.5)*step;
#pragma omp critical
    {
        sum += 4.0 / (1.0 + x * x);
    }
}

pi = step * sum;
cout << pi <<"\n";
printf("Time taken: %.5fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
getchar();
return 0;
}

我尝试过多次,为什么串行执行总是更快?

I have tried multiple times, the serial execution is always faster why?

串行执行时间:0.0200s 并行执行时间:0.02500s

Serial Execution Time: 0.0200s Parallel Execution Time: 0.02500s

为什么在这里串行执行更快?我以正确的方式计算执行时间吗?

why is serial execution faster here? am I calculation the execution time in the right way?

推荐答案

OpenMP在内部实现了多线程以进行并行处理,并且可以使用大量数据来衡量多线程的性能.数据量非常小,您无法衡量多线程应用程序的性能.原因:-

OpenMP internally implement multithreading for parallel processing and multi threading's performance can be measured with large volume of data. With very small volume of data you cannot measure the performance of multithreaded application. The reasons:-

a)要创建线程,操作系统需要为每个线程分配内存,这需要花费时间(即使很小).

a) To create a thread O/S need to allocate memory to each thread which take time (even though it is tiny bit.)

b)创建多线程时,需要上下文切换,这也需要时间.

b) When you create multi threads it needs context switching which also take time.

c)需要释放分配给线程的内存,这也需要时间.

c) Need to release memory allocated to threads which also take time.

d)这取决于计算机中的处理器数量和总内存(RAM)

d) It depends on number of processors and total memory (RAM) in your machine

因此,当您尝试对多线程进行小操作时,其性能将与单线程相同(默认情况下,O/S将一个线程分配给称为主线程的每个进程).因此,在这种情况下,您的结果是完美的.要衡量多线程体系结构的性能,请使用大量数据并进行复杂的操作,然后您才能看到其中的区别.

So when you try with small operation with multi threads it's performance will be as same as a single thread (O/S by default assign one thread to every process which is call main thread). So your outcome is perfect in this case. To measure the performance of multithread architecture use large amount of data with complex operation then only you can see the differences.

这篇关于使用OpenMP并行执行比串行执行c ++需要更长的时间,我是否以正确的方式计算执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆