openmp的一些示例代码

来源:互联网 发布:unity3d 地形材质 编辑:程序博客网 时间:2024/05/16 12:24
#include <omp.h>#include <stdio.h>#include <stdlib.h>void Test(int n) {  int i = 0;  for(i = 0; i < 10000; ++i)  {  //do nothing, just waste time  }  printf("%d, ", n);}int main(int argc,char* argv[]){  int i = 0;  #pragma omp parallel for  for(i = 0; i < 10; ++i)    Test(i);  printf("\n");}
#include <stdio.h>#include <stdlib.h>void Test(int n) {  int i = 0;  for(i = 0; i < 10000; ++i)  {  //do nothing, just waste time  }  printf("%d, ", n);}int main(int argc,char* argv[]){  int i = 0;  for(i = 0; i < 10; ++i)  Test(i);  printf("\n");}

#include <iostream>//#include <time.h>#include <sys/time.h> void test() {     int a = 0;     for (int i=0;i<100000000;i++)         a++; } int main() {     timeval tstart, tfinish;     if(gettimeofday(&tstart,NULL)!=0){printf("get time error!\n");return 1; }     //clock_t t1 = clock();     #pragma omp parallel for     for (int i=0;i<16;i++)         test();     if(gettimeofday(&tfinish,NULL)!=0){   printf("get time error!\n");   return 1; } double usec=(tfinish.tv_sec-tstart.tv_sec)*1000000+tfinish.tv_usec-tstart.tv_usec; usec/=1000000; std::cout<<"time: "<<usec<<std::endl;     //clock_t t2 = clock();     //std::cout<<"time: "<<t2-t1<<std::endl; }

#include <iostream>//#include <time.h>#include <sys/time.h> void test() {     int a = 0;     for (int i=0;i<100000000;i++)         a++; } int main() {     timeval tstart, tfinish;     if(gettimeofday(&tstart,NULL)!=0){printf("get time error!\n");return 1; }     //clock_t t1 = clock();     for (int i=0;i<16;i++)         test();     if(gettimeofday(&tfinish,NULL)!=0){   printf("get time error!\n");   return 1; } double usec=(tfinish.tv_sec-tstart.tv_sec)*1000000+tfinish.tv_usec-tstart.tv_usec; usec/=1000000; std::cout<<"time: "<<usec<<std::endl;     //clock_t t2 = clock();     //std::cout<<"time: "<<t2-t1<<std::endl; }

#include <iostream> int main() {     int sum = 0;     int a[10] = {1,2,3,4,5,6,7,8,9,10}; #pragma omp parallel for     for(int j=0;j<10;j++) {     #pragma omp parallel for      for (int i=0;i<10;i++)//#pragma omp atomic         sum += a[i];    }     std::cout<<"sum: "<<sum<<std::endl;     return 0; } 

#include <iostream> int main(){     int max = 0;     int a[10] = {11,2,33,49,113,20,321,250,689,16}; #pragma omp parallel for     for (int i=0;i<10;i++)     {         int temp = a[i]; #pragma omp critical         {             if (temp > max)                 max = temp;         }     }     std::cout<<"max: "<<max<<std::endl;     return 0; }

#include <stdio.h>#include <omp.h>int main(){    omp_set_nested(1);    omp_set_dynamic(0);    #pragma omp parallel num_threads(2)    {        if (omp_get_thread_num() == 0)            omp_set_num_threads(4);        else            omp_set_num_threads(6);/* 以下语句将打印 */* 行 A */ /* 行 B *//* 0: 2 4 * 1: 2 6 ** omp_get_num_threads() 返回组中的线程数* 因此,对于* 组中的两个线程来说情况是相同的。 */printf("%d:%d %d\n", omp_get_thread_num(),       omp_get_num_threads(),       omp_get_max_threads());/* 将创建两个内部的并行区域* 一个区域带有包含 4 个线程的组;* 另一个区域带有包含 6 个线程的组。 */#pragma omp parallel{    #pragma omp master    {/* 以下语句将打印 ** 内部: 4 * 内部: 6 */        printf("Inner:%d\n", omp_get_num_threads());    }    omp_set_num_threads(7);      /* line C */}/* 将再次创建两个内部的并行区域,* 一个区域带有包含 4 个线程的组;* 另一个区域带有包含 6 个线程的组。 ** C 行的 omp_set_num_threads(7) 调用* 此处无效,因为它只影响* 与 C 行在相同级别或内部嵌套级别* 的并行区域。*/        #pragma omp parallel        {            printf("count me.\n");        }}return(0); }

// omp_ordered.cpp// compile with: /openmp #include <stdio.h>#include <omp.h>static float a[1000], b[1000], c[1000];void test(int first, int last) {    #pragma omp for schedule(static) ordered    for (int i = first; i <= last; ++i) {        // Do something here.        printf("test()begin iteration %d\n", i);        if (i % 2)         {            #pragma omp ordered             printf("test() iteration %d\n", i);        }        printf("test()end iteration %d\n", i);    }}void test2(int iter) {    #pragma omp ordered    printf("test2() iteration %d\n", iter);}int main( ) {    int i;    #pragma omp parallel    {        test(1, 8);        #pragma omp for ordered        for (i = 0 ; i < 5 ; i++)            test2(i);    }}

// omp_ordered.cpp// compile with: /openmp #include <stdio.h>#include <omp.h>int tmp;void work(); int main( ) {    tmp = 100;    #pragma omp parallel private(tmp)    {    printf("Before work, thread %d, tmp = %d\n",omp_get_thread_num(),tmp);        work();        printf("After work, thread %d, tmp = %d\n",omp_get_thread_num(),tmp);    }    printf("End tmp = %d\n",tmp);}

// omp_ordered.cpp// compile with: /openmp #include <stdio.h>extern int tmp;void work( ) {    printf("tmp in work is %d.\n", tmp);    tmp = 10;}

// omp_ordered.cpp// compile with: /openmp #include <stdio.h>#include <omp.h>int tmp;int main( ) {    tmp = 100;    int j=0;    #pragma omp parallel for firstprivate(tmp) lastprivate(tmp)    for (j=0;j<16;++j) {      tmp+=j;      printf("threadid=%d, j=%d, tmp = %d\n",omp_get_thread_num(), j, tmp);    }    printf("End tmp = %d\n",tmp);}

//Threadprivate语句用法//threadprivate子句用来指定全局的对象被各个线程各自复制了一个私有的拷贝,即各个线程具有各自私有的全局对象。#include <stdio.h>#include <omp.h>int a,b,i,tid;float x;#pragma omp threadprivate(a,x)int main(){ //关闭动态线程分配 omp_set_dynamic(0); printf("1st Parallel Region:\n");#pragma omp parallel private(b,tid) num_threads(4) {  tid=omp_get_thread_num();  a=tid;  b=tid;  x=1.1*tid+1.0;  printf("Thread %d:   a,b,x=%d,%d,%f\n",tid,a,b,x); }//end of parallel section printf("**************************************"); printf("主线程中串行线程\n"); printf("**************************************"); printf("2nd Parallel Region:\n"); printf("Master:   a,x=%d,%f\n",a,x);#pragma omp parallel private(tid) num_threads(4) {  tid=omp_get_thread_num();  printf("Thread %d:   a,b,x=%d,%d,%f\n",tid,a,b,x); }//end of parallel section}/*执行结果:1st Parallel Region:Thread 0:   a,b,x=0,0,1.000000Thread 1:   a,b,x=1,1,2.100000Thread 3:   a,b,x=3,3,4.300000Thread 2:   a,b,x=2,2,3.200000**************************************主线程中串行线程**************************************2nd Parallel Region:Thread 0:   a,b,x=0,0,1.000000Thread 1:   a,b,x=1,0,2.100000Thread 2:   a,b,x=2,0,3.200000Thread 3:   a,b,x=3,0,4.300000*/

#include <stdio.h>#include <omp.h>  int A = 100;  #pragma omp threadprivate(A)  int main(int argc)  {  int B = 100;int C = 1000;    A = 105;    #pragma omp parallel num_threads(2) firstprivate(B) copyin(A) //不加copyin的值继承全局值,不要利用{printf("Initial A = %d\n", A);// 10 for all threadsprintf("Initial B = %d\n", B);// 20 for all threads        #pragma omp single copyprivate(A) copyprivate(B)// copyprivate(C)// C is shared, cannot use copyprivate!{A = 10;B = 20;}printf("Initial A = %d\n", A);// 10 for all threadsprintf("Initial B = %d\n", B);// 20 for all threads}    #pragma omp parallel num_threads(5) firstprivate(B) copyin(A)//将0号线程的值复制给其它线程,不加的话就是初始值{ printf("Initial A = %d\n", A);// 10 for all threadsprintf("Initial B = %d\n", B);    }printf("Global A: %d\n",A);// 10printf("Global B: %d\n",B);// 100. B is still 100! Will not be affected here!return 0;  }

#include <stdio.h>#include <omp.h>#include <unistd.h>#define NUM_THREADS 4#define STATIC_CHUNK 3#define DYNAMIC_CHUNK 3#define NUM_LOOPS 21#define SLEEP_EVERY_N 2//两次Copyin;线程增加, Sections线程少int main( ) {    int nStatic1[NUM_LOOPS],         nStaticN[NUM_LOOPS];    int nDynamic1[NUM_LOOPS],         nDynamicN[NUM_LOOPS];    int nGuided[NUM_LOOPS];    int nDefault[NUM_LOOPS];    omp_set_num_threads(NUM_THREADS);    #pragma omp parallel    {        #pragma omp for        for (int i = 0 ; i < NUM_LOOPS ; ++i)         {            if ((i % SLEEP_EVERY_N) == SLEEP_EVERY_N)                 sleep(0);            nDefault[i] = omp_get_thread_num();        }                #pragma omp for schedule(static, 1)        for (int i = 0 ; i < NUM_LOOPS ; ++i)         {            if ((i % SLEEP_EVERY_N) == SLEEP_EVERY_N)                 sleep(0);            nStatic1[i] = omp_get_thread_num( );        }        #pragma omp for schedule(static, STATIC_CHUNK)        for (int i = 0 ; i < NUM_LOOPS ; ++i)         {            if ((i % SLEEP_EVERY_N) == SLEEP_EVERY_N)                 sleep(0);            nStaticN[i] = omp_get_thread_num( );        }        #pragma omp for schedule(dynamic, 1)        for (int i = 0 ; i < NUM_LOOPS ; ++i)         {            if ((i % SLEEP_EVERY_N) == SLEEP_EVERY_N)                 sleep(0);            nDynamic1[i] = omp_get_thread_num( );        }        #pragma omp for schedule(dynamic, DYNAMIC_CHUNK)        for (int i = 0 ; i < NUM_LOOPS ; ++i)         {            if ((i % SLEEP_EVERY_N) == SLEEP_EVERY_N)                 sleep(0);            nDynamicN[i] = omp_get_thread_num( );        }        #pragma omp for schedule(guided)        for (int i = 0 ; i < NUM_LOOPS ; ++i)         {            if ((i % SLEEP_EVERY_N) == SLEEP_EVERY_N)                 sleep(0);            nGuided[i] = omp_get_thread_num( );        }    }    printf("------------------------------------------------\n");    printf("| static | static | dynamic | dynamic | guided | default |\n");    printf("|    1   |    %d   |    1    |    %d    |        |         |\n",             STATIC_CHUNK, DYNAMIC_CHUNK);    printf("------------------------------------------------\n");    for (int i=0; i<NUM_LOOPS; ++i)     {        printf("|    %d   |    %d   |    %d    |    %d    |"                 "    %d   |    %d    |\n",                 nStatic1[i], nStaticN[i],                 nDynamic1[i], nDynamicN[i], nGuided[i], nDefault[i]);    }    printf("------------------------------------------------\n");}

#include <stdio.h>#include <omp.h>#include <unistd.h>int main( ) {    omp_set_num_threads(1);    #pragma omp parallel    {        #pragma omp sections        {          #pragma omp section          {            printf("start sec1\n");            int tid=omp_get_thread_num();            printf("sec1, tid=%d\n",tid);            printf("end sec1\n");          }          #pragma omp section          {            printf("start sec2\n");            int tid=omp_get_thread_num();            printf("sec2, tid=%d\n",tid);            printf("end sec2\n");          }          #pragma omp section          {          printf("start sec3\n");            int tid=omp_get_thread_num();            printf("sec3, tid=%d\n",tid);            printf("end sec3\n");          }        }        int id = omp_get_thread_num();        printf("endsec,tid=%d\n",id);    }}

#include <omp.h>#include <stdio.h>void task(int p){printf("task, Thread ID: %d, task: %d\n", omp_get_thread_num(), p);}#define N50void init(int*a){for(int i = 0;i < N;i++)a[i] = i + 1;}int main()  {int a[N];init(a);#pragma omp parallel num_threads(4){#pragma omp single{for(int i = 0;i < N; i=i+a[i]){    printf("assign, Thread ID: %d\n", omp_get_thread_num());#pragma omp tasktask(a[i]);}}}return 0;  }//动态task的语义,定义一个独立的可并行执行的代码,有线程空闲的时候就分给线程执行,没有线程空闲,就等待到有线程空闲了之后再做执行。

#include <stdio.h>#include <omp.h>int fib(int n){  int i, j;  if (n<2)    return n;  else    {       #pragma omp task shared(i) firstprivate(n)       i=fib(n-1);       #pragma omp task shared(j) firstprivate(n)       j=fib(n-2);       #pragma omp taskwait       return i+j;    }}int main(){  int n = 10;  omp_set_dynamic(0);  omp_set_num_threads(4);  #pragma omp parallel shared(n)  {    #pragma omp single    printf ("fib(%d) = %d\n", n, fib(n));  }}


0 0
原创粉丝点击