SPModel: libSPModel/src/SurfaceMeshRegularDecomp.c Source File
VPAC - Computational Software Development
Main | SPModel | StGermain FrameWork |
Main Page | Alphabetical List | Class List | Directories | File List | Class Members | File Members

SurfaceMeshRegularDecomp.c

Go to the documentation of this file.
00001 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00002 **
00003 ** Copyright (C), 2004, Victorian Partnership for Advanced Computing (VPAC) Ltd, 110 Victoria Street, Melbourne, 3053, Australia.
00004 **
00005 ** Authors:
00006 **  Ogar R. Widjaja, Computational Scientist, VPAC.
00007 **  Raquibul Hassan, Software Engineer, VPAC. (raq@vpac.org)
00008 **  Keith Hsuan, Computational Scientist, VPAC (keith@vpac.org)
00009 **  William F. Appelbe, Director, VPAC. (bill@vpac.org)
00010 **  Stevan M. Quenette, Senior Software Engineer, VPAC. (steve@vpac.org)
00011 **  Patrick D. Sunter, Software Engineer, VPAC. (patrick@vpac.org)
00012 **
00013 ** This file may be distributed under the terms of the VPAC Public License
00014 ** as defined by VPAC of Australia and appearing in the file
00015 ** LICENSE.VPL included in the packaging of this file.
00016 **
00017 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00018 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00019 **
00020 */
00030 #include <mpi.h>
00031 #include <StGermain/StGermain.h>
00032 #include <Cascade/cascade.h>
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <assert.h>
00038 #include <math.h>
00039 #include <limits.h>
00040 #include "types.h"
00041 #include "SurfaceMeshDecomp.h"
00042 #include "SurfaceMeshRegularDecomp.h"
00043 #include "SurfaceMesh.h"
00044 #include "Catchment.h"
00045 #include "Misc.h"
00046 #include "CommHandler.h"
00047 
00048 const Type SurfaceMeshRegularDecomp_Type = "SurfaceMeshRegularDecomp";
00049 
00050 SurfaceMeshRegularDecomp            *SurfaceMeshRegularDecomp_DefaultNew( Name name )
00051 {
00052     return _SurfaceMeshRegularDecomp_New( sizeof( SurfaceMeshRegularDecomp ),
00053                                             SurfaceMeshRegularDecomp_Type,
00054                                             _SurfaceMeshRegularDecomp_Delete,
00055                                             _SurfaceMeshRegularDecomp_Print,
00056                                             NULL,
00057                                             (void*)SurfaceMeshRegularDecomp_DefaultNew,
00058                                             _SurfaceMeshRegularDecomp_Construct,
00059                                             _SurfaceMeshRegularDecomp_Build,
00060                                             _SurfaceMeshRegularDecomp_Initialise,
00061                                             _SurfaceMeshRegularDecomp_Execute,
00062                                             _SurfaceMeshRegularDecomp_Destroy,
00063                                             name,
00064                                             False,
00065                                             NULL,
00066                                             NULL,
00067                                             _SurfaceMeshDecomp_SyncMesh,
00068                                             _SurfaceMeshDecomp_ComputeHaloNodes,
00069                                             SurfaceMeshRegularDecomp_AllocateNodes );
00070 }
00071 
00072 SurfaceMeshRegularDecomp            *SurfaceMeshRegularDecomp_New( Name name,
00073                                                                         SurfaceMesh *mesh,
00074                                                                         Dictionary *dictionary )
00075 {
00076     return _SurfaceMeshRegularDecomp_New( sizeof( SurfaceMeshRegularDecomp ),
00077                                             SurfaceMeshRegularDecomp_Type,
00078                                             _SurfaceMeshRegularDecomp_Delete,
00079                                             _SurfaceMeshRegularDecomp_Print,
00080                                             NULL,
00081                                             (void*)SurfaceMeshRegularDecomp_DefaultNew,
00082                                             _SurfaceMeshRegularDecomp_Construct,
00083                                             _SurfaceMeshRegularDecomp_Build,
00084                                             _SurfaceMeshRegularDecomp_Initialise,
00085                                             _SurfaceMeshRegularDecomp_Execute,
00086                                             _SurfaceMeshRegularDecomp_Destroy,
00087                                             name,
00088                                             True,
00089                                             dictionary,
00090                                             mesh,
00091                                             _SurfaceMeshDecomp_SyncMesh,
00092                                             _SurfaceMeshDecomp_ComputeHaloNodes,
00093                                             SurfaceMeshRegularDecomp_AllocateNodes );
00094 }
00095 
00096 SurfaceMeshRegularDecomp            *_SurfaceMeshRegularDecomp_New( SizeT                           _sizeOfSelf,
00097                                     Type                            type,
00098                                     Stg_Class_DeleteFunction*               _delete,
00099                                     Stg_Class_PrintFunction*                _print,
00100                                     Stg_Class_CopyFunction*             _copy, 
00101                                     Stg_Component_DefaultConstructorFunction*   _defaultConstructor,
00102                                     Stg_Component_ConstructFunction*            _construct,
00103                                     Stg_Component_BuildFunction*        _build,
00104                                     Stg_Component_InitialiseFunction*       _initialise,
00105                                     Stg_Component_ExecuteFunction*      _execute,
00106                                     Stg_Component_DestroyFunction*      _destroy,
00107                                     Name                            name,
00108                                     Bool                            initFlag,
00109                                     Dictionary                      *dictionary,
00110                                     SurfaceMesh                     *_mesh,
00111                                     SurfaceMeshDecomp_SyncMeshFunction *_syncMesh,
00112                                     SurfaceMeshDecomp_ComputeHaloNodesFunction *_computeHaloNodes,
00113                                     SurfaceMeshDecomp_AllocateNodesFunction *_allocateNodes )
00114 {
00115     SurfaceMeshRegularDecomp *self = NULL;
00116     
00117     assert( _sizeOfSelf >= sizeof( SurfaceMeshRegularDecomp ) );
00118 
00119     self = (SurfaceMeshRegularDecomp*)_SurfaceMeshDecomp_New ( _sizeOfSelf,
00120                                                             type,
00121                                                             _delete,
00122                                                             _print,
00123                                                             _copy,
00124                                                             _defaultConstructor,
00125                                                             _construct,
00126                                                             _build,
00127                                                             _initialise,
00128                                                             _execute,
00129                                                             _destroy,
00130                                                             name,
00131                                                             initFlag,
00132                                                             dictionary,
00133                                                             _mesh,
00134                                                             _syncMesh,
00135                                                             _computeHaloNodes,
00136                                                             _allocateNodes );
00137 
00138     if( initFlag ){
00139         _SurfaceMeshRegularDecomp_Init( self );
00140     }
00141     
00142     return self;
00143 }
00144 
00145 void _SurfaceMeshRegularDecomp_Init( SurfaceMeshRegularDecomp *self )
00146 {
00147 
00148 }
00149 
00150 void _SurfaceMeshRegularDecomp_Print( void *surfaceMeshRegularDecomp, Stream *stream )
00151 {
00152     assert( surfaceMeshRegularDecomp );
00153     assert( stream );
00154 
00155     /* Parent print */
00156     _SurfaceMeshDecomp_Print( (_SurfaceMeshDecomp*)surfaceMeshRegularDecomp, stream );
00157     
00158     /* general info */
00159     Journal_Printf( stream, "SurfaceMeshRegularDecomp (ptr): (%p)\n", surfaceMeshRegularDecomp );
00160 
00161 }
00162 
00163 void _SurfaceMeshRegularDecomp_Delete( void *surfaceMeshRegularDecomp )
00164 {
00165     int rank, numProcs;
00166     SurfaceMeshRegularDecomp *self = (SurfaceMeshRegularDecomp*)surfaceMeshRegularDecomp;
00167     
00168     assert( self );
00169 
00170     rank = self->mesh->rank;
00171     numProcs = self->mesh->numProcs;
00172     
00173     if( rank == MASTER_PROC ){
00174         _SurfaceMeshDecomp_Delete( self );
00175     }
00176     else{
00177 
00178     }
00179 }
00180 
00181 void _SurfaceMeshRegularDecomp_Construct( void *surfaceMeshRegularDecomp, Stg_ComponentFactory *cf )
00182 {
00183     
00184 }
00185 
00186 void _SurfaceMeshRegularDecomp_Build( void *surfaceMeshRegularDecomp, void *data )
00187 {
00188     SurfaceMeshRegularDecomp *self = (SurfaceMeshRegularDecomp*)surfaceMeshRegularDecomp;
00189     
00190     assert( self );
00191     
00192     _SurfaceMeshDecomp_Build( self, data );
00193     
00194 }
00195 
00196 void _SurfaceMeshRegularDecomp_Initialise( void *surfaceMeshRegularDecomp, void *data )
00197 {
00198     
00199 }
00200 
00201 void _SurfaceMeshRegularDecomp_Execute( void *surfaceMeshRegularDecomp, void *data )
00202 {
00203     SurfaceMeshRegularDecomp *self = (SurfaceMeshRegularDecomp*)surfaceMeshRegularDecomp;
00204     
00205     assert( self );
00206     
00207     _SurfaceMeshDecomp_Execute( self, data );
00208 }
00209 
00210 void _SurfaceMeshRegularDecomp_Destroy( void *surfaceMeshRegularDecomp, void *data )
00211 {
00212     
00213 }
00214 
00217 void SurfaceMeshRegularDecomp_SyncMesh( SurfaceMeshRegularDecomp *surfaceMeshRegularDecomp, SurfaceMesh *mesh )
00218 {
00219     SurfaceMeshRegularDecomp *self = (SurfaceMeshRegularDecomp*)surfaceMeshRegularDecomp;
00220     
00221     assert( self );
00222     assert( mesh );
00223     
00224     _SurfaceMeshDecomp_SyncMesh( (_SurfaceMeshDecomp*)self, mesh );
00225 }
00226 
00227 void SurfaceMeshRegularDecomp_ComputeHaloNodes( SurfaceMeshRegularDecomp *surfaceMeshRegularDecomp )
00228 {
00229     SurfaceMeshRegularDecomp *self = (SurfaceMeshRegularDecomp*)surfaceMeshRegularDecomp;
00230     
00231     assert( self );
00232 
00233     _SurfaceMeshDecomp_ComputeHaloNodes( (_SurfaceMeshDecomp*)self );
00234 }
00235 
00236 void SurfaceMeshRegularDecomp_AllocateNodes( _SurfaceMeshDecomp *surfaceMeshDecomp )
00237 {
00238     int i, j;
00239     float minX, maxX;
00240     float lowerBound, upperBound;
00241     SurfaceMeshRegularDecomp *meshDecomp = NULL;
00242     SurfaceMesh *mesh = NULL;
00243 
00244     meshDecomp = (SurfaceMeshRegularDecomp*) surfaceMeshDecomp;
00245 
00246     assert( meshDecomp );
00247 
00248     mesh = meshDecomp->mesh;
00249 
00250     if( (mesh->numProcs % 2) && (mesh->numProcs != 1) ){
00251         fprintf( stderr, "Only even number of multiple processors supported..\n" );
00252         exit( 1 );
00253     }
00254 
00255     minX = 1.0e32;
00256     maxX = -1.0e32;
00257     
00258     for( i=0; i<mesh->numNodes; i++ ){
00259         if( minX > mesh->y[i] ){
00260             minX = mesh->y[i];
00261         }
00262         if( maxX < mesh->y[i] ){
00263             maxX = mesh->y[i];
00264         }
00265     }
00266     
00267     for( i=0; i<mesh->numProcs; i++ ){
00268         meshDecomp->processorLoad[i] = 0;
00269     }
00270 
00271     for( i=0; i<mesh->numNodes; i++ ){
00272         mesh->processor[i] = -1;
00273     }
00274 
00275     lowerBound = minX;
00276     upperBound = minX + (maxX - minX) / mesh->numProcs;
00277 
00278     for( i=0; i<mesh->numProcs; i++ ){
00279         for( j=0; j<mesh->numNodes; j++ ){
00280             if( mesh->y[j] >= lowerBound && mesh->y[j] <= upperBound ){
00281                 if( mesh->processor[j] == -1 ){
00282                     mesh->processor[j] = i;
00283                     meshDecomp->processorLoad[i]++;
00284                 }
00285                 else if ( mesh->processor[j] != i ){
00286                     meshDecomp->processorLoad[mesh->processor[j]]--;
00287                     mesh->processor[j] = i;
00288                     meshDecomp->processorLoad[mesh->processor[j]]++;
00289                 }
00290             }
00291         }
00292         lowerBound = upperBound;
00293         upperBound += (maxX - minX) / mesh->numProcs;
00294     }
00295 
00296     for( i=0; i<mesh->numProcs; i++ ){
00297         printf( "processor[%d] has %d nodes\n", i, meshDecomp->processorLoad[i] );
00298     }
00299 }
00300