在为顺序运行优化的程序上使用 openMP 后没有性能提升 [英] No performance gain after using openMP on a program optimize for sequential running

查看:20
本文介绍了在为顺序运行优化的程序上使用 openMP 后没有性能提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽可能多地优化了我的顺序运行功能.当我使用 openMP 时,我看不到性能的提高.我在一台 1 核的机器和一台 8 核的机器上尝试了我的程序,性能是一样的.
将年份设置为 20,我有
1 核:1 秒.
8 核:1 秒.

I have optimized as much as I could my function for sequential running. When I use openMP I see no gain in performance. I tried my program on a machine with 1 cores and on a machine with 8 cores, and the performance is the same.
With year set to 20, I have
1 core: 1 sec.
8 core: 1 sec.

将年份设置为 25 我有
1 核:40 秒.
8 核:40 秒.

With year set to 25 I have
1 core: 40 sec.
8 core: 40 sec.

1 核机器:我的笔记本电脑的 intel core 2 duo 1.8 GHz,ubuntu linux
8核机器:3.25GHz,ubuntu linux

1 core machine: my laptop's intel core 2 duo 1.8 GHz, ubuntu linux
8 core machine: 3.25 GHz, ubuntu linux

我的程序枚举二叉树的所有可能路径并对每条路径做一些工作.所以我的循环大小呈指数增长,我希望 openMP 线程的占用空间为零.在我的循环中,我只减少了一个变量.所有其他变量都是只读的.我只使用我写的函数,我认为它们是线程安全的.

My program enumerate all the possible path of a binomial tree and do some work on each path. So my loop size increase exponentially and I would expect the footprint of openMP thread to be zero. In my loop, I only do a reduction of one variable. All other variable are read-only. I only use function I wrote, and I think they are thread safe.

我还在我的程序上运行 Valgrind cachegrind.我不完全理解输出,但似乎没有缓存未命中或错误共享.

I also run Valgrind cachegrind on my program. I don't fully understand the output but there seems to be no cache miss or false sharing.

我用

gcc -O3 -g3 -Wall -c -fmessage-length=0 -lm -fopenmp -ffast-math

我的完整程序如下.抱歉发布了很多代码.我不熟悉 openMP 和 C,我无法在不失去主要任务的情况下继续我的代码.

My complete program is as below. Sorry for posting a lot of code. I'm not familiar with openMP nor C, and I couldn't resume my code more without loosing the main task.

在使用 openMP 时如何提高性能?
它们是否是一些编译器标志或 C 技巧,可以使程序运行得更快?

How can I improve performance when I use openMP?
Are they some compiler flags or C tricks that will make the program run faster?

test.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include "test.h"

int main(){

    printf("starting
");
    int year=20;
    int tradingdate0=1;

    globalinit(year,tradingdate0);

    int i;
    float v=0;
    long n=pow(tradingdate0+1,year);
    #pragma omp parallel for reduction(+:v)
    for(i=0;i<n;i++)
        v+=pathvalue(i);

    globaldel();
    printf("finished
");
    return 0;
}

//***function on which openMP is applied
float pathvalue(long pathindex) {
    float value = -ctx.firstpremium;
    float personalaccount = ctx.personalaccountat0;
    float account = ctx.firstpremium;
    int i;
    for (i = 0; i < ctx.year-1; i++) {
        value *= ctx.accumulationfactor;
        double index = getindex(i,pathindex);
        account = account * index;
        double death = fmaxf(account,ctx.guarantee[i]);
        value += qx(i) * death;
        if (haswithdraw(i)){
            double withdraw = personalaccount*ctx.allowed;
            value += px(i) * withdraw;
            personalaccount = fmaxf(personalaccount-withdraw,0);
            account = fmaxf(account-withdraw,0);
        }
    }

    //last year
    double index = getindex(ctx.year-1,pathindex);
    account = account * index;
    value+=fmaxf(account,ctx.guarantee[ctx.year-1]);

    return value * ctx.discountfactor;
}



int haswithdraw(int period){
    return 1;
}

float getindex(int period, long pathindex){
    int ndx = (pathindex/ctx.chunksize[period])%ctx.tradingdate;
    return ctx.stock[ndx];
}

float qx(int period){
    return 0;
}

float px(int period){
    return 1;
}

//****global
struct context ctx;

void globalinit(int year, int tradingdate0){
    ctx.year = year;
    ctx.tradingdate0 = tradingdate0;
    ctx.firstpremium = 1;
    ctx.riskfreerate = 0.06;
    ctx.volatility=0.25;
    ctx.personalaccountat0 = 1;
    ctx.allowed = 0.07;
    ctx.guaranteerate = 0.03;
    ctx.alpha=1;
    ctx.beta = 1;
    ctx.tradingdate=tradingdate0+1;
    ctx.discountfactor = exp(-ctx.riskfreerate * ctx.year);
    ctx.accumulationfactor = exp(ctx.riskfreerate);
    ctx.guaranteefactor = 1+ctx.guaranteerate;
    ctx.upmove=exp(ctx.volatility/sqrt(ctx.tradingdate0));
    ctx.downmove=1/ctx.upmove;

    ctx.stock=(float*)malloc(sizeof(float)*ctx.tradingdate);
    int i;
    for(i=0;i<ctx.tradingdate;i++)
        ctx.stock[i]=pow(ctx.upmove,ctx.tradingdate0-i)*pow(ctx.downmove,i);

    ctx.chunksize=(long*)malloc(sizeof(long)*ctx.year);
    for(i=0;i<year;i++)
        ctx.chunksize[i]=pow(ctx.tradingdate,ctx.year-i-1);

    ctx.guarantee=(float*)malloc(sizeof(float)*ctx.year);
    for(i=0;i<ctx.year;i++)
        ctx.guarantee[i]=ctx.beta*pow(ctx.guaranteefactor,i+1);
}

void globaldel(){
    free(ctx.stock);
    free(ctx.chunksize);
    free(ctx.guarantee);
}

test.h

float pathvalue(long pathindex);
int haswithdraw(int period);
float getindex(int period, long pathindex);
float qx(int period);
float px(int period);
//***global
struct context{
    int year;
    int tradingdate0;
    float firstpremium;
    float riskfreerate;
    float volatility;
    float personalaccountat0;
    float allowed;
    float guaranteerate;
    float alpha;
    float beta;
    int tradingdate;
    float discountfactor;
    float accumulationfactor;
    float guaranteefactor;
    float upmove;
    float downmove;
    float* stock;
    long* chunksize;
    float* guarantee;
};
struct context ctx;
void globalinit();
void globaldel();

EDIT 我将所有全局变量简化为常量.20 年来,程序运行速度提高了两倍(太棒了!).例如,我尝试使用 OMP_NUM_THREADS=4 ./test 设置线程数.但这并没有给我带来任何性能提升.
我的 gcc 有问题吗?

EDIT I simplify all global variables as constant. For 20 year, the program run two time faster (great!). I tried to set the number of thread with OMP_NUM_THREADS=4 ./test for example. But it didn't give me any performance gain.
Can my gcc have some problem?

test.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <omp.h>
#include "test.h"


int main(){

    starttimer();
    printf("starting
");
    int i;
    float v=0;

    #pragma omp parallel for reduction(+:v)
    for(i=0;i<numberofpath;i++)
        v+=pathvalue(i);

    printf("v:%f
finished
",v);
    endtimer();
    return 0;
}

//function on which openMP is applied
float pathvalue(long pathindex) {
    float value = -firstpremium;
    float personalaccount = personalaccountat0;
    float account = firstpremium;
    int i;
    for (i = 0; i < year-1; i++) {
        value *= accumulationfactor;
        double index = getindex(i,pathindex);
        account = account * index;
        double death = fmaxf(account,guarantee[i]);
        value += death;
        double withdraw = personalaccount*allowed;
        value += withdraw;
        personalaccount = fmaxf(personalaccount-withdraw,0);
        account = fmaxf(account-withdraw,0);
    }

    //last year
    double index = getindex(year-1,pathindex);
    account = account * index;
    value+=fmaxf(account,guarantee[year-1]);

    return value * discountfactor;
}



float getindex(int period, long pathindex){
    int ndx = (pathindex/chunksize[period])%tradingdate;
    return stock[ndx];
}

//timing
clock_t begin;

void starttimer(){
    begin = clock();
}

void endtimer(){
    clock_t end = clock();
    double elapsed = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("
elapsed: %f
",elapsed);
}

test.h

float pathvalue(long pathindex);
int haswithdraw(int period);
float getindex(int period, long pathindex);
float qx(int period);
float px(int period);
//timing
void starttimer();
void endtimer();
//***constant
const int year= 20 ;
const int tradingdate0= 1 ;
const float firstpremium= 1 ;
const float riskfreerate= 0.06 ;
const float volatility= 0.25 ;
const float personalaccountat0= 1 ;
const float allowed= 0.07 ;
const float guaranteerate= 0.03 ;
const float alpha= 1 ;
const float beta= 1 ;
const int tradingdate= 2 ;
const int numberofpath= 1048576 ;
const float discountfactor= 0.301194211912 ;
const float accumulationfactor= 1.06183654655 ;
const float guaranteefactor= 1.03 ;
const float upmove= 1.28402541669 ;
const float downmove= 0.778800783071 ;
const float stock[2]={1.2840254166877414, 0.7788007830714049};
const long chunksize[20]={524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
const float guarantee[20]={1.03, 1.0609, 1.092727, 1.1255088100000001, 1.1592740743, 1.1940522965290001, 1.2298738654248702, 1.2667700813876164, 1.304773183829245, 1.3439163793441222, 1.384233870724446, 1.4257608868461793, 1.4685337134515648, 1.512589724855112, 1.557967416600765, 1.6047064390987882, 1.6528476322717518, 1.7024330612399046, 1.7535060530771016, 1.8061112346694148};

推荐答案

即使您的程序从使用 OpenMP 中受益,您也不会看到它,因为您测量了错误的时间.

Even if your program benefits from using OpenMP, you won't see it because you are measuring the wrong time.

clock() 返回所有线程花费的总 CPU 时间.如果您使用四个线程运行并且每个线程运行 1/4 的时间,clock() 仍将返回相同的值,因为 4*(1/4) = 1.您应该测量 <强>挂钟时间.

clock() returns the total CPU time spent in all threads. If you run with four threads and each runs for 1/4 of the time, clock() will still return the same value since 4*(1/4) = 1. You should be measuring the wall-clock time instead.

omp_get_wtime()gettimeofday() 替换对 clock() 的调用.它们都提供高精度挂钟计时.

Replace calls to clock() with omp_get_wtime() or gettimeofday(). They both provide high precision wall-clock timing.

P.S.为什么周围这么多人使用 clock() 计时?

这篇关于在为顺序运行优化的程序上使用 openMP 后没有性能提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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