乡 曙光 计算,改变您的现在 DAWNING Include <pthread.h> void"Plworker void *arg) Include stdio. h" pthread_mutex_t reduction_mutex; int i, midi pthread_t tidF double sum, mypi, x; /*set individual id to start at*/ double pi,w; myid= pthread_selfo- tid[o] int n; /*integrate function*/ int num_threads; sum=0.0 for(i myid 1; i <= n; i+=num_threads double f(a) x=w*(double)i-0.5; double ai sum+= f(x; return(4.0/1.0+ a*aF mpi- w sumi /*reduce value*/ pthread_mutex_lock(&reduction_mutex); pi += mypii thread_ mutexunlock( &reduction_mutex; return 2006年4月 共享存储编程 21/108
2006年4月 共享存储编程 21/108 #include <pthread.h> #include "stdio.h" pthread_mutex_t reduction_mutex; pthread_t *tid; double pi,w; int n; int num_threads; double f(a) double a; { return (4.0/(1.0 + a*a)); } void *PIworker(void* arg) { int i,myid; double sum,mypi,x; /*set individual id to start at 0 */ myid = pthread_self() - tid[0]; /*integrate function*/ sum=0.0; for(i = myid + 1;i <= n; i+=num_threads){ x = w * ((double)i - 0.5); sum += f(x); } mypi = w * sum; /*reduce value*/ pthread_mutex_lock(&reduction_mutex); pi += mypi; pthread_mutex_unlock(&reduction_mutex); return(0); }
乡 曙光 gcc hello.c-pthread DAWNING a out 10005 转去 Openmp computed pi=3.1415927369231271 void main( argG argv) /*initilize lock*/ int argc; if(pthread_ mutex_init(&reduction_mutex char*argu; g NULL)X fprintf(stderr," Cannot init lockIn"); int i: exit(1); /check command line * if( l=3)t /create the threads*/ printf( Usage: %s Num_intervals for(i=0; i<num_threads; i++x Num_threads"argv[OD; if(pthread_create(&tido,nULL, Plwor exit(o); keY, NULLIK fprintf(stderr,"Cannot create thread /*get num intervals and num threads %d\n"i; from command line*/ exit(万 n atoi(argv[1D; Im_threads= atoi(argv[2]; w=1.0/(double)n; /join threads*/ p=00; for(i=0; i num_threads; i++) tid =(pthread_t*)calloc pthread join(tidO,NULL; (num_threads, sizeof(pthread_ti printf("computed pi=%.16f\n",pi); 2006年4月 共享存储编程 22/108
2006年4月 共享存储编程 22/108 void main(argc,argv) int argc; char* argv[]; { int i; /*check command line */ if(argc != 3) { printf("Usage: %s Num_intervals Num_threads\n",argv[0]); exit(0); } /*get num intervals and num threads from command line*/ n = atoi(argv[1]); num_threads = atoi(argv[2]); w = 1.0 / (double)n; pi = 0.0; tid = (pthread_t*) calloc (num_threads,sizeof(pthread_t)); /*initilize lock*/ if(pthread_mutex_init(&reduction_mutex ,NULL)){ fprintf(stderr,"Cannot init lock\n"); exit(1); } /*create the threads*/ for(i = 0; i<num_threads; i++){ if(pthread_create(&tid[i],NULL,PIwor ker,NULL)){ fprintf(stderr,"Cannot create thread %d\n",i); exit(1); } } /*join threads*/ for(i = 0; i < num_threads; i++) pthread_join(tid[i],NULL); printf("computed pi = %.16f\n",pi); } gcc hello.c –lpthread a.out 1000 5 computed pi = 3.1415927369231271 转去Openmp
乡 曙光 计算,改变您的现在 DAWNING 多线程并行编程特点 pthread_create0创建一个新线程比重新启动一个线程花费的时间 少需要时创建+任务结束立刻杀掉vs,维护一大堆的空闲线程并且 相互切换。 在加锁的前提下访问共享资源 不支持数据并行,适合于任务级并行,即一个线程单独执行一个任务; 不支持增量并行化,对于一个串行程序,很难用 Pthreads进行并行化 Pthreads主要是面向操作系统,而不是为高性能计算设计的,因此 不是并行计算程序设计的主流平台。但是“多线程并发执行”这种 思想却被广泛地应用于高性能计算。这就是我们即将要讲的共享存 储并行编程的另外一种被并行机制造商和广用并行计算用户广泛接 受的平台: OpenMP 2006年4月 共享存储编程 23/108
2006年4月 共享存储编程 23/108 多线程并行编程特点 pthread_create()创建一个新线程比重新启动一个线程花费的时间 少: 需要时创建+任务结束立刻杀掉 vs. 维护一大堆的空闲线程并且 相互切换. 在加锁的前提下访问共享资源 不支持数据并行,适合于任务级并行,即一个线程单独执行一个任务; 不支持增量并行化,对于一个串行程序,很难用Pthreads进行并行化 Pthreads主要是面向操作系统,而不是为高性能计算设计的,因此 不是并行计算程序设计的主流平台。但是“多线程并发执行”这种 思想却被广泛地应用于高性能计算。这就是我们即将要讲的共享存 储并行编程的另外一种被并行机制造商和广用并行计算用户广泛接 受的平台:OpenMP
乡 曙光 计算,改变您的现在 DAWNING 并行编程标准 线程库标准( Thread library) Win 32 AP POSIX threads线程模型 X3H5:概念性线程模型 编译制导( Compiler Directives OpenMP-portable shared memory parallelism 2006年4月 共享存储编程 24/108
2006年4月 共享存储编程 24/108 并行编程标准 线程库标准(Thread Library) – Win32 API. – POSIX threads线程模型. – X3H5:概念性线程模型 编译制导(Compiler Directives) – OpenMP - portable shared memory parallelism
乡 曙光 计算,改变您的现在 DAWNN www openmp.org Open TM An Industry Standard aP/ for Shared Memory Programming .An AP/ for Writing Multithreaded Applications ·一系列编译制导语句和库函数 ·使得 Fortran, c and c++的多线程编程更加容易 2006年4月 共享存储编程 25/108
2006年4月 共享存储编程 25/108 www.openmp.org •An Industry Standard API for Shared Memory Programming •An API for Writing Multithreaded Applications •一系列编译制导语句和库函数 •使得Fortran, C and C++的多线程编程更加容易