Ejemplo n.º 1
0
/**
 * Returns true if two clocks form a zero cycle in a priced DBM.
 *
 * @param  pdbm is a closed priced DBM of dimension \a dim.
 * @param  dim  is the dimension of \a pdbm.
 * @param  i    is the index of a clock
 * @param  j    is the index of a clock
 * @return True if and only if \a i and \a j�form a zero cycle in \a pdbm.
 */
static bool pdbm_areOnZeroCycle(const PDBM pdbm, cindex_t dim, cindex_t i, cindex_t j)
{
    assert(pdbm && dim && i < dim && j < dim && i != j);

    raw_t *dbm = pdbm_matrix(pdbm); 
    return (dbm_raw2bound(DBM(i, j)) == -dbm_raw2bound(DBM(j, i)));
}
Ejemplo n.º 2
0
static bool isRedundant(
    const raw_t *dbm, cindex_t dim, cindex_t i, cindex_t j, cindex_t *next)
{
    assert(dbm && dim && i < dim && j < dim && next);

    raw_t bij, bik, bkj;
    cindex_t k;

    bij = DBM(i, j);
    if (i != j && bij < dbm_LS_INFINITY)
    {
        for (k = 0; k < dim; k++)
        {
            if (i != k && j != k && !next[k])
            {
                bik = DBM(i, k);
                bkj = DBM(k, j);
                if (bik < dbm_LS_INFINITY &&
                    bkj < dbm_LS_INFINITY &&
                    bij >= dbm_addFiniteFinite(bik, bkj))
                {
                    return true;
                }
            }
        }
        return false;
    }
    return true;
}
Ejemplo n.º 3
0
void pdbm_getOffset(const PDBM pdbm, cindex_t dim, int32_t *valuation) 
{
    assert(pdbm && dim && valuation);

    raw_t *dbm = pdbm_matrix(pdbm);
    for (uint32_t i = 1; i < dim; i++) 
    {
        assert(dbm_raw2bound(DBM(0, i)) <= 0);
        valuation[i] = -dbm_raw2bound(DBM(0, i));
    }
}
Ejemplo n.º 4
0
/**
 * Find the zero cycles of a DBM. The zero cycles are represented by
 * an array which for each clock contains the next element on the zero
 * cycle, or * 0 if the clock is the last element. I.e. next[i] is the
 * index of the next clock on a zero cycle with i. 
 *
 * @post forall i.next[i] == 0 || next[i] > i
 */
static void dbm_findZeroCycles(const raw_t *dbm, cindex_t dim, cindex_t *next)
{
    cindex_t i, j;

    for (i = 0; i < dim; i++)
    {
        next[i] = 0;
        for (j = i + 1; j < dim; j++)
        {
            if (dbm_raw2bound(DBM(i, j)) == -dbm_raw2bound(DBM(j, i)))
            {
                next[i]= j;
                break;
            }
        }
    }
}
Ejemplo n.º 5
0
void test_diagonalExtrapolateMaxBounds(raw_t *dbm, cindex_t dim, const int32_t *max)
{
    cindex_t i,j;
    assert(dbm && dim > 0 && max);

    /* other rows
     */
    for (i = 1; i < dim; ++i)
    {
        for (j = 0; j < dim; ++j)
        {
            if (i != j &&
                (dbm_raw2bound(DBM(0,i)) < -max[i] ||
                 dbm_raw2bound(DBM(0,j)) < -max[j] ||
                 dbm_raw2bound(DBM(i,j)) >  max[i]))
            {
                DBM(i,j) = dbm_LS_INFINITY;
            }
        }
    }

    /* 1st row */
    for (j = 1; j < dim; ++j)
    {
        assert(dbm_raw2bound(DBM(0,j)) <= max[0]);

        if (dbm_raw2bound(DBM(0,j)) < -max[j])
        {
            DBM(0,j) = (max[j] >= 0
                        ? dbm_bound2raw(-max[j], dbm_STRICT)
                        : dbm_LE_ZERO);
        }
    }
    dbm_close(dbm, dim);
}
Ejemplo n.º 6
0
void test_diagonalExtrapolateLUBounds(raw_t *dbm, cindex_t dim,
                                     const int32_t *lower, const int32_t *upper)
{
    cindex_t i,j;
    assert(dbm && dim > 0 && lower && upper);

    /* other rows */
    for (i = 1; i < dim; ++i)
    {
        for (j = 0; j < dim; ++j)
        {
            if (i != j &&
                (dbm_raw2bound(DBM(i,j)) >  lower[i] ||
                 dbm_raw2bound(DBM(0,i)) < -lower[i] ||
                 dbm_raw2bound(DBM(0,j)) < -upper[j]))
            {
                DBM(i,j) = dbm_LS_INFINITY;
            }
        }
    }

    /* 1st row */
    for (j = 1; j < dim; ++j)
    {
        assert(dbm_raw2bound(DBM(0,j))<= lower[0]);
        
        if (dbm_raw2bound(DBM(0,j)) < -upper[j])
        {
            DBM(0, j) = (upper[j] >= 0 
                         ? dbm_bound2raw(-upper[j], dbm_STRICT)
                         : dbm_LE_ZERO);
        }
    }
    dbm_close(dbm, dim);
}
Ejemplo n.º 7
0
bool pdbm_findNextZeroCycle(const PDBM pdbm, cindex_t dim, cindex_t x, cindex_t *out)
{
    assert(pdbm && dim && x < dim && out);

    const raw_t *dbm    = pdbm_matrix(pdbm);
    cindex_t i = *out;

    while (i < dim)
    {
        if (i != x && dbm_raw2bound(DBM(i, x)) == -dbm_raw2bound(DBM(x, i)))
        {
            *out = i;
            return true;
        }
        i++;
    }
    *out = i;

    return false;
}
Ejemplo n.º 8
0
int32_t pdbm_getInfimumValuation(
    const PDBM pdbm, cindex_t dim, int32_t *valuation, const bool *free) 
{
    assert(pdbm && dim && valuation);
    assert(pdbm_isValid(pdbm, dim));

    raw_t copy[dim * dim];
    raw_t *dbm = pdbm_matrix(pdbm);
    int32_t *rates = pdbm_rates(pdbm);

    /* Compute the cost of the origin. 
     */
    int32_t cost = pdbm_cost(pdbm);
    for (uint32_t i = 1; i < dim; i++)
    {
        cost -= rates[i] * -dbm_raw2bound(DBM(0, i));
    }

    /* If not all clocks are free, then restrict a copy of the DBM
     * according to the valuation given.
     */
    if (free)
    {
        dbm_copy(copy, dbm, dim);
        dbm = copy;

        for (uint32_t i = 1; i < dim; i++)
        {
            if (!free[i])
            {
                constraint_t con[2] =
                    {
                        dbm_constraint(i, 0, valuation[i], dbm_WEAK),
                        dbm_constraint(0, i, -valuation[i], dbm_WEAK)
                    };
                if (!dbm_constrainN(dbm, dim, con, 2))
                {
                    throw std::out_of_range("No such evaluation exists");
                }
            }
        }
    }

    pdbm_infimum(dbm, dim, pdbm_cost(pdbm), rates, valuation);
    for (uint32_t i = 0; i < dim; i++)
    {
        cost += rates[i] * valuation[i];
    }

    ASSERT(cost == pdbm_getCostOfValuation(pdbm, dim, valuation),
           pdbm_print(stderr, pdbm, dim));
    return cost;
}
Ejemplo n.º 9
0
bool pdbm_constrainN(
    PDBM &pdbm, cindex_t dim, const constraint_t *constraints, size_t n)
{
    assert(pdbm && dim);

    raw_t *dbm = pdbm_matrix(pdbm);

    for (; n; n--, constraints++)
    {
        if (constraints->value < DBM(constraints->i, constraints->j))
        {
            pdbm_prepare(pdbm, dim);

            dbm = pdbm_matrix(pdbm);
            int32_t cost = pdbm_cost(pdbm);
            int32_t *rates = pdbm_rates(pdbm);

            for (uint32_t k = 1; k < dim; k++) 
            {
                cost += rates[k] * dbm_raw2bound(DBM(0, k));
            }
            
            if (dbm_constrainN(dbm, dim, constraints, n) == FALSE)
            {
                return false;
            }
            
            for (uint32_t k = 1; k < dim; k++) 
            {
                cost -= rates[k] * dbm_raw2bound(DBM(0, k));
            }

            pdbm_cost(pdbm) = cost;
            pdbm_cache(pdbm) = INVALID;
            break;
        }
    }
    return true;
}
Ejemplo n.º 10
0
int32_t pdbm_getCostOfVertex(const PDBM pdbm, cindex_t dim, const int32_t *valuation)
{
    assert(pdbm && dim && valuation);
    assert(pdbm_containsIntWeakly(pdbm, dim, valuation));

    raw_t  *dbm  = pdbm_matrix(pdbm);
    int32_t cost = pdbm_cost(pdbm);
    for (uint32_t i = 1; i < dim; i++)
    {
        cost += (valuation[i] - -dbm_raw2bound(DBM(0, i))) * pdbm_rates(pdbm)[i];
    }
    return cost;
}
Ejemplo n.º 11
0
void pdbm_freeDown(PDBM &pdbm, cindex_t dim, cindex_t index)
{
    assert(pdbm_rates(pdbm)[index] <= 0);

    pdbm_prepare(pdbm, dim);

    raw_t *dbm = pdbm_matrix(pdbm);    

    /* Move offset point.
     */
    int32_t rate = pdbm_rates(pdbm)[index];
    int32_t bound = -dbm_raw2bound(DBM(0, index));
    pdbm_cost(pdbm) -= bound * rate;

    /* Stretch down.
     */
    dbm_freeDown(dbm, dim, index);
}
Ejemplo n.º 12
0
static bool isPointIncludedWeakly(const int32_t *pt, const raw_t *dbm, cindex_t dim)
{
    cindex_t i, j;
    assert(pt && dbm && dim);

    for (i = 0; i < dim; ++i)
    {
        for (j = 0; j < dim; ++j)
        {
            if (pt[i] - pt[j] > dbm_raw2bound(DBM(i,j)))
            {
                return false;
            }
        }
    }

    return true;
}
Ejemplo n.º 13
0
/* Call print raw on all constraints.
 */
std::ostream& dbm_cppPrint(std::ostream& out, const raw_t *dbm, cindex_t dim)
{
    cindex_t i,j;
    
    if (dbm)
    {
        for (i = 0; i < dim; ++i)
        {
            out << _print_prefix;
            for (j = 0; j < dim; ++j)
            {
                dbm_cppPrintRaw(out, DBM(i,j)) << '\t';
            }
            out << "\\\n";
        }
    }

    return out;
}
Ejemplo n.º 14
0

#include "Python.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "gdbm.h"

#ifdef WIN32
#include "gdbmerrno.h"
extern const char * gdbm_strerror(gdbm_error);
#endif

static char gdbmmodule__doc__[] = "\
This module provides an interface to the GNU DBM (GDBM) library.\n\
\n\
This module is quite similar to the dbm module, but uses GDBM instead to\n\
provide some additional functionality. Please note that the file formats\n\
created by GDBM and dbm are incompatible. \n\
\n\
GDBM objects behave like mappings (dictionaries), except that keys and\n\
values are always strings. Printing a GDBM object doesn't print the\n\
keys and values, and the items() and values() methods are not\n\
supported.";

typedef struct {
	PyObject_HEAD
	int di_size;	/* -1 means recompute */
	GDBM_FILE di_dbm;
} dbmobject;
Ejemplo n.º 15
0
bool pdbm_constrain1(
    PDBM &pdbm, cindex_t dim, cindex_t i, cindex_t j, raw_t constraint)
{
    assert(pdbm && dim && i < dim && j < dim);

    raw_t *dbm = pdbm_matrix(pdbm);

    /* Check if entry is already tighter than constraint.
     */
    if (constraint >= DBM(i, j))
    {
        return true;
    }

    /* If constraint will make DBM empty, then mark it as empty.
     */
    if (dbm_negRaw(constraint) >= DBM(j, i))
    {
        if (pdbm_count(pdbm) > 1)
        {
            pdbm_decRef(pdbm);
            pdbm = NULL;
        }
        else
        {
            DBM(i,j) = constraint;
            DBM(0,0) = -1; /* consistent with isEmpty */
        }
        return false;
    }

    /* If DBM is shared, copy it first.
     */
    pdbm_prepare(pdbm, dim);
    dbm = pdbm_matrix(pdbm);

    /* Compute the cost at the origin.
     */
    int32_t cost = pdbm_cost(pdbm);
    int32_t *rates = pdbm_rates(pdbm);    
    for (uint32_t k = 1; k < dim; k++) 
    {
        cost += rates[k] * dbm_raw2bound(DBM(0, k));
    }

    /* Apply the constraint.
     */
    DBM(i,j) = constraint;    
    dbm_closeij(dbm, dim, i, j);

    /* Compute the cost at the new offset point and invalidate the
     * cache.
     */
    for (uint32_t k = 1; k < dim; k++) 
    {
        cost -= rates[k] * dbm_raw2bound(DBM(0, k));
    }
    pdbm_cost(pdbm) = cost;
    pdbm_cache(pdbm) = INVALID;

    assertx(pdbm_isValid(pdbm, dim));
    return true;
}
Ejemplo n.º 16
0
int main(int argc, char **argv)
{
	int rank, size, i, j;
	int table[MAX_PROCESSES][MAX_PROCESSES];
	int errors = 0;
	int block_size, begin_row, end_row, send_count, recv_count;

	DBM("calling MPI_Init\n");
	MPI_Init(&argc, &argv);
	DBM("calling MPI_Comm_rank\n");
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	DBM("calling MPI_Comm_size\n");
	MPI_Comm_size(MPI_COMM_WORLD, &size);

	for (i = 0; i < MAX_PROCESSES; i++)
		for (j = 0; j < MAX_PROCESSES; j++)
			table[i][j]=-1;

	/* Determine what rows are my responsibility */
	block_size = MAX_PROCESSES / size;
	begin_row  = rank * block_size;
	end_row    = (rank + 1) * block_size;
	send_count = block_size * MAX_PROCESSES;
	recv_count = send_count;

	/* A maximum of MAX_PROCESSES processes can participate */
	if (size > MAX_PROCESSES) {
		fprintf(stderr, "Number of processors is maximal %d\n",
				MAX_PROCESSES);
		DBM("calling MPI_Abort\n");
		MPI_Abort(MPI_COMM_WORLD, 1);
	}

	

	/* Paint my rows my color */
	for (i = begin_row; i < end_row ;i++)
		for (j = 0; j < MAX_PROCESSES; j++)
			table[i][j] = rank + 10;

#ifdef PRINTTABLE
	/* print table */
	printf("\n");
	for (i = 0; i < MAX_PROCESSES; i++)
	{
		for (j = 0; j < MAX_PROCESSES; j++)
			printf("%4d", table[i][j]);
		printf("\n");
	}
#endif
	DBM("starting algather\n");
	/* Everybody gets the gathered table */
	MPI_Allgather(&table[begin_row][0], send_count, MPI_INT,
			&table[0][0],         recv_count, MPI_INT,
			MPI_COMM_WORLD);

	/* Everybody should have the same table now.
	 * This test does not in any way guarantee there are no errors.
	 * Print out a table or devise a smart test to make sure it's correct */
#ifdef PRINTTABLE
	/* print table */
	printf("\n");
	for (i = 0; i < MAX_PROCESSES; i++)
	{
		for (j = 0; j < MAX_PROCESSES; j++)
			printf("%4d", table[i][j]);
		printf("\n");
	}
#endif

	for (i = 0; i < MAX_PROCESSES; i++) {
		if (table[i][0] - table[i][MAX_PROCESSES - 1] !=0)
		{
			Test_Failed(NULL);
			printf("error at i=%d\n", i);
		}
	}

	Test_Waitforall();
	errors = Test_Global_Summary();
	MPI_Finalize();

	return errors;
}
Ejemplo n.º 17
0
int main(int argc, char **argv)
{
    int rank, size, i, j;
    int table[MAX_PROCESSES][MAX_PROCESSES];
    int errors = 0;
    int displs[MAX_PROCESSES];
    int recv_counts[MAX_PROCESSES];
    int block_size, begin_row, end_row, send_count;

    for(i = 1; i < MAX_PROCESSES; i++)
        for(j = 1; j < MAX_PROCESSES; j++)
            table[i][j] = 0;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    /* Determine what rows are my responsibility */
    block_size = MAX_PROCESSES / size;
    begin_row  = rank * block_size;
    end_row    = (rank + 1) * block_size;
    send_count = block_size * MAX_PROCESSES;

    /* A maximum of MAX_PROCESSES processes can participate */
    if (size > MAX_PROCESSES) {
        fprintf(stderr, "Number of processors is maximum %d\n",
                MAX_PROCESSES);
        fflush(stderr);
        DBM("calling MPI_Abort\n");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }



    /* Fill in the displacements and recv_counts */
    for (i = 0; i < size; i++) {
        displs[i]      = i * block_size * MAX_PROCESSES;
        recv_counts[i] = send_count;
    }

    /* Paint my rows my color */
    for (i = begin_row; i < end_row; i++)
        for (j = 0; j < MAX_PROCESSES; j++)
            table[i][j] = rank + 10;

    /* Everybody gets the gathered data */
    MPI_Allgatherv(&table[begin_row][0], send_count, MPI_INT,
                   &table[0][0], recv_counts, displs, MPI_INT,
                   MPI_COMM_WORLD);

    /* Everybody should have the same table now.
     *
     * The entries are:
     *  Table[i][j] = i / block_size + 10;
     */
    for (i = 0; i < size; i++)
        if (table[i][0] - table[i][MAX_PROCESSES - 1] != 0)
            Test_Failed(NULL);

    for (i = 0; i < size; i++)
        for (j = 0; j < MAX_PROCESSES; j++)
            if (table[i][j] != i / block_size + 10)
                Test_Failed(NULL);

    errors = Test_Global_Summary();
    if (errors) {
        /* Print out table if there are any errors */
        for (i = 0; i < size; i++) {
            printf("\n");
            for (j = 0; j < MAX_PROCESSES; j++)
                printf("  %d", table[i][j]);
        }
        printf("\n");
    }

    Test_Waitforall();
    MPI_Finalize();
    return errors;
}