the asc language

82
The ASC Language Part 2 of Associative Computing

Upload: penny

Post on 15-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

The ASC Language. Part 2 of Associative Computing. Contents. References The MST example and background Variables and Data Types Operator Notation Input and Output Mask Control Statements Loop Control Statements Accessing Values in Parallel Variables Performance Monitor - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The ASC Language

The ASC Language

Part 2 of Associative Computing

Page 2: The ASC Language

2

Contents• References• The MST example and background • Variables and Data Types• Operator Notation• Input and Output• Mask Control Statements• Loop Control Statements• Accessing Values in Parallel Variables• Performance Monitor• Subroutines and other topics• Basic Program Structure• Software Location & Execution Procedures• An Online ASC Program & Data File to Execute

Page 3: The ASC Language

3

References

• “ASC Primer” by Professor Jerry Potter is the primary reference– Copy is posted on lab website under “software”

• Lab website is at www.cs.kent.edu/~parallel/

– ASC Primer is the reading assignment for this section

• “Associative Computing” book by Jerry Potter has a lot of additional information.

• Both references use a directed-graph version of the Minimal Spanning Tree as an important example.

Page 4: The ASC Language

4

Features of Potter’s MST Algorithm• Both versions of MST are based on Prim’s sequential

MST algorithm– See one of Baase’s algorithm books in references

• A drawback of this algorithm is that it requires 1 PE for each graph edge, which in worst case is n2 – n = (n2) – Unlike earlier MST algorithm, this is not an “optimal cost” parallel

algorithm• An advantage is that it works for undirected graphs

– Earlier MST algorithm can probably be extended to work for directed graphs.

• Uses less memory for most graphs than earlier algorithm – True especially for sparse graphs– Often will require only O(n) memory locations, since the memory

required for each PE is a small constant.– Even in worst case, at most O(n2) memory locations are needed.– Earlier algorithm always requires (n2) memory locations, as

each PE stores a row of the adjacency matrix.

Page 5: The ASC Language

5

Representing the Graph in a Procedural Language

• We need to find edges that are incident to a node of the graph. What kind of data structure could be used to make this easy? Typically there are two choices:– An adjacency matrix

• Label the rows and columns with the node names.• Put the weight w in row i and column j if i is

incident to j with weight w.• Doing this we would use the representation of the

graph in the problem as follows ...

Page 6: The ASC Language

6

Graph Example for MST

DE

HI

F CG

BA

8

6

53

3

2

2

2

1

6

1

4

2

47

Page 7: The ASC Language

7

An Adjacency Matrix For the Graph in the Problem

A

B

C

D

E

F

G

H

I

2 7 3

2 4 6

4 2 2

2 1 8

1 6 2

7 6 5

3 6 3 1

2 8 3 4

2 5 1 4

A B C D E F G H I

Page 8: The ASC Language

8

An Alternative Representation That Is Useful for Sequential Algorithms

• Another possibility is to use adjacency lists, which can allow some additional flexibility for this problem in representing the rest of the data – namely the sets v1, v2, and v3.

• It is in these type of representations that we see pointers or references play a role.

• We link off of each node, all of the nodes which are incident to it, keeping them in increasing order by label.

Page 9: The ASC Language

9

Adjacency Lists for the Graph in the Problem

A

B

C

D

E

F

G

H

I

B 2

A 2

G 3

G 6

F 7

C 4

ETC.....

G, H, and I will have 4 entries; all others have 3.

In each list, the nodes are in increasing order by node label.

Note: if the node label ordering is clear, the A, B, ... need not be stored.

Page 10: The ASC Language

10

Adding the Other Information Needed While Finding the Solution

• Consider one of the states during the run, right after the segment AG is selected:

V1

A

B

G

V2

C

F

I

H

How will this data be maintained?

Page 11: The ASC Language

11

• We need to know:– Which set each node is in.– What is each node’s parent in the tree formed

below by both collections.– A list of the candidate nodes.

V1

A

B

G

V2

C

F

I

H

Page 12: The ASC Language

12

A Typical Data Structure Used For This Problem Is Shown

1

A 2 A 1

F 4 B 2

3

3

7 A 2

F 3 A 1

C 3 G 2

H 1 G 2

A

B

C

D

E

F

G

H

I

I

V2 elements are linked via yellow entries with V2lnk the head and the tail:

I H C F

Light blue entries appeared in earlier states, but are no longer in use.

Red entries say what set the node is in.

Green entries give parent of node and orange entries give edge weights.

The adjacency lists are not shown, but are linked off to right.

V2lnk

Page 13: The ASC Language

13

I Is Now Selected and We Update

1

A 2 A 1

F 4 B 2

3

3

7 A 2

F 3 A 1

C 3 G 2

H 1 G 1

A

B

C

D

E

F

G

H

I

I V2lnk

I is now in V1 so change its set value to 1.

Look at nodes adjacent to I :

E, F, G, H

and add them to V2 if they are in V3:

E is added ...

Page 14: The ASC Language

14

E was Just Added to V2

1

A 2 A 1

F 4 B 2

3

H 2

7 A 2

F 3 A 1

C 3 G 2

H 1 G 1

A

B

C

D

E

F

G

H

I

E V2lnkStore I’s link H in E’s position and E in V2lnk. This makes I’s entry unreachable.

So V2 is now

E H C F

Now we have to add relevant edges for I to any node in V2.

Page 15: The ASC Language

15

Add Relevant Edges For I to V2 Nodes

1

A 2 A 1

F 4 B 2

3

H 2 I 2

5 I 2

F 3 A 1

C 3 G 2

H 1 G 1

A

B

C

D

E

F

G

H

I

E V2lnkWalk I’s adjacency list:

E F G H

E was just added so select EI with weight 2.

wgt(FI) = 5 < wgt(FA) was 7, so drop FA and add FI (see black blocks)

G is in V1 so don’t add GI.

wgt(HI) = 4 > wgt(HG) = 3, so no change.

This is now ready for next round.

Page 16: The ASC Language

16

Complexity Analysis (Time and Space) for Prim’s Sequential Algorithm

• Assume – the preceding data structure is used.– The number of nodes is n– The number of edges is m

• Space used is 4n plus the space for adjacency lists.– The adjacency list are Θ(m), which in worst case is Θ(n2) – This data structure sacrifices space for time.

• Time is Θ(n2) in the worst case.– The adjacency list of each node is traversed only once when it is

added to tree. The total work of comparing weights and updating the chart during all of these traversals is Θ(m),

– There are n-1 rounds, as one tree node is selected each round.– Walking the V2 list to find the minimum could require n-1 steps

the first round, n-2 the second, etc for a max of Θ(n2) steps

Page 17: The ASC Language

17

Alternate ASC Implemention of Prim’s Algorithm using this Approach

• After setting up a data structure for the problem, we now need to code it by manipulating each state as we did on the preceding slides.

• ASC model provides an easier approach.• Recall that ASC does NOT support pointers or

references.• The associative searching replaces the need for

these.• Recall, we collectively think of the PE processor

memories as a rectangular structure consisting of multiple records.

• We will next introduce basic features of the ASC language in order to implement this algorithm.

Page 18: The ASC Language

18

Structuring the MST Data for ASC

• There are 15 bidirectional edges in the graph or 30 edges in total.

• Each directed edge will have a head and a tail.

•So, the bidirectional edge AB will be represented twice – once as having head A and tail B and once as having head B and tail A

• We will use 30 processors and in each PE’s memory we will store an edge representation as:

• State is 0, 1, 2, or 3 and will be explained shortly.

head tail weight state

Page 19: The ASC Language

19

ASC Data Types and Variables

• ASC has eight data types…– int (i.e., integer), real, hex (i.e., base 16), oct

(i.e., base 8), bin (i.e., binary), card (i.e., cardinal), char (i.e., character), logical, index.

– Card is used for unsigned integer data.

• Variables can either be scalar or parallel.

Page 20: The ASC Language

20

ASC Parallel Variables

• Parallel variables reside in the memory of individual processors.

• Consequently, tail, head, weight, and state will be parallel variables.

• In ASC, parallel variables are declared using an array-like notation, with $ in index:char parallel tail[$], head[$];

int parallel weight[$], state[$];

Page 21: The ASC Language

21

ASC Scalar and Index Variables

• Scalar variables in ASC reside in the IS (i.e., the front end computer), not in the PE’s memories.

• They are declared aschar scalar node;

• Index variables in ASC are used to manipulate the index (i.e. choice of an individual processor) of a field. For example,

head[xx]• They are declared as:

index parallel xx[$];• They occupy 1 bit of space per processor

Page 22: The ASC Language

22

Logical Variables and Constants

• Logical variables in ASC are boolean variables. They can be scalar or parallel.– ASC does not formally distinguish between the index

parallel and logical parallel variables– The correct type should be selected, based on usage.

• If you prefer to work with the words TRUE and FALSE, you can define logical constants by deflog (TRUE, 1); deflog (FALSE, 0);

• Constant scalars can be defined by define (identifier, value);

Page 23: The ASC Language

23

Logical Parallel Variables needed for MST

• These are defined as follows:

logical parallel nextnod[$], graph[$], result[$];

• The use of these will become clear in later slides.

• For the moment, recognize they are just bit variables, one for each PE.

Page 24: The ASC Language

24

Array Dimensions

• A parallel variable can have up to 3 dimensions– First dimension is “$”, the parallel dimension

• The array numbering is zero-based, so the declaration

int parallel A[$,2]creates the following 1dimensional variables:

A[$,0], A[$,1], A[$,2]

Page 25: The ASC Language

25

Mixed Mode Operations

• Mixed mode operations are supported and their result has the “natural” mode. For example, given declarations

int scalar a, b, c;

int parallel p[$], q[$], r[$], t[$,4];

index parallel x[$], y[$];

then

c = a + b is a scalar integer

q[$] = a + p[$] is a parallel integer variable

a + p[x] is a integer value

r[$] = t[x,2]+3*p[$] is a parallel integer variable

x[$] = p[$] .eq. r[$] is an index parallel variable• More examples are given on page 9-10 of ASC Primer

Page 26: The ASC Language

26

The Memory Layout for MST• As with most programming languages, the order

of the declarations determines the order in which the variables are identified in memory.

• To illustrate, suppose we declare for MSTchar parallel tail[$], head[$];int parallel weight[$], state[$];int scalar node;index parallel xx[$];logical parallel nexnod[$], graph[$],

result[$];• The layout in the memories is given on next slide• Integers default to the word size of the machine

so ours would be 32 bits.

Page 27: The ASC Language

27

The Memory Layout for MST

PE

0

1

2

3

4

p-1

p

tail head weight state xx nxt gr res

Last 4 are bit fields. The last 3 are named:

nxtnod

graph

result

Page 28: The ASC Language

28

Operator Notation Relational and Logical Operators• Original syntax came from FORTRAN and the

examples in the ASC Primer use that syntax.• However, the more modern syntax is supported:

.lt. < .not. !

.gt. > .or. ||

.le. <= .and. &&

.ge. >= .xor. --

.eq. ==

.ne. !=Arithmetic Operators

addition +multiplication *division /

Page 29: The ASC Language

29

Parallel Input in ASC

• Input for parallel variables can be interactive or from a data file in ASC.

• We will run in a command window so file input will be handled by redirection

• If you are not familiar with command window handling or Linux (Unix), this will be shown.

• In either case, the data is entered in columns just like it will appear in the read command.– Do not use tabs.– THE LAST LINE MUST BE A BLANK LINE!

Page 30: The ASC Language

30

Parallel read and Associate Command

• The format of the Parallel read statement is

read parvar1, parvar2,... in <logical parallel var>• The command only works with parallel variables,

not scalars.• Input variables must be associated with a logical

parallel variable before the read statement.• The logical variable is used to indicate which PEs

was used on input.

After the read statement, the logical parallel variable will be true (i.e., 1) for all processors holding input values.

Page 31: The ASC Language

31

Parallel Input in ASC

• The associate command and the read command for MST would be:

associate head[$], tail[$], weight[$], state[$] in graph[$];

read tail[$], head[$], weight[$] in graph[$];

• Blanks can be used rather than commas, as indicated by MST example on pg 35 of Primer.

• Commenting Code:

/* This is the way to comment code in ASC */

Page 32: The ASC Language

32

Input of Graph

• Suppose we were just entering the data for AB, AG, AF, BA, BC, and BG.

• Order is not important,

but the data file would

look like:A B 2A G 5A F 9B A 2B C 4B G 6blank line

and memory would like:

tail head weight graph A B 2 1 A G 5 1 A F 9 1 B A 2 1 B C 4 1 B G 6 1 0 0 0 0

Page 33: The ASC Language

33

Scalar variable input• Static input can be handled in the code.• Also, define or deflog statements can be used to handle

static input.• Dynamic input is currently not supported directly, but

can be accomplished as follows:– Reserve a parallel variable dummy (of desired type) for input.– Reserve a parallel index variable used.– Values to be stored in scalar variables are first read into dummy

using a parallel-read and then transferred using get or next to the appropriate scalar variable.

– Example:

read dummy[$] in used[x];

get x in used[$]

scalar-variable = dummy[x];

endget x;

Page 34: The ASC Language

34

Input Summary• Direct scalar input is not directly supported.• Scalars can be set as constants or can be set

during execution using various commands.– We will see this shortly

• We will be able to output scalar variables – This will also be handy for debugging purposes.

• The main problem on input is to remember to include the blank line at the end.

• I suggest always printing your input data initially so you see it is going in properly.

Page 35: The ASC Language

35

Parallel Variable Output

• Format for parallel print statement isprint parvar1, parvar2,... in <logical parallel var>

• Again, variables to be displayed must be associated with a logical parallel variable first.

• You can use the same association as for the read command:associate tail[$], head[$], weight[$] with graph[$];read tail[$], head[$], weight[$] in graph[$];print tail[$], head[$], weight[$] in graph[$];

• You can use a logical parallel variable that has been set with another statement, like an IF statement, to control which PEs will output data.

Page 36: The ASC Language

36

MST Example

• Suppose state[$] is holding the holding the nodes in V1 and the data for the edge incident to them that was used.

• Then, you could set up an association by

if (state[$] == 1) then result[$] = TRUE; endif;• You can print with this association as follows:

print tail[$], head[$], weight[$] in result[$];– Only those records where state[$] == 1 would be printed.

Page 37: The ASC Language

37

Output Using “msg”

• The msg command– Used to display user text messages.– Used to display values of scalar variables.– Used to display a dump of the parallel

variables.• The entire parallel variable contents printed • Status of active responders or association

variables ignored• Format: msg “string” list’• msg “The answers are” max BB[X] B[$]

• See Page 13-14 of ASC Primer

Page 38: The ASC Language

38

Assignment Statements

• Assignment can be made with compatible expressions using the equal sign with– scalar variables– parallel variables– logical parallel variables

• The data types normally have to be the same on both sides of the assignment symbol – i.e. don’t mix scalar and parallel variables.

• A few special cases are covered on the next slide

Page 39: The ASC Language

39

Some Assignment Statement Special Cases

Declarations for Examples:int scalar k;int parallel b[$];Index parallel xx[$];

• If xx is an index variable with a 1 in at least one of its components, then following is valid:

k = aa[xx] + 5;– Here, the component of aa used is one where xx is 1.– While selection is arbitrary (e.g., pick-one), in current

implementation, the smallest index where xx[$] is 1 is used.• The assignment of integer arithmetic expressions to

integer parallel variables is supported. b[xx] = 3 + 5

– This statement assigns an 8 to the “xx component” of b.– This is normally the component identified by first “1” in xx.

• See pg 9-10 of Primer for more examples.

Page 40: The ASC Language

40

Exampleaa[$] = b[$] + c[$];1

• Before:

mask aa[$] b[$] c[$]

1 2 3 4

1 3 5 3

0 2 4 -3

0 6 4 1

1 2 -3 -6

• After:

mask aa[$] b[$] c[$]

1 7 3 4

1 8 5 3

0 2 4 -3

0 6 4 1

1 -9 -3 -6

1 Note: “a” is a reserved word in ASC and so it can’t be used as a variable name. (see ASC Primer, pgs 29-30 and 39)

Page 41: The ASC Language

41

Setscope Mask Control Statement

• Format:setscope <logical parallel variable>

bodyendsetscope;

• Resets the parallel mask register– setscope jumps out of current mask setting to the new

mask given by its logical parallel variable.– One use is to reactivate currently inactive processors.– Also allows an immediate return to a previously

calculated mask, such as an association.– Is an unstructured command such as go-to and

jumps from current environment to a new environment.

• Use sparingly– endsetscope resets mask to preceding setting.

Page 42: The ASC Language

42

Example

Before setscope:

mask aa used tail

1 5 1 7

1 22 0 6

1 5 1 9

0 41 0 7

After setscope: used=aa mask tail

5 1 100

22 0 6

5 1 100

41 0 7

logical parallel used[$];...used[$] = aa[$] == 5;setscope used[$]

tail[$] = 100;endsetscope;

After endsetscope:aa mask tail

5 1 100

22 1 6

5 1 100

41 0 7

Page 43: The ASC Language

43

The Scalar IF Statement

• Scalar IF – similar to what you have used before – i.e. a branching statement – with the else part optional.

Example: int scalar k; ... if k == 5 then sum =0; else b = sum;

endif;

Page 44: The ASC Language

44

The Parallel IF Mask Control Statement

• Looks like scalar IF except instead of a scalar logical expression, a parallel logical expression is encountered.

• Format: if <logical parallel expression> then

body of then [ else body of else ] endif

• Although it looks similar, the execution is considerably different.– The parallel version normally executes both “bodies”,

each for the appropriate processors• Useful as a parallel search control statement

Page 45: The ASC Language

45

Operation Steps of Parallel IF

1) Save the mask bit of processors that are currently active.

2) Broadcast code to the active processors to calculate the IF boolean expression.

3) It the boolean expression is true for an active processor, set its individual cell mask bit to TRUE; otherwise set its mask bit to FALSE.

4) Broadcast code for the “then” portion of the IF statement and execute it on the (TRUE) responders.

5) Compliment the mask bits for the processors that were active at step 1. (Others remain FALSE)

6) Broadcast code for the “else” portion of the IF statement and execute it on the active processors.

7) Reset the mask to original mask at Step 1.

Page 46: The ASC Language

46

Example

Before:

b mask

1 1

7 1

2 1

1 1

1 0

After:

b then mask else mask

2 1 0

-1 0 1

-1 0 1

2 1 0

1 0 0

if (b[$] == 1) then b[$] =2;else b[$] = -1 endif;

Page 47: The ASC Language

47

IF-NOT-ANY Format

if <logical parallel expression> thenbody of if

elsenanybody of not any

endif;

• Note this is an “if” statement with an embedded ELSENANY clause.

• The “any”, “elsenany”, and “nany” constructs implement the “AnyResponders” associative property in ASC.

Page 48: The ASC Language

48

The IF-NOT-ANY Mask Control Statement

• Only one part of IF statement is executed.• Useful as a parallel search control statement• Steps

1) Evaluate the conditional statement.

2) If there are one or more active responders, execute the “then” block.

3) If there is no active responders, the ELSE-NOT-ANY (ELSENANY) block is executed.

4) When executing the ELSENANY part, the original mask is used – i.e. the one prior to the IF-NOT-ANY statement.

Page 49: The ASC Language

49

Example

Before: aa b c 1 17 0 2 13 0 2 8 0 3 12 0 2 9 0 4 67 0 0 0 0 0 12 0

After:mask1 mask2 aa b c 0 0 1 17 0 1 0 2 13 0 1 0 2 8 0 1 1 3 12 1 1 0 2 9 0 0 0 4 67 0 0 0 0 0 0 0 0 0 12 0Recall: uses set mask

if aa[$] > 1 && aa[$] < 4 /*sets mask*/if b[$] == 12 then c[$] = 1 /* search for b ==12 */

elsenany c[$] = 9; endif; /* action if no b is 12*/endif;

Page 50: The ASC Language

50

Example

Before: aa b c 1 17 0 2 13 0 2 8 0 3 4 0 2 9 0 4 67 0 0 0 0 0 12 0

After:mask1 mask2 aa b c 0 0 1 17 0 1 0 2 13 9 1 0 2 8 9 1 0 3 4 9 1 0 2 9 9 0 0 4 67 0 0 0 0 0 0 0 0 0 12 0Recall – uses original mask

if aa[$] > 1 && aa[$] < 4 /*sets mask*/if b[$] == 12 then c[$] = 1 /* search for b ==12 */

elsenany c[$] = 9; endif; /* action if no b is 12*/endif;

Page 51: The ASC Language

51

The ANY Mask Control Statement

Format:any <logical parallel expression>

body[elsenany

body] endany;• This is another parallel search control statement.• It searches for all data items that satisfies the

conditional statement.• This is the primary construct used in ASC to

implement the “AnyResponders” associative property

Page 52: The ASC Language

52

The ANY StatementUsed to search all data items that satisfy the

conditional expression.There must be at least one responder for the body

statement to be performed.If there are no responders, the ANY statement

does nothing unless an ELSENANY is used.The mask used to execute the body part is the

original mask prior to the ANY statement. Consequently, all active responders are affected if the conditional expression of the ANY evaluates to TRUE.

This is the key command in ASC for implementing the “AnyResponders” associative property.

Page 53: The ASC Language

53

Example

Before:mask aa b 1 3 0 0 9 0 1 16 0 1 10 0 1 8 0 0 0 0 1 0 0

After:mask aa b 0 3 0 0 9 0 1 16 11 1 10 11 1 8 11 0 0 0 0 0 0

if aa[$] > 7 then /* set mask */

any aa[$] == 10 b[$] = 11; endany;

endif;

Page 54: The ASC Language

54

The Loop Control Statements

• Loop controlled by either a scalar test or a parallel test– LOOP-UNTIL statement

• Conditional is evaluated every iteration

• Loop controlled by a parallel test– Parallel FOR-Loop

• Conditional is evaluated only once

– Parallel While-Loop• Conditional is evaluated every iteration

Page 55: The ASC Language

55

The LOOP-UNTIL Statement• Similar to REPEAT UNTIL loops in other languages.• However, it appears to be more flexible since the UNTIL

conditional test can appear anywhere in the body of the loop.

• Format:first

initializationloop

body1until (logical scalar expression) or

(logical parallel expression) or(NANY logical parallel expression)body 2

endloop;• Parallel exit conditions

– The UNTIL exits when responder(s) are detected– With NANY, the UNTIL exits when a no-responder condition occurs

Page 56: The ASC Language

56

Example

Before: mask aa b 1 0 3 1 3 4 1 0 1 0 1 3 1 1 5 1 4 6 1 5 2

After : i=0 mask aa b 1 0 5 1 3 4 1 0 3 0 1 3 1 1 5 1 4 6 1 5 2

first i = 0;loop

if aa[$] = i then b[$] =b[$] + 2; endif’;i = i + 1;

until i > 4; endloop;

Page 57: The ASC Language

57

Example

Before: mask aa b 1 0 3 1 3 4 1 0 1 0 1 3 1 1 5 1 4 6 1 5 2

After : i=0 i=1 mask aa b b 1 0 5 5 1 3 4 4 1 0 3 3 0 1 3 3 1 1 5 7 1 4 6 6 1 5 2 2

first i = 0;loop

if aa[$] = i then b[$] = b[$] + 2; endif’;i = i + 1;until i > 4; endloop;

Page 58: The ASC Language

58

Example

Before: mask aa b 1 0 3 1 3 4 1 0 1 0 1 3 1 1 5 1 4 6 1 5 2

After : i=0 i=1 i=3 mask aa b b b 1 0 5 5 5 1 3 4 4 6 1 0 3 3 3 0 1 3 3 3 1 1 5 8 8 1 4 6 6 6 1 5 2 2 2

first i = 0;loop

if aa[$] = i then b[$] = b[$] + 2; endif’;i = i + 1;until i > 4; endloop;

Page 59: The ASC Language

59

Example

Before: mask aa b 1 0 3 1 3 4 1 0 1 0 1 3 1 1 5 1 4 6 1 5 2

After : i=0 i=1 i=3 i= 4 mask aa b b b b 1 0 5 5 5 5 1 3 4 4 9 9 1 0 3 3 3 3 0 1 3 3 3 3 1 1 5 8 8 8 1 4 6 6 6 8 1 5 2 2 2 2

first i = 0;loop

if aa[$] = i then b[$] = b[$] +2 ; endif’;i = i + 1;until i > 4; endloop;

Note: The example is to illustrate only; it could be done easier.

Page 60: The ASC Language

60

The Parallel FOR-LOOP

• FOR is used for looping and retrieving

• Used when a process must be repeated for each cell that satisfies a certain condition.

• It is similar to usual FOR, but the conditional logical expression must be a parallel one.

• Initially, the conditional expression is evaluated and the active responders are stored in an index variable.

Page 61: The ASC Language

61

The Parallel FOR-LOOP (cont)

• The top responder is processed during each pass through the FOR-loop until no responders remain.

• The contents of the index variable is updated at the bottom of the loop (i.e., the first “1” is changed to “0”)

• The index variable is used to walk through the responders and to retrieve each responder’s records.

• The conditional condition is never re-evaluated.

Page 62: The ASC Language

62

Example

sum = 0;for xx in tail[$] != 999 /*evaluates and stores in xx*/

sum = sum + value[xx];endfor xx;

tail xx value 3 1 10 1st time : sum = sum + 10 = 10 5 1 20 2nd time: sum = sum + 20 = 30 999 0 30 6 1 40 3rd time: sum = sum + 40 = 70

Page 63: The ASC Language

63

The Parallel WHILE Loop

• Similar to FOR loop except it reevaluates the conditional expression before each iteration.

• Format: WHILE <para index var> in <para logical expression>

bodyendwhile <para index var>

• The iteration terminates when there are no responders to the parallel logical expression.

• Note the number of responders can increase, decrease, or remain the same during a run.

• Unlike the FOR loop, this loop can be infinite.

Page 64: The ASC Language

64

The Parallel WHILE Loop• Unlike the FOR statement, this construct re-

evaluates the logical conditional statement prior to each execution of the body of the while.

• The bit array resulting from the evaluation of the conditional statement is assigned to the index parallel variable on each pass.

• The index parallel array is available for use within the body for each loop and can be changed within the body.

• The iteration is terminated when the conditional statement is tested and there are no responders.– That is, all zeros in the index parallel variable.

• See ASC Primer pg 21-22 for more information

Page 65: The ASC Language

65

Before:aa b c1 17 02 13 02 8 13 11 12 9 04 67 0

After 1st loop:In loop, sumit is 13DUMP OF ASSOCIATION ACTIVE FOLLOWS: AA,C, 1 0 7 0 2 1 3 1 2 0 4 0

sumit = 0;while xx in (aa[$] == 2)

sumit = sumit + b[xx]; if (c[xx] == 1) then

if (aa[$] == 2) then aa[$] = 5; endif; else aa[xx] = 7; endif; msg "In loop, sumit is " sumit; print aa[$], c[$] in active[$]; endwhile xx;

After 2nd loop:In loop, sumit is 21DUMP OF ASSOCIATION ACTIVE FOLLOWS: AA,C,

1 0 7 0 5 1 3 1 5 0 4 0

Page 66: The ASC Language

66

When is Conditional Tested in Loops?

• UNTIL loops evaluate the test condition each time the UNTIL statement is encountered.

• WHILE loops have the test condition reevaluated before each iteration.

• The FOR loop evaluates the conditional expression and stores the resulting active responders in an index variable. This index variable is then used to retrieve a data item in the association.

Page 67: The ASC Language

67

Special Commands to Obtain Parallel Variable Values

• Get Statement• Next Statement• Minimum and maximum values• These commands are needed to implement

some of the associative functions.– In particular, “get” and “next” allow the programmer to

select an active responder for further processing.– This is sometimes called the “PickOne” property.

Page 68: The ASC Language

68

GET Statement• Used to access a specific field in the memory of an

active processor.• Format:

get <parallel index var> in <parallel logical expression>body

[elsenanybody]

• The parallel logical expression is evaluated and its value assigned to the parallel index variable.

• The parallel index variable will identify the first active responder (if one exists) that satisfies the conditional test

• The body of the GET then uses the parallel index variable to access the specific item.

• If there are no responders, the GET body is not executed.

• GET may also contain an ELSENANY statement.

Page 69: The ASC Language

69

Example

Before:

tail val

10 100

1 90

2 77

1 83

Before:

tail val

10 100

1 0

2 77

1 83

get xx in tail[$] = 1val[xx] = 0;

endget xx;

Page 70: The ASC Language

70

The NEXT Statement• Similar to GET statement, except NEXT deactivates the

responder accessed each time it is called.• Format:

next <parallel index var> in <parallel logical expression>

body

[elsenany

body]

• Unlike GET, two successive calls to NEXT is expected to select two distinct PEs and association records.

• NEXT is almost always used within a looping statement to walk through the various PEs to do something in each.

Page 71: The ASC Language

71

Before: aa used b 1 0 2 4 1 2 4 1 2 19 0 2 4 1 2

After: aa used b 1 0 2 4 0 -1 4 1 2 19 0 2 4 1 2

Example

int parallel aa[$], b[$]; used[$] = aa[$] ==4;logical parallel used[$]; next xx in used[$]index parallel xx[$]; b[xx] = -1; endnext xx;

Caution: xx in aa[$] ==4 is not allowed. A logical variable must be involved.

Page 72: The ASC Language

72

Example – see next slide for resultsmain tryoutint scalar k;int parallel aa[$], b[$], c[$];logical parallel used [$], active[$];index parallel xx[$];

associate aa[$], b[$], c[$] with active[$];read aa[$], b[$], c[$] in active[$];print aa[$], b[$], c[$] in active[$]; /* to see input */

/* Tryout assignment statement */ b[$] = aa[$] + 5; c[$] = 3 + 5; used[$] = aa[$] == 5; /* selects all processors with 5 in aa field */ next xx in used[$] /* selects the top processor in used */

k = b[xx] +2; /* could do these two lines in one – just */ c[xx] = k; /* done this way to show a scalar can be */ endnext xx; /* set */

print aa[$], b[$], c[$] in active[$];end;

Page 73: The ASC Language

73

Before:

DUMP OF ASSOCIATION ACTIVE FOLLOWS:

AA,B,C,

1 2 3

2 3 4

5 6 7

8 9 10

11 12 13

5 1 2

5 2 1

After:

DUMP OF ASSOCIATION ACTIVE FOLLOWS:

AA,B,C,

1 6 8 Arrows show

2 7 8 PEs in used

5 10 12

8 13 8 xx is first PE

11 16 8

5 10 8

5 10 8

b[$] = aa[$] + 5; next xx in used[$]c[$] = 3 + 5; k = b[xx]+2;used[$] = aa[$] == 5; c[xx] = k;

endnext xx;

Page 74: The ASC Language

74

Printing Scalars, Text Messages, and Dumping the Entire Parallel Array for a Field

Format: msg “string” list;Example:msg "The values are " aa[$], k;

The values are PE 0: 0 1 2 5 8 11 5 5 0 0 0 0 0 0 0 0 PE 16: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PE 32: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PE 48: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...PE288: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 PE304: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

12

Page 75: The ASC Language

75

MAXVAL and MINVAL Functionsand Other Functions

• MAXVAL(MINVAL) returns the maximum (minimum) value among active responders.

If (tail[$] != 1) then k = maxval(weight[$]);endif;

• MAXDEX (MINDEX) returns the index of an entry where the maximum (minimum) value of the specified item occurs among the active responders.

• Recall – With an associative SIMD, above are constant time functions as they are supported in hardware.

• With a SIMD that is not associative, they can still be performed, but they are not constant time functions and their timings depend upon the interconnection network.

• There are several variations of above functions in ASC Primer – i.e. finding the nth smallest value.

• The function COUNT() returns the number of active responders. It can be useful in debugging.

Page 76: The ASC Language

76

Dynamic Storage Allocation • allocate is used to identify a processor whose association record is

currently unused.– Will be used to store a new association record– Creates a parallel index that points to the processor selected

• release is used to de-allocate storage of specified records in an association– Can release multiple records simultaneously.

• Example:Example:char parallel node[$], parent[$];logical parallel tree[$];index parallel x[$];associate node[$], level[$], parent[$] with tree[$];......allocate x in tree[$]

node[x] = ‘B’endallocate x;release parent[$] .eq. ‘A’ from tree[$].

Page 77: The ASC Language

77

Performance Monitor

• Keeps track of number of scalar and parallel operations.• It is turned on and off using the PERFORM statement

– perform = 1;– perform = 0;

• The number of scalar and parallel operations can be printed using the MSG command– MSG “Number of parallel and scalar operations are”

PA_PERFORM SC_PERFORM;• The ASC Monitor is important for evaluation and

comparison of various ASC algorithms and software.– It can also be used to estimate or determine running

time. • See Pg 30-31 of ASC Primer for more information

Page 78: The ASC Language

78

Additional Features

• Restricted subroutine capability is currently available– See call and include on pg 25-7 of ASC Primer.– ASC has a rather simplistic subroutine capability.– While not difficult, the subroutine details will not be

covered in slides.– Assignment will not require use of subroutines.

• Use of personal pronouns and articles in ASC make code easier to read and shorter.– See page 29 of ASC Primer.– Again, the details are not covered in slides.

Page 79: The ASC Language

79

Basic Program Structure

• Main program_name– Constants;– Variables;– Associations;– Body;

• End;

Page 80: The ASC Language

80

Software

• Compiler and Emulator– DOS/Windows, UNIX

(Linux)– WaveTracer– Connection Machine

• http://www.cs.kent.edu/~parallel/ and look under “software”

• Use any text editor.– Careful on moving files

between DOS and UNIX!

ASC Compiler

ASC Emulator

Anyprog.asc

Anyprog.iob

Standard I/O File I/O

-e-wt-cm

-e-wt-cm

Page 81: The ASC Language

81

Simple ASC Program

• Example:– Consider an ASC Program that computes the

area of various simple shapes (circle, rectangle, triangle).

• Here is an example shapes.asc

• Here is the data shapes.dat

• Here is the shapes.out

NOTE: Above links are only active during the “slide show”.

Page 82: The ASC Language

82

Software

• Example:– To compile your program…

– % asc1.exe –e shapes.asc

– To execute your program…– % asc2.exe –e shapes.iob– % asc2.exe –e shapes.iob < shapes.dat– % asc2.exe –e shapes.iob < shapes.dat > shapes.out