Skip to content

Instantly share code, notes, and snippets.

@kirarpit
Created April 22, 2019 20:49
Show Gist options
  • Save kirarpit/61fa93e02f8e05c815ceefb1c16aedd6 to your computer and use it in GitHub Desktop.
Save kirarpit/61fa93e02f8e05c815ceefb1c16aedd6 to your computer and use it in GitHub Desktop.
A naive implementation of row-major double precision floating point general matrix multiplication (DGEMM) in Common Lisp.
;; row major matrix multiplication
(defun row_major_mm (a b c)
(flet (
(col (mat i) (mapcar #'(lambda (row) (elt row i)) mat))
(row (mat i) (elt mat i))
)
(loop for k from 0 below (length (row a 0))
do (loop for i from 0 below (length a)
do (let ((temp (elt (elt a i) k)))
(loop for j from 0 below (length a)
do (setf (nth j (nth i c))
(+ (elt (elt c i) j)
(* temp (elt (elt b k) j))
)
)
)
)
)
)
)
)
;; create a double floating point random matrix
(defun get_random_matrix (N)
(loop for i from 0 below N
collect (loop for j from 0 below N
collect (random 1d0)
)
)
)
(loop for num_runs from 0 below 100
do (loop for N from 10 below 100
do (let ((a (get_random_matrix N)) (b (get_random_matrix N)) (c (get_random_matrix N)))
;; warm-up
(row_major_mm a b c)
;; run enough number of times to calculate FLOPS
(time (loop for i from 0 below 10
do (row_major_mm a b c)))
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment