Previous: , Up: Header declarations   [Contents][Index]


4.9.3 Backend-specific declarations

The BACKEND EXECUTES statement (see Injecting arbitrary code) provides support for executing non-coNCePTuaL code from a coNCePTuaL program. A related construct, BACKEND DECLARES, provides support for embedding non-coNCePTuaL variable and function declarations in a coNCePTuaL program:

<backend_decl> ::= THE BACKEND DECLARES <string>

Like BACKEND EXECUTES, BACKEND DECLARES produces nonportable code. Its use is therefore strongly discouraged. However, BACKEND DECLARES and BACKEND EXECUTES together help ensure that all of the target language/library’s features are available to coNCePTuaL.

The following example uses BACKEND DECLARES to declare a C global variable and two C functions that access that variable:

THE BACKEND DECLARES "
int tally = 0;

void increment_tally (void)
{
  tally++;
}

void show_tally (char *msg)
{
  printf(\"%s%d.\\n\", msg, tally);
}
".

FOR 3 REPETITIONS PLUS 2 WARMUP REPETITIONS {
  ALL TASKS src SEND A 512-BYTE MESSAGE TO TASK src+1 THEN
  ALL TASKS BACKEND EXECUTE "increment_tally();"
} THEN
TASK 0 BACKEND EXECUTES "show_tally(\"Tally is\\n==> \");".

The preceding code works only when using a C-based backend such as c_mpi or c_udgram. Eliciting the same behavior from a Python-based backend such as interpret or latex_vis requires a complete rewrite of the coNCePTuaL code:

THE BACKEND DECLARES "
global tally
tally = 0

def increment_tally():
    global tally
    tally = tally + 1

def show_tally(msg):
    global tally
    print \"%s%d.\\n\" % (msg, tally)
"

FOR 3 REPETITIONS PLUS 2 WARMUP REPETITIONS {
  ALL TASKS src SEND A 512-BYTE MESSAGE TO TASK src+1 THEN
  ALL TASKS BACKEND EXECUTE "increment_tally()"
} THEN
TASK 0 BACKEND EXECUTES "show_tally(\"Tally is\\n==> \")".

It is because of this need to rewrite programs for each set of backends that BACKEND DECLARES and BACKEND EXECUTES should be used only when absolutely necessary.


Previous: , Up: Header declarations   [Contents][Index]

Scott Pakin, pakin@lanl.gov