Hi all,

Can anyone explain why I need what would appear to be a superfluous
optimize in the code below:

;;; -*- mode: lisp -*-
;;; $Id: matrix.cmucl,v 1.3 2001/04/15 20:38:35 doug Exp $
;;; http://www.bagley.org/~doug/shootout/
;;; from Jochen Schmidt ([EMAIL PROTECTED], http://www.dataheaven.de)
;;; and in the process of being modified by Adam Warner 

(declaim (optimize (speed 3) (safety 0) (debug 0) (compilation-speed 0)))
(declaim (start-block main))

(defun matmul (a b c n m k)
  (declare (optimize (speed 3) (safety 0) (debug 0))
           (type (simple-array (unsigned-byte 32) (*)) a b c)
           (fixnum n m k))
  (let ((sum 0)
        (i1 (- m))
        (k2 0))
    (declare (type (unsigned-byte 32) sum) (type fixnum i1 k2))
    (dotimes (i n c)
      (declare (fixnum i))
      (setf i1 (+ i1 m)) ;; i1=i*m
      (dotimes (j k)
        (declare (fixnum j))
        (setf sum 0)
        (setf k2 (- k))
        (dotimes (l m)
          (declare (fixnum l))
          (setf k2 (+ k2 k)) ;; k2= l*k
          (setf sum (the (unsigned-byte 32) (+ (the (unsigned-byte 32) sum) 
                                               (the (unsigned-byte 32) (* (aref a (+ 
i1 l))
                                                                          (aref b (+ 
k2 j))))))))
        (setf (aref c (+ i1 j)) sum)))))

(defun main ()
  (let* ((m1 (make-matrix 30 30))
         (m2 (make-matrix 30 30))
         (m3 (make-matrix 30 30))
         (mm (make-array '(30 30) :element-type '(unsigned-byte 32) :displaced-to m3)))
    (dotimes (i 10000) (matmul m1 m2 m3 30 30 30))
    (format t "~D ~D ~D ~D~%"
            (aref mm 0 0) (aref mm 2 3) (aref mm 3 2) (aref mm 4 4))))

(defun make-matrix (rows cols)
  (declare (type (unsigned-byte 32) rows cols))
  (let* ((space (* rows cols))
         (matrix (make-array space
                             :element-type '(unsigned-byte 32))))
    (declare (type (simple-array (unsigned-byte 32) (*)) matrix)
             (fixnum space))
    (loop :for i :of-type fixnum :from 0 :below space
          :do (setf (aref matrix i) (1+ i)))
    matrix))

(declaim (end-block))


This version takes around 7.86s to run.

If I comment out the (optimize (speed 3) (safety 0) (debug 0)) from
matmul and recompile it takes around 9.2s to run.

I was under the impression that declaim creates a compile-time global
declaration and that it can be used in place of many individual optimize
declarations.

Thanks,
Adam


Reply via email to