Permalink
Browse files

modify to use cl-cuda memory block instead of raw cffi foreign objects

  • Loading branch information...
1 parent 457b590 commit f5d8306ae412c590467c75873734b6c8c7f701e1 @takagi committed Jun 2, 2012
Showing with 15 additions and 20 deletions.
  1. +15 −20 examples/vector-add.lisp
View
@@ -16,13 +16,13 @@
(defun random-init (data n)
(dotimes (i n)
- (setf (cffi:mem-aref data :float i) (random 1.0))))
+ (setf (mem-aref data i) (random 1.0))))
(defun verify-result (as bs cs n)
(dotimes (i n)
- (let ((a (cffi:mem-aref as :float i))
- (b (cffi:mem-aref bs :float i))
- (c (cffi:mem-aref cs :float i)))
+ (let ((a (mem-aref as i))
+ (b (mem-aref bs i))
+ (c (mem-aref cs i)))
(let ((sum (+ a b)))
(when (> (abs (- c sum)) 1.0)
(error (format nil "verification fault, i:~A a:~A b:~A c:~A"
@@ -38,22 +38,17 @@
(defun main ()
(let* ((dev-id 0)
(n 1024)
- (size (* n 4)) ; 4 is size of float
(threads-per-block 256)
(blocks-per-grid (/ n threads-per-block)))
(with-cuda-context (dev-id)
- (cffi:with-foreign-objects ((h-a :float n)
- (h-b :float n)
- (h-c :float n))
- (with-cuda-memory-blocks ((d-a size)
- (d-b size)
- (d-c size))
- (random-init h-a n)
- (random-init h-b n)
- (cu-memcpy-host-to-device (cffi:mem-ref d-a 'cu-device-ptr) h-a size)
- (cu-memcpy-host-to-device (cffi:mem-ref d-b 'cu-device-ptr) h-b size)
- (vec-add-kernel d-a d-b d-c n
- :grid-dim (list blocks-per-grid 1 1)
- :block-dim (list threads-per-block 1 1))
- (cu-memcpy-device-to-host h-c (cffi:mem-ref d-c 'cu-device-ptr) size)
- (verify-result h-a h-b h-c n))))))
+ (with-memory-blocks ((a 'float n)
+ (b 'float n)
+ (c 'float n))
+ (random-init a n)
+ (random-init b n)
+ (memcpy-host-to-device a b)
+ (vec-add-kernel a b c n
+ :grid-dim (list blocks-per-grid 1 1)
+ :block-dim (list threads-per-block 1 1))
+ (memcpy-device-to-host c)
+ (verify-result a b c n)))))

0 comments on commit f5d8306

Please sign in to comment.