The TaskGraph
Meta-programming Library is a C++ package that supports
The library is available at on github.
It is presented in
a talk
and a paper
(however this distribution has a tidied and more typesafe design).
#include <stdio.h>
#include <stdlib.h>
#include <TaskGraph>
using namespace tg;
typedef TaskGraph<int, int> TG_IntResIntArg;
int main( int argc, char *argv[] ) {
TG_IntResIntArg T;
int n = atoi( argv[1] );
taskgraph( TG_IntResIntArg,
T, tuple1(a) ) {
tVar(int, i);
tVar(int, j);
tFor(i, 0, n-1)
{
tFor(j, 0, n-1) {
tPrintf("Iteration i=%d, j=%d\n", i, j);
}
}
tReturn(a + n);
}
InterchangeSettings inter;
inter.firstLoop =
LoopIdentifier ( 1 );
inter.secondLoop =
LoopIdentifier ( 1, 1 );
T.applyOptimisation (
"interchange", &inter );
T.compile( tg::GCC, true );
printf( "T(%d) =
%d\n", n, T(n) );
}
If you run this with input 2, it
prints:
Iteration i=0, j=0
Iteration i=1, j=0
Iteration i=0, j=1
Iteration i=1, j=1
T(2) = 4
At runtime it generates the
following code:
extern int printf(char *, ...);
extern int taskGraph_0(void **);
extern int taskGraph_0(void **params)
{
int *a;
int i;
int j;
static char string0_[23] = "Iteration i=%d, j=%d\n";
a = *params;
for (j = 0; j <= 1; j++)
{
for (i = 0; i <= 1; i++)
{
printf(string0_, i, j);
}
}
return *a + 2;
}
./addc 1
This should print "T(b) = 2"!