 RAPTOR Syntax and Semantics - Arrays Array variable - Array variables are used to store many values (of the same type) without having to have many variable names. Instead of many variables names a count-controlled loop is used to gain access (index) the individual elements (values) of an array variable. RAPTOR has one and two dimensional arrays of numbers. A one dimensional array can be thought of as a sequence (or a list). A two dimensional array can be thought of as a table (grid or matrix). To create an array variable in RAPTOR, use it like an array variable. i.e. have an index, ex. Score, Values[x], Matrix[3,4], etc. All array variables are indexed starting with 1 and go up to the largest index used so far. RAPTOR array variables grow in size as needed. The assignment statement GPAs ŌåÉ 4.0 assigns the value 4.0 to the 24th element of the array GPAs. If the array variable GPAs had not been used before then the other 23 elements of the GPAs array are initialized to 0 at the same time. i.e. The array variable GPAs would have the following values: 1 2 3 4ŌĆ” Array variables in action- Arrays and count-controlled loop statements were made for each other. Notice in each example below the connection between the Loop Control Variable and the array index! Notice how the Length_Of function can be used in the count-controlled loop test! Notice that each example below is a count-controlled loop and has an Initialize, Test, Execute, and Modify part (I.T.E.M)! Assigning values to an array variable Reading values into an array variable Writing out an array variableŌĆÖs values Computing the total and average of an array variableŌĆÖs values Index ŌåÉ 1 Index ŌåÉ 1 Index ŌåÉ 1 Total ŌåÉ 0 Loop Loop Loop Index ŌåÉ 1 PUT ŌĆ£The value of the array at position ŌĆ£ + Index + ŌĆ£ is ŌĆ£ + GPAs[Index] Loop GPAs[Index] ŌåÉ 4.0 ŌĆ£Enter the GPA of student ŌĆ£ŌĆØ + Index + ŌĆ£: ŌĆ£ GET GPAs[Index] Index >= 24 Index >= 24 Index >= Length_Of (GPAs) Index ŌåÉ Index + 1 Index ŌåÉ Index + 1 Index ŌåÉ Index + 1 Total ŌåÉ Total + GPAs[Index] Index >= Length_Of(GPAs) Index ŌåÉ Index + 1 ŌĆ” 23 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4.0 The initialization of previous elements to 0 happens only when the array variable is created. Successive assignment statements to the GPAs variable affect only the individual element listed. For example, the following successive assignment statements GPAs GPAs ŌåÉ ŌåÉ 1.7 3.2 would place the value 1.7 into the 20th position of the array, and would place the value 3.2 into the 11th position of the array. i.e. GPAs ŌåÉ 1.7 GPAs ŌåÉ 3.2 1 2 3 4ŌĆ” ŌĆ” 23 24 Initialize the elements of a two dimensional array (A two dimensional array requires two loops) Row ŌåÉ 1 Loop Average ŌåÉ Total / Length_Of(GPAs) Find the largest value of all the values in an array variable Find the INDEX of the largest value of all the values in an array variable Highest_GPA ŌåÉ GPAs Highest_GPA_Index ŌåÉ1 Index ŌåÉ 1 Index ŌåÉ 1 Loop Loop GPAs[Index] > Highest_GPA GPAs[Index] >= GPAs[Highest_GPA_Index] Column ŌåÉ 1 Loop 0 0 0 0 0 0 0 0 0 0 3.2 0 0 0 0 0 0 0 0 1.7 0 0 0 4.0 An array variable name, like GPAs, refers to ALL elements of the array. Adding an index (position) to the array variable enables you to refer to any specific element of the array variable. Two dimensional arrays work similarly. i.e. Table[7,2] refers to the element in the 7 th row and 2nd column. Individual elements of an array can be used exactly like any other variable. E.g. the array element GPAs can be used anywhere the number variable X can be used. The Length_Of function can be used to determine (and return) the number of elements that are associated with a particular array variable. For example, after all the above, Length_Of(GPAs) is 24. Matrix[Row, Column] ŌåÉ 1 Column >= 20 Column ŌåÉ Column + 1 Highest_GPA ŌåÉ GPAs[Index] Highest_GPA_Index ŌåÉ Index Index >= Length_Of(GPAs) Index >= Length_Of(GPAs) Index ŌåÉ Index + 1 Index ŌåÉ Index + 1 PUT ŌĆ£The highest GPA is ŌĆ£ + Highest_GPA┬Č PUT ŌĆ£The highest GPA is ŌĆ£ + GPAs[Highest_GPA_Index] + ŌĆ£ it is at position ŌĆ£ + Highest_GPA_Index┬Č Row >= 20 Row ŌåÉ Row + 1
View full slide show hybrid incr/absolute encoders ŌĆó add more information to index channel to reduce amount of seeking required to find reference position. ŌĆó interface requires lots of wires (parallel) or a special comm. protocol
View full slide show Slightly Less Simple Loops (C) for (index = 0; index < length; index++) { dst[index] = pow(src1[index], src2[index]); } for (index = 0; index < length; index++) { dst[index] = src1[index] % src2[index]; } for (index = 0; index < length; index++) { dst[index] = sqrt(src[index]); } for (index = 0; index < length; index++) { dst[index] = cos(src[index]); } for (index = 0; index < length; index++) { dst[index] = exp(src[index]); } for (index = 0; index < length; index++) { dst[index] = log(src[index]); } Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 48
View full slide show View full slide show Some Simple Loops (C) for (index = 0; index < length; index++) { dst[index] = src1[index] + src2[index]; } for (index = 0; index < length; index++) { dst[index] = src1[index] - src2[index]; } for (index = 0; index < length; index++) { dst[index] = src1[index] * src2[index]; } for (index = 0; index < length; index++) { dst[index] = src1[index] / src2[index]; } for (index = 0; index < length; index++) { sum = sum + src[index]; } Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 46
View full slide show Jagged arrays ’ü» Implemented as arrays of arrays , 4 index type index lower bound index upper bound address address , 3 , 3 index type index type index lower bound index lower bound index upper bound index upper bound address address , 7 , 7 index type index type index lower bound index lower bound index upper bound index upper bound address address , 4 , 4 index type index type index lower bound index lower bound index upper bound index upper bound address address , 5 , 5 index type index type index lower bound index lower bound index upper bound index upper bound address address 24
View full slide show A Very Complicated Loop lot = 0.0 DO index = 1, length lot = lot + & src1(index) * src2(index) + & src3(index) * src4(index) + & (src1(index) + src2(index)) * & (src3(index) + src4(index)) * & (src1(index) - src2(index)) * & (src3(index) - src4(index)) * & (src1(index) - src3(index) + & src2(index) - src4(index)) * & (src1(index) + src3(index) & src2(index) + src4(index)) + & (src1(index) * src3(index)) + & (src2(index) * src4(index)) END DO & & & & & & & & & & & & 24 arithmetic ops per iteration 4 memory/cache loads per iteration Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 63
View full slide show Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words wordsWordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words wordsWordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MOR AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words Wordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words wordsWordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words wordsWordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words wordsWordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words wordsWordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words wordsWordswords words MOREWORDS WORDSWordswords words MOREWORDS WORDS MORE AND MORE WORDS WORDS words worsds wordswordswords WORDS WORDS more words long words hosrt words words
View full slide show Why Synchronize? Synchronizing is necessary when the code that follows a parallel section needs all threads to have their final answers. #pragma omp parallel for for (index = 0; index < length; index++) { x[index] = index / 1024.0; if ((index / 1000) < 1) { y[index] = log(x[index]); } else { y[index] = x[index] + 2; } } /* Need to synchronize here! */ for (index = 0; index < length; index++) { z[index] = y[index] + y[length ŌĆō index + 1]; } Parallel Programming: Shared Memory Parallel OK Supercomputing Symposium, Tue Oct 11 2011 76
View full slide show View full slide show Reading Array Values Using for Loop #1 #include int main () { /* main */ const int z_length = 6; const int program_success_code = 0; float z[z_length], z_squared[z_length]; int index; for (index = 0; index < z_length; index++) { printf("Input z[%d]:\n", index); scanf("%f", &z[index]); } /* for index */ for (index = 0; index < z_length; index++) { z_squared[index] = z[index] * z[index]; } /* for index */ for (index = 0; index < z_length; index++) { printf("%19.7f^2 = %19.7f\n", z[index], z_squared[index]); } /* for index */ return program_success_code; ŌĆ£Use at least 19 spaces total, } /* main */ 7 of which are to the right of the decimal point.ŌĆØ Array Lesson 2 2 CS1313 Spring 2019
View full slide show Named Constants as Loop Bounds #1 #include int main () { /* main */ const int z_length = 6; const int lower_bound = 0; const int program_success_code = 0; float z[z_length], z_squared[z_length]; int index; for (index = lower_bound; index < z_length; index++) { printf("Input z[%d]:\n", index); scanf("%f", &z[index]); } /* for index */ for (index = lower_bound; index < z_length; index++) { z_squared[index] = z[index] * z[index]; } /* for index */ for (index = lower_bound; index < z_length; index++) { printf("%19.7f^2 = %19.7f\n", z[index], z_squared[index]); } /* for index */ return program_success_code; } /* main */ Array Lesson 2 CS1313 Spring 2019 11
View full slide show Slightly Less Simple Loops (F90) DO index = 1, length dst(index) = src1(index) ** src2(index) !! src1 ^ src2 END DO DO index = 1, length dst(index) = MOD(src1(index), src2(index)) END DO DO index = 1, length dst(index) = SQRT(src(index)) END DO DO index = 1, length dst(index) = COS(src(index)) END DO DO index = 1, length dst(index) = EXP(src(index)) END DO DO index = 1, length dst(index) = LOG(src(index)) END DO Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 47
View full slide show Some Complicated Loops DO index = 1, length madd (or FMA): dst(index) = src1(index) + 5.0 * src2(index) mult then add END DO (2 ops) dot = 0 DO index = 1, length dot product dot = dot + src1(index) * src2(index) (2 ops) END DO DO index = 1, length dst(index) = src1(index) * src2(index) + & & src3(index) * src4(index) END DO from our example (3 ops) DO index = 1, length diff12 = src1(index) - src2(index) Euclidean distance (6 ops) diff34 = src3(index) - src4(index) dst(index) = SQRT(diff12 * diff12 + diff34 * diff34) END DO Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 62
View full slide show Why Synchronize? Synchronizing is necessary when the code that follows a parallel section needs all threads to have their final answers. #pragma omp parallel for for (index = 0; index < length; index++) { x[index] = index / 1024.0; if ((index / 1000) < 1) { y[index] = log(x[index]); } else { y[index] = x[index] + 2; } } /* Need to synchronize here! */ for (index = 0; index < length; index++) { z[index] = y[index] + y[length ŌĆō index + 1]; } Sponsored by DOD HPCMP, SC11/ACM, NCSI and OK EPSCoR Shared Memory Multithreading Intermediate Parallel, July 31 ŌĆō Aug 6 2011 86
View full slide show Quadrature Shaft Encoding ŌĆó Basic shaft encoding method: measures how far an axle rotates and its speed, but cannot tell when the axle changes direction ŌĆó Quadrature Shaft Encoding: measures precise rotation of axles and velocity; maintains accurate counts even when the axleŌĆÖs direction of rotation changes ŌĆó Applications: ŌĆō Position monitoring of trapped systems, where the mechanics of a system limit travel between known stop positions, e.g., rotary robot arms, where encoders are used to measure joint angles, and Cartesian robots, where the rotation of a long worm screw moves a rack back and forth ŌĆō Measure the motion of robot wheels, as part of dead-reckoning robot positioning systems. By accumulating the result of a robotŌĆÖs wheels driving it along a surface, an estimate of overall translational movement can be made. A pair of encoders is used on a single shaft. The encoders are aligned so that their two data streams are one quarter cycle (90 deg.) out of phase. When rapidly sampling the data from the two encoders, only one of the encoders will change state at a time. Which encoder changes determines the direction that the shaft is rotating. Copyright Prentice Hall, 2001 12
View full slide show Reading Array on One Line of Input #2 #include int main () { /* main */ const int z_length = 6; const int program_success_code = 0; float z[z_length], z_squared[z_length]; int index; printf("Input all %d values of z:\n", z_length); for (index = 0; index < 6; index++) { scanf("%f", &z[index]); } /* for index */ for (index = 0; index < 6; index++) { z_squared[index] = z[index] * z[index]; } /* for index */ for (index = 0; index < 6; index++) { printf("%19.7f^2 = %19.7f\n", z[index], z_squared[index]); } /* for index */ return program_success_code; } /* main */ Array Lesson 2 CS1313 Spring 2019 8
View full slide show