Arithmetic

Chapter 6, Problem 6  requires some knowledge of arithmetic.

1.  Arithmetic Example

The critical issue here is to decide which operations can be done simultaneously.  Before doing theory, try the following problems and keep careful track of what you actually did.

Problem Answer
2*(7 + 6) + 27/10 - 1 27.7
2*7 + 6 + 27 /(10 - 1) 23

Notice that you did arithmetic operations on only one pair of numbers at a time.  Notice also that the order in which you do operations makes a difference.

In the first problem, you can do the following operations simultaneously without affecting the end result:

Time Slice Processor #1 Processor #2
0 7 + 6 27 / 10
1 2 * 13 2.7 - 1
2 26 + 1.7  

We can achieve this by the following code:

COBEGIN

A = 7 + 6

B = 27 / 10

COEND

COBEGIN

C = 2 * A

D = B - 1

COEND

ANSWER = C + D

In the second problem, we compute

Time Slice Processor #1 Processor #2
0 2 * 7 10 - 1
1 14 + 6 27 / 9
2 20 + 3  

We achieve this by the following:

COBEGIN

A = 2 * 7

B = 10 - 1

COEND

COBEGIN

C = A + 6

D = 27 / B

COEND

ANSWER = C + D

2.  Algebra Example

Problem
A*(B + C) + D/E - F
A*B + C + D /(E - F)

For the first problem, we compute:

Time Slice Processor 1 Processor 2
0 V = B + C W = D / E
1 X = A * V Y = W - F
2 Z = X + Y  

The code to implement this can be:

COBEGIN

V = B + C

W = D / E

COEND

COBEGIN

X = A * V

Y = W - F

COEND

Z = X + Y

In the second example, we compute

Time Slice Processor 1 Processor 2
0 P = A * B Q = E - F
1 R = P + C S = D / Q
2 T = R + S  

The code to implement this can be:

COBEGIN

P = A * B

Q + E - F

COEND

COBEGIN

R = P + C

S = D / Q

COEND

T = R + S

3.  Hierarchy of Arithmetic Operations.  There is a general consensus on the order in which SOME operations are to be done, but there is not universal agreement on EVERY operation.  Programming languages differ slightly on the order in which operations are done.  Refer to your programming language reference manual when dealing with a particular programming language.  The following precedence of mathematical operations, taken from C++, is typical.

Priority Parsing Direction Operations Fortran Version Type
1 (highest priority) left to right (   ) (   ) Parentheses
2 left to right *   /   %  (% is modulus) *   /   mod Multiplicative
3 left to right +   - +   - Additive
4 left to right <   <=   >   >= .LT.   .LE.   .GT.   .GE. Relational
5 left to right ==   != .EQ.   .NE. Equalities (relational)
6 left to right <<   >>   Insertion, Extraction
7 (lowest priority) right to left = = Assignment

4.  Properties of Real Numbers from High School Algebra.  Knowledge of these properties is mandatory for understanding which parts of an equation can be done in parallel.  The primary property to focus on for deciding what things can be done in parallel is the Associative Property.

Name of Property Property
0 is the Additive Identity X + 0 = X
-X is the Additive Inverse of X -X + X = 0
Associative Property of Addition (X + Y) + Z = X + (Y + Z)
Commutative Property of Addition X + Y = Y + X
1 is the Multiplicative Identity Y * 1 = Y
(1/Y) is the Multiplicative Inverse of Y (1/Y) * Y = 1
Associative Property of Multiplication (X * Y) * Z = X * (Y * Z)
* Commutative Property of Multiplication X * Y = Y * X
Left Distributive Property X * (Y + Z) = (X * Y) + (X * Z)
Right Distributive Property (A + B) * C = (A * C) + (B * C)

* Commutative Property of Multiplication:  When you study matrices in MAT 161, know that the Commutative Property of Multiplication is NOT TRUE for matrix multiplication (with some special case exceptions).