forgot barrier in mt-matmul
[riscv-tests.git] / benchmarks / mt-matmul / matmul_gendata.pl
1 #!/usr/bin/perl -w
2 #==========================================================================
3 # matmul_gendata.pl
4 #
5 # Author : Christopher Batten (cbatten@mit.edu)
6 # Date : April 29, 2005
7 #
8 (our $usageMsg = <<'ENDMSG') =~ s/^\#//gm;
9 #
10 # Simple script which creates an input data set and the reference data
11 # for the matmul benchmark.
12 #
13 ENDMSG
14
15 use strict "vars";
16 use warnings;
17 no warnings("once");
18 use Getopt::Long;
19
20 #--------------------------------------------------------------------------
21 # Command line processing
22 #--------------------------------------------------------------------------
23
24 our %opts;
25
26 sub usage()
27 {
28
29 print "\n";
30 print " Usage: matmul_gendata.pl [options] \n";
31 print "\n";
32 print " Options:\n";
33 print " --help print this message\n";
34 print " --size size of input data [1000]\n";
35 print " --seed random seed [1]\n";
36 print "$usageMsg";
37
38 exit();
39 }
40
41 sub processCommandLine()
42 {
43
44 $opts{"help"} = 0;
45 $opts{"size"} = 1000;
46 $opts{"seed"} = 1;
47 Getopt::Long::GetOptions( \%opts, 'help|?', 'size:i', 'seed:i' ) or usage();
48 $opts{"help"} and usage();
49
50 }
51
52 #--------------------------------------------------------------------------
53 # Helper Functions
54 #--------------------------------------------------------------------------
55
56 sub printArray
57 {
58 my $arrayName = $_[0];
59 my $arrayRef = $_[1];
60
61 my $numCols = 20;
62 my $arrayLen = scalar(@{$arrayRef});
63
64 print "static data_t ".$arrayName."[ARRAY_SIZE] = \n";
65 print "{\n";
66
67 if ( $arrayLen <= $numCols ) {
68 print " ";
69 for ( my $i = 0; $i < $arrayLen; $i++ ) {
70 print sprintf("%3d",$arrayRef->[$i]);
71 if ( $i != $arrayLen-1 ) {
72 print ", ";
73 }
74 }
75 print "\n";
76 }
77
78 else {
79 my $numRows = int($arrayLen/$numCols);
80 for ( my $j = 0; $j < $numRows; $j++ ) {
81 print " ";
82 for ( my $i = 0; $i < $numCols; $i++ ) {
83 my $index = $j*$numCols + $i;
84 print sprintf("%3d",$arrayRef->[$index]);
85 if ( $index != $arrayLen-1 ) {
86 print ", ";
87 }
88 }
89 print "\n";
90 }
91
92 if ( $arrayLen > ($numRows*$numCols) ) {
93 print " ";
94 for ( my $i = 0; $i < ($arrayLen-($numRows*$numCols)); $i++ ) {
95 my $index = $numCols*$numRows + $i;
96 print sprintf("%3d",$arrayRef->[$index]);
97 if ( $index != $arrayLen-1 ) {
98 print ", ";
99 }
100 }
101 print "\n";
102 }
103
104 }
105
106 print "};\n\n";
107 }
108
109
110
111 #--------------------------------------------------------------------------
112 # Matmul
113 #--------------------------------------------------------------------------
114
115 # http://answers.oreilly.com/topic/418-how-to-multiply-matrices-in-perl/
116
117 sub mmult {
118 my ($m1,$m2) = @_;
119 my ($m1rows,$m1cols) = matdim($m1);
120 my ($m2rows,$m2cols) = matdim($m2);
121
122 my $result = [ ];
123 my ($i, $j, $k);
124
125 for $i (range($m1rows)) {
126 for $j (range($m2cols)) {
127 for $k (range($m1cols)) {
128 $result->[$i][$j] += $m1->[$i][$k] * $m2->[$k][$j];
129 }
130 }
131 }
132 return $result;
133 }
134
135 sub range { 0 .. ($_[0] - 1) }
136
137
138 sub veclen {
139 my $ary_ref = $_[0];
140 my $type = ref $ary_ref;
141 if ($type ne "ARRAY") { die "$type is bad array ref for $ary_ref" }
142 return scalar(@$ary_ref);
143 }
144
145 sub matdim {
146 my $matrix = $_[0];
147 my $rows = veclen($matrix);
148 my $cols = veclen($matrix->[0]);
149 return ($rows, $cols);
150 }
151
152 #--------------------------------------------------------------------------
153 # Main
154 #--------------------------------------------------------------------------
155
156 sub main()
157 {
158
159 processCommandLine();
160 srand($opts{"seed"});
161
162 # create random input arrays
163 my $mat_values1;
164 my $mat_values2;
165 for ( my $i = 0; $i < $opts{"size"}; $i++ ) {
166 for ( my $j = 0; $j < $opts{"size"}; $j++ ) {
167 $mat_values1->[$i][$j] = int(rand(4));
168 $mat_values2->[$i][$j] = int(rand(4));
169 }
170 }
171
172 # perform matmul
173 my $mat_results = mmult( $mat_values1, $mat_values2 );
174
175 # translate 2d arrays to 1d-somethings (I don't know how to code in perl - Chris)
176 my @values1;
177 my @values2;
178 my @results;
179 for ( my $i = 0; $i < $opts{"size"}; $i++ ) {
180 for ( my $j = 0; $j < $opts{"size"}; $j++ ) {
181 my $value1 = $mat_values1->[$i][$j];
182 my $value2 = $mat_values2->[$i][$j];
183 my $result = $mat_results->[$i][$j];
184 push( @values1, $value1 );
185 push( @values2, $value2 );
186 push( @results, $result );
187 }
188 }
189
190 print "\n\#define ARRAY_SIZE ".($opts{"size"}*$opts{"size"})." \n\n";
191 print "\n\#define DIM_SIZE ".$opts{"size"}." \n\n";
192
193 printArray( "input1_data", \@values1 );
194 printArray( "input2_data", \@values2 );
195 printArray( "verify_data", \@results);
196
197 }
198
199 main();
200