00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00019
00020 #define MED_HAVE_MPI
00021
00022 #include <med.h>
00023 #define MESGERR 1
00024 #include "med_utils.h"
00025 #include "med_config.h"
00026
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <assert.h>
00030
00031 #if TIME_WITH_SYS_TIME
00032 # include <sys/time.h>
00033 # include <time.h>
00034 #else
00035 # if HAVE_SYS_TIME_H
00036 # include <sys/time.h>
00037 # else
00038 # include <time.h>
00039 # endif
00040 #endif
00041
00042 #ifndef HAVE_UNISTD_H
00043 #error "unistd.h reuired."
00044 #endif
00045
00046 #include "getBlocksOfEntitiesPartition.h"
00047 #include "generateDatas.h"
00048 #include "generateFilterArray.h"
00049
00050
00051 #ifdef DEF_LECT_ECR
00052 #define MODE_ACCES MED_ACC_RDWR
00053 #elif DEF_LECT_AJOUT
00054 #define MODE_ACCES MED_ACC_RDEXT
00055 #else
00056 #define MODE_ACCES MED_ACC_CREAT
00057 #endif
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 int main (int argc, char **argv)
00083
00084
00085 {
00086 med_err _ret=0;
00087 med_idt _fid;
00088
00089 int mpi_size, mpi_rank;
00090 MPI_Comm comm = MPI_COMM_WORLD;
00091 MPI_Info info = MPI_INFO_NULL;
00092
00093 med_int _nbofentitiesfiltered=0;
00094 med_int *_filterarray=NULL;
00095
00096
00097 MPI_Init(&argc, &argv);
00098 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
00099 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
00100
00101
00102 med_err generateFieldFile( const med_size nbofentities, const med_size nbofvaluesperentity, const med_size nbofconstituentpervalue,
00103 const med_switch_mode constituentmode,void (*getBlockOfEntities)(), const med_int nbblocksperproc, void (*generateDatas)(),
00104 const med_storage_mode storagemode, const med_size profilearraysize, const char * const fieldnameprefix ) {
00105
00106
00107 med_err _ret=-1;
00108 char _filename [255]="";
00109 char _meshname[MED_NAME_SIZE+1]="Empty mesh";
00110 med_int _meshdim=3;
00111 char _meshcomponentname[3*MED_SNAME_SIZE+1] = "x y z ";
00112 char _meshcomponentunit[3*MED_SNAME_SIZE+1] = "cm cm cm ";
00113 char _fieldname [MED_NAME_SIZE+1]="";
00114 char *componentname,*componentunit;
00115 char _profilename[MED_NAME_SIZE+1]=MED_NO_PROFILE;
00116 med_int *_profilearray=0;
00117 int _i=0,_j=0,_k=0, _lastusedrank=0;
00118 med_size _blocksize=0,_lastblocksize=0,_count=0,_stride=0,_start=0,_index=0;
00119 med_float *_arrayvalues;
00120 med_filter filter = MED_FILTER_INIT;
00121 med_size _nbofusedentities = nbofentities;
00122 med_size _io_count = nbblocksperproc;
00123 med_idt _fidseq;
00124
00125
00126 char *_MED_MODE_SWITCH_MSG[3]={"MED_UNDEF_INTERLACE","MED_FULL_INTERLACE", "MED_NO_INTERLACE"};
00127 char *_MED_STORAGE_MODE_MSG[3]={"MED_NO_PFLMODE","MED_GLOBAL_PFLMODE", "MED_COMPACT_PFLMODE"};
00128
00129 med_geometry_type _geotype = MED_TRIA6;
00130 med_int _geodim = _geotype/100;
00131 med_int _geonbofnodes = _geotype%100;
00132 char _ipointname[MED_NAME_SIZE+1];
00133 med_float* _ipointrefcoo = 0;
00134 med_int _ipoint = nbofvaluesperentity;
00135 med_float* _ipointcoo = 0;
00136 med_float* _ipointwg = 0;
00137
00138 sprintf(_filename,"%s_CPU-%03d_@_%s_%s.med",fieldnameprefix,mpi_size,_MED_MODE_SWITCH_MSG[constituentmode],_MED_STORAGE_MODE_MSG[storagemode]);
00139
00140
00141 if ((_fid = MEDparFileOpen(_filename, MODE_ACCES ,comm, info)) < 0){
00142 MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_FILE,_filename);
00143 goto ERROR;
00144 }
00145
00146
00147 if (MEDmeshCr( _fid,_meshname,_meshdim,_meshdim, MED_UNSTRUCTURED_MESH,
00148 "Un maillage pour le test parallel","s", MED_SORT_DTIT,
00149 MED_CARTESIAN, _meshcomponentname, _meshcomponentunit) < 0) {
00150 MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_MESH,_meshname);
00151 goto ERROR;
00152 };
00153
00154 componentname = (char*) malloc((nbofconstituentpervalue*MED_SNAME_SIZE+1)*sizeof(char));
00155 componentunit = (char*) malloc((nbofconstituentpervalue*MED_SNAME_SIZE+1)*sizeof(char));
00156
00157 strcpy(componentname,"");
00158 strcpy(componentunit,"");
00159 strcpy(_fieldname,fieldnameprefix);
00160 if ( MEDfieldCr(_fid,_fieldname,MED_FLOAT64,nbofconstituentpervalue,componentname,componentunit,"s",_meshname ) < 0) {
00161 MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_FIELD,_fieldname);
00162 goto ERROR;
00163 };
00164 free(componentname);
00165 free(componentunit);
00166
00167
00168 if ( _ipoint > 1 ) {
00169
00170 MESSAGE("Creating a localization of integration points...");
00171 strcpy(_ipointname,_fieldname);
00172 strcat(_ipointname,"_loc");
00173
00174
00175 _ipointrefcoo = (med_float *) calloc(_geodim*_geonbofnodes,sizeof(med_float));
00176 _ipointcoo = (med_float *) calloc(_ipoint*_geodim,sizeof(med_float));
00177 _ipointwg = (med_float *) calloc(_ipoint,sizeof(med_float));
00178
00179 if (MEDlocalizationWr(_fid, _ipointname, _geotype, _geotype/100, _ipointrefcoo, constituentmode,
00180 _ipoint, _ipointcoo, _ipointwg, MED_NO_INTERPOLATION, MED_NO_MESH_SUPPORT ) < 0) {
00181 MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_LOCALIZATION,_ipointname);
00182 ISCRUTE_int(constituentmode);
00183 goto ERROR;
00184 }
00185 free(_ipointrefcoo );
00186 free(_ipointcoo );
00187 free(_ipointwg );
00188
00189 } else {
00190 strcpy(_ipointname,MED_NO_LOCALIZATION);
00191 }
00192
00193 if (profilearraysize) {
00194 MESSAGE("Creating a profile...");
00195
00196 strcpy(_profilename,_fieldname);strcat(_profilename,"_profile");
00197
00198 _profilearray = (med_int*) calloc(profilearraysize,sizeof(med_int));
00199
00200 for (_i=0; _i < profilearraysize; ++_i) _profilearray[_i]=_i;
00201 if ( MEDprofileWr(_fid,_profilename,profilearraysize,_profilearray) < 0) {
00202 MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_PROFILE,_profilename);
00203 goto ERROR;
00204 };
00205 _nbofusedentities = profilearraysize;
00206 } else {
00207 strcpy(_profilename,MED_NO_PROFILE);
00208 }
00209
00210
00211 MESSAGE("Generating partition...");
00212 getBlockOfEntities ( mpi_rank , mpi_size, _nbofusedentities,
00213 &_start, &_stride, &_io_count, &_blocksize,
00214 &_lastusedrank, &_lastblocksize);
00215
00216 _count=_io_count;
00217 MESSAGE("Generating filter...");
00218 if ( MEDfilterBlockOfEntityCr(_fid, nbofentities, nbofvaluesperentity, nbofconstituentpervalue,
00219 MED_ALL_CONSTITUENT, constituentmode, storagemode, _profilename,
00220 _start,_stride,_count,_blocksize,_lastblocksize, &filter) < 0 ) {
00221 MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_FILTER,"");
00222 goto ERROR;
00223 }
00224
00225 MESSAGE("Generating datas...");
00226 generateDatas(mpi_rank, _lastusedrank, sizeof(med_float),
00227 storagemode, profilearraysize, _profilearray,
00228 _start, _stride, _count, _blocksize, _lastblocksize,
00229 nbofentities, nbofvaluesperentity, nbofconstituentpervalue,
00230 &_arrayvalues );
00231
00232 MESSAGE("Writing field...");
00233 if ( MEDfieldValueAdvancedWr(_fid,_fieldname,MED_NO_DT,MED_NO_IT,0.0, MED_CELL, _geotype,
00234 _ipointname, &filter, (unsigned char*)_arrayvalues ) < 0) {
00235 MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_FIELD,_fieldname);
00236 ISCRUTE(mpi_rank);
00237 goto ERROR;
00238 }
00239
00240
00241
00242 H5Fflush(_fid, H5F_SCOPE_GLOBAL );
00243
00244
00245 if (mpi_rank == 0 ) {
00246 MESSAGE("Reading field...");
00247
00248
00249 med_int _nbofentitiesarrayvalues=0;
00250 med_float *_filteredarrayvalues=NULL;
00251 med_filter filter2=MED_FILTER_INIT;
00252 int _ind=0;
00253 FILE * _asciifile;
00254 char _asciifilename[255]="";
00255
00256
00257 if ((_fidseq = MEDfileOpen(_filename, MED_ACC_RDONLY )) < 0){
00258 MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_FILE,_filename);
00259 goto ERROR;
00260 }
00261
00262 sprintf(_asciifilename,"%s_CPU-%03d_@_%s_%s.ascii",fieldnameprefix,mpi_size,_MED_MODE_SWITCH_MSG[constituentmode],_MED_STORAGE_MODE_MSG[storagemode]);
00263 _asciifile=fopen(_asciifilename, "w");
00264
00265
00266
00267 if (!_filterarray)
00268 if ( generateFilterArray( nbofentities, nbofvaluesperentity, nbofconstituentpervalue,
00269 profilearraysize, _profilearray,
00270 &_nbofentitiesfiltered, &_filterarray ) < 0 ) {
00271 goto ERROR;
00272 }
00273
00274 ISCRUTE(_nbofentitiesfiltered);
00275
00276 for (_i=0; _i < _nbofentitiesfiltered; ++_i ) {
00277
00278 fprintf(_asciifile,"%d ",_filterarray[_i]) ;
00279 }
00280 fprintf(_asciifile,"\n") ;
00281
00282
00283
00284 if ( profilearraysize ) {
00285 _nbofentitiesarrayvalues = profilearraysize;
00286 } else {
00287 _nbofentitiesarrayvalues = nbofentities;
00288 }
00289
00290 _filteredarrayvalues = (med_float*) malloc(_nbofentitiesarrayvalues*
00291 nbofvaluesperentity*
00292 nbofconstituentpervalue*sizeof(med_float));
00293
00294
00295 for (_i=0;_i<_nbofentitiesarrayvalues*nbofvaluesperentity*nbofconstituentpervalue; ++_i)
00296 _filteredarrayvalues[_i]=-_i;
00297
00298
00299
00300 if ( MEDfilterEntityCr(_fidseq, nbofentities, nbofvaluesperentity, nbofconstituentpervalue,
00301 MED_ALL_CONSTITUENT, constituentmode, storagemode, _profilename,
00302 _nbofentitiesfiltered,_filterarray, &filter2) < 0 ) {
00303 MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_FILTER,"");
00304 goto ERROR;
00305 }
00306
00307 if ( MEDfieldValueAdvancedRd(_fidseq,_fieldname,MED_NO_DT,MED_NO_IT, MED_CELL, _geotype,
00308 &filter2, (unsigned char*)_filteredarrayvalues ) < 0) {
00309 MED_ERR_(_ret,MED_ERR_READ,MED_ERR_FIELD,_fieldname);
00310 ISCRUTE(mpi_rank);
00311 goto ERROR;
00312 }
00313
00314
00315
00316 if ( storagemode == MED_GLOBAL_PFLMODE ) {
00317 switch (constituentmode) {
00318 case MED_FULL_INTERLACE:
00319 for (_i=0; _i < _nbofentitiesfiltered; ++_i)
00320 for (_j=0; _j < nbofvaluesperentity; ++_j)
00321 for (_k=0; _k < nbofconstituentpervalue; ++_k) {
00322 _ind = (_filterarray[_i]-1)*nbofvaluesperentity*nbofconstituentpervalue+ _j*nbofconstituentpervalue+_k;
00323
00324 fprintf(_asciifile,"%f\n",_filteredarrayvalues[_ind]) ;
00325 }
00326 break;
00327 case MED_NO_INTERLACE:
00328 for (_j=0; _j < _nbofentitiesfiltered; ++_j)
00329 for (_k=0; _k < nbofvaluesperentity; ++_k)
00330 for (_i=0; _i < nbofconstituentpervalue; ++_i) {
00331 _ind =_i*nbofentities*nbofvaluesperentity+ (_filterarray[_j]-1)*nbofvaluesperentity +_k;
00332
00333 fprintf(_asciifile,"%f\n",_filteredarrayvalues[_ind]);
00334 }
00335 break;
00336 }
00337 } else
00338 switch (constituentmode) {
00339 case MED_FULL_INTERLACE:
00340 for (_i=0; _i < _nbofentitiesfiltered; ++_i )
00341 for (_j=0; _j < nbofvaluesperentity; ++_j)
00342 for (_k=0; _k < nbofconstituentpervalue; ++_k) {
00343 _ind = (_filterarray[_i]-1)*nbofvaluesperentity*nbofconstituentpervalue+_j*nbofconstituentpervalue+_k;
00344
00345 fprintf(_asciifile,"%f\n",_filteredarrayvalues[_ind]) ;
00346 }
00347 break;
00348 case MED_NO_INTERLACE:
00349 for (_j=0; _j < _nbofentitiesfiltered; ++_j)
00350 for (_k=0; _k < nbofvaluesperentity; ++_k)
00351 for (_i=0; _i < nbofconstituentpervalue; ++_i) {
00352 _ind =_i*_nbofentitiesarrayvalues*nbofvaluesperentity+ (_filterarray[_j]-1)*nbofvaluesperentity +_k;
00353
00354 fprintf(_asciifile,"%f\n",_filteredarrayvalues[_ind]);
00355 }
00356 break;
00357 }
00358
00359
00360 free(_filteredarrayvalues);
00361
00362 fclose(_asciifile);
00363
00364 if ( MEDfilterClose(&filter2) < 0 ) {
00365 MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_FILTER,"");
00366 goto ERROR;
00367 }
00368
00369 }
00370
00371 if ( MEDfilterClose(&filter) < 0 ) {
00372 MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_FILTER,"");
00373 goto ERROR;
00374 }
00375
00376
00377 _ret=0;
00378 ERROR:
00379 if (_arrayvalues) free(_arrayvalues);
00380 if (profilearraysize) free(_profilearray);
00381
00382 if ( MEDfileClose(_fid) < 0) {
00383 MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_FILE,""); _ret = -1;
00384 }
00385
00386 if (mpi_rank == 0 ) {
00387 if ( MEDfileClose(_fidseq) < 0) {
00388 MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_FILE,""); _ret = -1;
00389 }
00390 }
00391
00392 return _ret;
00393 }
00394
00395
00396
00397
00398
00399 med_size _nbblocksperproc = 0;
00400 int _nbofentities = 0;
00401 int _nbofvaluesperentity = 0;
00402 int _nbofconstituentpervalue = 0;
00403
00404 if (mpi_rank == 0 ) {
00405
00406 struct tm *_tm ;
00407 time_t _tt=time(0);
00408 _tm = localtime(&_tt);
00409
00410 srandom((*_tm).tm_sec * (*_tm).tm_min );
00411 _nbblocksperproc = 1 + (int) (mpi_size * (random() / (RAND_MAX + 1.0)));
00412 _nbofentities = 1 + (int) (1000.0 * (random() / (RAND_MAX + 1.0)));
00413 _nbofvaluesperentity = 1 + (int) (11.0 * (random() / (RAND_MAX + 1.0)));
00414 _nbofconstituentpervalue = 1 + (int) (7.0 * (random() / (RAND_MAX + 1.0)));
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 }
00425
00426 assert(sizeof(med_size) == (sizeof(MPI_LONG)));
00427
00428 MPI_Bcast(&_nbblocksperproc , 1, MPI_LONG, 0, MPI_COMM_WORLD);
00429 MPI_Bcast(&_nbofentities , 1, MPI_LONG, 0, MPI_COMM_WORLD);
00430 MPI_Bcast(&_nbofvaluesperentity , 1, MPI_LONG, 0, MPI_COMM_WORLD);
00431 MPI_Bcast(&_nbofconstituentpervalue , 1, MPI_LONG, 0, MPI_COMM_WORLD);
00432
00433 char _fieldnameprefix[255] = "";
00434
00435 sprintf(_fieldnameprefix,"NENT-%03d_NVAL-%03d_NCST-%03d_NBL-%03llu",_nbofentities,_nbofvaluesperentity,
00436 _nbofconstituentpervalue,_nbblocksperproc);
00437
00438 void (*generateDatas)() = 0;
00439 med_switch_mode _switchmode = MED_UNDEF_INTERLACE;
00440 med_storage_mode _storagemode = MED_UNDEF_PFLMODE;
00441 med_int _profilearraysize=0;
00442
00443
00444 for (_switchmode = MED_FULL_INTERLACE ; _switchmode <= MED_NO_INTERLACE; ++_switchmode) {
00445
00446 if ( _switchmode == MED_FULL_INTERLACE ) generateDatas = generateFullIDatas;
00447 else generateDatas = generateNoIDatas;
00448
00449 for (_storagemode = MED_GLOBAL_PFLMODE ; _storagemode <= MED_COMPACT_PFLMODE; ++_storagemode) {
00450
00451 if ( (_storagemode == MED_GLOBAL_PFLMODE ) && (_profilearraysize) ) _profilearraysize=0;
00452
00453 if ( generateFieldFile( _nbofentities, _nbofvaluesperentity, _nbofconstituentpervalue,
00454 _switchmode, getCyclicBlocksOfEntities, _nbblocksperproc, generateDatas,
00455 _storagemode, _profilearraysize, _fieldnameprefix ) < 0 ) {
00456 MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_FIELD,_fieldnameprefix);
00457 ISCRUTE(mpi_rank);
00458 goto ERROR;
00459 }
00460
00461 }
00462 }
00463
00464
00465 _ret = 0;
00466 ERROR:
00467
00468 if (_filterarray) free(_filterarray);
00469
00470
00471 MPI_Finalize();
00472
00473 return -1;
00474
00475
00476 }
00477
00478
00479
00480