1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/* testIVallgather.c */
#include "../spoolesMPI.h"
#include "../../Drand.h"
#include "../../timings.h"
/*--------------------------------------------------------------------*/
int
main ( int argc, char *argv[] )
/*
-------------------------------------------------------
this program tests the IV_MPI_allgather() method
(1) each process generates the same owners[n] map
(2) each process creates a vec[n] vector
and fills its owned entries with random numbers
(3) the processes gather-all the entries of vec[]
created -- 98may20, cca
-------------------------------------------------------
*/
{
char *buffer ;
double chksum, globalsum, t1, t2 ;
Drand drand ;
int ii, length, myid, msglvl, n, nproc, rc, seed, tag ;
int *owners, *vec ;
int stats[4] ;
IV *objIV, *ownersIV ;
FILE *msgFile ;
/*
---------------------------------------------------------------
find out the identity of this process and the number of process
---------------------------------------------------------------
*/
MPI_Init(&argc, &argv) ;
MPI_Comm_rank(MPI_COMM_WORLD, &myid) ;
MPI_Comm_size(MPI_COMM_WORLD, &nproc) ;
fprintf(stdout, "\n process %d of %d, argc = %d", myid, nproc, argc) ;
fflush(stdout) ;
if ( argc != 5 ) {
fprintf(stdout,
"\n\n usage : %s msglvl msgFile n seed "
"\n msglvl -- message level"
"\n msgFile -- message file"
"\n n -- number of entries in the vector"
"\n seed -- random number seed"
"\n", argv[0]) ;
return(0) ;
}
msglvl = atoi(argv[1]) ;
if ( strcmp(argv[2], "stdout") == 0 ) {
msgFile = stdout ;
} else {
length = strlen(argv[2]) + 1 + 4 ;
buffer = CVinit(length, '\0') ;
sprintf(buffer, "%s.%d", argv[2], myid) ;
if ( (msgFile = fopen(buffer, "w")) == NULL ) {
fprintf(stderr, "\n fatal error in %s"
"\n unable to open file %s\n",
argv[0], argv[2]) ;
return(-1) ;
}
CVfree(buffer) ;
}
n = atoi(argv[3]) ;
seed = atoi(argv[4]) ;
fprintf(msgFile,
"\n %s "
"\n msglvl -- %d"
"\n msgFile -- %s"
"\n n -- %d"
"\n seed -- %d"
"\n",
argv[0], msglvl, argv[2], n, seed) ;
fflush(msgFile) ;
/*
----------------------------
generate the ownersIV object
----------------------------
*/
MARKTIME(t1) ;
ownersIV = IV_new() ;
IV_init(ownersIV, n, NULL) ;
owners = IV_entries(ownersIV) ;
Drand_setDefaultFields(&drand) ;
Drand_setSeed(&drand, seed) ;
Drand_setUniform(&drand, 0, nproc) ;
Drand_fillIvector(&drand, n, owners) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : initialize the ownersIV object",
t2 - t1) ;
fflush(msgFile) ;
fprintf(msgFile, "\n\n ownersIV generated") ;
if ( msglvl > 2 ) {
IV_writeForHumanEye(ownersIV, msgFile) ;
} else {
IV_writeStats(ownersIV, msgFile) ;
}
fflush(msgFile) ;
/*
----------------------------------------------
set up the vec[] vector and fill owned entries
----------------------------------------------
*/
MARKTIME(t1) ;
objIV = IV_new() ;
IV_init(objIV, n, NULL) ;
vec = IV_entries(objIV) ;
IVfill(n, vec, -1) ;
Drand_setSeed(&drand, seed + myid) ;
Drand_setUniform(&drand, 0, n) ;
for ( ii = 0 ; ii < n ; ii++ ) {
if ( owners[ii] == myid ) {
vec[ii] = (int) Drand_value(&drand) ;
}
}
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : initialize the objIV object",
t2 - t1) ;
fflush(msgFile) ;
if ( msglvl > 2 ) {
IV_writeForHumanEye(objIV, msgFile) ;
} else {
IV_writeStats(objIV, msgFile) ;
}
fflush(msgFile) ;
/*
----------------------------------------
compute the local checksum of the vector
----------------------------------------
*/
for ( ii = 0, chksum = 0.0 ; ii < n ; ii++ ) {
if ( owners[ii] == myid ) {
chksum += (ii + 1)*(vec[ii] + 1) ;
}
}
fprintf(msgFile, "\n\n local partial chksum = %12.4e", chksum) ;
fflush(msgFile) ;
/*
-----------------------
get the global checksum
-----------------------
*/
rc = MPI_Allreduce((void *) &chksum, (void *) &globalsum,
1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD) ;
/*
--------------------------------
execute the all-gather operation
--------------------------------
*/
tag = 47 ;
IVzero(4, stats) ;
IV_MPI_allgather(objIV, ownersIV, stats,
msglvl, msgFile, tag, MPI_COMM_WORLD) ;
if ( msglvl > 0 ) {
fprintf(msgFile, "\n\n return from IV_MPI_allgather()") ;
fprintf(msgFile, "\n\n stats") ;
IVfprintf(msgFile, 4, stats) ;
fflush(msgFile) ;
}
if ( msglvl > 2 ) {
fprintf(msgFile, "\n\n objIV") ;
IV_writeForHumanEye(objIV, msgFile) ;
fflush(msgFile) ;
}
/*
-----------------------------------------
compute the checksum of the entire vector
-----------------------------------------
*/
for ( ii = 0, chksum = 0.0 ; ii < n ; ii++ ) {
chksum += (ii + 1)*(vec[ii] + 1) ;
}
fprintf(msgFile,
"\n globalsum = %12.4e, chksum = %12.4e, error = %12.4e",
globalsum, chksum, fabs(globalsum - chksum)) ;
fflush(msgFile) ;
/*
----------------
free the objects
----------------
*/
IV_free(ownersIV) ;
IV_free(objIV) ;
/*
------------------------
exit the MPI environment
------------------------
*/
MPI_Finalize() ;
fprintf(msgFile, "\n") ;
fclose(msgFile) ;
return(0) ; }
/*--------------------------------------------------------------------*/
|