Commit 5710fe83 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

stepper works

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 1213
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent e3052c2d
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.7 2002-12-17 19:19:54 forbrig Exp $
# $Id: Makefile,v 1.8 2002-12-18 17:41:32 forbrig Exp $
#
# Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
#
......@@ -52,7 +52,7 @@ HEADERS=$(shell find . -name \*.h)
# (see below for the configuration of a preinstantiated version of template
# code)
SRC=lib/error.cc dump.cc lib/strided.cc lib/stridedstepper.cc
SRC=lib/error.cc dump.cc lib/strided.cc
#SRC=lib/error.cc dump.cc lib/strided.cc
# test programs are placed in a subdirectory
TESTS=$(wildcard tests/*.cc)
# whereever we find a README, we will use it
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: README.changelog,v 1.3 2002-12-18 09:58:50 forbrig Exp $
* $Id: README.changelog,v 1.4 2002-12-18 17:41:32 forbrig Exp $
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
......@@ -25,6 +25,7 @@
- \b 18/12/2002 (thof)
- defined dump function for Strided
- started with tests/shapetest.cc
- reworked stridedstepper to fit into new concept
- \b 17/12/2002 (thof)
- introduced class Series
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: stridedstepper.cc,v 1.2 2002-12-15 20:48:01 forbrig Exp $
* $Id: stridedstepper.cc,v 1.3 2002-12-18 17:41:33 forbrig Exp $
* \author Thomas Forbriger
* \since 08/12/2002
*
......@@ -13,168 +13,83 @@
*
* REVISIONS and CHANGES
* - 08/12/2002 V1.0 copied from libcontxx
* - 18/12/2002 V1.1 (thof)
* - reorganized the whole thing
* - removed template parameters
* - removed inline template recursion class
* - use carry arithmetic
*
* ============================================================================
*/
#define AFF_STRIDEDSTEPPER_CC_VERSION \
"AFF_STRIDEDSTEPPER_DEF_H V1.0"
"AFF_STRIDEDSTEPPER_DEF_H V1.1"
#define AFF_STRIDEDSTEPPER_CC_CVSID \
"$Id: stridedstepper.cc,v 1.2 2002-12-15 20:48:01 forbrig Exp $"
"$Id: stridedstepper.cc,v 1.3 2002-12-18 17:41:33 forbrig Exp $"
#include<aff/lib/stridedstepper.h>
namespace aff {
namespace util {
template<Tdim N, Tdim M>
class StepperInline {
public:
/*! \brief Index increment with range given by first, last, and stride
*
* \param I array of current index values to multidimensional array
* \param F array of smallest index values that are allowed
* \param L array of largest index values that are allowed
* \param S array of strides
* \return offset to previous linear offset position
* (\p I indicates next position on return)
*
* \sa Stepper
* \sa contxx::shape::Strided
* \sa contxx::shape::DenseStrided
*/
static inline Tsubscript increment(Tsubscript* I,
const Tsubscript* F, const Tsubscript* L, const Tsize* S)
{
if (I[N-M]<L[N-M])
{
I[N-M]++;
return(Tsubscript(S[N-M]));
}
else
{
Tsubscript R=-(I[N-M]-F[N-M])*S[N-M];
I[N-M]=F[N-M];
R+=StepperInline<N, M-1>::increment(I,F,L,S);
return(R);
}
return(0);
}
/*! \brief Index decrement with range given by first, last, and stride
*
* \param I array of current index values to multidimensional array
* \param F array of smallest index values that are allowed
* \param L array of largest index values that are allowed
* \param S array of strides
* \return offset to previous linear offset position
* (\p I indicates next position on return)
*
* \sa Stepper
* \sa contxx::shape::Strided
* \sa contxx::shape::DenseStrided
*/
static inline Tsubscript decrement(Tsubscript* I,
const Tsubscript* F, const Tsubscript* L, const Tsize* S)
{
if (I[N-M]>F[N-M])
{
I[N-M]--;
return(Tsubscript(-S[N-M]));
}
else
{
Tsubscript R=(L[N-M]-I[N-M])*S[N-M];
I[N-M]=L[N-M];
R+=StepperInline<N, M-1>::decrement(I,F,L,S);
return(R);
}
return(0);
}
}; // StepperInline
//! specialization to stop recursion
template<Tdim N>
class StepperInline<N, 0>
{
public:
static inline Tsubscript increment(Tsubscript* I,
const Tsubscript* F, const Tsubscript* L, const Tsize* S)
{ return(0); }
static inline Tsubscript decrement(Tsubscript* I,
const Tsubscript* F, const Tsubscript* L, const Tsize* S)
{ return(0); }
}; // StepperInline
//! initialize
StridedStepper::StridedStepper(const Strided& strided):
Mshape(strided), Mcurrent(strided.first_offset()),
Mindex(strided.first()), Mcarry(0)
{
// calculate Mcarry
for (Tsubscript i=0; i<(Tshape::Mmax_dimen); i++)
{ Mcarry[i]=strided.size(i)*strided.stride(i); }
}
/*======================================================================*/
/*----------------------------------------------------------------------*/
// initialize StridedStepper
template<class Shape, Tdim N>
StridedStepper<Shape, N>::StridedStepper(const Tindex& first,
const Tstrides& size, const Tstrides& stride, const Tsubscript& offset):
Mfirst(first), Mlast(first), Mstride(stride), Moffset(offset),
Mcurrent(0), Mindex(first)
//! next position
StridedStepper& StridedStepper::incr()
{
Tsubscript i=0;
while (i<Tshape::Mmax_dimen)
{
Mcurrent=Moffset;
for (int i=0; i<N; i++)
++Mindex[i];
Mcurrent+=Mshape.stride(i);
if (Mindex[i]>Mshape.last(i))
{
Mlast[i]=Mfirst[i]+size[i]-1;
Mcurrent+=Mindex[i]*Mstride[i];
Mindex[i]=Mshape.first(i);
Mcurrent-=Mcarry[i];
++i;
}
}
/*----------------------------------------------------------------------*/
template<class Shape, Tdim N>
StridedStepper<Shape, N>& StridedStepper<Shape, N>::tofirst()
{
Mcurrent=Moffset;
for (int i=0; i<N; i++)
else
{
Mindex[i]=Mfirst[i];
Mcurrent+=Mindex[i]*Mstride[i];
i=Tshape::Mmax_dimen;
}
return(*this);
}
return(*this);
}
/*----------------------------------------------------------------------*/
template<class Shape, Tdim N>
StridedStepper<Shape, N>& StridedStepper<Shape, N>::tolast()
//! previous position
StridedStepper& StridedStepper::decr()
{
Tsubscript i=0;
while (i<Tshape::Mmax_dimen)
{
Mcurrent=Moffset;
for (int i=0; i<N; i++)
--Mindex[i];
Mcurrent-=Mshape.stride(i);
if (Mindex[i]<Mshape.first(i))
{
Mindex[i]=Mlast[i];
Mcurrent+=Mindex[i]*Mstride[i];
Mindex[i]=Mshape.last(i);
Mcurrent+=Mcarry[i];
++i;
}
else
{
i=Tshape::Mmax_dimen;
}
return(*this);
}
/*----------------------------------------------------------------------*/
template<class Shape, Tdim N>
bool StridedStepper<Shape, N>::more() const
{ return(inline_anysmaller(Mindex, Mlast)); }
/*----------------------------------------------------------------------*/
template<class Shape, Tdim N>
bool StridedStepper<Shape, N>::less() const
{ return(inline_anylarger(Mindex, Mfirst)); }
/*----------------------------------------------------------------------*/
template<class Shape, Tdim N>
StridedStepper<Shape, N>& StridedStepper<Shape, N>::incr()
{
Mcurrent+=StepperInline<N, N>::increment(Mindex.pointer(),
Mfirst.pointer(), Mlast.pointer(), Mstride.pointer());
return(*this);
}
/*----------------------------------------------------------------------*/
template<class Shape, Tdim N>
StridedStepper<Shape, N>& StridedStepper<Shape, N>::decr()
{
Mcurrent+=StepperInline<N, N>::decrement(Mindex.pointer(),
Mfirst.pointer(), Mlast.pointer(), Mstride.pointer());
return(*this);
}
return(*this);
}
} // namespace util
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: stridedstepper.h,v 1.3 2002-12-15 20:48:01 forbrig Exp $
* $Id: stridedstepper.h,v 1.4 2002-12-18 17:41:33 forbrig Exp $
* \author Thomas Forbriger
* \since 08/12/2002
*
......@@ -19,6 +19,8 @@
* - 08/12/2002 V1.0 copied from libcontxx
* - 15/12/2002 V1.1 (thof)
* - never place in namespace prebuilt
* - 18/12/2002 V1.2 (thof)
* - reorganized the whole thing
*
* ============================================================================
*/
......@@ -27,68 +29,72 @@
#ifndef AFF_STRIDEDSTEPPER_H_VERSION
#define AFF_STRIDEDSTEPPER_H_VERSION \
"AFF_STRIDEDSTEPPER,H_H V1.1"
"AFF_STRIDEDSTEPPER,H_H V1.2"
#define AFF_STRIDEDSTEPPER_H_CVSID \
"$Id: stridedstepper.h,v 1.3 2002-12-15 20:48:01 forbrig Exp $"
"$Id: stridedstepper.h,v 1.4 2002-12-18 17:41:33 forbrig Exp $"
#include<aff/lib/types.h>
#include<aff/lib/rawarfun.h>
#include<aff/lib/simplearray.h>
#include<aff/lib/strided.h>
namespace aff {
namespace util {
/*! \brief A stepper for all strided shapes.
*
* \note
* The Stepper holds a reference to the shape. It is designed to be a
* temporary object.
*/
class StridedStepper
{
public:
//! Type of Shape corresponding class
typedef aff::Strided Tshape;
//! type of stride array
typedef typename Shape::Tstrides Tstrides;
typedef Tshape::TIndexVec TIndexVec;
//! type of limit arrays
typedef typename Shape::Tlimits Tindex;
//! Type of Shape corresponding class
typedef Shape Tshape;
typedef Tshape::TSizeVec TSizeVec;
//
//! only non-copy constructor
StridedStepper(const Tindex& first, const Tstrides& size,
const Tstrides& stride, const Tsubscript& offset);
StridedStepper(const Strided& strided);
//! return current index value for Representation access
inline
const Tsubscript& current() const { return(Mcurrent); }
//! return current index vector for array access
inline
const Tindex& index() const { return(Mindex); }
const TIndexVec& index() const { return(Mindex); }
//! return current index vector for array access
const Tsubscript& index(const Tdim& i) const { return(Mindex[i]); }
//! increment offset - return reference to itself
inline
StridedStepper& incr();
//! decrement offset - return reference to itself
inline
StridedStepper& decr();
//! returns true if there are more elements in incr-direction
inline
bool more() const;
bool more() const
{ return(Mcurrent<Mshape.last_offset()); }
//! returns true if there are more elements in decr-direction
inline
bool less() const;
bool less() const
{ return(Mcurrent>Mshape.first_offset()); }
//! set current element index to the first - return reference to itself
StridedStepper& tofirst();
StridedStepper& tofirst()
{
Mindex=Mshape.first();
Mcurrent=Mshape.first_offset();
return(*this);
}
//! set current element index to the last - return reference to itself
StridedStepper& tolast();
StridedStepper& tolast()
{
Mindex=Mshape.last();
Mcurrent=Mshape.last_offset();
return(*this);
}
private:
//! set of smallest index values
const Tindex& Mfirst;
//! set of largest index values
Tindex Mlast;
//! set of strides
Tstrides Mstride;
//! position offset defined by strided shape
const Tsubscript& Moffset;
//! current position
//! hold reference to shape
const Tshape& Mshape;
//! hold current position offset to memory
Tsubscript Mcurrent;
//! current position
Tindex Mindex;
//! hold current index vector
TIndexVec Mindex;
//! hold carry values
TSizeVec Mcarry;
}; // StridedStepper
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: shapetest.cc,v 1.2 2002-12-18 09:58:52 forbrig Exp $
* $Id: shapetest.cc,v 1.3 2002-12-18 17:41:34 forbrig Exp $
* \author Thomas Forbriger
* \date 25/05/2002
*
......@@ -20,11 +20,12 @@
#define AFF_SHAPETEST_CC_VERSION \
"AFF_SHAPETEST_CC V1.1"
#define AFF_SHAPETEST_CC_CVSID \
"$Id: shapetest.cc,v 1.2 2002-12-18 09:58:52 forbrig Exp $"
"$Id: shapetest.cc,v 1.3 2002-12-18 17:41:34 forbrig Exp $"
#include<iostream>
#include<aff/dump.h>
#include<aff/shaper.h>
#include<aff/lib/stridedstepper.h>
using std::cout;
using std::endl;
......@@ -57,6 +58,34 @@ const char* bvalue(const bool& b)
//! print value of a bool
#define BOOLIS( S ) cout << " *** " << #S << ": " << bvalue(S);
//! cycle steppers
void steppers(const Strided& shape, const int& nmax=30)
{
StridedStepper s1(shape), s2(shape);
s2.tolast();
for (int i=0; i<nmax; i++)
{
cout.width(5); cout << i+1;
cout << " ";
cout.width(5); cout << s1.current() << "=[";
for (int j=0; j<Strided::Mmax_dimen; j++)
{
if (j) cout << ", ";
cout.width(3); cout << s1.index(j);
}
cout << "] ";
cout.width(5); cout << s2.current() << "=[";
for (int j=0; j<Strided::Mmax_dimen; j++)
{
if (j) cout << ", ";
cout.width(3); cout << s2.index(j);
}
cout << "]" << endl;
s1.incr();
s2.decr();
}
}
//! test shape class
int main()
{
......@@ -88,6 +117,35 @@ int main()
cout << endl;
DUMP( Strided(Shaper(10)(15)(20)).collapse(1,5) );
cout << endl;
DUMP( Strided(Shaper(11,20)(11,20)(11,20)).shift(1,5) );
cout << endl;
DUMP( Strided(Shaper(11,20)(11,20)(11,20)).setfirst(1,5) );
section("Test stepper");
{
cout << endl << "Basic functionality" << endl;
CODE( Strided shape(Shaper(3)(4)(2)(3)); )
DUMP( shape );
CODE( steppers(shape, shape.size()+10); )
}
{
cout << endl << "Shape with gaps" << endl;
CODE( Strided shape(Shaper(0,9)(0,9)(0,9)(0,9)); )
DUMP( shape.shrink(0,3,5).shrink(1,4,6).shrink(2,5,7).shrink(3,6,8) );
CODE( steppers(shape, shape.size()+10); )
}
{
cout << endl << "Small array" << endl;
CODE( Strided shape(Shaper(3,5,10)(3)); )
DUMP( shape );
CODE( steppers(shape, shape.size()+10); )
}
}
/* ----- END OF shapetest.cc ----- */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment