Matrix multiplication: Difference between revisions
Content added Content deleted
m (→[[ALGOL 68]]: ALGOL 68 Revised Report allows keywords, types and operators in a different typeface.) |
m (→[[ALGOL 68]]: use the bold form of the if ~ then ~ else ~ fi syntax. - easier to read blocks.) |
||
Line 110: | Line 110: | ||
<u>proc</u> <u>void</u> raise index error := <u>void</u>: <u>goto</u> exception index error; |
<u>proc</u> <u>void</u> raise index error := <u>void</u>: <u>goto</u> exception index error; |
||
<u>sema</u> idle cpus = <u>level</u> ( 8 - 1 ); # number of |
<u>sema</u> idle cpus = <u>level</u> ( 8 - 1 ); # 8 = number of CPU cores minus parent CPU # |
||
# define an operator to slice array into quarters # |
# define an operator to slice array into quarters # |
||
Line 122: | Line 122: | ||
<u>op</u> × = (<u>vec</u> a, b)<u>field</u>: ( # dot product # |
<u>op</u> × = (<u>vec</u> a, b)<u>field</u>: ( # dot product # |
||
<u>if</u> ⌊a≠⌊b ∨ ⌈a≠⌈b <u>then</u> raise index error <u>fi</u>; |
|||
<u>if</u> ⌊a = ⌈a <u>then</u> |
|||
a[⌈a] × b[⌈b] |
a[⌈a] × b[⌈b] |
||
<u>else</u> |
|||
<u>field</u> begin, end; |
<u>field</u> begin, end; |
||
[]<u>proc</u> <u>void</u> thread schedule=( |
[]<u>proc</u> <u>void</u> thread schedule=( |
||
Line 131: | Line 131: | ||
<u>void</u>: end :=a[<u>right</u> a:] × b[<u>right</u> b:] |
<u>void</u>: end :=a[<u>right</u> a:] × b[<u>right</u> b:] |
||
); |
); |
||
<u>if</u> <u>level</u> idle cpus = 0 <u>then</u> # use current CPU # |
|||
<u>for</u> thread <u>to</u> ⌈thread schedule <u>do</u> thread schedule[thread] <u>od</u> |
<u>for</u> thread <u>to</u> ⌈thread schedule <u>do</u> thread schedule[thread] <u>od</u> |
||
<u>else</u> |
|||
<u>par</u> ( # run vector in parallel # |
<u>par</u> ( # run vector in parallel # |
||
thread schedule[1], # assume parent CPU # |
thread schedule[1], # assume parent CPU # |
||
( ↓idle cpus; thread schedule[2]; ↑idle cpus) |
( ↓idle cpus; thread schedule[2]; ↑idle cpus) |
||
) |
) |
||
<u>fi</u>; |
|||
begin+end |
begin+end |
||
<u>fi</u> |
|||
); |
); |
||
<u>op</u> × = (<u>mat</u> a, b)<u>mat</u>: # matrix multiply # |
<u>op</u> × = (<u>mat</u> a, b)<u>mat</u>: # matrix multiply # |
||
<u>if</u> ⌊a = ⌈a ∧ 2 ⌊b = 2 ⌈b <u>then</u> |
|||
a[⌊a, ] × b[, 2 ⌈b] # dot product # |
a[⌊a, ] × b[, 2 ⌈b] # dot product # |
||
<u>else</u> |
|||
[⌈a, 2 ⌈b] <u>field</u> out; |
[⌈a, 2 ⌈b] <u>field</u> out; |
||
<u>if</u> 2 ⌊a≠⌊b ∨ 2 ⌈a≠⌈b <u>then</u> raise index error <u>fi</u>; |
|||
[]<u>struct</u>(<u>bool</u> required, <u>proc</u> <u>void</u> thread) schedule = ( |
[]<u>struct</u>(<u>bool</u> required, <u>proc</u> <u>void</u> thread) schedule = ( |
||
( <u>true</u>, # calculate top left corner # |
( <u>true</u>, # calculate top left corner # |
||
Line 159: | Line 159: | ||
<u>void</u>: out[<u>bot</u> a:, <u>right</u> b:] := a[<u>bot</u> a:, ] × b[, <u>right</u> b:]) |
<u>void</u>: out[<u>bot</u> a:, <u>right</u> b:] := a[<u>bot</u> a:, ] × b[, <u>right</u> b:]) |
||
); |
); |
||
<u>if</u> <u>level</u> idle cpus = 0 <u>then</u> # use current CPU # |
|||
<u>for</u> thread <u>to</u> ⌈schedule <u>do</u> (required →schedule[thread] | thread →schedule[thread] ) <u>od</u> |
<u>for</u> thread <u>to</u> ⌈schedule <u>do</u> (required →schedule[thread] | thread →schedule[thread] ) <u>od</u> |
||
<u>else</u> |
|||
<u>par</u> ( # run vector in parallel # |
<u>par</u> ( # run vector in parallel # |
||
thread →schedule[1], # thread is always required, and assume parent CPU # |
thread →schedule[1], # thread is always required, and assume parent CPU # |
||
Line 169: | Line 169: | ||
( required →schedule[2] | ↓idle cpus; thread →schedule[2]; ↑idle cpus) |
( required →schedule[2] | ↓idle cpus; thread →schedule[2]; ↑idle cpus) |
||
) |
) |
||
<u>fi</u>; |
|||
out |
out |
||
<u>fi</u>; |
|||
<u>format</u> real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals # |
<u>format</u> real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals # |
||
Line 199: | Line 199: | ||
exception index error: |
exception index error: |
||
putf(stand error, $x"Exception: index error."l$) |
putf(stand error, $x"Exception: index error."l$) |
||
Note: In the ALGOL 68 Revised Report the keywords, types and operators were permitted in a different typeface. Hence the underling of these identifiers above. Effectively this puts these identifiers into a different name space. |
Note: In the ALGOL 68 Revised Report the keywords, types and operators were permitted in a different typeface. Hence the underling of these identifiers above. Effectively this puts these identifiers into a different name space. |
||