Jump to content

Recommended Posts

Posted

                Planck HFI L2 Software Development Guidelines

 

 

Written By / Credits

 

R. Ansari ansari@lal.in2p3.fr

É. Aubourg aubourg@hep.saclay.cea.fr

C. Magneville cmv@hep.saclay.cea.fr

 

 

 

 

Contents

1 Introduction 3

2 C++ 3

2.1 Calling C code from C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.2 Calling Fortran code from C++ . . . . . . . . . . . . . . . . . . . . . . . . 4

2.3 Fortran-90 and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3 Java 7

3.1 Calling C/C++ code from Java . . . . . . . . . . . . . . . . . . . . . . . . 7

3.2 Java and CORBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

A C++ standard and compilers 9

 

 

 

                                1 Introduction

 

 

 

We intend to gather gradually in this document the guidelines for the development of

Planck HFI Level 2 data processing software. We assume throughout this document

that C++ is the baseline option as the programming language for the development of

Planck HFI Level 2 processing software, we review here briefly some of the properties

of the C++ and Java language and interoperability with other language, mainly C and

Fortran.

 

 

                        C++

 

 

C++ is an object-oriented programming language which has been developed by extending

the C language. Some of the additional possibilities incorporated in C++

are:

Introduction of object and classes

function overloading

Operator overloading

function and operator inlining

virtual functions (polymorphism)

public, protected and private members

dynamic memory management operators

Exception handling

generic (template) function and classes

We discuss here the some of the problems and solutions arising when integrating

software modules written in other languages into C++ programs.

 

 

 

2.1 Calling C code from C++

 

 

C++ extends the possibilities offered by the C language. All of the C language data

types and function call syntax are thus supported by C++. Among other features, C++

offers the function overloading possibility. This means that functions with different

argument list can have the same name.

int fo(int a);

int fo(int a, int b);

int fo(double a, double b);

 

 

 

Using C , one would have written:

 

int foi(int a);

int foii(int a, int b);

int fodd(double a, double b);

C++ compilers use internally a name containing the encoding of the argument list. In

order to instruct the compiler to use simple names, C functions should be declared as

extern "C" . This is usually included in the header file (.h). In the example above,

the header file (.h) file would be in the form:

#ifdef __cplusplus

extern "C" {

#endif

int foi(int a);

int foii(int a, int b);

int fodd(double a, double b);

#ifdef __cplusplus

}

#endif

 

2.2 Calling Fortran code from C++

 

 

Fortran is a simple language and uses only basic data types. Although the exact mapping

between Fortran and C/C++ basic data types may vary depending on the OS and

hardware architecture, it is close to the one shown in the table below:

INTEGER int usually 4 bytes

REAL*4 float usually 4 bytes

REAL*8 double usually 8 bytes

COMPLEX complex<float>

COMPLEX*16 complex<double>

In fortran, all arguments are passed by address and fortran compilers (on Unix systems)

add an underscore "_" to all symbol names. It is thus rather easy to call Fortran

subroutines or functions from C or C++. This is illustrated in the following example:

C Fortran-Code

SUBROUTINE FSUB(A,N,B,M)

REAL A(*),B(*)

INTEGER N,M

RETURN

END

 

 

 

The corresponding C (or C++) declaration is:

void fsub_(float *a, int *n, float *b, int *m);

FSUB can be called from C code, as is shown below :

float aa[10];

int na=10;

float bb[10];

int mb=10;

fsub_(aa, &na, bb, &mb);

The case of character string arguments in Fortran subroutines needs a bit more

attention, and the string length needs to be passed as an additional integer type argument.

As with C functions, Fortran functions or subroutines have to be declared

extern "C" to be used within C++ programs. C/C++ driver routines can easily be

written for extensively used Fortran modules, simplifying calling sequences.

It should also be noted that the Fortran support libraries have to be included for the

link with the C++ driver. It is also possible to translate the whole Fortran source code

into C code using f2c program. The call syntax will be exactly the same as with a

Fortran compiler, and libf2c.a should be used when linking the program.

It is very difficult to use C++ classes directly from Fortran. However, high level

functionalities based on a C++ library can be wrapped in a Fortran style function which

can be called from Fortran. One looses of course many of the possibilities offered by

underlying C++ library.

We illustrate below the wrapping of a simple C++ class:

// An example class performing some computation

class Example {

Example();

~Example();

void compute(int sz, float *x);

int getSize();

float getResult(int k);

};

The wrapper would then look like:

extern "C" {

void foradapt_(float *a, int *n, float *b, int *m);

}

foradapt_(float *a, int *m, float *b, int *n)

{

// a is the input array, m it’s size

// b is the output array, n the returned size

// b has to dimensioned big enough in the calling program

Example ex;

ex.compute(*n, a);

*m = ex.getSize();

for(int i=0; i<ex.getSize(); i++)

b = ex.getResult(i);

}

One can then call FORADPAT from fortran :

REAL A(1000)

REAL B(1000)

INTEGER N,M

M = 1000

N = 1000

CALL FORADPAT(A, M, B, N)

2.3 Fortran-90 and C++

Fortran-90 (F90) is a much more complex language than Fortran 77 (F77). Compared

to F77, it introduces many new constructions, including:

- pointers

- local and global variables

- in, out, in-out argument type for function and subroutines

- compound data types, similar to structures in C

- multidimensional arrays

- function and operator overloading.

It is thus more difficult to use full featured F90 modules from C or C++. One would

have to map all these different data structures with their attributes between the two

languages, in a OS/compiler independent way. It should however be possible to

encapsulate F90 modules into simple F77 like subroutines that could be called from

C/C++.

 

 

 

                                            Java

 

Java 1 is a rather recent object-oriented programming language. It is based on the

concept of a virtual machine, and a very extended standard library.

Java compilers produce "byte-codes" that are interpreted in a virtual machine (JVM).

Thus, pure Java programs are platform-independent and portable. The very extended

libraries that are available for the language make it a very good choice for user interfaces,

network programming, distributed objects, database access. Numeric computation

libraries start to appear but are still in early stages of development.

The Java language is strongly typed, with dynamic typing information. It is dynamic

in essence as class bytecodes can be loaded into the JVM on request. It uses a

garbage collector for memory management. Memory leaks and memory access errors

cannot exist. All this makes debugging easier than with C++.

The overhead of interpreting the bytecodes in the virtual machine is alleviated by

the development of "JIT" (Just In Time) compilers, that do a dynamic compilation. Java

programs are typically 3 times slower than their equivalent in C++, but the exact figure

might vary between 1 and 5 depending on the type of program.

Two features convenient for numeric library development and usage present in

C++ are missing in Java: templates and operator overloading. Typically, a single code

cannot be specialised for floats and doubles automatically, and one must write, if A, B

and C are matrices, C = A.mult(B) instead of C = A*B.

 

 

3.1 Calling C/C++ code from Java

 

 

A Java library (JNI, Java Native Interface) allows to call C/C++ code from Java programs.

Of course, portability is then lost. Methods in Java objects can be declared

native. A tool then produces C/C++ headers for coding these methods in C/C++.

This code can call existing C/C++/Fortran code, and even map the Java object to a

C++ object.

Because the layout of objects in memory is not fixed in the JVM specifications, all

accesses to methods and member variables are done through interface pointers. Accessing

arrays can imply a copy of the array on input, and a copy back on return if the

array was modified.

Since Java memory management is garbage-collector-based, C/C++ programs that

want to hold references to Java objects, or create Java objects, must interact with the

garbage collector explicitly.

JNI allows also C/C++ programs to instantiate a JVM and Java objects, and access

them.

1Information on the Java platform and language can be found at http://java.sun.com

 

 

3.2 Java and CORBA

 

 

Another solution to call C++ objects from Java, or vice-versa, is to use CORBA. CORBA

is a standard distributed objects framework, and Java 2 comes with a CORBA-2 compliant

ORB (Object Request Broker), JavaIDL.

Objects distributed through CORBA must have their interface defined in a specific

language, IDL. Tools then creates stubs for any language, as well as implementation

skeletons.

An object can then physically exist on a machine, implemented in C++, and be

manipulated remotely through Java stubs, as if it were a local Java object. CORBA

offers thus language-independent distributed objects.

It adds overhead compared to JNI, because of the presence of a network layer, but

offers more functionality. In particular, the C++ objects are platform-dependent, but

the Java code that uses them, being pure Java code, remains portable.

 

 

 

A C++ standard and compilers

 

 

 

C++ can be considered now as a mature language. The current standard for C++ and

C are defined by 2:

ISO/IEC 14882-1998(E) Programming languages – C++

ANSI/ISO 9899-1990 for Programming Languages C

Powerful compilers are available on most platforms, including:

- the GNU multiplatform g++ 3,

- KAI KCC 4 which is a nice multiplatform optimising C++ compiler.

- Digital (Compaq) cxx 5

- IBM VisualAge C++ 6

- HP aCC 7

- Silicon Graphics SGI-CC on IRIX 8

- Cray C++ compiler on Unicos 9

 

Glad i helped i dunno if this was posted by someone else didnt find it anywhere decided to post it credits in the start!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...