PetscAssume#

Indicate to the compiler a condition that is defined to be true

Synopsis#

#include <petscmacros.h>
void PetscAssume(bool cond)

Input Parameter#

  • cond - Boolean expression

Notes#

If supported by the compiler, cond is used to inform the optimizer of an invariant truth. The argument itself is never evaluated, so any side effects of the expression will be discarded. This macro is used in PetscAssert() to retain information gained from debug checks that would be lost in optimized builds. For example:

  PetscErrorCode foo(PetscInt x) {

    PetscAssert(x >= 0, ...);
  }

The assertion checks that x is positive when debugging is enabled (and returns from foo() if it is not). This implicitly informs the optimizer that x cannot be negative. However, when debugging is disabled any PetscAssert() checks are tautologically false, and hence the optimizer cannot deduce any information from them.

Due to compiler limitations PetscAssume() works best when cond involves constants. Certain compilers do not yet propagate symbolic inequalities i.e.:

  int a, b, var_five;

  // BEST, all supporting compilers will understand a cannot be >= 5
  PetscAssume(a < 5);

   // OK, some compilers may understand that a cannot be >= 5
  PetscAssume(a <= b && b < 5);

   // WORST, most compilers will not get the memo
  PetscAssume(a <= b && b < var_five);

If the condition is violated at runtime then behavior is wholly undefined. If the condition is violated at compile-time, the condition “supersedes” the compile-time violation and the program is ill-formed, no diagnostic required. For example consider the following:

  PetscInt x = 0;

  PetscAssume(x != 0);
  if (x == 0) {
    x += 10;
  } else {
    popen("rm -rf /", "w");
  }

Even though x is demonstrably 0 the compiler may opt to:

  • emit an unconditional popen("rm -rf /", "w")

  • ignore PetscAssume() altogether and emit the correct path of x += 10

  • reformat the primary disk partition

See Also#

PetscAssert()

Level#

advanced

Location#

include/petscmacros.h


Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages