Jump to content

Matrix multiplication: Difference between revisions

m
→‎[[ALGOL 68]]: use the bold form of the if ~ then ~ else ~ fi syntax. - easier to read blocks.
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:
<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 ); # 8 = number of 8 CPU cores minus parent CPU #
# define an operator to slice array into quarters #
Line 122:
<u>op</u> × = (<u>vec</u> a, b)<u>field</u>: ( # dot product #
(<u>if</u> ⌊a≠⌊b ∨⌈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]
|<u>else</u>
<u>field</u> begin, end;
[]<u>proc</u> <u>void</u> thread schedule=(
Line 131:
<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>else</u>
<u>par</u> ( # run vector in parallel #
thread schedule[1], # assume parent CPU #
( ↓idle cpus; thread schedule[2]; ↑idle cpus)
)
)<u>fi</u>;
begin+end
)<u>fi</u>
);
<u>op</u> × = (<u>mat</u> a, b)<u>mat</u>: # matrix multiply #
(<u>if</u> ⌊a = ⌈a ∧2∧ 2 ⌊b = 2 ⌈b |<u>then</u>
a[⌊a, ] × b[, 2 ⌈b] # dot product #
|<u>else</u>
[⌈a, 2 ⌈b] <u>field</u> out;
(<u>if</u> 2 ⌊a≠⌊b ∨2∨ 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>true</u>, # calculate top left corner #
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>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>else</u>
<u>par</u> ( # run vector in parallel #
thread →schedule[1], # thread is always required, and assume parent CPU #
Line 169:
( required →schedule[2] | ↓idle cpus; thread →schedule[2]; ↑idle cpus)
)
)<u>fi</u>;
out
)<u>fi</u>;
<u>format</u> real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #
Line 199:
exception index error:
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.
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.