[cig-commits] r4820 - in long/3D/Gale/trunk/src/StGermain: . Base/Container/src Base/Container/tests

walter at geodynamics.org walter at geodynamics.org
Wed Oct 11 13:46:00 PDT 2006


Author: walter
Date: 2006-10-11 13:46:00 -0700 (Wed, 11 Oct 2006)
New Revision: 4820

Modified:
   long/3D/Gale/trunk/src/StGermain/
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/MPIRoutines.c
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.c
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.h
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h
   long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testMPIRoutines.c
   long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.0of1.expected
   long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.c
Log:
 r2877 at earth:  boo | 2006-10-11 13:42:28 -0700
  r2793 at earth (orig r3781):  LukeHodkinson | 2006-09-05 20:03:14 -0700
  * Added some set operations to RangeSet.
  * Fixed a bug in the intersection operation.
  * Updating the tests for basic MPI routines.
  
 



Property changes on: long/3D/Gale/trunk/src/StGermain
___________________________________________________________________
Name: svk:merge
   - 1ef209d2-b310-0410-a72d-e20c9eb0015c:/cig:2876
afb6c753-b9d0-0310-b4e7-dbd8d91cdd35:/trunk/StGermain:3780
   + 1ef209d2-b310-0410-a72d-e20c9eb0015c:/cig:2877
afb6c753-b9d0-0310-b4e7-dbd8d91cdd35:/trunk/StGermain:3781

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/src/MPIRoutines.c
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/MPIRoutines.c	2006-10-11 20:45:57 UTC (rev 4819)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/MPIRoutines.c	2006-10-11 20:46:00 UTC (rev 4820)
@@ -265,34 +265,35 @@
 void Array_1DTo2D( unsigned nBlocks, unsigned* sizes, void* srcArray, 
 		   void*** dstArrays, size_t itemSize )
 {
+	Byte**		tmp;
+	unsigned*	tmpSizes;
+	unsigned	curPos = 0;
+	unsigned	b_i;
+
 	if( nBlocks == 0 ) {
 		*dstArrays = NULL;
 		return;
 	}
 
-	/*
-	** Dump a 1D array of definite block sizes into a 2D equivalent for easy access.
-	*/
-	
-	{
-		void**		tmp;
-		unsigned	curPos = 0;
-		unsigned	block_i;
-		
-		tmp = Memory_Alloc_Array_Unnamed( void*, nBlocks );
-		for( block_i = 0; block_i < nBlocks; block_i++ ) {
-			if( sizes[block_i] == 0 ) {
-				tmp[block_i] = NULL;
-				continue;
-			}
+	/* Calculate sizes. */
+	tmpSizes = Memory_Alloc_Array_Unnamed( unsigned, nBlocks );
+	for( b_i = 0; b_i < nBlocks; b_i++ )
+		tmpSizes[b_i] = sizes[b_i] * (unsigned)itemSize;
 
-			tmp[block_i] = Memory_Alloc_Array_Bytes_Unnamed( itemSize, sizes[block_i], "unknown" );
-			memcpy( tmp[block_i], (unsigned char*)srcArray + curPos * itemSize, itemSize * sizes[block_i] );
-			curPos += sizes[block_i];
-		}
+	/* Allocate. */
+	tmp = Memory_Alloc_2DComplex_Unnamed( Byte, nBlocks, tmpSizes );
+	for( b_i = 0; b_i < nBlocks; b_i++ ) {
+		if( sizes[b_i] == 0 )
+			continue;
 
-		*dstArrays = tmp;
+		memcpy( tmp[b_i], (Byte*)srcArray + curPos, tmpSizes[b_i] );
+		curPos += tmpSizes[b_i];
 	}
+
+	/* Free memory. */
+	FreeArray( tmpSizes );
+
+	*dstArrays = (void**)tmp;
 }
 
 void Array_2DTo1D( unsigned nBlocks, unsigned* sizes, void** srcArrays, 
@@ -310,16 +311,16 @@
 	
 	{
 		unsigned	netSize;
-		unsigned	block_i;
+		unsigned	b_i;
 		unsigned*	tmpDisps;
 		
 		tmpDisps = Memory_Alloc_Array_Unnamed( unsigned, nBlocks );
 		
 		tmpDisps[0] = 0;
 		netSize = sizes[0];
-		for( block_i = 1; block_i < nBlocks; block_i++ ) {
-			tmpDisps[block_i] = tmpDisps[block_i - 1] + sizes[block_i - 1];
-			netSize += sizes[block_i];
+		for( b_i = 1; b_i < nBlocks; b_i++ ) {
+			tmpDisps[b_i] = tmpDisps[b_i - 1] + sizes[b_i - 1];
+			netSize += sizes[b_i];
 		}
 		
 		if( netSize > 0 ) {
@@ -328,10 +329,10 @@
 			
 			tmpArray = Memory_Alloc_Array_Bytes_Unnamed( itemSize, netSize, "unknown" );
 
-			for( block_i = 0; block_i < nBlocks; block_i++ ) {
+			for( b_i = 0; b_i < nBlocks; b_i++ ) {
 				dest = tmpArray;
-				dest += (tmpDisps[block_i] * itemSize);
-				memcpy( dest, srcArrays[block_i], itemSize * sizes[block_i] );
+				dest += (tmpDisps[b_i] * itemSize);
+				memcpy( dest, srcArrays[b_i], itemSize * sizes[b_i] );
 			}
 			
 			*dstArray = tmpArray;

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.c
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.c	2006-10-11 20:45:57 UTC (rev 4819)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.c	2006-10-11 20:46:00 UTC (rev 4820)
@@ -194,10 +194,42 @@
 }
 
 
+void RangeSet_Union( void* rangeSet, RangeSet* rSet ) {
+	RangeSet*	self = (RangeSet*)rangeSet;
+	unsigned	maxInds;
+	unsigned	nInds = 0;
+	unsigned*	inds;
+	unsigned	r_i;
+
+	assert( self );
+	assert( rSet );
+
+	maxInds = rSet->nInds + self->nInds;
+	inds = Memory_Alloc_Array_Unnamed( unsigned, maxInds );
+	for( r_i = 0; r_i < self->nRanges; r_i++ ) {
+		RangeSet_Range*	range = self->ranges + r_i;
+		unsigned	ind_i;
+
+		for( ind_i = range->begin; ind_i < range->end; ind_i += range->step )
+			inds[nInds++] = ind_i;
+	}
+	for( r_i = 0; r_i < rSet->nRanges; r_i++ ) {
+		RangeSet_Range*	range = rSet->ranges + r_i;
+		unsigned	ind_i;
+
+		for( ind_i = range->begin; ind_i < range->end; ind_i += range->step )
+			inds[nInds++] = ind_i;
+	}
+
+	RangeSet_SetIndices( self, nInds, inds );
+	FreeArray( inds );
+}
+
+
 void RangeSet_Intersection( void* rangeSet, RangeSet* rSet ) {
 	RangeSet*	self = (RangeSet*)rangeSet;
 	unsigned	maxInds;
-	unsigned	nInds;
+	unsigned	nInds = 0;
 	unsigned*	inds;
 	unsigned	r_i;
 
@@ -222,6 +254,34 @@
 }
 
 
+void RangeSet_Subtraction( void* rangeSet, RangeSet* rSet ) {
+	RangeSet*	self = (RangeSet*)rangeSet;
+	unsigned	maxInds;
+	unsigned	nInds = 0;
+	unsigned*	inds;
+	unsigned	r_i;
+
+	assert( self );
+	assert( rSet );
+
+	maxInds = self->nInds;
+	inds = Memory_Alloc_Array_Unnamed( unsigned, maxInds );
+
+	for( r_i = 0; r_i < self->nRanges; r_i++ ) {
+		RangeSet_Range*	range = self->ranges + r_i;
+		unsigned	ind_i;
+
+		for( ind_i = range->begin; ind_i < range->end; ind_i += range->step ) {
+			if( !RangeSet_HasIndex( rSet, ind_i ) )
+				inds[nInds++] = ind_i;
+		}
+	}
+
+	RangeSet_SetIndices( self, nInds, inds );
+	FreeArray( inds );
+}
+
+
 void RangeSet_Unpickle( void* rangeSet, unsigned nBytes, Byte* bytes ) {
 	RangeSet*	self = (RangeSet*)rangeSet;
 
@@ -242,6 +302,28 @@
 }
 
 
+void RangeSet_GetIndices( void* rangeSet, unsigned* nInds, unsigned** inds ) {
+	RangeSet*	self = (RangeSet*)rangeSet;
+	unsigned	r_i;
+
+	assert( self );
+
+	*nInds = 0;
+	if( self->nInds )
+		*inds = Memory_Alloc_Array_Unnamed( unsigned, self->nInds );
+	else
+		*inds = NULL;
+
+	for( r_i = 0; r_i < self->nRanges; r_i++ ) {
+		RangeSet_Range*	range = self->ranges + r_i;
+		unsigned	ind_i;
+
+		for( ind_i = range->begin; ind_i < range->end; ind_i += range->step )
+			(*inds)[(*nInds)++] = ind_i;
+	}
+}
+
+
 Bool RangeSet_HasIndex( void* rangeSet, unsigned ind ) {
 	RangeSet*	self = (RangeSet*)rangeSet;
 	unsigned	r_i;

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.h
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.h	2006-10-11 20:45:57 UTC (rev 4819)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/RangeSet.h	2006-10-11 20:46:00 UTC (rev 4820)
@@ -53,8 +53,6 @@
 		unsigned	step;
 	} RangeSet_Range;
 
-	typedef unsigned char	Byte;
-
 	#define __RangeSet				\
 		/* General info */			\
 		__Stg_Class				\
@@ -101,9 +99,12 @@
 
 	void RangeSet_SetIndices( void* rangeSet, unsigned nInds, unsigned* inds );
 	void RangeSet_Clear( void* rangeSet );
+	void RangeSet_Union( void* rangeSet, RangeSet* rSet );
 	void RangeSet_Intersection( void* rangeSet, RangeSet* rSet );
+	void RangeSet_Subtraction( void* rangeSet, RangeSet* rSet );
 	void RangeSet_Unpickle( void* rangeSet, unsigned nBytes, Byte* bytes );
 
+	void RangeSet_GetIndices( void* rangeSet, unsigned* nInds, unsigned** inds );
 	Bool RangeSet_HasIndex( void* rangeSet, unsigned ind );
 	unsigned RangeSet_GetNIndices( void* rangeSet );
 	unsigned RangeSet_GetNRanges( void* rangeSet );

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h	2006-10-11 20:45:57 UTC (rev 4819)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h	2006-10-11 20:46:00 UTC (rev 4820)
@@ -65,8 +65,10 @@
 	typedef struct MaxHeap			MaxHeap;
 	typedef struct UIntMap			UIntMap;
 	typedef struct RangeSet			RangeSet;
+
+
+	typedef unsigned char		Byte;
 	
-	
 	typedef char					BitField;
 	
 	typedef struct BTreeNode			BTreeNode;

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testMPIRoutines.c
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testMPIRoutines.c	2006-10-11 20:45:57 UTC (rev 4819)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testMPIRoutines.c	2006-10-11 20:46:00 UTC (rev 4820)
@@ -109,7 +109,7 @@
 		    dst2D[1][0] != 2 || dst2D[1][1] != 3 || dst2D[1][2] != 4 || dst2D[1][3] != 5 || 
 		    dst2D[2][0] != 6 )
 		{
-			FreeArray2D( nBlocks, dst2D );
+			FreeArray( dst2D );
 			return False;
 		}
 
@@ -118,23 +118,22 @@
 		    dst1D[4] != 4 || dst1D[5] != 5 || dst1D[6] != 6 || 
 		    disps[0] != 0 || disps[1] != 2 || disps[2] != 6 )
 		{
-			FreeArray2D( nBlocks, dst2D );
+			FreeArray( dst2D );
 			FreeArray( dst1D );
 			FreeArray( disps );
 			return False;
 		}
 
-		FreeArray2D( nBlocks, dst2D );
+		FreeArray( dst2D );
 		FreeArray( dst1D );
 		FreeArray( disps );
 
 		sizes[1] = 0;
 		Array_1DTo2D( nBlocks, sizes, src, (void***)&dst2D, sizeof(unsigned) );
 		if( dst2D[0][0] != 0 || dst2D[0][1] != 1 || 
-		    dst2D[1] != NULL || 
 		    dst2D[2][0] != 2 )
 		{
-			FreeArray2D( nBlocks, dst2D );
+			FreeArray( dst2D );
 			return False;
 		}
 
@@ -142,13 +141,13 @@
 		if( dst1D[0] != 0 || dst1D[1] != 1 || dst1D[2] != 2 || 
 		    disps[0] != 0 || disps[1] != 2 || disps[2] != 2 )
 		{
-			FreeArray2D( nBlocks, dst2D );
+			FreeArray( dst2D );
 			FreeArray( dst1D );
 			FreeArray( disps );
 			return False;
 		}
 
-		FreeArray2D( nBlocks, dst2D );
+		FreeArray( dst2D );
 		FreeArray( dst1D );
 		FreeArray( disps );
 	}
@@ -208,7 +207,7 @@
 			if( dstSizes[i] != size ) break;
 		if( i < nProcs ) {
 			FreeArray( dstSizes );
-			FreeArray2D( nProcs, dstArrays );
+			FreeArray( dstArrays );
 			return False;
 		}
 
@@ -221,12 +220,12 @@
 		}
 		if( i < nProcs ) {
 			FreeArray( dstSizes );
-			FreeArray2D( nProcs, dstArrays );
+			FreeArray( dstArrays );
 			return False;
 		}
 
 		FreeArray( dstSizes );
-		FreeArray2D( nProcs, dstArrays );
+		FreeArray( dstArrays );
 	}
 
 	return True;
@@ -247,7 +246,7 @@
 			if( dstSizes[i] != size ) break;
 		if( i < nProcs ) {
 			FreeArray( dstSizes );
-			FreeArray2D( nProcs, dstArrays );
+			FreeArray( dstArrays );
 			return False;
 		}
 
@@ -261,13 +260,13 @@
 		}
 		if( i < nProcs ) {
 			FreeArray( dstSizes );
-			FreeArray2D( nProcs, dstArrays );
+			FreeArray( dstArrays );
 			return False;
 		}
 	}
 
 	FreeArray( dstSizes );
-	FreeArray2D( nProcs, dstArrays );
+	FreeArray( dstArrays );
 
 	return True;
 }
@@ -294,13 +293,13 @@
 			if( dstSizes[i] != 1 || dstArrays[i][0] != rank ) break;
 		if( i < nProcs ) {
 			FreeArray( dstSizes );
-			FreeArray2D( nProcs, dstArrays );
+			FreeArray( dstArrays );
 			return False;
 		}
 	}
 
 	FreeArray( dstSizes );
-	FreeArray2D( nProcs, dstArrays );
+	FreeArray( dstArrays );
 
 	return True;
 }

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.0of1.expected
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.0of1.expected	2006-10-11 20:45:57 UTC (rev 4819)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.0of1.expected	2006-10-11 20:46:00 UTC (rev 4820)
@@ -1,5 +1,7 @@
    Running test 'construct'... passed
    Running test 'set indices'... passed
    Running test 'ranges'... passed
+   Running test 'union'... passed
    Running test 'intersection'... passed
+   Running test 'subtraction'... passed
    Running test 'pickle'... passed

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.c
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.c	2006-10-11 20:45:57 UTC (rev 4819)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/tests/testRangeSet.c	2006-10-11 20:46:00 UTC (rev 4820)
@@ -40,9 +40,11 @@
 
 
 Bool testConstruct( unsigned rank, unsigned nProcs, unsigned watch );
-Bool testSetIndices( unsigned rank, unsigned nProcs, unsigned watch );
+Bool testIndices( unsigned rank, unsigned nProcs, unsigned watch );
 Bool testRanges( unsigned rank, unsigned nProcs, unsigned watch );
+Bool testUnion( unsigned rank, unsigned nProcs, unsigned watch );
 Bool testIntersection( unsigned rank, unsigned nProcs, unsigned watch );
+Bool testSubtraction( unsigned rank, unsigned nProcs, unsigned watch );
 Bool testPickle( unsigned rank, unsigned nProcs, unsigned watch );
 
 
@@ -103,9 +105,11 @@
 
 	/* Run some tests. */
 	runTest( "construct", testConstruct, 10, rank, nProcs, watch );
-	runTest( "set indices", testSetIndices, 10, rank, nProcs, watch );
+	runTest( "set indices", testIndices, 10, rank, nProcs, watch );
 	runTest( "ranges", testRanges, 10, rank, nProcs, watch );
+	runTest( "union", testIntersection, 10, rank, nProcs, watch );
 	runTest( "intersection", testIntersection, 10, rank, nProcs, watch );
+	runTest( "subtraction", testIntersection, 10, rank, nProcs, watch );
 	runTest( "pickle", testPickle, 10, rank, nProcs, watch );
 
 	/* Finalise StGermain. */
@@ -130,10 +134,12 @@
 }
 
 
-Bool testSetIndices( unsigned rank, unsigned nProcs, unsigned watch ) {
+Bool testIndices( unsigned rank, unsigned nProcs, unsigned watch ) {
 	unsigned	nInds = 100;
 	unsigned	inds[100];
 	RangeSet*	set;
+	unsigned	nDstInds;
+	unsigned*	dstInds;
 	unsigned	i;
 
 	for( i = 0; i < nInds; i++ )
@@ -151,8 +157,27 @@
 		}
 	}
 
-	FreeObject( set );
+	RangeSet_GetIndices( set, &nDstInds, &dstInds );
+	FreeObject(set );
+	if( rank == watch ) {
+		unsigned	ind_i;
 
+		if( nDstInds != nInds ) {
+			FreeArray( dstInds );
+		}
+
+		for( ind_i = 0; ind_i < nDstInds; ind_i++ ) {
+			if( dstInds[ind_i] != inds[ind_i] )
+				break;
+		}
+		if( ind_i < nDstInds ) {
+			FreeArray( dstInds );
+			return False;
+		}
+	}
+
+	FreeArray( dstInds );
+
 	return True;
 }
 
@@ -194,6 +219,40 @@
 }
 
 
+Bool testUnion( unsigned rank, unsigned nProcs, unsigned watch ) {
+	unsigned	nInds = 100;
+	unsigned	inds[100];
+	RangeSet*	set0;
+	RangeSet*	set1;
+	unsigned	i;
+
+	for( i = 0; i < nInds; i++ )
+		inds[i] = (i/10)*10 + i;
+	set0 = RangeSet_New();
+	RangeSet_SetIndices( set0, nInds, inds );
+
+	for( i = 0; i < nInds; i++ )
+		inds[i] = (i/10)*10 + 100 + i;
+	set1 = RangeSet_New();
+	RangeSet_SetIndices( set1, nInds, inds );
+
+	RangeSet_Union( set0, set1 );
+	FreeObject( set1 );
+	if( rank == watch ) {
+		if( RangeSet_GetNIndices( set0 ) != nInds + nInds / 2 || 
+		    RangeSet_GetNRanges( set0 ) != nInds / 10 + nInds / 20 )
+		{
+			FreeObject( set0 );
+			return False;
+		}
+	}
+
+	FreeObject( set0 );
+
+	return True;
+}
+
+
 Bool testIntersection( unsigned rank, unsigned nProcs, unsigned watch ) {
 	unsigned	nInds = 100;
 	unsigned	inds[100];
@@ -228,6 +287,40 @@
 }
 
 
+Bool testSubtraction( unsigned rank, unsigned nProcs, unsigned watch ) {
+	unsigned	nInds = 100;
+	unsigned	inds[100];
+	RangeSet*	set0;
+	RangeSet*	set1;
+	unsigned	i;
+
+	for( i = 0; i < nInds; i++ )
+		inds[i] = (i/10)*10 + i;
+	set0 = RangeSet_New();
+	RangeSet_SetIndices( set0, nInds, inds );
+
+	for( i = 0; i < nInds; i++ )
+		inds[i] = (i/10)*10 + 100 + i;
+	set1 = RangeSet_New();
+	RangeSet_SetIndices( set1, nInds, inds );
+
+	RangeSet_Subtraction( set0, set1 );
+	FreeObject( set1 );
+	if( rank == watch ) {
+		if( RangeSet_GetNIndices( set0 ) != nInds / 2 || 
+		    RangeSet_GetNRanges( set0 ) != nInds / 20 )
+		{
+			FreeObject( set0 );
+			return False;
+		}
+	}
+
+	FreeObject( set0 );
+
+	return True;
+}
+
+
 Bool testPickle( unsigned rank, unsigned nProcs, unsigned watch ) {
 	unsigned	nInds = 100;
 	unsigned	inds[100];



More information about the cig-commits mailing list