//*********************************************************************** // Stenciler.h // authors: Matthew Judd and John Karpovich // mrj2p@virginia.edu and jfk3w@virginia.edu // University of Virginia // December 7, 1992 // // adapted from code by Ambar Sarkar // // compilation: // make -f serial - serial version // make all - parallel MENTAT version // // description: // Class definitions for Stenciler and related objects // // user responsibility: // Write setRowsAndCols() code - this code initializes the number of rows // and columns in the image. It also initializes the window variables to // include the whole image. // // Write getNextStencil() code - this function both returns the next stencil // to apply to the matrix, and decides whether or not to stop applying // filters. // // Write prepareDest() code - this function prepares the destination file // to receive the final image from the stencil_workers. Any information // the destination file needs other than that image must be written here. //*********************************************************************** // **************************************************************************** // // Copyright (c) 1992 // University of Virginia // All Rights Reserved // // This software is the property of the University of Virginia. // This software is provided `as is' and without any express or // implied warrenties. // // Send problems & suggestions to: // Andrew Grimshaw (grimshaw@cs.virginia.edu) // // ************************************************************************** #include #include "matrix_defs.h" #define LEAF 10 #define NODE 20 #define TRUE 1 #define FALSE 0 // ************************************************************************** // This structure is used by get_worker_coordinates() to represent the coord- // inates to a part of that workers' piece of the data. // ************************************************************************** struct rect { int ulr; int ulc; int lrr; int lrc; }; // ************************************************************************** // This structure is used to store a sequential list of stencils to be used // by getNextStencil() to determine the next stencil to send to the workers. // ************************************************************************** struct stencil_seq { stencil *st; stencil_seq *next; }; class mentat_object; // ************************************************************************** // ************************************************************************** // // STENCILER CLASS DEFINITION // // ************************************************************************** // ************************************************************************** #ifndef SERIAL sequential mentat #endif class Stenciler { public: string *srcNm, *destNm; /* Name of source and destination files for matrix data */ stencil_seq *stencilList, *stencilListTail, *currentStencil; /* Pointers to the head and tail members of the list of stencils to be applied */ int numRows, numCols; /* Number of rows and columns in the input matrix. */ int numPieces; /* Maximum number of pieces/workers to be used. User must set this, otherwise only 1 piece/ worker will be used; it will be sequential. */ int maxStencilRows, maxStencilCols; /* Maximum number of rows/cols in any stencil in the stencil list. */ FILE *srcFile, *destFile; /* File pointers to the source and destination files. May be changed if method of read/write changes. User must also change the read, write and init functions for Stenciler and stencil_worker. */ int numRowPieces; /* Number of rows to break matrix into. Initially set to 0 by default, if left as 0 the matrix will be split into numPieces square pieces/workers. If set to one, matrix is broken into numPieces cols. If set to numPieces, matrix will be broken into numPieces rows by 1 col. If set to 1 < nunRows < numPieces, st numPieces is evenly divisible by numRows, then matrix will be split as numRows x numPieces/numRows */ int numColPieces; /* Number of cols to break matrix into. Calculated based on numRows and numPieces */ int ul_row_window, ul_col_window, lr_row_window, lr_col_window; /* Corner coordinates for the portion of the overall matrix where the stencil is to be applied. */ int ulRow, ulCol, lrRow, lrCol; /* Upper left and lower right corner coords for the piece this worker is responsible for. Some portion of this piece will have the stencil applied to it - as determined by the window coords below */ int w_ulRow, w_ulCol, w_lrRow, w_lrCol; /* Upper left and lower right corner coords for the window this worker should apply the stencil to. */ int currStRows, currStCols; /* Size of boundary for current/last stencil applied. */ int matrixRows, matrixCols; /* Total number of rows and columns in matrix. Used in reading from and writing file. */ Stenciler *workers; /* Worker objects that will actually apply the stencil to the matrix window. */ stencil *nextStencil; /* Pointer to the stencil to apply next */ int currIter, endIter; /* number of current and end iterations for iteration. This is provided solely for use by user-defined code - Built-in functions do not use. */ float convVal, testPrevVal; /* the convergence factor and the previous value of the test number for convergence. This is provided solely for use by user-defined code - Built-in functions do not use. */ MATRIX_TYPE *srcArray, *destArray; /* source and destination arrays for our piece of the work. Both include the maximum boundary region. */ int myPieces, myRows, myCols, myPiecesRows, myPiecesCols; int workerType; #ifndef SERIAL long w_elapsed; mentat_timer tm; #endif /* FUNCTIONS */ // These functions are internal only, and should be protected. // At present, mplc has problems doing this. MATRIX_TYPE *get_boundary_top(int stRows); MATRIX_TYPE *get_boundary_bottom(int stRows); MATRIX_TYPE *get_boundary_left(int stCols); MATRIX_TYPE *get_boundary_right(int stCols); MATRIX_TYPE *get_boundary_up_left(int stRows, int stCols); MATRIX_TYPE *get_boundary_up_right(int stRows, int stCols); MATRIX_TYPE *get_boundary_low_left(int stRows, int stCols); MATRIX_TYPE *get_boundary_low_right(int stRows, int stCols); int put_boundary_top(MATRIX_TYPE* st); int put_boundary_bottom(MATRIX_TYPE* st); int put_boundary_left(MATRIX_TYPE* st); int put_boundary_right(MATRIX_TYPE* st); int put_boundary_up_left(MATRIX_TYPE* st); int put_boundary_up_right(MATRIX_TYPE* st); int put_boundary_low_left(MATRIX_TYPE* st); int put_boundary_low_right(MATRIX_TYPE* st); int S_init(string *sNm, string *dNm, rect my_coords, rect win_coords, /* int ulR, int ulC, int lrR, int lrC, */ /* int w_ulR, int w_ulC, int w_lrR, int w_lrC, */ int maxStDim, int mRows, int mCols, int myRws, int myCls); int getMatrix(); int putMatrix(); int doStencilWork(stencil* st); MATRIX_TYPE* getRegion(int ulr, int ulc, int lrr, int lrc); /* extracts a matrix from the workers of the given coords. Coords are the upper left and lower right corners of the bounding rectangle for the matrix. This function will figure out which worker regions need to be retrieved and will assemble the results. */ int putRegion(int ulr, int ulc, MATRIX_TYPE* inRegion); /* overlays the given matrix over the appropriate portion of the worker's matrices. This function will figure out which workers are effected and will overlay each worker's piece as necessary. */ int calcRowPieces(); void calcMyDecomp(); int bestFactor(int sz); int cleanup_workers(); int test_top(); int test_bottom(); int test_left(); int test_right(); int test_up_left(); int test_up_right(); int test_low_left(); int test_low_right(); rect get_worker_coords(int workerNum); rect get_worker_coords(); // These functions must be defined by the user in the inheriting class. virtual void workers_alloc(int pieces); virtual void destroy_workers(int pieces); virtual void getMatrixPiece(); virtual void putMatrixPiece(); virtual void doStencilPiece(stencil *st); virtual void prepareDest(); virtual stencil* getNextStencil(); virtual int setRowsCols(); virtual float checkConvergence(); virtual float checkConvergencePiece(); // These functions are called by the user, and should remain public void init(); int setSource(string *sNm); int setDest(string *dNm); int setRowPieces(int xPieces); int setWindow(int w_ulRow, int w_ulCol, int w_lrRow, int w_lrCol); int setPieces(int); int setIterations(int); int setGoal(float); int setRowsAndCols(int r, int c); int addStencil (stencil *); int getNumRows(); int getNumCols(); int doStencil(); int cleanup(); };