matmul.ocm 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "dec.ocm"
  2. proc prompt(value str[])=
  3. seq i=[1 for str[byte 0]]
  4. output ! str[byte i]
  5. :
  6. def N=20 :
  7. var n:
  8. var A[N*N], x[N], k[N], y[N] :
  9. proc initialise=
  10. var c:
  11. seq
  12. prompt("n?*n")
  13. c:='*s'
  14. decin(input, n, c)
  15. prompt("A?*n")
  16. seq i= [0 for n]
  17. seq j= [0 for n]
  18. decin(input, A[(i*n)+j], c)
  19. prompt("x?*n")
  20. seq i= [0 for n]
  21. decin(input, x[i], c)
  22. prompt("k?*n")
  23. seq i= [0 for n]
  24. decin(input, k[i], c) :
  25. proc produce.xj(value j, chan south) =
  26. -- north row: source of x values
  27. while true
  28. south ! x[j] :
  29. proc consume.yi(value i, chan east) =
  30. -- west column: read y values
  31. east ? y[i] :
  32. proc offset(value ki, chan west) =
  33. -- east column: source of k offsets
  34. while true
  35. west ! ki :
  36. proc multiplier(value aij, chan north, south, west, east) =
  37. -- middle: responsible for a values
  38. var xj, aij.times.xj, yi :
  39. seq
  40. north ? xj
  41. while true
  42. seq
  43. par
  44. south ! xj
  45. aij.times.xj:= aij*xj
  46. east ? yi
  47. par
  48. west ! yi+aij.times.xj
  49. north ? xj :
  50. proc sink(chan north) =
  51. -- south row: sink for unused outputs
  52. while true
  53. north ? any :
  54. seq
  55. initialise
  56. chan north.south[(N+1)*N], east.west[N*(N+1)] :
  57. par
  58. par j= [0 for n] -- producer of co-ordinates x[j]
  59. produce.xj(j, north.south[j])
  60. par -- the matrix multiplier
  61. par i= [0 for n]
  62. offset(k[i], east.west[(n*n)+i])
  63. par i= [0 for n]
  64. par j= [0 for n]
  65. multiplier(A[(n*i)+j],
  66. north.south[(n*i)+j],
  67. north.south[(n*(i+1))+j],
  68. east.west[i+(n*j)],
  69. east.west[i+(n*(j+1))])
  70. par j= [0 for n]
  71. sink(north.south[(n*n)+j])
  72. seq
  73. par i= [0 for n]-- consumer of transformed co-ordinates
  74. consume.yi(i, east.west[i])
  75. seq i= [0 for n]
  76. seq
  77. output ! 'y'; '['
  78. decout(output, i, 0)
  79. output ! ']'; '='
  80. decout(output, y[i], 5)
  81. output ! '*n'
  82. exit(0)