misc.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ; -*- mode: scheme; coding: utf-8 -*-
  2. ; (c) Daniel Llorens - 2017
  3. ; This library is free software; you can redistribute it and/or modify it under
  4. ; the terms of the GNU General Public License as published by the Free
  5. ; Software Foundation; either version 3 of the License, or (at your option) any
  6. ; later version.
  7. ;;; Commentary:
  8. ;; Alternative implementations (both for ra and array) for testing and benchmarking.
  9. ;;; Code:
  10. (define-module (test misc)
  11. #:export (ra-map*! array-map*! array-print* %ra-loop ra-loop array-loop))
  12. (import (newra base) (newra map)
  13. (only (rnrs base) vector-map vector-for-each) (rnrs io ports) (srfi srfi-4 gnu))
  14. (define ra-map*!
  15. (case-lambda
  16. ((ra-slice-for-each o f)
  17. (ra-slice-for-each
  18. (ra-rank o)
  19. (lambda (o) (ra-set! o (f)))
  20. o)
  21. o)
  22. ((ra-slice-for-each o f a0)
  23. (ra-slice-for-each
  24. (ra-rank o)
  25. (lambda (o a0) (ra-set! o (f (ra-ref a0))))
  26. o a0)
  27. o)
  28. ((ra-slice-for-each o f a0 a1)
  29. (ra-slice-for-each
  30. (ra-rank o)
  31. (lambda (o a0 a1) (ra-set! o (f (ra-ref a0) (ra-ref a1))))
  32. o a0 a1)
  33. o)
  34. ((ra-slice-for-each o f . args)
  35. (apply ra-slice-for-each (ra-rank o)
  36. (lambda (o . args) (ra-set! o (apply f (map ra-ref args))))
  37. o args)
  38. o)))
  39. (define (array-map*! o f . args)
  40. (apply array-slice-for-each (array-rank o)
  41. (lambda (o . args)
  42. (array-set! o (apply f (map array-ref args))))
  43. o args)
  44. o)
  45. (define array-print-prefix (@@ (ice-9 arrays) array-print-prefix))
  46. ; this is a direct translation of scm_i_print_array_dimension() in arrays.c.
  47. (define (array-print* a port)
  48. (define lo caar)
  49. (define hi cadar)
  50. (array-print-prefix a port)
  51. (let ((s (array-shape a))
  52. (i (shared-array-increments a))
  53. (r (shared-array-root a))
  54. (b (shared-array-offset a)))
  55. (let ((ref (case (array-type r)
  56. ((#t) vector-ref)
  57. ((c64) c64vector-ref)
  58. ((c32) c32vector-ref)
  59. ((f64) f64vector-ref)
  60. ((f32) f32vector-ref)
  61. ((s64) s64vector-ref)
  62. ((s32) s32vector-ref)
  63. ((s16) s16vector-ref)
  64. ((s8) s8vector-ref)
  65. ((u64) u64vector-ref)
  66. ((u32) u32vector-ref)
  67. ((u16) u16vector-ref)
  68. ((u8) u8vector-ref)
  69. ((a) string-ref)
  70. ((b) bitvector-ref)
  71. (else (throw 'bad-type (array-type r))))))
  72. ; special case
  73. (if (zero? (array-rank a))
  74. (begin
  75. (display #\( port)
  76. (display (ref b) port)
  77. (display #\) port))
  78. (let loop ((ss s) (ii i) (b b))
  79. (if (null? ss)
  80. (display (ref r b) port)
  81. (let ((lo (lo ss))
  82. (hi (hi ss))
  83. (i (car ii)))
  84. (put-char port #\()
  85. (do ((j lo (+ 1 j))
  86. (b b (+ b i)))
  87. ((> j hi))
  88. (loop (cdr ss) (cdr ii) b)
  89. (when (< j hi)
  90. (put-char port #\space)))
  91. (put-char port #\)))))))))
  92. (define-syntax %ra-loop-inner
  93. (lambda (stx-inner)
  94. (syntax-case stx-inner ()
  95. ((_ lens k u body nn ...)
  96. (let ((uu (syntax->datum #'u)))
  97. (if (= uu (syntax->datum #'k))
  98. #'body
  99. (with-syntax ((nu (list-ref #'(nn ...) uu)))
  100. #`(let ((end (vector-ref lens u)))
  101. (let loop ((nu 0))
  102. (unless (= nu end)
  103. (%ra-loop-inner lens k #,(+ uu 1) body nn ...)
  104. (loop (+ nu 1))))))))))))
  105. (define-syntax %ra-loop
  106. (lambda (stx)
  107. (syntax-case stx ()
  108. ((_ lens k (i ...) body)
  109. #'(begin
  110. (unless (= (vector-length lens) k) (throw 'bad-rank))
  111. (%ra-loop-inner lens k 0 body i ...))))))
  112. (define (loop-fun dims f)
  113. (case (vector-length dims)
  114. ((0) (%ra-loop dims 0 () (f)))
  115. ((1) (%ra-loop dims 1 (i) (f i)))
  116. ((2) (%ra-loop dims 2 (i j) (f i j)))
  117. ((3) (%ra-loop dims 3 (i j k) (f i j k)))
  118. ((4) (%ra-loop dims 4 (i j k l) (f i j k l)))
  119. ((5) (%ra-loop dims 5 (i j k l m) (f i j k l m)))
  120. (else (throw 'not-implemented))))
  121. (define (ra-loop ra f)
  122. (loop-fun (vector-map dim-len (ra-dims ra)) f))
  123. (define (array-loop a f)
  124. (loop-fun (list->vector (array-dimensions a)) f))