Newer
Older
/***************************************************************************
DistributedGridIO_NeshFunction.h - description
-------------------
begin : October 5, 2017
copyright : (C) 2017 by Tomas Oberhuber
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#pragma once
#include <TNL/Functions/MeshFunction.h>
namespace TNL {
namespace Meshes {
namespace DistributedMeshes {
/*
* This variant cerate copy of MeshFunction but smaller, reduced to local entities, without overlap.
* It is slow and has high RAM consumption
*/
typename Device>
class DistributedGridIO<Functions::MeshFunction<MeshType>,LocalCopy,Device>
{
public:
typedef typename Functions::MeshFunction<MeshType> MeshFunctionType;
typedef typename MeshFunctionType::MeshType::CoordinatesType CoordinatesType;
typedef typename MeshFunctionType::MeshType::PointType PointType;
typedef typename MeshFunctionType::VectorType VectorType;
//typedef DistributedGrid< MeshType,MeshFunctionType::getMeshDimension()> DistributedGridType;
static bool save(const String& fileName, MeshFunctionType &meshFunction)
{
auto *distrGrid=meshFunction.getMesh().getDistributedMesh();
if(distrGrid==NULL) //not distributed
{
return meshFunction.save(fileName);
}
MeshType mesh=meshFunction.getMesh();
PointType spaceSteps=mesh.getSpaceSteps();
PointType origin=mesh.getOrigin();
CoordinatesType localSize=distrGrid->getLocalSize();
CoordinatesType localBegin=distrGrid->getLocalBegin();
Pointers::SharedPointer<MeshType> newMesh;
newMesh->setDimensions(localSize);
newMesh->setSpaceSteps(spaceSteps);
Tomáš Oberhuber
committed
newMesh->setOrigin(origin+TNL::Containers::Scale(spaceSteps,localBegin));
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
File meshFile;
meshFile.open( fileName+String("-mesh-")+distrGrid->printProcessCoords()+String(".tnl"),IOMode::write);
newMesh->save( meshFile );
meshFile.close();
VectorType newDof(newMesh-> template getEntitiesCount< typename MeshType::Cell >());
MeshFunctionType newMeshFunction;
newMeshFunction.bind(newMesh,newDof);
CoordinatesType zeroCoord;
zeroCoord.setValue(0);
CopyEntitiesHelper<MeshFunctionType>::Copy(meshFunction,newMeshFunction,localBegin,zeroCoord,localSize);
File file;
file.open( fileName+String("-")+distrGrid->printProcessCoords()+String(".tnl"), IOMode::write );
bool ret=newMeshFunction.save(file);
file.close();
return ret;
};
static bool load(const String& fileName,MeshFunctionType &meshFunction)
{
auto *distrGrid=meshFunction.getMesh().getDistributedMesh();
if(distrGrid==NULL) //not distributed
{
return meshFunction.boundLoad(fileName);
}
MeshType mesh=meshFunction.getMesh();
PointType spaceSteps=mesh.getSpaceSteps();
PointType origin=mesh.getOrigin();
CoordinatesType localSize=distrGrid->getLocalSize();
CoordinatesType localBegin=distrGrid->getLocalBegin();
Pointers::SharedPointer<MeshType> newMesh;
newMesh->setDimensions(localSize);
newMesh->setSpaceSteps(spaceSteps);
Tomáš Oberhuber
committed
newMesh->setOrigin(origin+TNL::Containers::Scale(spaceSteps,localBegin));
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
VectorType newDof(newMesh-> template getEntitiesCount< typename MeshType::Cell >());
MeshFunctionType newMeshFunction;
newMeshFunction.bind(newMesh,newDof);
CoordinatesType zeroCoord;
zeroCoord.setValue(0);
File file;
file.open( fileName+String("-")+distrGrid->printProcessCoords()+String(".tnl"), IOMode::read );
bool result=newMeshFunction.boundLoad(file);
file.close();
CopyEntitiesHelper<MeshFunctionType>::Copy(newMeshFunction,meshFunction,zeroCoord,localBegin,localSize);
return result;
};
};
/*
* Save distributed data into single file without overlaps using MPIIO and MPI datatypes,
* EXPLOSIVE: works with only Grids and MPI
* BAD IMPLEMENTTION creating MPI-Types at every save! -- I dont want contamine more places by MPI..
*/
#ifdef HAVE_MPI
template<typename MeshFunctionType>
class DistributedGridIO_MPIIOBase
{
public:
using RealType = typename MeshFunctionType::RealType;
typedef typename MeshFunctionType::MeshType MeshType;
typedef typename MeshFunctionType::MeshType::CoordinatesType CoordinatesType;
typedef typename MeshFunctionType::MeshType::PointType PointType;
typedef typename MeshFunctionType::VectorType VectorType;
//typedef DistributedGrid< MeshType,MeshFunctionType::getMeshDimension()> DistributedGridType;
static bool save(const String& fileName, MeshFunctionType &meshFunction, RealType *data)
{
auto *distrGrid=meshFunction.getMesh().getDistributedMesh();
if(distrGrid==NULL) //not distributed
{
return meshFunction.save(fileName);
}
MPI_Comm group=*((MPI_Comm*)(distrGrid->getCommunicationGroup()));
MPI_File file;
MPI_File_open( group,
const_cast< char* >( fileName.getString() ),
MPI_MODE_CREATE | MPI_MODE_WRONLY,
MPI_INFO_NULL,
&file);
int written=save(file,meshFunction, data,0);
MPI_File_close(&file);
return written>0;
};
static int save(MPI_File &file, MeshFunctionType &meshFunction, RealType *data, int offset)
{
auto *distrGrid=meshFunction.getMesh().getDistributedMesh();
MPI_Comm group=*((MPI_Comm*)(distrGrid->getCommunicationGroup()));
MPI_Datatype ftype;
MPI_Datatype atype;
int dataCount=CreateDataTypes(distrGrid,&ftype,&atype);
int headerSize;
MPI_File_set_view(file,0,MPI_BYTE,MPI_BYTE,"native",MPI_INFO_NULL);
if(Communicators::MpiCommunicator::GetRank(group)==0)
{
MPI_File_seek(file,offset,MPI_SEEK_SET);
headerSize=writeMeshFunctionHeader(file,meshFunction,dataCount);
}
MPI_Bcast(&headerSize, 1, MPI_INT,0, group);
offset +=headerSize;
MPI_File_set_view(file,offset,
Communicators::MPITypeResolver<RealType>::getType(),
ftype,"native",MPI_INFO_NULL);
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
MPI_Status wstatus;
MPI_File_write(file,data,1,atype,&wstatus);
MPI_Type_free(&atype);
MPI_Type_free(&ftype);
return headerSize+dataCount*sizeof(RealType); //size of written data
};
template<typename DitsributedGridType>
static int CreateDataTypes(DitsributedGridType *distrGrid,MPI_Datatype *ftype,MPI_Datatype *atype)
{
int dim=distrGrid->getMeshDimension();
int fstarts[dim];
int flsize[dim];
int fgsize[dim];
hackArray(dim,fstarts,distrGrid->getGlobalBegin().getData());
hackArray(dim,flsize,distrGrid->getLocalSize().getData());
hackArray(dim,fgsize,distrGrid->getGlobalSize().getData());
MPI_Type_create_subarray(dim,
fgsize,flsize,fstarts,
MPI_ORDER_C,
Communicators::MPITypeResolver<RealType>::getType(),
ftype);
MPI_Type_commit(ftype);
int agsize[dim];
int alsize[dim];
int astarts[dim];
hackArray(dim,astarts,distrGrid->getLocalBegin().getData());
hackArray(dim,alsize,distrGrid->getLocalSize().getData());
hackArray(dim,agsize,distrGrid->getLocalGridSize().getData());
MPI_Type_create_subarray(dim,
agsize,alsize,astarts,
MPI_ORDER_C,
Communicators::MPITypeResolver<RealType>::getType(),
atype);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
MPI_Type_commit(atype);
int dataCount=1;
for(int i=0;i<dim;i++)
dataCount*=fgsize[i];
return dataCount;
}
template<typename Index>
static void hackArray(int dim, int* out, Index* in)
{
if(dim==1)
{
out[0]=in[0];
}
if(dim==2)
{
out[1]=in[0];
out[0]=in[1];
}
if(dim==3)
{
out[0]=in[2];
out[1]=in[1];
out[2]=in[0];
}
}
static unsigned int writeMeshFunctionHeader(MPI_File file,MeshFunctionType &meshFunction, int length )
{
unsigned int size=0;
int count;
MPI_Status wstatus;
//Magic
MPI_File_write( file, const_cast< void* >( ( const void* ) "TNLMN" ), 5, MPI_CHAR,&wstatus );
MPI_Get_count(&wstatus,MPI_CHAR,&count);
size+=count*sizeof(char);
//Meshfunction type
String meshFunctionSerializationType=meshFunction.getSerializationTypeVirtual();
int meshFunctionSerializationTypeLength=meshFunctionSerializationType.getLength();
MPI_File_write(file,&meshFunctionSerializationTypeLength,1,MPI_INT,&wstatus);
MPI_Get_count(&wstatus,MPI_INT,&count);
size+=count*sizeof(int);
MPI_File_write(file,meshFunctionSerializationType.getString(),meshFunctionSerializationType.getLength(),MPI_CHAR,&wstatus);
MPI_Get_count(&wstatus,MPI_CHAR,&count);
size+=count*sizeof(char);
//Magic
MPI_File_write( file, const_cast< void* >( ( const void* ) "TNLMN" ),5,MPI_CHAR,&wstatus);
MPI_Get_count(&wstatus,MPI_CHAR,&count);
size+=count*sizeof(char);
//Vector Type
String dataSerializationType=meshFunction.getData().getSerializationTypeVirtual();
int dataSerializationTypeLength=dataSerializationType.getLength();
MPI_File_write(file,&dataSerializationTypeLength,1,MPI_INT,&wstatus);
MPI_Get_count(&wstatus,MPI_INT,&count);
size+=count*sizeof(int);
MPI_File_write(file,dataSerializationType.getString(),dataSerializationType.getLength(),MPI_CHAR,&wstatus);
MPI_Get_count(&wstatus,MPI_CHAR,&count);
size+=count*sizeof(char);
//Data count
MPI_File_write(file,&(length),1,MPI_INT,&wstatus);
MPI_Get_count(&wstatus,MPI_INT,&count);
size+=count*sizeof(int);
return size;
};
static bool load(const String& fileName,MeshFunctionType &meshFunction, double *data )
{
auto *distrGrid=meshFunction.getMesh().getDistributedMesh();
if(distrGrid==NULL) //not distributed
{
return meshFunction.boundLoad(fileName);
}
MPI_Comm group=*((MPI_Comm*)(distrGrid->getCommunicationGroup()));
MPI_File file;
MPI_File_open( group,
const_cast< char* >( fileName.getString() ),
MPI_MODE_RDONLY,
MPI_INFO_NULL,
&file );
bool ret= load(file, meshFunction, data,0)>0;
MPI_File_close(&file);
return ret;
}
/* Funky bomb - no checks - only dirty load */
static int load(MPI_File &file,MeshFunctionType &meshFunction, double *data, int offset )
{
auto *distrGrid=meshFunction.getMesh().getDistributedMesh();
MPI_Comm group=*((MPI_Comm*)(distrGrid->getCommunicationGroup()));
MPI_Datatype ftype;
MPI_Datatype atype;
int dataCount=CreateDataTypes(distrGrid,&ftype,&atype);
MPI_File_set_view(file,0,MPI_BYTE,MPI_BYTE,"native",MPI_INFO_NULL);
int headerSize=0;
if(Communicators::MpiCommunicator::GetRank(group)==0)
{
MPI_File_seek(file,offset,MPI_SEEK_SET);
headerSize=readMeshFunctionHeader(file,meshFunction,dataCount);
}
MPI_Bcast(&headerSize, 1, MPI_INT,0, group);
if(headerSize<0)
return false;
offset+=headerSize;
MPI_File_set_view(file,offset,
Communicators::MPITypeResolver<RealType>::getType(),
ftype,"native",MPI_INFO_NULL);
MPI_Status wstatus;
MPI_File_read(file,(void*)data,1,atype,&wstatus);
MPI_Type_free(&atype);
MPI_Type_free(&ftype);
return headerSize+dataCount*sizeof(RealType); //size of readed data;
};
//it shoudl check some loaded files...... but chcek nothing...
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
static int readMeshFunctionHeader(MPI_File file,MeshFunctionType &meshFunction, int length)
{
MPI_Status rstatus;
char buffer[255];
int size=0;
int count=0;
MPI_File_read(file, (void *)buffer,5, MPI_CHAR, &rstatus);//MAGIC
size+=5*sizeof(char);
MPI_File_read(file, (void *)&count,1, MPI_INT, &rstatus);//SIZE OF DATATYPE
size+=1*sizeof(int);
MPI_File_read(file, (void *)&buffer,count, MPI_CHAR, &rstatus);//DATATYPE
size+=count*sizeof(char);
MPI_File_read(file, (void *)buffer,5, MPI_CHAR, &rstatus);//MAGIC
size+=5*sizeof(char);
MPI_File_read(file, (void *)&count,1, MPI_INT, &rstatus);//SIZE OF DATATYPE
size+=1*sizeof(int);
MPI_File_read(file, (void *)&buffer,count, MPI_CHAR, &rstatus);//DATATYPE
size+=count*sizeof(char);
MPI_File_read(file, (void *)&count,1, MPI_INT, &rstatus);//DATACOUNT
size+=1*sizeof(int);
if(count!=length)
{
std::cerr<<"Chyba načítání MeshFunction, délka dat v souboru neodpovídá očekávané délce" << std::endl;
size=-1;
}
return size;
};
};
#endif
template<typename MeshType>
class DistributedGridIO<Functions::MeshFunction<MeshType>,MpiIO,TNL::Devices::Cuda>
{
public:
typedef typename Functions::MeshFunction<MeshType> MeshFunctionType;
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
static bool save(const String& fileName, MeshFunctionType &meshFunction)
{
#ifdef HAVE_MPI
if(Communicators::MpiCommunicator::IsInitialized())//i.e. - isUsed
{
using HostVectorType = Containers::Vector<typename MeshFunctionType::RealType, Devices::Host, typename MeshFunctionType::IndexType >;
HostVectorType hostVector;
hostVector=meshFunction.getData();
typename MeshFunctionType::RealType * data=hostVector.getData();
return DistributedGridIO_MPIIOBase<MeshFunctionType>::save(fileName,meshFunction,data);
}
#endif
std::cout << "MPIIO can be used only with MPICommunicator." << std::endl;
return false;
};
static bool load(const String& fileName,MeshFunctionType &meshFunction)
{
#ifdef HAVE_MPI
if(Communicators::MpiCommunicator::IsInitialized())//i.e. - isUsed
{
using HostVectorType = Containers::Vector<typename MeshFunctionType::RealType, Devices::Host, typename MeshFunctionType::IndexType >;
HostVectorType hostVector;
hostVector.setLike(meshFunction.getData());
double * data=hostVector.getData();
DistributedGridIO_MPIIOBase<MeshFunctionType>::load(fileName,meshFunction,data);
meshFunction.getData()=hostVector;
return true;
}
#endif
std::cout << "MPIIO can be used only with MPICommunicator." << std::endl;
return false;
};
};
template<typename MeshType>
class DistributedGridIO<Functions::MeshFunction<MeshType>,MpiIO,TNL::Devices::Host>
public:
typedef typename Functions::MeshFunction<MeshType> MeshFunctionType;
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
static bool save(const String& fileName, MeshFunctionType &meshFunction)
{
#ifdef HAVE_MPI
if(Communicators::MpiCommunicator::IsInitialized())//i.e. - isUsed
{
typename MeshFunctionType::RealType * data=meshFunction.getData().getData();
return DistributedGridIO_MPIIOBase<MeshFunctionType>::save(fileName,meshFunction,data);
}
#endif
std::cout << "MPIIO can be used only with MPICommunicator." << std::endl;
return false;
};
static bool load(const String& fileName,MeshFunctionType &meshFunction)
{
#ifdef HAVE_MPI
if(Communicators::MpiCommunicator::IsInitialized())//i.e. - isUsed
{
double * data=meshFunction.getData().getData();
return DistributedGridIO_MPIIOBase<MeshFunctionType>::load(fileName,meshFunction,data);
}
#endif
std::cout << "MPIIO can be used only with MPICommunicator." << std::endl;
return false;
};
};
}
}
}