From: Faheem M. <fa...@fa...> - 2012-06-04 21:12:07
|
Hi, I got some feedback, and decided to try reworking this exercise using an array. I ran into some issues, at least one of which is quite worrying. The code is below, and here are the issue, in descending order of importance. 1) I'm having GC problems. If I run this code a number of times, like Here is the output for (room) for a newly started REPL. CL-USER> (room) Dynamic space usage is: 92,358,952 bytes. Read-only space usage is: 3,512 bytes. Static space usage is: 2,224 bytes. Control stack usage is: 5,928 bytes. Binding stack usage is: 536 bytes. Control and binding stack usage is for the current thread only. Garbage collection is currently enabled. Breakdown for dynamic space: 21,981,408 bytes for 565,633 instance objects. 15,453,048 bytes for 1,931,631 cons objects. 12,000,912 bytes for 16,217 code objects. 11,585,944 bytes for 210,891 simple-vector objects. 8,400,184 bytes for 61,316 simple-character-string objects. 5,903,840 bytes for 42,632 simple-array-unsigned-byte-32 objects. 17,717,104 bytes for 1,177,704 other objects. 93,042,440 bytes for 4,006,024 dynamic objects (space total.) Now I run this a bunch of times. (time (gibbs 10000 1000)) The dynamic space usage keeps climbing, and eventually I get a heap error. At that point (room) gives CL-USER> (room) Dynamic space usage is: 1,010,419,640 bytes. Read-only space usage is: 3,512 bytes. Static space usage is: 2,224 bytes. Control stack usage is: 5,928 bytes. Binding stack usage is: 536 bytes. Control and binding stack usage is for the current thread only. Garbage collection is currently enabled. Breakdown for dynamic space: 960,000,104 bytes for 13 simple-array-double-float objects. 50,673,608 bytes for 1,673,714 other objects. 1,010,673,712 bytes for 1,673,727 dynamic objects (space total.) ; No value Yikes. After I waited a bit, the dynamic space usage did go down a bit, but only a little. Breakdown for dynamic space: 800,000,088 bytes for 11 simple-array-double-float objects. I don't understand this. These were dynamically created objects. Shouldn't they be destroyed when the function exited? Did I make some mistake in writing the code? Tangential question: is there some way I can inspect these objects which are occupying the dynamic space? Tangent question 2. What are all these millions of objects? 93,042,440 bytes for 4,006,024 dynamic objects (space total.)?? 2) This code emits a bunch of compiler warnings. Let me just focus on one. ; note: forced to do GENERIC-* (cost 30) ; unable to do inline fixnum arithmetic (cost 4) because: ; The result is a (VALUES (MOD 288230374541099011) &OPTIONAL), not a (VALUES ; FIXNUM ; &REST ; T). ; unable to do inline (signed-byte 32) arithmetic (cost 5) because: ; The result is a (VALUES (MOD 288230374541099011) &OPTIONAL), not a (VALUES ; (SIGNED-BYTE ; 32) ; &REST ; T). ; etc. ; I've seen the term MOD appear in this context frequently. I've searched, but have not been able to find any explanation of this. Would someone be kind enough to explain MOD and the magic number 288230374541099011, and also if possible a suggestion for getting rid of the warning? By guess is that this reflects some kind of integer overflow issue, but I'm fuzzy on the details. Would some kind of range restriction be a solution? The following are more minor issues. 3) If I pass the size of the array, as a function argument, the compiler won't let me declare (declare (type (simple-array (double-float 0.0 *) (somefunctionargument)) xvec)) using the function argument, though if I use a literal value like 10000000 as in the code above, it works. The error is not very helpful - it says bad dimension. Is this because for this declaration, the array length needs to be a compile time constant? 4) The compiler is Ok with (declare ((integer 0 536870911) i j)) (positive fixnum, basically) but not with (declare ((integer 0 most-positive-fixnum) i j)) though, if I understand this correctly, most-positive-fixnum is just the integer 536870911. Also, I get CL-USER> (type-of most-positive-fixnum) (INTEGER 0 536870911) CL-USER> (type-of 2) (INTEGER 0 536870911) So SBCL thinks most-positive-fixnum is of the same type as 2. Please CC me on any reply. Thanks. Regards, Faheem #################################################################### (eval-when (:compile-toplevel :load-toplevel :execute) (require :cl-rmath) (setf *read-default-float-format* 'double-float)) (declaim (inline cl-rmath:rnorm cl-rmath:rgamma)) (cffi:defcfun ("rgamma" rgamma) :double (arg0 :double) (arg1 :double)) (cffi:defcfun ("rnorm" rnorm) :double (arg0 :double) (arg1 :double)) (defun gibbs (N thin) (declare (fixnum N thin)) (declare (optimize (speed 3) (safety 1))) (declare (optimize debug)) (let ((x 0.0) (y 0.0) (xvec (make-array 10000000 :element-type '(double-float 0.0 *) :adjustable nil :fill-pointer nil :displaced-to nil)) (yvec (make-array 10000000 :element-type 'double-float :adjustable nil :fill-pointer nil :displaced-to nil))) (declare (double-float x y)) (declare (type (simple-array (double-float 0.0 *) (10000000)) xvec)) (declare (type (simple-array double-float (10000000)) yvec)) (dotimes (i N) (dotimes (j thin) (declare (fixnum i j)) (declare ((integer 0 536870911) i j)) ;;(declare ((integer 0 most-positive-fixnum) i j)) (symbol-macrolet ((xval (aref xvec (+ (the fixnum (* i thin)) j))) (yval (aref yvec (+ (the fixnum (* i thin)) j)))) (setf xval (cl-rmath::rgamma 3.0 (/ 1 (+ (* yval yval) 4)))) (setf yval (cl-rmath::rnorm (/ 1 (+ xval 1)) (/ 1 (sqrt (+ (* 2 xval) 2))))) (setf x xval) (setf y yval))) (format t "~a ~a ~a~%" i x y)))) (defun main (argv) (declare (ignore argv)) ;;(gibbs 50000 1000 10000000)) ;;(gibbs 10000 1000 10000000)) (gibbs 10000 1000)) |