OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / mvp / test-binary-cache.scm

108 lines · scm ref:
#!/usr/bin/env guile
!#
;;
;; test-binary-cache.scm - Test the OpenCL binary caching feature
;;
;; This script tests that:
;; 1. First run compiles and caches the binary
;; 2. Second run loads from cache (much faster)
;;
;; Usage:
;;   guile -s test-binary-cache.scm
;;
;; Expected output:
;;   First run: "Compiling kernel from source" message, several seconds
;;   Second run: "Loaded cached binary" message, milliseconds
;;

(use-modules (opencog) (opencog exec))
(use-modules (opencog sensory) (opencog opencl))
(use-modules (ice-9 format))

(define (get-time-ms)
  "Get current time in milliseconds."
  (let ((t (gettimeofday)))
    (+ (* (car t) 1000) (/ (cdr t) 1000))))

(display "\n")
(display "═══════════════════════════════════════════════════════════════\n")
(display "  OpenCL Binary Cache Test\n")
(display "═══════════════════════════════════════════════════════════════\n\n")

;; Check cache directory
(display "Cache directory: ~/.cache/opencog/opencl/\n\n")

;; Create a simple test kernel
(define test-kernel-path "/tmp/test-cache-kernel.cl")
(with-output-to-file test-kernel-path
  (lambda ()
    (display "
// Simple test kernel for binary caching
__kernel void vec_add(
    __global const double* a,
    __global const double* b,
    __global double* result,
    const unsigned long sz)
{
    size_t i = get_global_id(0);
    if (i < sz)
        result[i] = a[i] + b[i];
}
")))

(display "Created test kernel at: ")
(display test-kernel-path)
(newline)
(newline)

;; Time the first open (should compile and cache)
(display "Opening OpenCL device (first time - may compile)...\n")
(define t0 (get-time-ms))

(define gpu (OpenclNode (string-append "opencl://:" test-kernel-path)))

(catch #t
  (lambda ()
    (cog-execute! (SetValue gpu (Predicate "*-open-*") (Type 'FloatValue))))
  (lambda (key . args)
    (format #t "Error opening device: ~a ~a\n" key args)
    (exit 1)))

(define t1 (get-time-ms))
(format #t "First open took: ~,0f ms\n\n" (- t1 t0))

;; Close the device
(cog-execute! (SetValue gpu (Predicate "*-close-*") (StringValue "bye")))

;; Now test a second open - should use cache
(display "Opening OpenCL device (second time - should use cache)...\n")
(define t2 (get-time-ms))

(define gpu2 (OpenclNode (string-append "opencl://:" test-kernel-path)))

(catch #t
  (lambda ()
    (cog-execute! (SetValue gpu2 (Predicate "*-open-*") (Type 'FloatValue))))
  (lambda (key . args)
    (format #t "Error opening device: ~a ~a\n" key args)
    (exit 1)))

(define t3 (get-time-ms))
(format #t "Second open took: ~,0f ms\n\n" (- t3 t2))

;; Report speedup
(define speedup (/ (- t1 t0) (max 1 (- t3 t2))))
(format #t "Speedup: ~,1fx\n\n" speedup)

;; Show cache files
(display "Cache files created:\n")
(system "ls -la ~/.cache/opencog/opencl/*/")

(display "\n")
(display "═══════════════════════════════════════════════════════════════\n")
(if (> speedup 2)
    (display "  ✓ Binary caching is working! Significant speedup observed.\n")
    (display "  ⚠ Speedup less than expected. Check logs for details.\n"))
(display "═══════════════════════════════════════════════════════════════\n")
(display "\n")