Control Flow
The Julia set program also illustrates control flow. The main computation encapsulated in the program object, a complex multiplication and addition, is repeated until a maximum number of iterations is reached or the norm of the complex number grows greater than four. Because the latter condition depends on a computation being performed in the program object, we cannot simply include a C++ for loop here. The for loop would attempt to determine the value of std::norm(z) < 4.0f as a bool, but the value of z is not yet determined because the program is merely being collected, not executed. You can use C++ for loops in program definitions, but their conditions need to be based on nonRapidMind types. For example, it's possible to run a loop that iterates a constant number of times and that unrolls the RapidMind computations it contains.
RapidMind does allow dynamic control flow in programs using constructs such as RM_FOR, RM_IF, and RM_WHILE. These constructs generally behave like their C++ counterparts, but are collected into the program. By letting you use control constructs, RapidMind exposes a Single Program Multiple Data (SPMD) programming model, as opposed to a Single Instruction Multiple Data (SIMD) model. This is one of the reasons why RapidMind has an explicit Program object. In systems where basic operations would be executed on arrays and those operations collected into program objects implicitly, control flow is difficult or impossible to express, reducing the generality of the programming model as well as posing performance problems.
The platform provides a number of other operations that let you express almost any kind of computation. In addition to functions and operators that can be used on values, the platform provides operations that affect entire arrays. For example, array contents can be rearranged using "access patterns" and "collective computations" over an entire array, so that finding the sum of all entries can be accomplished. Table 1 lists some of the types and functions.
Feature | Description |
Types | Value, Array, Program, Bundle, Matrix, Exception, ... |
Arithmetic | +, -, *, /, %, ... |
Trigonometric & Exponential | sin, atan2, exp10, pow, ... |
Geometry | dot, cross, distance, ... |
Logical | <, >, &&, ||, cond, all, any, ... |
Miscellaneous | lerp, join, cast, ... |
Control Flow | RM_FOR, RM_IF, RM_WHILE, RM_DO, ... |
Array Access | [], (), read_data, boundary, adopt_array, ... |
Access Patterns | take, offset, shift, slice, ... |
Virtual Arrays | grid, dice, ... |
Collective | reduce, sum, min, max, count, exists, ... |