Skip to content

Commit

Permalink
Add matmul sample
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardiff committed Aug 13, 2019
1 parent d178265 commit 7c4f6e0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
45 changes: 45 additions & 0 deletions sample/matmul.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copied with little modifications from: https://github.com/attractivechaos/plb/blob/master/matmul/matmul_v1.rb

def matmul(a, b)
m = a.size
n = a[0].size
p = b[0].size
# transpose
b2 = Array.new(n) { Array.new(p, 0.0) }
(0...n).each do |i|
(0...p).each do |j|
b2[j][i] = b[i][j]
end
end
# multiplication
c = Array.new(m) { Array.new(p, 0.0) }
(0...m).each do |i|
(0...p).each do |j|
s = 0.0
ai, b2j = a[i], b2[j]
(0...n).each do |k|
s += ai[k] * b2j[k]
end
c[i][j] = s
end
end
c
end

def matgen(n)
tmp = 1.0 / n / n
a = Array.new(n) { Array.new(n, 0.0) }
(0...n).each do |i|
(0...n).each do |j|
a[i][j] = tmp * (i - j) * (i + j)
end
end
a
end

n = (ARGV[0]? || 500).to_i
n = n / 2 * 2
a = matgen(n)
b = matgen(n)
c = matmul(a, b)
puts c[n / 2][n / 2]
19 changes: 19 additions & 0 deletions sample/matmul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: http
context:
crystal: crystal eval 'puts Crystal::VERSION'
host: uname
measure:
- time
- cpu_time
- max_rss
run: ./matmul $SIZE
repeat: 3
before: crystal build --release matmul.cr -o ./matmul
after: rm -f ./matmul ./matmul.dwarf
matrix:
env:
SIZE:
- 10
- 100
- 1000
- 2000

0 comments on commit 7c4f6e0

Please sign in to comment.