Chapter 5.4-5.7

Cn for the working C programmer
5.4 Mixing poly and mono variables
 Legal
poly int x = 1;
int y = 1;
x = x+y;
 Illegal
y = x;
y = x+y;
5.5 Flow control
Cn supports the following flow control statements.
• If statement
• For loops
• While loops
• Do while loops
• Switch statements (mono only)
They are mono or poly based on the test condition for
the flow control statement.
5.5.1 If statments
 Mono or Poly
 Mono is same as in C
 Poly will execute both branches (first true and then
false) and depending on the poly condition the be will
be disabled for one of these two branches.
 With Poly if statements Mono operations are always
executed!!!
5.5.1 Example
Poly short penum = get_penum();
Mono int I;
If (penum < 96)
{
… // do poly work
I=0;
}
Else
{
… // do poly work
I = 1;
}
 When this code is executed the final value of I will be 1
even though none of the PE’s had penum greater then 96.
5.5.2 Loops
 Mono is the same as C
 Poly continues to loop as long as the condition is true
on any PE.
 When a PE evaluates false it is disabled for the rest of
the loop
 As with If statements Mono operations are executed
on every iteration!!!
5.5.3 Goto statment
 The goto statement has been added in version 3.0
 Restriction of the goto statement can not cross a poly
boundary
 E.g. poly boundry would be anywhere a PE might be
disabled such as a poly flow control statement.
 According to the older getting started manual you can
also implement a similar level of control by using the
break and continue statements with labels
5.5.3 Example
For_i:
for (I = o; I < 10000; i++)
{
while(j>100)
{
do
{
…
if (foo == bar)
break for_i;
} while (a != b);
}
}
 This allows you similar functionality to the goto statement.
5.5.4 Switch statements
 Mono only
 Poly can be done the ugly old fashion way using a ton
of if/elseif/else statements.
5.6 Functions
 Cn does not support function pointers.
 Functions with a mono return type are mono functions
 Functions with a poly return type are poly functions
 Mono functions end like in C when a return is executed
 Poly functions only end when all PE’s have executed a
return statement.
 Special case: When a return statement is executed by a PE
in a if statement both branches will still execute before a
check is made to see if all PE’s have executed a return
statement.
5.7 Other features
Data transfer between mono and poly
provided by library functions.




Both mono to poly and poly to mono supported
“memcpym2p” mono to poly transfer
“memcpyp2m” poly to mono transfer
“memcpym2p_strided” mono to poly transfer using strided
mode.
 “memcpyp2m_strided” poly to mono transfer using strided
mode.
 Asynchronous functions also available.
5.7 continued
Cashing I/O
 Normal access by the program to the mono memory is
cached.
 I/O transfers between the PE’s and the mono memory
are not cached.
 “dcache_flush” use this statement to make sure the
cache and the mono memory are consistent when
using a mix of normal and I/O transfers.
5.7 Continued
Semaphores
 A semaphore is a non-negative number and two associated
operations: Signal and Wait.
 A signal is a atomic operation that increases the
semaphores value.
 Wait is a atomic operation that decrements the semaphores
value.
 Valid user semaphore numbers are 0-92
 93-127 are reserved for system use
 An atomic operation is one that can not be interrupted.