-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatmul_simple_omp.cpp
134 lines (110 loc) · 3.74 KB
/
matmul_simple_omp.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//******************************************************************************
// matmul_simple_omp.cpp : Matrix multiplication based on simple triple loop way
// with OpenMP parallelization
// Copyright (C) 2019 Tomonori Kouya
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation, either version 3 of the License or any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//******************************************************************************
#include <iostream>
#include <iomanip>
#include <cmath>
// matmul_gflops, byte_double_sqmt, and normf_dmatrix_array
#include "matmul_block.h"
// Time estimation: get_secv, get_real_secv
#include "get_secv.h"
#ifdef _OPENMP
#include <omp.h>
#endif // _OPENMP
using namespace std;
// square matrix multiplication (row-major order)
void matmul_simple(double ret[], double mat_a[], double mat_b[], int dim)
{
int i, j, k, ij_index;
for(i = 0; i < dim; i++)
{
#pragma omp parallel for private(ij_index, k)
for(j = 0; j < dim; j++)
{
ij_index = i * dim + j; // row-major
ret[ij_index] = 0.0;
for(k = 0; k < dim; k++)
ret[ij_index] += mat_a[i * dim + k] * mat_b[k * dim + j];
}
}
}
// main function
int main(int argc, char *argv[])
{
int i, j, min_dim, max_dim, dim, iter, max_iter;
double *mat_a, *mat_b, *mat_c;
double stime, etime;
if(argc < 3)
{
cout << "Usage: " << argv[0] << " [min. dimension] [max.dimension]"<< endl;
return 1;
}
min_dim = atoi(argv[1]);
max_dim = atoi(argv[2]);
if(min_dim <= 0)
{
cout << "Illegal dimension! (min_dim = " << min_dim << ")" << endl;
return 1;
}
#ifdef _OPENMP
int num_threads;
cout << "num_threads: ";
cin >> num_threads;
omp_set_num_threads(num_threads);
#endif // _OPENMP
// main loop
cout << setw(5) << " dim : SECONDS GFLOPS Mat.KB ||C||_F" << endl;
for(dim = min_dim; dim <= max_dim; dim += 16)
{
// initialize matrices
//mat_a = new double[dim * dim];
mat_a = (double *)calloc(dim * dim, sizeof(double));
mat_b = (double *)calloc(dim * dim, sizeof(double));
mat_c = (double *)calloc(dim * dim, sizeof(double));
// set values to mat_a and mat_b
for(i = 0; i < dim; i++)
{
for(j = 0; j < dim; j++)
{
mat_a[i * dim + j] = sqrt(5.0) * (double)(i + j + 1);
mat_b[i * dim + j] = sqrt(3.0) * (double)(dim - (i + j));
// mat_a[i * dim + j] = 1.0 / (double)(i + j + 1);
// mat_b[i * dim + j] = (double)(i + j + 1);
}
}
max_iter = 3; // the number of trial time is 3 at least
do
{
stime = get_real_secv();
for(iter = 0; iter < max_iter; iter++)
matmul_simple(mat_c, mat_a, mat_b, dim);
etime = get_real_secv(); etime -= stime;
if(etime >= 1.0) break; // repeat until total time is over 1 second
max_iter *= 2;
} while(0);
etime /= (double)max_iter; // // average time
// output
cout << setw(5) << dim << " : " << setw(10) << setprecision(5) << etime << " " << matmul_gflops(etime, dim) << " " << byte_double_sqmat(dim) / 1024 << " " << normf_dmatrix_array(mat_c, dim, dim) << endl;
// delete matrices
free(mat_a);
free(mat_b);
free(mat_c);
} // end of main loop
return 0;
}