美文网首页
2020-04-02 openMP

2020-04-02 openMP

作者: Silly_N_Fool | 来源:发表于2020-04-02 20:49 被阅读0次
    int nthreads, tid;
     #pragma omp parallel private(nthreads, tid)
     {
 
         /* Obtain thread number */
         tid = omp_get_thread_num();
         printf("Hello World from thread = %d\n", tid);
 
         /* Only master thread does this */
         if (tid == 0)
         {
             nthreads = omp_get_num_threads();
             printf("Number of threads = %d\n", nthreads);
         }

    }  /* All threads join master thread and disband */  
    int x = 2;
    # pragma omp parallel num_threads(2) shared(x)
    {
        if (omp_get_thread_num() == 0){
            x = 5;
        }else{
            printf("1: Thread# %d: x = %d\n", omp_get_thread_num(),x);
        }
        #pragma omp barrier
        if (omp_get_thread_num() == 0){
            printf("2: Thread# %d: x = %d\n", omp_get_thread_num(),x);
        }else{
            printf("3: Thread# %d: x = %d\n", omp_get_thread_num(),x);
        }
    }
    int nthreads,tid,var3;
    var3=30;
    char buf[32];
/* Fork a team of threads */
    #pragma omp parallel private(nthreads,tid) shared(var3)
    {
        tid= omp_get_thread_num(); /* Obtain and print thread id */
        printf("Hello, world from OpenMPthread %d, shared var=%d\n", tid,var3);
        if (tid== 0) /*Only master thread does this */
        {
            nthreads= omp_get_num_threads();
            printf(" Number of threads %d, shared var=%d\n",nthreads,var3);
        }
    }
static long num_steps= 10000000;
double step;
#define NUM_THREADS 4
int main(){    
    int i; 
    double x, pi, sum = 0.0, start_time,end_time;
    step = 1.0/(double) num_steps;
    start_time=clock();
    for (i=1;i<= num_steps; i++){
        x = (i-0.5)*step;
        sum = sum+ 4.0/(1.0+x*x);
    }
    pi = step * sum;
    end_time=clock();
    printf("Pi=%.10lf\nRunning time %d\n", pi, end_time-start_time);

    return 0;
}
static long num_steps= 100000;
double step;
#define NUM_THREADS 4
int main(){

    int i ; 
    double pi, sum[NUM_THREADS] , start_time, end_time;
    step = 1.0/(double) num_steps;
    omp_set_num_threads(NUM_THREADS);
    start_time=omp_get_wtime();
    #pragma omp parallel
    {
        int id; double x;
        id = omp_get_thread_num();
        for (i=id, sum[id]=0.0; i< num_steps; i=i+NUM_THREADS){
            x = (i+0.5)*step; 
            sum[id] += 4.0/(1.0+x*x);
        }   
    }
    for (i=0, pi=0.0;i<NUM_THREADS;i++) {
        pi += sum[i] * step;
    } 
    end_time=omp_get_wtime();
    printf("Pi=%lf\nRunning time %d\n", pi, end_time-start_time);

    return 0;
}
    int i,id; 
    double start_time, end_time;
    double x, pi, sum[NUM_THREADS];
    step = 1.0/(double) num_steps;
    omp_set_num_threads(NUM_THREADS);
    start_time=omp_get_wtime();
    #pragma omp  parallel private(x, id)
    {
        id = omp_get_thread_num(); sum[id] = 0;
        #pragma omp  for
        for (i=0;i< num_steps; i++){
        x = (i+0.5)*step;
        sum[id] += 4.0/(1.0+x*x);
        }
    }
    for(i=0, pi=0.0;i<NUM_THREADS; i++) pi += sum[i] * step;
    end_time=omp_get_wtime();
    printf("Pi=%lf\nRunning time %d\n", pi, end_time-start_time);

要避免数据竞争就要声明私有变量
局部变量都是私有的
Pi的并行计算每个id占据一个数组位置,最后对数组进行累加

相关文章

  • 2020-04-02 openMP

    要避免数据竞争就要声明私有变量局部变量都是私有的Pi的并行计算每个id占据一个数组位置,最后对数组进行累加

  • OpenMP入门指南

    What's OpenMP The OpenMP API supports multi-platform shar...

  • Cython:OpenMP配置

    在Cython中使用parallel、prange或者OpenMP时,除了添加/openmp编译参数,还需要将vc...

  • openMP 函数总结(并行程序设计导论)

    本篇文章只是记录api的用法和回顾,方便记忆 openMP openMP提供“基于指令”的共享内存API。这就意味...

  • Windows上配置MPI环境

    本文目标:配置MS-MPI,mpich2, openmp的运行环境,openmp配置非常简单,关键是算法。 mpi...

  • 多线程 Windows MSVC OpenMP

    Thread affinity with Windows, MSVC, and OpenMP[https://st...

  • Mac下安装OpenMP并完成编译

    OpenMP在Mac上的安装,涉及到一些编译器的历史。。。 OpenMP环境添加(Mac) 安装错误 安装错误参考...

  • 杂记

    并行计算之OpenMP入门简介 对基于数据分集的多线程程序设计,OpenMP是一个很好的选择。同时,使用OpenM...

  • 【转载】OpenMP、MPICH与OpenMPI

    原文网址 openmp比较简单,修改现有的大段代码也容易。基本上openmp只要在已有程序基础上根据需要加并行语句...

  • C++速度很快

    测试内存访问密集型 计算10亿个整型数的和,开了OpenMP,花费时间是700毫秒,不开OpenMP则更快,150...

网友评论

      本文标题:2020-04-02 openMP

      本文链接:https://www.haomeiwen.com/subject/ubvfphtx.html