7 #include "PoPs_private.h"
8 #include "PoPs_Bcast_private.h"
10 #define NumberOfBcastArrays 3
12 enum PoPs_Bcast_mode { PoPs_Bcast_mode_count, PoPs_Bcast_mode_pack, PoPs_Bcast_mode_unpack };
14 typedef struct PoPs_Bcast_info {
15 enum PoPs_Bcast_mode mode;
16 int int_count, char_count, double_count;
22 static int PoPs_Bcast3( statusMessageReporting *smr, MPI_Comm comm, PoPs_Bcast_info *info, unitsDB *
unitsRoot, PoPs *
popsRoot );
23 static int PoPs_Bcast_PoPs( statusMessageReporting *smr, PoPs_Bcast_info *info,
int index, PoPs *
popsRoot );
24 static int PoPs_Bcast_PoPs2( statusMessageReporting *smr, PoPs_Bcast_info *info, PoP *pop );
25 static int PoPs_Bcast_int( statusMessageReporting *smr, PoPs_Bcast_info *info,
int *value );
26 static int PoPs_Bcast_charAllocate( statusMessageReporting *smr, PoPs_Bcast_info *info,
char **value );
27 static int PoPs_Bcast_double( statusMessageReporting *smr, PoPs_Bcast_info *info,
double *value );
31 int PoPs_Bcast2( statusMessageReporting *smr, MPI_Comm comm,
int bossRank, unitsDB *
unitsRoot, PoPs *
popsRoot ) {
34 int description[NumberOfBcastArrays];
35 PoPs_Bcast_info info = { PoPs_Bcast_mode_count, 0, 0, 0, NULL, NULL, NULL };
37 if( ( status = MPI_Errhandler_set( comm, MPI_ERRORS_RETURN ) ) != 0 )
return( status );
41 if( ( status = MPI_Comm_rank( comm, &myRank ) ) != 0 )
return( status );
43 if( myRank == bossRank ) {
44 info.mode = PoPs_Bcast_mode_count;
45 if( ( status = PoPs_Bcast3( smr, comm, &info, unitsRoot, popsRoot ) ) != 0 )
return( status );
46 description[0] = info.int_count;
47 description[1] = info.char_count;
48 description[2] = info.double_count;
49 if( ( info.int_array = (
int *) smr_malloc2( smr, info.int_count *
sizeof(
int ), 1,
"info.int_array" ) ) == NULL )
goto err;
50 if( ( info.char_array = (
char *) smr_malloc2( smr, info.char_count *
sizeof(
char ), 1,
"info.char_array" ) ) == NULL )
goto err;
51 if( ( info.double_array = (
double *) smr_malloc2( smr, info.double_count *
sizeof(
double ), 1,
"info.double_array" ) ) == NULL )
goto err;
53 info.mode = PoPs_Bcast_mode_pack;
56 info.double_count = 0;
57 if( ( status = PoPs_Bcast3( smr, comm, &info, unitsRoot, popsRoot ) ) != 0 )
return( status );
58 if( info.int_count != description[0] ) {
59 smr_setReportError2( smr,
PoPs_smr_ID, 1,
"int counting count = %d != packing count = %d", info.int_count, description[0] );
62 if( info.char_count != description[1] ) {
63 smr_setReportError2( smr,
PoPs_smr_ID, 1,
"char counting count = %d != packing count = %d", info.char_count, description[1] );
66 if( info.double_count != description[2] ) {
67 smr_setReportError2( smr,
PoPs_smr_ID, 1,
"double counting count = %d != packing count = %d", info.double_count, description[2] );
72 if( ( status = MPI_Bcast( description, NumberOfBcastArrays, MPI_INT, bossRank, comm ) ) != 0 )
goto err;
74 if( myRank != bossRank ) {
75 if( ( info.int_array = (
int *) smr_malloc2( smr, description[0] *
sizeof(
int ), 1,
"info.int_array (2)" ) ) == NULL )
goto err;
76 if( ( info.char_array = (
char *) smr_malloc2( smr, description[1] *
sizeof(
char ), 1,
"info.char_array (2)" ) ) == NULL )
goto err;
77 if( ( info.double_array = (
double *) smr_malloc2( smr, description[2] *
sizeof(
double ), 1,
"info.double_array (2)" ) ) == NULL )
goto err;
79 if( ( status = MPI_Bcast( info.int_array, description[0], MPI_INT, bossRank, comm ) ) != 0 )
goto err;
80 if( ( status = MPI_Bcast( info.char_array, description[1], MPI_CHAR, bossRank, comm ) ) != 0 )
goto err;
81 if( ( status = MPI_Bcast( info.double_array, description[2], MPI_DOUBLE, bossRank, comm ) ) != 0 )
goto err;
83 if( myRank != bossRank ) {
84 info.mode = PoPs_Bcast_mode_unpack;
85 if( ( status = PoPs_Bcast3( smr, comm, &info, unitsRoot, popsRoot ) ) != 0 )
goto err;
88 if( info.int_array != NULL )
smr_freeMemory( (
void **) &(info.int_array) );
89 if( info.char_array != NULL )
smr_freeMemory( (
void **) &(info.char_array) );
90 if( info.double_array != NULL )
smr_freeMemory( (
void **) &(info.double_array) );
95 if( info.int_array != NULL )
smr_freeMemory( (
void **) &(info.int_array) );
96 if( info.char_array != NULL )
smr_freeMemory( (
void **) &(info.char_array) );
97 if( info.double_array != NULL )
smr_freeMemory( (
void **) &(info.double_array) );
98 if( unitsRoot->unsorted != NULL )
smr_freeMemory( (
void **) &(unitsRoot->unsorted) );
99 if( popsRoot->pops != NULL )
smr_freeMemory( (
void **) &(popsRoot->pops) );
100 if( popsRoot->sorted != NULL )
smr_freeMemory( (
void **) &(popsRoot->sorted) );
106 static int PoPs_Bcast3( statusMessageReporting *smr, MPI_Comm comm, PoPs_Bcast_info *info, unitsDB *unitsRoot, PoPs *popsRoot ) {
111 if( ( status = PoPs_Bcast_int( smr, info, &(unitsRoot->numberOfUnits) ) ) != 0 )
return( status );
112 numberOfUnits = unitsRoot->numberOfUnits;
113 if( info->mode == PoPs_Bcast_mode_unpack ) {
114 unitsRoot->allocated = unitsRoot->numberOfUnits;
115 unitsRoot->numberOfUnits = 0;
116 if( ( unitsRoot->unsorted = (
char const **) smr_malloc2( smr, unitsRoot->allocated *
sizeof(
char const ** ), 1,
"unitsRoot->unsorted" ) ) == NULL )
return( -1 );
118 for( i = 0; i < numberOfUnits; i++ ) {
119 if( ( status = PoPs_Bcast_charAllocate( smr, info, (
char **) &(unitsRoot->unsorted[i]) ) ) != 0 )
return( status );
120 if( info->mode == PoPs_Bcast_mode_unpack ) unitsRoot->numberOfUnits++;
123 if( ( status = PoPs_Bcast_int( smr, info, &(popsRoot->numberOfParticles) ) ) != 0 )
return( status );
124 numberOfParticles = popsRoot->numberOfParticles;
125 if( info->mode == PoPs_Bcast_mode_unpack ) {
126 popsRoot->allocated = popsRoot->numberOfParticles;
127 popsRoot->numberOfParticles = 0;
128 if( ( popsRoot->pops = (PoP **) smr_malloc2( smr, popsRoot->allocated *
sizeof( PoP * ), 1,
"popsRoot->pops" ) ) == NULL )
return( -1 );
129 if( ( popsRoot->sorted = (PoP **) smr_malloc2( smr, popsRoot->allocated *
sizeof( PoP * ), 1,
"popsRoot->unsorted" ) ) == NULL )
return( -1 );
132 if( ( status = PoPs_Bcast_PoPs( smr, info, i, popsRoot ) ) != 0 )
return( status );
139 static int PoPs_Bcast_PoPs( statusMessageReporting *smr, PoPs_Bcast_info *info,
int index, PoPs *popsRoot ) {
144 if( info->mode != PoPs_Bcast_mode_unpack )
return( PoPs_Bcast_PoPs2( smr, info, popsRoot->pops[index] ) );
145 if( ( status = PoPs_Bcast_PoPs2( smr, info, &pop ) ) != 0 )
return( status );
151 static int PoPs_Bcast_PoPs2( statusMessageReporting *smr, PoPs_Bcast_info *info, PoP *pop ) {
155 if( ( status = PoPs_Bcast_int( smr, info, &(pop->index) ) ) != 0 )
return( status );
156 if( ( status = PoPs_Bcast_int( smr, info, &(pop->properIndex) ) ) != 0 )
return( status );
157 if( ( status = PoPs_Bcast_int( smr, info, &(pop->aliasIndex) ) ) != 0 )
return( status );
158 if( ( status = PoPs_Bcast_int( smr, info, (
int *) &(pop->genre) ) ) != 0 )
return( status );
160 if( ( status = PoPs_Bcast_int( smr, info, &(pop->Z) ) ) != 0 )
return( status );
161 if( ( status = PoPs_Bcast_int( smr, info, &(pop->A) ) ) != 0 )
return( status );
162 if( ( status = PoPs_Bcast_int( smr, info, &(pop->l) ) ) != 0 )
return( status );
163 if( ( status = PoPs_Bcast_double( smr, info, &(pop->mass) ) ) != 0 )
return( status );
165 if( info->mode == PoPs_Bcast_mode_pack ) {
167 if( pop->massUnit != NULL ) {
168 if( ( n =
unitsDB_index( smr, pop->massUnit ) ) < 0 )
return( n );
171 if( ( status = PoPs_Bcast_int( smr, info, &n ) ) != 0 )
return( status );
172 if( ( status = PoPs_Bcast_charAllocate( smr, info, (
char **) &(pop->name) ) ) != 0 )
return( status );
174 if( info->mode == PoPs_Bcast_mode_unpack ) {
175 pop->aliasIndex = -1;
178 pop->massUnit = NULL; }
190 if( info->mode == PoPs_Bcast_mode_unpack ) {
198 static int PoPs_Bcast_int( statusMessageReporting *smr, PoPs_Bcast_info *info,
int *value ) {
200 if( info->mode == PoPs_Bcast_mode_pack ) {
201 info->int_array[info->int_count] = *value; }
202 else if( info->mode == PoPs_Bcast_mode_unpack ) {
203 *value = info->int_array[info->int_count];
211 static int PoPs_Bcast_charAllocate( statusMessageReporting *smr, PoPs_Bcast_info *info,
char **value ) {
213 int i, n = 0, status;
215 if( info->mode != PoPs_Bcast_mode_unpack ) {
216 n = (int) strlen( *value ) + 1;
217 if( ( status = PoPs_Bcast_int( smr, info, &n ) ) != 0 )
return( status );
218 if( info->mode == PoPs_Bcast_mode_pack ) {
219 for( i = 0; i <
n; i++ ) info->char_array[info->char_count + i] = (*value)[i];
222 if( ( status = PoPs_Bcast_int( smr, info, &n ) ) != 0 )
return( status );
223 if( ( *value = (
char *) smr_malloc2( smr, n *
sizeof(
char ), 0,
"*value" ) ) == NULL )
return( -1 );
224 for( i = 0; i <
n; i++ ) (*value)[i] = info->char_array[info->char_count + i];
226 info->char_count +=
n;
233 static int PoPs_Bcast_double( statusMessageReporting *smr, PoPs_Bcast_info *info,
double *value ) {
235 if( info->mode == PoPs_Bcast_mode_pack ) {
236 info->double_array[info->double_count] = *value; }
237 else if( info->mode == PoPs_Bcast_mode_unpack ) {
238 *value = info->double_array[info->double_count];
240 info->double_count++;
static int numberOfParticles
int unitsDB_index(statusMessageReporting *, char const *unit)
int PoPs_releasePrivate(statusMessageReporting *)
void * smr_freeMemory(void **p)
char const * unitsDB_stringFromIndex(statusMessageReporting *smr, int index)
PoP * PoPs_copyAddParticleIfNeeded(statusMessageReporting *smr, PoP *pop)