简单了解 OpenMP

OpenMP(Open Multi-Processing)是一个应用程序接口(API),由一组主要的计算机硬件和软件供应商联合定义。OpenMP 为共享内存并行应用程序的开发人员提供了一个可移植的、可伸缩的模型。可用于显式地指示多线程、共享内存并行性。

OpenMP支持的编程语言包括C、C++和Fortran。

运行时库函数

1
#include <omp.h>

用OpenMP加速矩阵相乘

并行优化10000*10000的矩阵相乘,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <omp.h>
#include <stdlib.h>
#include <time.h>

const int NUM_THREADS = 20;
typedef long long ll;
int m = 10000, n = 10000;
int mat[10000][10000];
int vec[10000], ans[10000];

void makeRandomMatrix()
{
srand(time(NULL));
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 10 + 1;
}
}
}

void makeRandomVector()
{
srand(time(NULL));
int i;
for (i = 0; i < n; i++)
{
vec[i] = rand() % 10 + 1;
}
}

void funy(int a[], int cur)
{
int i;
for (i = 0; i < n; i++)
{
ans[cur] += a[i] * vec[i];
}
}

void f()
{
int i;
for (i = 0; i < m; i++)
{
funy(mat[i], i);
}
}

void fp()
{
int i;
#pragma omp parallel
{
int id = omp_get_thread_num();
#pragma omp parallel for
for (i = id; i < m; i += NUM_THREADS)
{
funy(mat[i], i);
}
}
}

int main()
{
printf("Makeing matrix(10000*10000) & vector(10000*1)...\n");
makeRandomMatrix();
makeRandomVector();
double start_time = omp_get_wtime();
f();
// for (int i = 0; i < m; i ++) printf("%d%c\n", ans[i], ' \n'[i==n-1]);
double end_time = omp_get_wtime();
printf("1 thread --- Running time=%f s\n", end_time - start_time);
start_time = omp_get_wtime();
fp();
end_time = omp_get_wtime();
// for (int i = 0; i < m; i ++) printf("%d%c\n", ans[i], ' \n'[i==n-1]);
printf("20 threads --- Running time=%f s\n", end_time - start_time);
return 0;
}

教程分享: