预处理指令pragma:在系统中加入预处理器指令一般是用来允许不是基本c语言规范部分的行为。不支持pragma的编译器会忽略pragma指令提示的那些语句,这样就允许使用pragma的程序在不支持它们的平台上运行。
#include <stdio.h> #include <stdlib.h> #include <omp.h> void Hello(void); // Thread function int main(int argc, char* argv[]) { // Get number of threads from command line int thread_count = strtol(argv[1], NULL, 10); #pragma omp parallel num_threads(thread_count) Hello(); return 0; } void Hello(void) { int my_rank = omp_get_thread_num(); int thread_count = omp_get_num_threads(); printf("Hello from thread %d of %dnn ", my_rank, thread_count); } //Hello
# pragma omp parallel :
# pragma omp parallel num_threads ( thread_count )
#include <omp.h>
使用openmp必须含omp.h头文件
long strtol(const char* number p,char** end p,int base)
//串行算法实现 // Input: a, b, n h = (b*a)/n; approx = (f(a) + f(b))/2.0; for (i = 1; i <= n-1; i++) { x_i = a + ih; approx += f(x_i); } approx = h*approx;
# pragma omp critical
global_result += my_result ;
#include <stdio.h> #include <stdlib.h> #include <omp.h> void Trap(double a, double b, int n, double global_result p); int main(int argc, char argv[]){ double global_result = 0.0; double a, b; int n; int thread_count; thread_count = strtol(argv[1], NULL, 10); printf("Enter a, b, and n n"); scanf("%lf %lf %d", &a, &b, &n); # pragma omp parallel num_threads(thread_count) Trap(a, b, n, &global_result); printf("With n = %d trapezoids, our estimate n", n); printf("of the integral from %f to %f = %.14e n", a, b, global_result); return 0; } /∗ main ∗/ void Trap(double a, double b, int n, double* global_result_p) double h, x, my_result; double local_a, local_b; int i, local n; int my_rank = omp_get_thread_num(); int thread_count = omp_get_num_threads(); h = (b−a)/n; local_n = n/thread_count; local_a = a + my_rank*local_n*h; local_b = local_a + local_n*h; my_result = (f(local_a) + f(local_b))/2.0; for (i = 1; i <= local_n−1; i++){ x = local_a + i*h; my_result += f(x); } ` ` my_result = my_result*h; # pragma omp critical ∗global_result_p += my_result; } /∗ Trap ∗/
规约从句
所有操作的中间结果存储在一个变量中:归约变量
reduction(<operator>:<variable list>)
global_result = 0.0; # pragma omp parallel num threads(thread count) reduction(+: global_result) global_result += Local_trap(double a, double b, int n);
parallel for:
h = (b−a)/n; approx = (f(a) + f(b))/2.0; # pragma omp parallel for num threads(thread_count) reduction(+: approx) for (i = 1; i <= n−1; i++) approx += f(a + i∗h); approx = h∗approx;
ps:
数据依赖:
default子句:
double sum = 0.0; # pragma omp parallel for num threads(thread count) default(none) reduction(+:sum) private(k, factor) shared(n) for (k = 0; k < n; k++){ if (k % 2 == 0) factor = 1.0; else factor = −1.0; sum += factor/(2∗k+1); }
# pragma omp for
schedule ( type , chunksize )
//chunksize 是一个正整数