Programming problems - PowerPoint PPT Presentation

About This Presentation
Title:

Programming problems

Description:

Programming problems These s will give few tips for avoiding the common problems encountered during initial experience programming in Cn language. – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 6
Provided by: Soum150
Learn more at: https://www.cs.kent.edu
Category:

less

Transcript and Presenter's Notes

Title: Programming problems


1
Programming problems
  • These slides will give few tips for avoiding the
    common problems encountered during initial
    experience programming in Cn language.

2
Effects of function return type
  • Care must be taken when we are using return
    inside a function.
  • Multiplicity specifiers can be used with the
    arguments and the return type of a function. The
    return type defines multiplicity of the function
    that is, a function returning a poly type is
    referred to as a poly function.
  • The multiplicity of a function affects how
    returns are handled. This is analogous to the way
    conditional statements are handled.
  • A mono function will return immediately a
    return is executed.
  • A poly function will only return once all of the
    PEs have returned a value and all remaining mono
    code has executed. In effect, a poly function
    will execute to the end with the PEs that have
    returned disabled. If there is no mono code, and
    all PEs have returned, then a compiler may be
    able to optimize by returning early.

3
Using mono variables inside poly conditionals
  • The expression used in any flow control statement
    can be either mono or poly. The Cn mono variant
    of these expressions is the same as standard C.
  • In general, if a poly expression is used for flow
    control, then all alternatives will be executed,
    each branch being enabled on a different subset
    of the PEs.
  • This has important implications for mono objects
    that are used inside poly flow control
    statements. Consider the following code snippet
  • poly short penum get_penum()
  • mono int i
  • if (penum lt 32)
  • ... / Do some work /
  • i 0 / Set mono variable /
  • else
  • ... / Do some other work /
  • i 1 / Set mono variable /
  • Result The value of i is initially set to 0 and
    then will be set by 1

4
Mixing mono and poly conditions
  • There are also cases of conditional execution
    where the conditional behavior is not explicit.
    One example is the use of boolean operators,
    typically to combine conditional expressions like
    and
  • Consider the code
  • mono int m
  • poly int p
  • ...
  • if ((m 0) (p 0))
  • Here if m is not zero, then it is known that the
    whole expression is false and the value of p is
    never tested.
  • Again
  • if ((p 0) (m 0))
  • In this case, the value of p is tested first.
    However, if this is false it simply disables the
    execution of further poly code. The processor
    will continue executing mono code and,
    specifically, it will evaluate the second
    expression to determine the value of m. This
    means that the second expression is always
    evaluated and any side-effects of its evaluation
    will always occur.

5
Dereferencing monopoly pointers
  • A mono value can, in general, be assigned to a
    poly variable. One case that can cause confusion
    is dereferencing (implicitly or explicitly) a
    monopoly pointer.
  • In this case, each PEs instance of the pointer
    can point to a different location in mono memory.
    This means that dereferencing the pointer would
    require a complex operation where a different
    memory location is copied to each PE--this can be
    done, but requires the use of library functions
    such as memcpym2p().
  • mono int array96
  • mono int poly p
  • poly int x, n
  • int i
  • for (i 0 i lt 96 i)
  • arrayi i // initialise array contents
  • x i // a legal mono to poly assignment
  • n get_penum() // different value on each PE
  • p array n // calculate a different address
    on each PE
  • x p // illegal explicit dereference
  • x arrayn // illegal (implicit) dereference
  • This is illegal and will be reported as an error
    by the compiler.
Write a Comment
User Comments (0)
About PowerShow.com