Next: , Previous: , Up: Backend creation   [Contents][Index]


6.2.2 A minimal C-based backend

A backend derived from codegen_c_generic.py starts by defining an NCPTL_CodeGen child class that inherits much of its functionality from the parent NCPTL_CodeGen class. There are only two items that a C-based backend must define: backend_name, the name of the backend in the form ‘c_library’; and, backend_desc , a brief phrase describing the backend. (These are used for error messages and file comments.) Also, a backend’s __init__ method must accept an options parameter, which is given a list of command-line parameters not recognized by ncptl.py. After NCPTL_CodeGen ’s __init__ method processes the entries in options that it recognizes, it should pass the remaining options to its parent class’s __init__ method for further processing. (For proper initialization, the parent class’s __init__ method must be called, even if there are no remaining options to process.)

The following is the complete source code to a minimal coNCePTuaL backend. This backend, codegen_c_seq.py , supports only sequential coNCePTuaL programs (e.g., ‘TASK 0 OUTPUTS "Hello, world!"’); any attempt to use communication statements (see Communication statements) will result in a compile-time error.

#! /usr/bin/env python

#######################################################
# Code generation module for the coNCePTuaL language: #
# Minimal C-based backend -- all communication        #
# operations result in a compiler error               #
#                                                     #
# By Scott Pakin <pakin@lanl.gov>                     #
#######################################################

import codegen_c_generic

class NCPTL_CodeGen(codegen_c_generic.NCPTL_CodeGen):
    def __init__(self, options):
        "Initialize the sequential C code generation module."
        self.backend_name = "c_seq"
        self.backend_desc = "C, sequential code only"
        codegen_c_generic.NCPTL_CodeGen.__init__(self, options)

        # We don't have our own command-line options but we handle
        # --help, nevertheless.
        for arg in range(0, len(options)):
            if options[arg] == "--help":
                # Output a help message.
                self.show_help()
                raise SystemExit, 0

The c_seq backend can be used like any other:

ncptl --backend=c_seq \
  --program='For each i in {10, ..., 1} task 0 outputs i.' | \
  indent > myprogram.c

( codegen_c_generic.py outputs unindented code, deferring attractive formatting to the Unix indent utility.)

One sequential construct the c_seq backend does not support is randomness, as needed by A RANDOM PROCESSOR (see Reordering task IDs) and A RANDOM TASK (see Binding variables). codegen_c_generic.py cannot support randomness itself because doing so requires broadcasting the seed for the random-number generator to all tasks. Broadcasting requires messaging-layer support, which a derived backend provides through the code_def_init_reseed_BCAST hook (see Hook methods). For the sequential backend presented above, a broadcast can be implemented as a no-op:

def code_def_init_reseed_BCAST(self, localvars):
    '"Broadcast" a random-number seed to all tasks.'
    return []

In fact, that same do-nothing hook method is used by the c_udgram backend. c_udgram seeds the random-number generator before calling fork(), thereby ensuring that all tasks have the same seed without requiring an explicit broadcast.


Next: , Previous: , Up: Backend creation   [Contents][Index]

Scott Pakin, pakin@lanl.gov