diff options
author | Henry Cook <hcook@eecs.berkeley.edu> | 2013-06-13 15:30:16 -0700 |
---|---|---|
committer | Henry Cook <hcook@eecs.berkeley.edu> | 2013-06-13 15:30:16 -0700 |
commit | 60f056880ec6929c5f23af4d66aea0f0cb7b0245 (patch) | |
tree | a2f4cbc9902df362534ede13d65883ee47fba2d8 /mt/ay_matmul | |
parent | 4412b96c81ca09dcce6305579dd86d4bf3b808da (diff) | |
download | riscv-tests-60f056880ec6929c5f23af4d66aea0f0cb7b0245.zip riscv-tests-60f056880ec6929c5f23af4d66aea0f0cb7b0245.tar.gz riscv-tests-60f056880ec6929c5f23af4d66aea0f0cb7b0245.tar.bz2 |
multithreading tests from 152 lab 5
Diffstat (limited to 'mt/ay_matmul')
-rw-r--r-- | mt/ay_matmul/.matmul.c.swp | bin | 0 -> 20480 bytes | |||
-rw-r--r-- | mt/ay_matmul/ay_matmul.c | 210 | ||||
-rwxr-xr-x | mt/ay_matmul/dataset.h | 174 | ||||
-rwxr-xr-x | mt/ay_matmul/matmul_gendata.pl | 200 | ||||
-rw-r--r-- | mt/ay_matmul/matmul_mi.c | 258 |
5 files changed, 842 insertions, 0 deletions
diff --git a/mt/ay_matmul/.matmul.c.swp b/mt/ay_matmul/.matmul.c.swp Binary files differnew file mode 100644 index 0000000..9ebeb79 --- /dev/null +++ b/mt/ay_matmul/.matmul.c.swp diff --git a/mt/ay_matmul/ay_matmul.c b/mt/ay_matmul/ay_matmul.c new file mode 100644 index 0000000..2a1e04c --- /dev/null +++ b/mt/ay_matmul/ay_matmul.c @@ -0,0 +1,210 @@ +//**************************************************************************
+// Multi-threaded Matrix Multiply benchmark
+//--------------------------------------------------------------------------
+// TA : Christopher Celio
+// Student:
+//
+//
+// This benchmark multiplies two 2-D arrays together and writes the results to
+// a third vector. The input data (and reference data) should be generated
+// using the matmul_gendata.pl perl script and dumped to a file named
+// dataset.h.
+
+
+// print out arrays, etc.
+//#define DEBUG
+
+//--------------------------------------------------------------------------
+// Includes
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+
+//--------------------------------------------------------------------------
+// Input/Reference Data
+
+typedef float data_t;
+#include "dataset.h"
+
+
+//--------------------------------------------------------------------------
+// Basic Utilities and Multi-thread Support
+
+__thread unsigned long coreid;
+unsigned long ncores;
+
+#include "util.h"
+
+#define stringify_1(s) #s
+#define stringify(s) stringify_1(s)
+#define stats(code) do { \
+ unsigned long _c = -rdcycle(), _i = -rdinstret(); \
+ code; \
+ _c += rdcycle(), _i += rdinstret(); \
+ if (coreid == 0) \
+ printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
+ stringify(code), _c, _c/DIM_SIZE/DIM_SIZE/DIM_SIZE, 10*_c/DIM_SIZE/DIM_SIZE/DIM_SIZE%10, _c/_i, 10*_c/_i%10); \
+ } while(0)
+
+
+//--------------------------------------------------------------------------
+// Helper functions
+
+void printArray( char name[], int n, data_t arr[] )
+{
+ int i;
+ if (coreid != 0)
+ return;
+
+ printf( " %10s :", name );
+ for ( i = 0; i < n; i++ )
+ printf( " %3ld ", (long) arr[i] );
+ printf( "\n" );
+}
+
+void __attribute__((noinline)) verify(size_t n, const data_t* test, const data_t* correct)
+{
+ if (coreid != 0)
+ return;
+
+ size_t i;
+ for (i = 0; i < n; i++)
+ {
+ if (test[i] != correct[i])
+ {
+ printf("FAILED test[%d]= %3ld, correct[%d]= %3ld\n",
+ i, (long)test[i], i, (long)correct[i]);
+ exit(-1);
+ }
+ }
+
+ return;
+}
+
+//--------------------------------------------------------------------------
+// matmul function
+
+// single-thread, naive version
+void __attribute__((noinline)) matmul_naive(const int lda, const data_t A[], const data_t B[], data_t C[] )
+{
+ int i, j, k;
+
+ if (coreid > 0)
+ return;
+
+ for ( i = 0; i < lda; i++ )
+ for ( j = 0; j < lda; j++ )
+ {
+ for ( k = 0; k < lda; k++ )
+ {
+ C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
+ }
+ }
+
+}
+
+void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
+{
+ static __thread int i, j, k;
+ static __thread data_t tempA0, tempA1, tempA2, tempA3, tempA4, tempA5, tempA6, tempA7;
+ static __thread data_t tempC0, tempC1, tempC2, tempC3, tempC4, tempC5, tempC6, tempC7, tempC8, tempC9, tempC10, tempC11, tempC12, tempC13, tempC14, tempC15;
+
+ static __thread int start, end, jStride, jToRow, jToCol;
+
+ start = coreid << 9;
+ end = (coreid+1) << 9;
+ jStride = 8;
+
+ for (j=start; j < end; j+=jStride) {
+ jToRow = (j>>5)<<5;
+ jToCol = j%32;
+ tempC0 = 0;
+ tempC1 = 0;
+ tempC2 = 0;
+ tempC3 = 0;
+ tempC4 = 0;
+ tempC5 = 0;
+ tempC6 = 0;
+ tempC7 = 0;
+ for ( i=0; i < lda; i+=2 ) {
+ tempA0 = A[i + jToRow];
+ tempA1 = A[i+1 + jToRow];
+ tempC0 += tempA0 * B[(jToCol ) + (i<<5)];
+ tempC1 += tempA0 * B[(jToCol+1 ) + (i<<5)];
+ tempC2 += tempA0 * B[(jToCol+2 ) + (i<<5)];
+ tempC3 += tempA0 * B[(jToCol+3 ) + (i<<5)];
+ tempC4 += tempA0 * B[(jToCol+4 ) + (i<<5)];
+ tempC5 += tempA0 * B[(jToCol+5 ) + (i<<5)];
+ tempC6 += tempA0 * B[(jToCol+6 ) + (i<<5)];
+ tempC7 += tempA0 * B[(jToCol+7 ) + (i<<5)];
+ tempC0 += tempA1 * B[(jToCol ) + ((i+1)<<5)];
+ tempC1 += tempA1 * B[(jToCol+1 ) + ((i+1)<<5)];
+ tempC2 += tempA1 * B[(jToCol+2 ) + ((i+1)<<5)];
+ tempC3 += tempA1 * B[(jToCol+3 ) + ((i+1)<<5)];
+ tempC4 += tempA1 * B[(jToCol+4 ) + ((i+1)<<5)];
+ tempC5 += tempA1 * B[(jToCol+5 ) + ((i+1)<<5)];
+ tempC6 += tempA1 * B[(jToCol+6 ) + ((i+1)<<5)];
+ tempC7 += tempA1 * B[(jToCol+7 ) + ((i+1)<<5)];
+ }
+ C[j] =tempC0;
+ C[j + 1 ]=tempC1;
+ C[j + 2 ]=tempC2;
+ C[j + 3 ]=tempC3;
+ C[j + 4 ]=tempC4;
+ C[j + 5 ]=tempC5;
+ C[j + 6 ]=tempC6;
+ C[j + 7 ]=tempC7;
+ }
+
+}
+
+//--------------------------------------------------------------------------
+// Main
+//
+// all threads start executing thread_entry(). Use their "coreid" to
+// differentiate between threads (each thread is running on a separate core).
+
+void thread_entry(int cid, int nc)
+{
+ coreid = cid;
+ ncores = nc;
+
+ // static allocates data in the binary, which is visible to both threads
+ static data_t results_data[ARRAY_SIZE];
+
+
+ //// Execute the provided, naive matmul
+ //barrier();
+ //stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
+
+ //
+ //// verify
+ //verify(ARRAY_SIZE, results_data, verify_data);
+ //
+ //// clear results from the first trial
+ //size_t i;
+ //if (coreid == 0)
+ // for (i=0; i < ARRAY_SIZE; i++)
+ // results_data[i] = 0;
+ //barrier();
+
+
+ // Execute your faster matmul
+ barrier();
+ stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
+
+#ifdef DEBUG
+ printArray("results:", ARRAY_SIZE, results_data);
+ printArray("verify :", ARRAY_SIZE, verify_data);
+#endif
+
+ // verify
+ verify(ARRAY_SIZE, results_data, verify_data);
+ barrier();
+
+ exit(0);
+}
+
+
diff --git a/mt/ay_matmul/dataset.h b/mt/ay_matmul/dataset.h new file mode 100755 index 0000000..dde3ee4 --- /dev/null +++ b/mt/ay_matmul/dataset.h @@ -0,0 +1,174 @@ + +#define ARRAY_SIZE 1024 + + +#define DIM_SIZE 32 + +static data_t input1_data[ARRAY_SIZE] = +{ + 0, 3, 2, 0, 3, 1, 0, 3, 2, 3, 2, 0, 3, 3, 1, 2, 3, 0, 0, 1, + 1, 1, 2, 3, 1, 2, 3, 1, 1, 3, 2, 2, 0, 1, 3, 2, 2, 2, 0, 0, + 1, 0, 1, 3, 3, 0,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t verify_data[ARRAY_SIZE] = +{ + 72, 75, 88, 101, 80, 88, 73, 75, 80, 81, 58, 75, 86, 65, 60, 80, 84, 83, 87, 83, + 108, 93, 85, 76, 72, 98, 79, 86, 80, 96, 91, 85, 72, 64, 70, 83, 68, 92, 51, 54, + 85, 85, 60, 58, 90, 64, 55, 69, 72, 48, 94, 77, 91, 83, 70, 69, 67, 77, 59, 50, + 67, 74, 77, 67, 67, 62, 72, 71, 68, 79, 54, 61, 67, 61, 55, 62, 78, 60, 53, 64, + 67, 69, 99, 68, 88, 60, 66, 63, 70, 62, 65, 50, 53, 66, 70, 72, 75, 78, 85, 95, + 71, 89, 70, 68, 86, 88, 58, 77, 84, 70, 65, 68, 73, 75, 91, 96, 105, 92, 76, 68, + 86, 69, 80, 59, 73, 83, 88, 75, 64, 63, 71, 99, 77, 77, 69, 55, 80, 73, 54, 73, + 87, 78, 60, 69, 65, 78, 86, 89, 95, 92, 63, 69, 89, 61, 80, 65, 70, 77, 89, 77, + 79, 79, 73, 92, 64, 81, 60, 78, 81, 80, 61, 63, 89, 65, 56, 83, 77, 65, 102, 70, + 98, 86, 96, 68, 72, 89, 73, 73, 70, 89, 84, 76, 48, 61, 63, 70, 70, 79, 50, 53, + 64, 63, 43, 51, 59, 62, 43, 63, 55, 77, 79, 74, 75, 74, 64, 44, 65, 69, 72, 66, + 54, 71, 74, 72, 69, 76, 68, 89, 94, 75, 65, 53, 85, 79, 65, 74, 82, 73, 58, 70, + 84, 77, 99, 72, 92, 84, 78, 62, 59, 83, 71, 74, 63, 85, 80, 78, 71, 72, 79, 83, + 73, 82, 60, 85, 76, 82, 60, 70, 82, 68, 54, 85, 84, 70, 86, 74, 100, 88, 98, 68, + 67, 87, 69, 73, 68, 88, 76, 71, 47, 43, 47, 80, 54, 65, 40, 37, 59, 53, 33, 48, + 62, 40, 36, 55, 36, 62, 53, 57, 70, 69, 45, 43, 53, 61, 42, 57, 56, 63, 51, 47, + 59, 75, 64, 89, 83, 75, 59, 75, 91, 92, 58, 64, 83, 74, 58, 60, 76, 66, 97, 69, + 90, 95, 92, 64, 78, 75, 77, 73, 65, 78, 82, 75, 47, 54, 59, 71, 59, 56, 53, 42, + 60, 55, 40, 51, 60, 46, 36, 59, 46, 57, 67, 43, 51, 53, 53, 38, 54, 56, 55, 48, + 41, 46, 63, 63, 80, 77, 89, 102, 89, 98, 74, 86, 98, 93, 63, 76, 98, 77, 48, 101, + 86, 88, 100, 82, 102, 90, 95, 75, 86, 103, 83, 98, 80, 104, 98, 86, 71, 74, 80, 90, + 86, 87, 73, 70, 81, 83, 55, 66, 90, 66, 58, 84, 77, 84, 93, 72, 99, 75, 85, 65, + 70, 89, 71, 82, 64, 79, 82, 80, 67, 73, 86, 101, 78, 97, 66, 64, 84, 80, 55, 64, + 79, 73, 51, 79, 89, 68, 94, 77, 109, 102, 82, 61, 66, 93, 88, 70, 82, 82, 85, 69, + 69, 72, 66, 97, 85, 90, 70, 59, 76, 89, 53, 56, 90, 79, 71, 64, 70, 67, 100, 92, + 106, 89, 83, 78, 73, 80, 70, 72, 65, 70, 92, 88, 57, 76, 55, 85, 66, 80, 61, 63, + 63, 78, 54, 58, 71, 73, 54, 63, 63, 62, 89, 76, 86, 81, 83, 54, 70, 81, 78, 64, + 56, 72, 74, 81, 75, 63, 68, 89, 65, 77, 58, 68, 75, 83, 52, 62, 82, 63, 55, 75, + 51, 70, 95, 66, 83, 77, 86, 61, 64, 77, 48, 70, 66, 82, 72, 75, 79, 71, 72, 89, + 78, 78, 66, 59, 91, 80, 55, 64, 79, 68, 54, 71, 67, 75, 87, 84, 100, 101, 76, 58, + 74, 82, 61, 74, 75, 97, 85, 79, 61, 55, 69, 68, 72, 65, 52, 64, 80, 73, 48, 54, + 71, 66, 42, 61, 66, 63, 92, 64, 85, 77, 73, 54, 74, 73, 76, 66, 62, 79, 85, 70, + 71, 84, 87, 81, 88, 86, 77, 77, 93, 88, 78, 71, 101, 89, 58, 84, 95, 81, 89, 97, + 104, 79, 83, 76, 90, 81, 91, 74, 70, 76, 91, 80, 51, 48, 56, 69, 47, 63, 54, 42, + 63, 63, 42, 52, 66, 56, 39, 59, 61, 52, 59, 63, 62, 68, 57, 35, 67, 58, 56, 52, + 61, 63, 60, 47, 85, 75, 89, 106, 88, 95, 74, 82, 107, 107, 64, 78, 98, 90, 62, 91, + 79, 87, 111, 84, 104, 106, 96, 68, 94, 99, 81, 89, 79, 105, 95, 86, 65, 63, 77, 89, + 66, 88, 56, 73, 82, 92, 41, 62, 85, 66, 50, 81, 57, 71, 77, 78, 86, 89, 77, 53, + 67, 78, 61, 63, 72, 82, 69, 66, 59, 46, 55, 70, 56, 64, 45, 50, 65, 64, 42, 56, + 78, 49, 51, 52, 38, 56, 72, 55, 73, 72, 61, 50, 63, 60, 47, 57, 55, 73, 53, 68, + 85, 88, 91, 96, 82, 89, 73, 76, 87, 86, 67, 69, 96, 84, 57, 89, 87, 89, 99, 88, + 104, 90, 85, 75, 88, 92, 85, 75, 74, 87, 103, 94, 55, 48, 56, 65, 72, 50, 45, 51, + 63, 62, 47, 57, 79, 53, 36, 63, 54, 68, 71, 59, 63, 61, 63, 41, 50, 73, 57, 59, + 56, 76, 73, 65, 61, 64, 61, 79, 53, 73, 57, 44, 61, 59, 59, 56, 81, 59, 49, 62, + 65, 55, 69, 72, 79, 70, 58, 57, 68, 61, 62, 50, 57, 60, 66, 66, 63, 77, 81, 89, + 85, 81, 76, 73, 78, 95, 59, 70, 81, 77, 46, 79, 78, 79, 83, 81, 84, 82, 85, 48, + 74, 85, 85, 74, 74, 80, 80, 74, 60, 76, 80, 97, 88, 93, 66, 66, 73, 84, 56, 70, + 90, 63, 58, 78, 73, 93, 90, 78, 94, 88, 82, 67, 85, 70, 81, 86, 74, 82, 88, 82, + 68, 73, 75, 91, 78, 97, 71, 66, 74, 85, 50, 59, 86, 77, 70, 74, 75, 74, 99, 82, + 99, 91, 86, 65, 80, 77, 72, 69, 60, 78, 90, 87, 79, 69, 74, 98, 70, 86, 81, 67, + 69, 78, 48, 65, 88, 70, 70, 70, 69, 72, 96, 90, 99, 82, 81, 76, 98, 73, 74, 71, + 69, 73, 94, 89 +}; + diff --git a/mt/ay_matmul/matmul_gendata.pl b/mt/ay_matmul/matmul_gendata.pl new file mode 100755 index 0000000..f21bb46 --- /dev/null +++ b/mt/ay_matmul/matmul_gendata.pl @@ -0,0 +1,200 @@ +#!/usr/bin/perl -w +#========================================================================== +# matmul_gendata.pl +# +# Author : Christopher Batten (cbatten@mit.edu) +# Date : April 29, 2005 +# +(our $usageMsg = <<'ENDMSG') =~ s/^\#//gm; +# +# Simple script which creates an input data set and the reference data +# for the matmul benchmark. +# +ENDMSG + +use strict "vars"; +use warnings; +no warnings("once"); +use Getopt::Long; + +#-------------------------------------------------------------------------- +# Command line processing +#-------------------------------------------------------------------------- + +our %opts; + +sub usage() +{ + + print "\n"; + print " Usage: matmul_gendata.pl [options] \n"; + print "\n"; + print " Options:\n"; + print " --help print this message\n"; + print " --size size of input data [1000]\n"; + print " --seed random seed [1]\n"; + print "$usageMsg"; + + exit(); +} + +sub processCommandLine() +{ + + $opts{"help"} = 0; + $opts{"size"} = 1000; + $opts{"seed"} = 1; + Getopt::Long::GetOptions( \%opts, 'help|?', 'size:i', 'seed:i' ) or usage(); + $opts{"help"} and usage(); + +} + +#-------------------------------------------------------------------------- +# Helper Functions +#-------------------------------------------------------------------------- + +sub printArray +{ + my $arrayName = $_[0]; + my $arrayRef = $_[1]; + + my $numCols = 20; + my $arrayLen = scalar(@{$arrayRef}); + + print "static data_t ".$arrayName."[ARRAY_SIZE] = \n"; + print "{\n"; + + if ( $arrayLen <= $numCols ) { + print " "; + for ( my $i = 0; $i < $arrayLen; $i++ ) { + print sprintf("%3d",$arrayRef->[$i]); + if ( $i != $arrayLen-1 ) { + print ", "; + } + } + print "\n"; + } + + else { + my $numRows = int($arrayLen/$numCols); + for ( my $j = 0; $j < $numRows; $j++ ) { + print " "; + for ( my $i = 0; $i < $numCols; $i++ ) { + my $index = $j*$numCols + $i; + print sprintf("%3d",$arrayRef->[$index]); + if ( $index != $arrayLen-1 ) { + print ", "; + } + } + print "\n"; + } + + if ( $arrayLen > ($numRows*$numCols) ) { + print " "; + for ( my $i = 0; $i < ($arrayLen-($numRows*$numCols)); $i++ ) { + my $index = $numCols*$numRows + $i; + print sprintf("%3d",$arrayRef->[$index]); + if ( $index != $arrayLen-1 ) { + print ", "; + } + } + print "\n"; + } + + } + + print "};\n\n"; +} + + + +#-------------------------------------------------------------------------- +# Matmul +#-------------------------------------------------------------------------- + +# http://answers.oreilly.com/topic/418-how-to-multiply-matrices-in-perl/ + +sub mmult { + my ($m1,$m2) = @_; + my ($m1rows,$m1cols) = matdim($m1); + my ($m2rows,$m2cols) = matdim($m2); + + my $result = [ ]; + my ($i, $j, $k); + + for $i (range($m1rows)) { + for $j (range($m2cols)) { + for $k (range($m1cols)) { + $result->[$i][$j] += $m1->[$i][$k] * $m2->[$k][$j]; + } + } + } + return $result; +} + +sub range { 0 .. ($_[0] - 1) } + + +sub veclen { + my $ary_ref = $_[0]; + my $type = ref $ary_ref; + if ($type ne "ARRAY") { die "$type is bad array ref for $ary_ref" } + return scalar(@$ary_ref); +} + +sub matdim { + my $matrix = $_[0]; + my $rows = veclen($matrix); + my $cols = veclen($matrix->[0]); + return ($rows, $cols); +} + +#-------------------------------------------------------------------------- +# Main +#-------------------------------------------------------------------------- + +sub main() +{ + + processCommandLine(); + srand($opts{"seed"}); + + # create random input arrays + my $mat_values1; + my $mat_values2; + for ( my $i = 0; $i < $opts{"size"}; $i++ ) { + for ( my $j = 0; $j < $opts{"size"}; $j++ ) { + $mat_values1->[$i][$j] = int(rand(4)); + $mat_values2->[$i][$j] = int(rand(4)); + } + } + + # perform matmul + my $mat_results = mmult( $mat_values1, $mat_values2 ); + + # translate 2d arrays to 1d-somethings (I don't know how to code in perl - Chris) + my @values1; + my @values2; + my @results; + for ( my $i = 0; $i < $opts{"size"}; $i++ ) { + for ( my $j = 0; $j < $opts{"size"}; $j++ ) { + my $value1 = $mat_values1->[$i][$j]; + my $value2 = $mat_values2->[$i][$j]; + my $result = $mat_results->[$i][$j]; + push( @values1, $value1 ); + push( @values2, $value2 ); + push( @results, $result ); + } + } + + print "\n\#define ARRAY_SIZE ".($opts{"size"}*$opts{"size"})." \n\n"; + print "\n\#define DIM_SIZE ".$opts{"size"}." \n\n"; + + printArray( "input1_data", \@values1 ); + printArray( "input2_data", \@values2 ); + printArray( "verify_data", \@results); + +} + +main(); + diff --git a/mt/ay_matmul/matmul_mi.c b/mt/ay_matmul/matmul_mi.c new file mode 100644 index 0000000..d58c5b8 --- /dev/null +++ b/mt/ay_matmul/matmul_mi.c @@ -0,0 +1,258 @@ +//**************************************************************************
+// Multi-threaded Matrix Multiply benchmark
+//--------------------------------------------------------------------------
+// TA : Christopher Celio
+// Student:
+//
+//
+// This benchmark multiplies two 2-D arrays together and writes the results to
+// a third vector. The input data (and reference data) should be generated
+// using the matmul_gendata.pl perl script and dumped to a file named
+// dataset.h.
+
+
+// print out arrays, etc.
+//#define DEBUG
+
+//--------------------------------------------------------------------------
+// Includes
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+
+//--------------------------------------------------------------------------
+// Input/Reference Data
+
+typedef float data_t;
+#include "dataset.h"
+
+
+//--------------------------------------------------------------------------
+// Basic Utilities and Multi-thread Support
+
+__thread unsigned long coreid;
+unsigned long ncores;
+
+#include "util.h"
+
+#define stringify_1(s) #s
+#define stringify(s) stringify_1(s)
+#define stats(code) do { \
+ unsigned long _c = -rdcycle(), _i = -rdinstret(); \
+ code; \
+ _c += rdcycle(), _i += rdinstret(); \
+ if (coreid == 0) \
+ printf("%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
+ stringify(code), _c, _c/DIM_SIZE/DIM_SIZE/DIM_SIZE, 10*_c/DIM_SIZE/DIM_SIZE/DIM_SIZE%10, _c/_i, 10*_c/_i%10); \
+ } while(0)
+
+
+//--------------------------------------------------------------------------
+// Helper functions
+
+void printArray( char name[], int n, data_t arr[] )
+{
+ int i;
+ if (coreid != 0)
+ return;
+
+ printf( " %10s :", name );
+ for ( i = 0; i < n; i++ )
+ printf( " %3ld ", (long) arr[i] );
+ printf( "\n" );
+}
+
+void __attribute__((noinline)) verify(size_t n, const data_t* test, const data_t* correct)
+{
+ if (coreid != 0)
+ return;
+
+ size_t i;
+ for (i = 0; i < n; i++)
+ {
+ if (test[i] != correct[i])
+ {
+ printf("FAILED test[%d]= %3ld, correct[%d]= %3ld\n",
+ i, (long)test[i], i, (long)correct[i]);
+ exit(-1);
+ }
+ }
+
+ return;
+}
+
+//--------------------------------------------------------------------------
+// matmul function
+
+// single-thread, naive version
+void __attribute__((noinline)) matmul_naive(const int lda, const data_t A[], const data_t B[], data_t C[] )
+{
+ int i, j, k;
+
+ if (coreid > 0)
+ return;
+
+ for ( i = 0; i < lda; i++ )
+ for ( j = 0; j < lda; j++ )
+ {
+ for ( k = 0; k < lda; k++ )
+ {
+ C[i + j*lda] += A[j*lda + k] * B[k*lda + i];
+ }
+ }
+
+}
+
+void __attribute__((noinline)) matmul(const int lda, const data_t A[], const data_t B[], data_t C[] )
+{
+ static __thread int i, j, k;
+ static __thread data_t tempA0, tempA1, tempA2, tempA3, tempA4, tempA5, tempA6, tempA7;
+ static __thread data_t tempC0, tempC1, tempC2, tempC3, tempC4, tempC5, tempC6, tempC7, tempC8, tempC9, tempC10, tempC11, tempC12, tempC13, tempC14, tempC15;
+
+ static __thread int start, end, jStride, jToRow, jToCol;
+ static data_t A1[1024], B1[1024];;
+
+ start = coreid << 9;
+ end = (coreid+1) << 9;
+ jStride = 8;
+
+ if (coreid == 0) {
+ for (j=start; j < end; j+=jStride) {
+ jToRow = (j>>5)<<5;
+ jToCol = j%32;
+ tempC0 = 0;
+ tempC1 = 0;
+ tempC2 = 0;
+ tempC3 = 0;
+ tempC4 = 0;
+ tempC5 = 0;
+ tempC6 = 0;
+ tempC7 = 0;
+ for ( i=0; i < lda; i+=2 ) {
+ tempA0 = A[i + jToRow];
+ tempA1 = A[i+1 + jToRow];
+ tempC0 += tempA0 * B[(jToCol ) + (i<<5)];
+ tempC1 += tempA0 * B[(jToCol+1 ) + (i<<5)];
+ tempC2 += tempA0 * B[(jToCol+2 ) + (i<<5)];
+ tempC3 += tempA0 * B[(jToCol+3 ) + (i<<5)];
+ tempC4 += tempA0 * B[(jToCol+4 ) + (i<<5)];
+ tempC5 += tempA0 * B[(jToCol+5 ) + (i<<5)];
+ tempC6 += tempA0 * B[(jToCol+6 ) + (i<<5)];
+ tempC7 += tempA0 * B[(jToCol+7 ) + (i<<5)];
+ tempC0 += tempA1 * B[(jToCol ) + ((i+1)<<5)];
+ tempC1 += tempA1 * B[(jToCol+1 ) + ((i+1)<<5)];
+ tempC2 += tempA1 * B[(jToCol+2 ) + ((i+1)<<5)];
+ tempC3 += tempA1 * B[(jToCol+3 ) + ((i+1)<<5)];
+ tempC4 += tempA1 * B[(jToCol+4 ) + ((i+1)<<5)];
+ tempC5 += tempA1 * B[(jToCol+5 ) + ((i+1)<<5)];
+ tempC6 += tempA1 * B[(jToCol+6 ) + ((i+1)<<5)];
+ tempC7 += tempA1 * B[(jToCol+7 ) + ((i+1)<<5)];
+ }
+ C[j] =tempC0;
+ C[j + 1 ]=tempC1;
+ C[j + 2 ]=tempC2;
+ C[j + 3 ]=tempC3;
+ C[j + 4 ]=tempC4;
+ C[j + 5 ]=tempC5;
+ C[j + 6 ]=tempC6;
+ C[j + 7 ]=tempC7;
+ }
+ }
+ else {
+ for (i = 0; i < 1024; i++) {
+ A1[i] = A[i];
+ B1[i] = B[i];
+ }
+ for (j=start; j < end; j+=jStride) {
+ jToRow = (j>>5)<<5;
+ jToCol = j%32;
+ tempC0 = 0;
+ tempC1 = 0;
+ tempC2 = 0;
+ tempC3 = 0;
+ tempC4 = 0;
+ tempC5 = 0;
+ tempC6 = 0;
+ tempC7 = 0;
+ for ( i=0; i < lda; i+=2 ) {
+ tempA0 = A1[i + jToRow];
+ tempA1 = A1[i+1 + jToRow];
+ tempC0 += tempA0 * B1[(jToCol ) + (i<<5)];
+ tempC1 += tempA0 * B1[(jToCol+1 ) + (i<<5)];
+ tempC2 += tempA0 * B1[(jToCol+2 ) + (i<<5)];
+ tempC3 += tempA0 * B1[(jToCol+3 ) + (i<<5)];
+ tempC4 += tempA0 * B1[(jToCol+4 ) + (i<<5)];
+ tempC5 += tempA0 * B1[(jToCol+5 ) + (i<<5)];
+ tempC6 += tempA0 * B1[(jToCol+6 ) + (i<<5)];
+ tempC7 += tempA0 * B1[(jToCol+7 ) + (i<<5)];
+ tempC0 += tempA1 * B1[(jToCol ) + ((i+1)<<5)];
+ tempC1 += tempA1 * B1[(jToCol+1 ) + ((i+1)<<5)];
+ tempC2 += tempA1 * B1[(jToCol+2 ) + ((i+1)<<5)];
+ tempC3 += tempA1 * B1[(jToCol+3 ) + ((i+1)<<5)];
+ tempC4 += tempA1 * B1[(jToCol+4 ) + ((i+1)<<5)];
+ tempC5 += tempA1 * B1[(jToCol+5 ) + ((i+1)<<5)];
+ tempC6 += tempA1 * B1[(jToCol+6 ) + ((i+1)<<5)];
+ tempC7 += tempA1 * B1[(jToCol+7 ) + ((i+1)<<5)];
+ }
+ C[j] =tempC0;
+ C[j + 1 ]=tempC1;
+ C[j + 2 ]=tempC2;
+ C[j + 3 ]=tempC3;
+ C[j + 4 ]=tempC4;
+ C[j + 5 ]=tempC5;
+ C[j + 6 ]=tempC6;
+ C[j + 7 ]=tempC7;
+ }
+ }
+}
+
+//--------------------------------------------------------------------------
+// Main
+//
+// all threads start executing thread_entry(). Use their "coreid" to
+// differentiate between threads (each thread is running on a separate core).
+
+void thread_entry(int cid, int nc)
+{
+ coreid = cid;
+ ncores = nc;
+
+ // static allocates data in the binary, which is visible to both threads
+ static data_t results_data[ARRAY_SIZE];
+
+
+ //// Execute the provided, naive matmul
+ //barrier();
+ //stats(matmul_naive(DIM_SIZE, input1_data, input2_data, results_data); barrier());
+
+ //
+ //// verify
+ //verify(ARRAY_SIZE, results_data, verify_data);
+ //
+ //// clear results from the first trial
+ //size_t i;
+ //if (coreid == 0)
+ // for (i=0; i < ARRAY_SIZE; i++)
+ // results_data[i] = 0;
+ //barrier();
+
+
+ // Execute your faster matmul
+ barrier();
+ stats(matmul(DIM_SIZE, input1_data, input2_data, results_data); barrier());
+
+#ifdef DEBUG
+ printArray("results:", ARRAY_SIZE, results_data);
+ printArray("verify :", ARRAY_SIZE, verify_data);
+#endif
+
+ // verify
+ verify(ARRAY_SIZE, results_data, verify_data);
+ barrier();
+
+ exit(0);
+}
+
+
|