-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathdgemm.py
executable file
·143 lines (123 loc) · 5.17 KB
/
dgemm.py
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
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
#
# Copyright (c) 2015, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Intel Corporation nor the names of its
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#*******************************************************************
#
# NAME: dgemm
#
# PURPOSE: This program tests the efficiency with which a dense matrix
# dense multiplication is carried out
#
# USAGE: The program takes as input the matrix order,
# the number of times the matrix-matrix multiplication
# is carried out.
#
# <progname> <# iterations> <matrix order>
#
# The output consists of diagnostics to make sure the
# algorithm worked, and of timing statistics.
#
# HISTORY: Written by Rob Van der Wijngaart, February 2009.
# Converted to Python by Jeff Hammond, February 2016.
# Fixed timing err, Ave+std_dev, more pythonic, Tim Mattson May 2021
# *******************************************************************
import numpy as np
import sys
print('Python version = ', str(sys.version_info.major)+'.'+str(sys.version_info.minor))
if sys.version_info >= (3, 3):
from time import process_time as timer
else:
from timeit import default_timer as timer
def main():
# ********************************************************************
# read and test input parameters
# ********************************************************************
print('Parallel Research Kernels version ') #, PRKVERSION
print('Python Dense matrix-matrix multiplication: C = A x B')
if len(sys.argv) != 3:
print('argument count = ', len(sys.argv))
sys.exit("Usage: ./dgemm <# iterations> <matrix order>")
iters = int(sys.argv[1])
if iters < 1:
sys.exit("ERROR: iterations must be >= 1")
order = int(sys.argv[2])
if order < 1:
sys.exit("ERROR: order must be >= 1")
print('Number of iterations = ', iters)
print('Matrix order = ', order)
# ********************************************************************
# ** Allocate space for the input and transpose matrix
# ********************************************************************
A = np.zeros((order,order))
B = np.zeros((order,order))
C = np.zeros((order,order))
for i in range(order):
A[:,i] = float(i)
B[:,i] = float(i)
for kiter in range(0,iters+1):
if kiter==1:
t0 = timer()
tSum=0.0
tsqSum=0.0
for i in range(order):
for k in range(order):
for j in range(order):
C[i][j] += A[i][k] * B[k][j]
if kiter>0:
tkiter = timer()
t = tkiter - t0
tSum = tSum + t
tsqSum = tsqSum+t*t
t0 = tkiter
dgemmAve = tSum/iters
dgemmStdDev = ((tsqSum-iters*dgemmAve*dgemmAve)/(iters-1))**0.5
# ********************************************************************
# ** Analyze and output results.
# ********************************************************************
checksum = 0.0;
for i in range(order):
for j in range(order):
checksum += C[i][j];
ref_checksum = 0.25*order*order*order*(order-1.0)*(order-1.0)
ref_checksum *= (iters+1)
epsilon=1.e-8
if abs((checksum - ref_checksum)/ref_checksum) < epsilon:
print('Solution validates')
nflops = 2.0*order*order*order
recipDiff = (1.0/(dgemmAve-dgemmStdDev) - 1.0/(dgemmAve+dgemmStdDev))
GfStdDev = 1.e-6*nflops*recipDiff/2.0
print('nflops: ',nflops)
print('Rate: ',1.e-6*nflops/dgemmAve,' +/- (MF/s): ',GfStdDev)
else:
print('ERROR: Checksum = ', checksum,', Reference checksum = ', ref_checksum,'\n')
sys.exit("ERROR: solution did not validate")
if __name__ == '__main__':
main()