Sunday, July 02, 2006

Metaprogramming


Metaprogramming


The term "programing a program" reflects metaprogramming. In other words writing a program that writes or maintains other programs. Seem ambiguous: you write a code that generates other code which is actually needed. Thus metaprogramming is generating some thing -actually code- at compile time rather than run time.
metaprogramming is very language dependent.

Article Contents:

1. Uses metaprogramming.
2. Benefits of metaprogramming.
3. Drawbacks of metaprogramming.
4. Examples of metaprogramming.
5. Starter example of metaprogramming.
6. Other template metaprogramming example.
7. Using metaprogramming in numerical analysis.
8. Unrolling loops.

Uses metaprogramming:

1. Generation of look up test tables.
2. Some small functions can be parametrized and used allover the program can be generated by metaprogramming.
3. Unroll looping. of course unrolled loops are more efficient than rolled loops.
4. Other uses you can imagine.

Benefits of metaprogramming:

1. Minimizes code.
2. Achieve more functionality.
3. Provides less effort for programmers and reduces maintenance effort.
4. Meta code is generated at compile time. Thus, results faster programs.

Drawbacks of metaprogramming:

1. Impossible to debug.
2. Hard to trace.
3. Ambiguous to some extent.

Examples of metaprogramming:

1. C/C++ macro processor.
2. C++ templates.
3. M4 processor.

Starter example of metaprogramming:

This small macro can be considered as a metaprogramming:
#define swap(x,y,type){ type __temp = x ; x = y ; y = __temp ; }
int x=4 , y=7 ;
swap(x,y,int) ;

Other template metaprogramming example:

This template calss has an emumeration data type that reserves 3 ^ n where n is the template parameter.
Note that template parameters can be ordinary variables.

// A template class to compute 3 to power n
template
class Pow3 {
public:
enum { result=3*Pow3::result };
};

// Base condition to end the recursion
template<>
class Pow3<0> {
public:
enum { result = 1 };
};

The line:
enum { result=3*Pow3::result };
Assignes result enumeration to result of Pow3 and till recursivly reaches Pow3<0>.

Using metaprogramming in numerical analysis:
Bisection method:

Solve the following equation:
x*x*x +x*x - 6*x = 0

// A template to solve the equation.
template
class Solve
{

public:

// compute the midpoint
enum { mid = (low+high+1)/2 };

// search in the halved interval
// equation is mid*mid*mid +mid*mid - 6*mid
enum { result = ( (mid*mid*mid +mid*mid - 6*mid) > 0 ) ?
Solve::result : Solve::result };

};

template
class Solve
{
public:
enum { result=N};
};


#include

int main(void)
{
std::cout << "Sol of the equation is" <<>::result;
return 0 ;
}

The variable mid is used to compute mid point for bisection method
mid = (low+high+1)/2
while result holds the result.
result = ( (mid*mid*mid +mid*mid - 6*mid) > 0 ) ? Solve::result : Solve::result
This code can't get the zero solution do you know why?
Try use the interval 0,1000 this results in large compilation time but same run time.

Unrolling loops:

// primary template
template
class ADD {
public:
static int result (int* a) {
return *a + ADD::result(a+1);
}
};

// partial specialization as end criteria
class ADD<1> {
public:
static int result (int* a) {
return *a ;
}
};

int main(void)
{
int a[] = { 1,3,5 } ;
std::cout << "Adding 3 elements:" <<>::result(a) ;
return 0 ;
}

Here we used ADD template class to unroll the addition.
Unrolling generally reduces the run time but leads to bigger output file.

Conclusion:

Metaprogramming enhances program efficiency by computing some thing at compilation time rather than runtime. Also it reduces source code size and increases it's functionality.

Links:

http://en.wikipedia.org/wiki/Metaprogramming
http://en.wikipedia.org/wiki/Template_metaprogramming
http://boost-consulting.com/mplbook/
http://www-128.ibm.com/developerworks/linux/library/l-metaprog1.html
http://www-128.ibm.com/developerworks/linux/library/l-metaprog2.html
http://www-128.ibm.com/developerworks/linux/library/l-metaprog3/?ca=dgr-wikiaMetaprogP3

No comments: