-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLATfield2_IO_server.hpp
executable file
·2178 lines (1701 loc) · 81.3 KB
/
LATfield2_IO_server.hpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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
413
414
415
416
417
418
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
456
457
458
459
460
461
462
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef LATFIELD2_IOSERVER_HPP
#define LATFIELD2_IOSERVER_HPP
/*! \file LATfield2_IO_server.hpp
\brief LATfield2_IO_server.hpp contains the class IOserver definition.
\author David Daverio
*/
#include "int2string.hpp"
#include <cstring>
#include "mpi.h"
#define SERVER_READY 0
#define SERVER_BUSY 1
#define OSTREAM_SUCCESS 0
#define OSTREAM_FAIL 1
#define CONTROL_STOP 0
#define CONTROL_OPEN_OSTREAM 1
#define CONTROL_CLOSE_OSTREAM 2
#define SERVER_STATE_TAG 0
#define SERVER_CONTROL_TAG 1
//tags 0 to 9999 reserved for files_ID
#define FILE_OPEN_TAG 10000
#define FILE_CLOSE_TAG 10001
#define CLOSE_OSTREAM_TAG 10002
#define GET_DATA_TAG 10003
#define GET_ATTR_TAG 10004
#define GET_DSET_TAG 10005
#define GET_HEADER_TAG 10006
#define OFFSET_ATTR_TAG 10050
#define OFFSET_DSET_TAG 20050
#define UNSTRUCTURED_BIN_FILE 0
#define UNSTRUCTURED_H5_FILE 1
#define STRUCTURED_H5_FILE 2
#define DEFAULT_SEND 0
#define BUFFERED_SEND 1
#define IO_CORE_BUFFER_SIZE 1073741824
#define MPI_BSEND_BUFFER_SIZE 40960
/*! \struct ioserver_file
\brief Structure describing a output server file (used by compute processes)
*/
struct ioserver_file
{
int ID;
bool is_open;
int type;
int sizeof_mem_dtype; //used only by h5 files
int dim; //used for h5 structured file
int components; //used for h5 structured file
int array_size; //used for h5 structured file
ioserver_file(){
ID=-1;
type=-1;
is_open = false;
sizeof_mem_dtype = -1;
dim =-1;
components=-1;
array_size=-1;
}
};
#ifndef DOXYGEN_SHOULD_SKIP_THIS
struct h5_attr
{
string name;
hid_t dtype;
hsize_t size;
char * attr;
};
struct h5_dset
{
string name;
hid_t dtype;
hsize_t dim;
hsize_t * size;
char * dset;
};
struct unstruct_message
{
char * data;
long size;
int core;
};
bool operator<(struct unstruct_message m,struct unstruct_message m1){return (m.core<m1.core);}
struct unstruct_bin_file
{
char * filename;
int fnSize;
int ID;
int isClosed;
int sizeof_mem_dtype;
int * isClosed_client;
int totalSize;
int headerSize;
char * header;
list<unstruct_message> msg;
};
bool operator<(struct unstruct_bin_file f,struct unstruct_bin_file f1){return (f.ID<f1.ID);}
struct unstruct_h5_file : unstruct_bin_file
{
hid_t mem_dtype;
hid_t file_dtype;
list<h5_attr> attr;
list<h5_dset> dset;
};
bool operator<(struct unstruct_h5_file f,struct unstruct_h5_file f1){return (f.ID<f1.ID);}
struct struct_message
{
char * data;
hsize_t * size;
hsize_t * offset;
};
struct struct_h5_file
{
char * filename;
int fnSize;
int ID;
int isClosed;
int * isClosed_client;
int sizeof_mem_dtype;
hid_t mem_dtype;
hid_t file_dtype;
int dim;
hsize_t * size;
hsize_t offset;
int components;
int array_size;
list<struct_message> msg;
list<h5_attr> attr;
list<h5_dset> dset;
};
bool operator<(struct struct_h5_file f,struct struct_h5_file f1){return (f.ID<f1.ID);}
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/*! \class IOserver
\brief \brief A class to handle the outputs using MPI process reserved for output purpose.
The output server is a group of processes reserved for output purpose. It's usage is exposed in details by the Ioserver example.
As for the parallel object, users should never instanciate an IOserver object as the IOserver objet (ioserver) is instanciate within the library header.
*/
class IOserver {
public:
~IOserver();
/*!
Initialize the I/O server, this method is called by parallel.initialize(...). Should never be called. When using the server, the method parallel.initalize() is used to initialize both the parallel and ioserver objects.
*/
void initialize(int proc_size0,int proc_size1,
int IOserver_size, int IO_node_size);
/*! \brief Server method (only called by server nodes)
Method which is called to start the server.
*/
void start();
/*! \brief Client method (only called by compute nodes)
Method which is called to stop the server.
*/
void stop();
/*! \brief Client method (only called by compute nodes)
Method to open an Ostream. Meaning a stream from the compute to the server processes.
\return OSTREAM_SUCCESS if the stream is open.
\return OSTREAM_FAIL if the stream cannot be open.
*/
int openOstream();
/*! \brief Client method (only called by compute nodes)
Method to close the current Ostream. After the stream is closed, the server will start to write the files it have in memory.
*/
void closeOstream();
/*! \brief Client method (only called by compute nodes)
Method to create a new file, it return the fileID.
\param string filename: name of the file (including the path...)
\param int type: type of the file. Can take the value: UNSTRUCTURED_BIN_FILE, UNSTRUCTURED_H5_FILE or STRUCTURED_H5_FILE.
\param hid_t mem_datatype: used only for HDF5 files, hdf5 datatype used for memory.
\param hid_t file_datatype: used only for HDF5 files, hdf5 datatype used for memory.
\param LATfield2::Lattice * lat: used only for structured HDF5 files, pointer to a Lattice object. The lattice describe the dataset used for the file, and its distribution in the compute process grid.
\param int components: used only for structured HDF5 files, number of elements per lattice per lattice site.
\param int array_size: used only for structured HDF5 files, each elements/components can be an array of a given type, size of this array.
\return oiserver_file
*/
ioserver_file openFile(string file_name,
int type,
hid_t mem_datatype = -1,
hid_t file_datatype = -1,
LATfield2::Lattice * lat = NULL,
int components = 1,
int array_size = 1);
/*! \brief Client method (only called by compute nodes)
Method to close a given file.
\param ioserver_file file: file to close.
*/
void closeFile(ioserver_file file);
/*! \brief Client method (only called by compute nodes)
Method send data to unstructured files (UNSTRUCTURED_BIN_FILE and UNSTRUCTURED_H5_FILE).
\param ioserver_file file: destination file
\param char * message: pointer to the array containing the data to be send.
\param long size: size of the message array, in byte.
\param int sendType: method use for the MPI send. Default is: DEFAULT_SEND, using MPI_Send. BUFFERED_SEND will use MPI_Bsend
*/
void sendData(ioserver_file file, char * message,long size,int sendType);
/*! \brief Client method (only called by compute nodes)
Method send data to an UNSTRUCTURED_H5_FILE files.
\param ioserver_file file: destination file
\param char * message: pointer to the array containing the data to be send. Stored continuously, in row major order.
\param hsize_t * size: pointer to an array contining the size of each demension of the sub-lattice which is sent.
\param hsize_t * offset: offset of the sub-lattice in respect to the entire lattice (global coordinate of the lowest site of the message.).
\param int sendType: method use for the MPI send. Default is: DEFAULT_SEND, using MPI_Send. BUFFERED_SEND will use MPI_Bsend
*/
void sendData(ioserver_file file, char * message,hsize_t * size, hsize_t * offset,int sendType);
/*! \brief Client method (only called by compute nodes)
Method send a header to an UNSTRUCTURED_BIN_FILE files. The header is sent only the root compute process of the server nodes. This list of processes can be selected using: if(isClientFileRoot_){}
Headers are send using MPI_Bsend.
\param ioserver_file file: destination file
\param char * header: pointer to the array containing the header to send.
\param int size: size of the header in byte.
*/
void sendHeader(ioserver_file file,char* header,int size);
/*! \brief Client method (only called by compute nodes)
Method send a attribute to HDF5 files. The attribute is sent only the root compute process of the server nodes. This list of processes can be selected using: if(isClientFileRoot_){}
Attributes are send using MPI_Bsend.
\param ioserver_file file: destination file
\param string attr_name: string containing the name of the attribute to write.
\param char * attr: pointer to the array containing the attribute to send.
\param int size: size of the attr array. Given in units of H5Tget_size(dtype).
\param hid_t dtype: HDF5 datatype of the attribute.
*/
void sendATTR(ioserver_file file,string attr_name,char * attr, int size,hid_t dtype);
/*! \brief Client method (only called by compute nodes)
Method send a dataset to an HDF5 files. The dataset is sent only the root compute process of the server nodes. This list of processes can be selected using: if(isClientFileRoot_){}
Attributes are send using MPI_Bsend.
\param ioserver_file file: destination file
\param string dset_name: string containing the name of the dataset to write.
\param char * dset: pointer to the array containing the dataset to send.
\param hsize_t dim: number of dismension of the dataset.
\param hsize_t * size: size of each dimension of the dataset.
\param hid_t dtypee: HDF5 datatype of the attribute.
*/
void sendDataset(ioserver_file file,string dset_name, char * dset, hsize_t dim, hsize_t * size, hid_t dtype);
/*! \brief Client method (only called by compute nodes)
\return MPI_Comm compute_file_comm_: This communicator contains all compute processes linked to the same IO node. All compute processes contained in this communicator will write within the same files. This communicator is used to broadcast the ID of a file to each processes which share the same file. It is used by the openFile(...) method of the IOserver class.
*/
MPI_Comm compute_file_comm(){return compute_file_comm_;};
/*! \brief Client method (only called by compute nodes)
\return int compute_file_size_: size of the compute_file_comm_ communicator.
\sa compute_file_comm()
*/
int compute_file_size(){return compute_file_size_;};
/*! \brief Client method (only called by compute nodes)
\return int compute_file_rank_: rank in the compute_file_comm_ communicator.
\sa compute_file_comm()
*/
int compute_file_rank(){return compute_file_rank_;};
/*! \brief Client & server method
\return int io_node_number_: number of server nodes.
*/
int io_node_number(){return io_node_number_;};
/*! \brief Client & server method
\return int io_node_number_: return the node in which this process is.
*/
int my_node(){return my_node_;};
/*! \brief Client method (only called by compute nodes)
\return bool isClientFileRoot: return true if the process is the root of the compute_file_comm_.
*/
bool isClientFileRoot(){return isClientFileRoot_;};
private:
list<unstruct_bin_file> usb_files_;
list<unstruct_h5_file> ush_files_;
list<struct_h5_file> sh_files_;
void ostream();
void write_files();
MPI_Comm compute_world_comm_;
MPI_Group compute_world_group_;
MPI_Comm io_world_comm_;
MPI_Group io_world_group_;
MPI_Comm compute_file_comm_;
MPI_Group compute_file_group_;
MPI_Comm io_node_comm_;
MPI_Group io_node_group_;
MPI_Comm client_comm_;
MPI_Group client_group_;
MPI_Comm sync_global_comm_;
MPI_Group sync_global_group_;
int io_node_number_;
int io_node_size_;
int my_node_;
int io_node_rank_;
int compute_file_size_;
int compute_file_rank_;
int client_size_;
int proc_size0_;
int proc_size1_;
bool isIO_;
bool isRoot_;
bool isClientRoot_;
bool isClientFileRoot_;
bool isIONodeRoot_;
int state_;
int filesNumber_;
char * data_;
char * wp_data_;
char * sendBuffer_;
};
IOserver::~IOserver()
{
int * size;
*size =MPI_BSEND_BUFFER_SIZE;
MPI_Buffer_detach(sendBuffer_,size);
free(sendBuffer_);
//cout<<size<<endl;
delete[] data_;
}
void IOserver::initialize(int proc_size0,int proc_size1,
int IOserver_size, int IO_node_size)
{
if((proc_size0*proc_size1) % IOserver_size!=0)
{
cout<<"IOserver: wrong number of total process"<<endl;
exit(-44);
}
if( IOserver_size % IO_node_size!=0)
{
cout<<"IOserver: IOserver_size % IO_node_size!=0"<<endl;
exit(-44);
}
if(proc_size1 % (IOserver_size/IO_node_size) !=0)
{
cout<<"IOserver: proc_size1 % (IOserver_size/IO_node_size)!=0"<<endl;
exit(-44);
}
if(IOserver_size % proc_size1 !=0)
{
cout<<"IOserver: IOserver_size % proc_size1 !=0"<<endl;
exit(-44);
}
if(proc_size0 % (IOserver_size / proc_size1) !=0)
{
cout<<"IOserver: proc_size0 % (IOserver_size / proc_size1) !=0"<<endl;
exit(-44);
}
if(IO_node_size % (IOserver_size / proc_size1) !=0)
{
cout<<"IOserver: IO_node_size % (IOserver_size / proc_size1) !=0"<<endl;
exit(-44);
}
data_ = new char[IO_CORE_BUFFER_SIZE];
sendBuffer_ = (char*)malloc(MPI_BSEND_BUFFER_SIZE);
MPI_Buffer_attach(sendBuffer_,MPI_BSEND_BUFFER_SIZE);
proc_size0_ = proc_size0;
proc_size1_ = proc_size1;
MPI_Group groupTemp1,groupTemp2;
//int world_rank;
int rank;
int compute_rank;
int io_world_rank;
int rang[3];
MPI_Group world_group;
MPI_Comm_group(MPI_COMM_WORLD,&world_group);
rang[0]=0;
rang[1]=(proc_size0*proc_size1)-1;
rang[2]=1;
MPI_Group_range_incl(world_group,1,&rang,&compute_world_group_);
MPI_Comm_create(MPI_COMM_WORLD,compute_world_group_ , &compute_world_comm_);
MPI_Group_rank(compute_world_group_, &compute_rank);
rang[0]=proc_size0*proc_size1;
rang[1]=(proc_size0*proc_size1) + IOserver_size - 1 ;
rang[2]=1;
MPI_Group_range_incl(world_group,1,&rang,&io_world_group_);
MPI_Comm_create(MPI_COMM_WORLD,io_world_group_ , &io_world_comm_);
MPI_Group_rank(io_world_group_, &io_world_rank);
if(io_world_rank != MPI_UNDEFINED)isIO_=true;
io_node_size_ = IO_node_size;
io_node_number_ = IOserver_size / IO_node_size;
compute_file_size_ = proc_size0*proc_size1 / io_node_number_;
client_size_ = proc_size0*proc_size1 / IOserver_size;
if(io_world_rank != MPI_UNDEFINED)
my_node_ = io_world_rank / IO_node_size;
else
my_node_ = compute_rank / compute_file_size_;
rang[0]= (proc_size0*proc_size1) + (my_node_ * io_node_size_) ;
rang[1]= (proc_size0*proc_size1) + ((my_node_+1) * io_node_size_) -1;
rang[2]=1;
MPI_Group_range_incl(world_group,1,&rang,&io_node_group_);
MPI_Comm_create(MPI_COMM_WORLD,io_node_group_ , &io_node_comm_);
MPI_Group_rank(io_node_group_, &io_node_rank_);
if(io_node_rank_==0)isIONodeRoot_=true;
else isIONodeRoot_=false;
rang[0]= my_node_ * compute_file_size_ ;
rang[1]= ((my_node_+1) * compute_file_size_) -1;
rang[2]=1;
MPI_Group_range_incl(world_group,1,&rang,&compute_file_group_);
MPI_Comm_create(MPI_COMM_WORLD,compute_file_group_ , &compute_file_comm_);
MPI_Group_rank(compute_file_group_, &rank);
if(rank!=MPI_UNDEFINED)compute_file_rank_=rank;
else compute_file_rank_ = -1;
if(rank==0)isClientFileRoot_ =true;
else isClientFileRoot_=false;
rang[0]=proc_size0*proc_size1;
rang[1]=0;
MPI_Group_incl(world_group,2,&rang[0],&sync_global_group_);
MPI_Comm_create(MPI_COMM_WORLD,sync_global_group_ , &sync_global_comm_);
MPI_Group_rank(sync_global_group_, &rank);
if(rank!=MPI_UNDEFINED)isRoot_=true;
else isRoot_=false;
int which_master;
if(io_world_rank != MPI_UNDEFINED) which_master = io_world_rank;
else which_master = compute_rank / client_size_;
//cout<< compute_rank<< " , "<< io_world_rank<<" , "<< my_node_<< " , " << which_master <<endl;
rang[0]= which_master * client_size_;
rang[1]= ((which_master+1) * client_size_)-1;
rang[2]= 1;
MPI_Group_range_incl(world_group,1,&rang,&groupTemp1);
rang[0]=(proc_size0*proc_size1) + which_master;
MPI_Group_incl(world_group,2,&rang[0],&groupTemp2);
MPI_Group_union(groupTemp1,groupTemp2,&client_group_);
MPI_Comm_create(MPI_COMM_WORLD,client_group_ , &client_comm_);
MPI_Group_rank(client_group_, &rank);
if(rank==0)isClientRoot_ = true ;
else isClientRoot_ = false ;
}
void IOserver::stop()
{
if(isRoot_)
{
int send=0;
MPI_Ssend(&send,1,MPI::INT,0,SERVER_CONTROL_TAG,sync_global_comm_);
//cout<< "call stop"<<endl;
}
MPI_Barrier(compute_world_comm_);
}
int IOserver::openOstream()
{
MPI_Status status;
int flag=1;
if(isRoot_)
{
bool getState=true;
while(getState)
{
MPI_Iprobe(0,SERVER_STATE_TAG,sync_global_comm_,&flag,&status);
if(flag==true)
{
MPI_Recv(&state_,1,MPI::INT,0,SERVER_STATE_TAG,sync_global_comm_,&status);
}
else getState=false;
}
if(state_ == SERVER_READY)
{
int send=CONTROL_OPEN_OSTREAM ;
MPI_Bsend(&send,1,MPI::INT,0,SERVER_CONTROL_TAG,sync_global_comm_);
//cout << "call openostream"<<endl;
}
}
MPI_Bcast(&state_,1,MPI::INT,0,compute_world_comm_);
if(state_ == SERVER_READY) return OSTREAM_SUCCESS;
else return OSTREAM_FAIL;
}
void IOserver::closeOstream()
{
int send = CONTROL_CLOSE_OSTREAM;
MPI_Send(&send,1,MPI::INT,client_size_,CLOSE_OSTREAM_TAG,client_comm_);
}
ioserver_file IOserver::openFile(string file_name,
int type,
hid_t mem_datatype,
hid_t file_datatype,
LATfield2::Lattice * lat,
int components,
int array_size
)
{
//verification:
if(type ==UNSTRUCTURED_H5_FILE)
{
if(mem_datatype == -1|| file_datatype == -1 )
{
if(isRoot_)cout<< "IOserver::openFile: UNSTRUCTURED_H5_FILE have to have their datatype set!"<<endl;
if(isRoot_)cout<< "Aborting" << endl;
MPI_Abort(MPI_COMM_WORLD,0);
}
}
if(type ==STRUCTURED_H5_FILE)
{
if(mem_datatype == -1|| file_datatype == -1 || lat==NULL)
{
if(isRoot_)cout<< "IOserver::openFile: STRUCTURED_H5_FILE have to have their datatype,lattice set!"<<endl;
if(isRoot_)cout<< "Aborting" << endl;
MPI_Abort(MPI_COMM_WORLD,0);
}
}
ioserver_file file;
MPI_Status status;
char * send;
int send_size;
int fileType = type;
int dim;
hsize_t * size;
hsize_t offset;
int itemp;
string my_filename;
my_filename = file_name + "_" + int2string(my_node_,999);
int filename_len=my_filename.size();
//cout<<filename_len<<endl;
if(type==UNSTRUCTURED_BIN_FILE)
{
//cout<<"compute: opening unstructured bin file: "<<my_filename<<endl;
send_size = (2*sizeof(int)) + filename_len;
send = new char[send_size];
}
else if(type == UNSTRUCTURED_H5_FILE)
{
//cout<<"compute: opening unstructured bin file: "<<my_filename<<endl;
size_t mem_datatype_size;
size_t file_datatype_size;
char * buf_datatype;
buf_datatype =NULL;
H5Tencode(mem_datatype,buf_datatype,&mem_datatype_size);
H5Tencode(file_datatype,buf_datatype,&file_datatype_size);
file.sizeof_mem_dtype = H5Tget_size(mem_datatype);
send_size = (2*sizeof(int))+ filename_len + (2*sizeof(hsize_t)) + mem_datatype_size + file_datatype_size;
send = new char[send_size];
memcpy(send + ( (2*sizeof(int))+ filename_len ) ,(char*)&mem_datatype_size,sizeof(hsize_t));
memcpy(send + ( (2*sizeof(int))+ filename_len ) + sizeof(hsize_t) ,(char*)&file_datatype_size,sizeof(hsize_t));
buf_datatype = new char[mem_datatype_size];
H5Tencode(mem_datatype,buf_datatype,&mem_datatype_size);
memcpy(send+( (2*sizeof(int))+ filename_len + (2*sizeof(hsize_t)) ),buf_datatype,mem_datatype_size);
delete[] buf_datatype;
buf_datatype = new char[file_datatype_size];
H5Tencode(file_datatype,buf_datatype,&file_datatype_size);
memcpy(send+( (2*sizeof(int))+ filename_len + (2*sizeof(hsize_t)) + mem_datatype_size),buf_datatype,file_datatype_size);
delete[] buf_datatype;
}
else if(type ==STRUCTURED_H5_FILE)
{
dim = lat->dim();
size = new hsize_t[dim];
for(int i=0;i<dim;i++)size[i]=lat->size(i);
int bproc = my_node_ * proc_size1_ / io_node_number_;
offset=0;
for(int i=0;i < bproc;i++) offset += lat->sizeLocalAllProcDim1()[i];
size[dim-2]=0;
for(int i=0;i < (proc_size1_ / io_node_number_);i++)size[dim-2]+= lat->sizeLocalAllProcDim1()[bproc+i];
size_t mem_datatype_size;
size_t file_datatype_size;
char * buf_datatype;
buf_datatype =NULL;
H5Tencode(mem_datatype,buf_datatype,&mem_datatype_size);
H5Tencode(file_datatype,buf_datatype,&file_datatype_size);
file.sizeof_mem_dtype = H5Tget_size(mem_datatype);
send_size = (5*sizeof(int)) + filename_len + ((2+dim+1)*sizeof(hsize_t)) + mem_datatype_size + file_datatype_size ;
send = new char[send_size];
memcpy(send + ( (2*sizeof(int))+ filename_len ) ,(char*)&mem_datatype_size,sizeof(hsize_t));
memcpy(send + ( (2*sizeof(int))+ filename_len ) + sizeof(hsize_t) ,(char*)&file_datatype_size,sizeof(hsize_t));
buf_datatype = new char[mem_datatype_size];
H5Tencode(mem_datatype,buf_datatype,&mem_datatype_size);
memcpy(send+( (2*sizeof(int))+ filename_len + (2*sizeof(hsize_t)) ),buf_datatype,mem_datatype_size);
delete[] buf_datatype;
buf_datatype = new char[file_datatype_size];
H5Tencode(file_datatype,buf_datatype,&file_datatype_size);
memcpy(send+( (2*sizeof(int))+ filename_len + (2*sizeof(hsize_t)) + mem_datatype_size),buf_datatype,file_datatype_size);
delete[] buf_datatype;
memcpy(send+( (2*sizeof(int))+ filename_len + (2*sizeof(hsize_t)) + mem_datatype_size + file_datatype_size ),(char*)&dim,sizeof(int));
memcpy(send+( (3*sizeof(int))+ filename_len + (2*sizeof(hsize_t)) + mem_datatype_size + file_datatype_size ),(char*)size,dim*sizeof(hsize_t));
memcpy(send+( (3*sizeof(int))+ filename_len + ((2+dim)*sizeof(hsize_t)) + mem_datatype_size + file_datatype_size),(char*)&offset,sizeof(hsize_t));
itemp =components;
memcpy(send+( (3*sizeof(int))+ filename_len + ((2+dim+1)*sizeof(hsize_t)) + mem_datatype_size + file_datatype_size ),(char*)&itemp,sizeof(int));
itemp =array_size;
memcpy(send+( (4*sizeof(int))+ filename_len + ((2+dim+1)*sizeof(hsize_t)) + mem_datatype_size + file_datatype_size ),(char*)&itemp,sizeof(int));
file.dim = dim;
file.components = components;
file.array_size = array_size;
delete[] size;
}
memcpy(send,(char*)&fileType,sizeof(int));
memcpy(send+sizeof(int),(char*)&filename_len,sizeof(int));
for(int i=0;i<filename_len;i++)send[(2*sizeof(int))+i] = my_filename[i];
int fileInfo[2];
if(isClientRoot_)MPI_Bsend(send,send_size,MPI::CHAR,client_size_,FILE_OPEN_TAG,client_comm_);
if(isClientFileRoot_)MPI_Recv(fileInfo,2,MPI::INT,client_size_,FILE_OPEN_TAG,client_comm_,&status);
//(&fileID,1,MPI::INT,0,io_node_comm_);
MPI_Bcast(fileInfo,2,MPI::INT,0,compute_file_comm_);
file.ID=fileInfo[0];
file.type = fileInfo[1];
file.is_open = true;
if(file.type != type)cout<< "ARGARGARG file created but wrong type"<<endl;
delete[] send;
MPI_Barrier(compute_world_comm_);
return file;
}
void IOserver::closeFile(ioserver_file file)
{
if(file.is_open == false)
{
cout<< "IOServer: file is not open, cant close it!"<<endl;
return;
}
else
{
int send[2];
send[0]= file.ID;
send[1]= file.type;
MPI_Bsend(send,2,MPI::INT,client_size_,FILE_CLOSE_TAG,client_comm_);
file.is_open = false;
}
}
void IOserver::sendData(ioserver_file file, char * message,long size,int sendType = DEFAULT_SEND)
{
//verify if the file is unstructured...
if(file.type!=UNSTRUCTURED_BIN_FILE && file.type!=UNSTRUCTURED_H5_FILE)
{
cout<< "not a unstructured file! wrong method to send data, more argument needed..."<<endl;
return;
}
long sendinfo[3];
sendinfo[0] = file.ID;
sendinfo[1] = size;
sendinfo[2] = file.type;
MPI_Bsend(sendinfo,3,MPI::LONG,client_size_,GET_DATA_TAG,client_comm_);
//cout<< "send data to file id : "<<sendinfo[0]<<", size :"<<sendinfo[1]<<endl;
if(sendType == DEFAULT_SEND)MPI_Send(message,size,MPI::CHAR,client_size_,file.ID,client_comm_);
else if(sendType == BUFFERED_SEND)MPI_Bsend(message,size,MPI::CHAR,client_size_,file.ID,client_comm_);
//cout<< "send has been sent data to file id : "<<sendinfo[0]<<endl;
}
void IOserver::sendData(ioserver_file file, char * message,hsize_t * size, hsize_t * offset,int sendType = DEFAULT_SEND)
{
if(file.type!=STRUCTURED_H5_FILE)
{
cout<< "not a structured h5file! wrong method to send data, less argument needed..."<<endl;
return;
}
long sendinfo[3 + (file.dim*2)];
long mSize=1;
for(int i = 0;i<file.dim;i++) mSize *= size[i];
sendinfo[0]= file.ID;
sendinfo[1]= mSize * file.sizeof_mem_dtype * file.components * file.array_size;
//cout<<sendinfo[1]<<endl;
sendinfo[2]= file.type;
for(int i = 0;i<file.dim;i++)sendinfo[3+i] = size[i];
for(int i = 0;i<file.dim;i++)sendinfo[3+file.dim+i] = offset[i];
MPI_Bsend(sendinfo,3 + (file.dim*2),MPI::LONG,client_size_,GET_DATA_TAG,client_comm_);
if(sendType == DEFAULT_SEND)MPI_Send(message,sendinfo[1],MPI::CHAR,client_size_,file.ID,client_comm_);
else if(sendType == BUFFERED_SEND)MPI_Bsend(message,sendinfo[1],MPI::CHAR,client_size_,file.ID,client_comm_);
}
void IOserver::sendHeader(ioserver_file file,char* header,int size)
{
if(file.type != UNSTRUCTURED_BIN_FILE)
{
cout<<"IOserver::sendHeader: trying to add header to a non binary file..."<<endl;
cout<<"IOserver::sendHeader: returning without adding the header"<<endl;
return;
}
if(isClientFileRoot_)
{
char * send;
int send_size;
int ID=file.ID;
int header_size = size;
send_size = size + (2 * sizeof(int));
send = new char[send_size];
memcpy(send,(char*)&ID,sizeof(int));
memcpy(send + sizeof(int),(char*)&header_size,sizeof(int));
memcpy(send + (2*sizeof(int)),header,header_size);
MPI_Bsend(send,send_size,MPI::CHAR,client_size_,GET_HEADER_TAG,client_comm_);
delete[] send;
}
}
void IOserver::sendATTR(ioserver_file file,string attr_name, char * attr, int size,hid_t dtype)
{
if(file.type != UNSTRUCTURED_H5_FILE && file.type!=STRUCTURED_H5_FILE)
{
cout<<"IOserver::sendATTR: trying to add attribute to a non hdf5 file..."<<endl;
cout<<"IOserver::sendATTR: returning without adding the attribute"<<endl;
return;
}
if(isClientFileRoot_)
{
//cout<<"adding attribute"<<endl;
char * send;
char * buf_datatype;
size_t buf_datatype_size;
int ID = file.ID;
int attr_size = size;
int send_size;
int attr_name_size = attr_name.size();
buf_datatype =NULL;
H5Tencode(dtype,buf_datatype,&buf_datatype_size);
//cout<<buf_datatype_size<<endl;
buf_datatype = new char[buf_datatype_size];
H5Tencode(dtype,buf_datatype,&buf_datatype_size);
send_size = buf_datatype_size + sizeof(size_t) + (3*sizeof(int)) + attr_name_size + (attr_size*H5Tget_size(dtype));
send = new char[send_size];
//cout<<buf_datatype_size<<endl;
memcpy(send,(char*)&ID,sizeof(int));
memcpy(send + sizeof(int),(char*)&buf_datatype_size,sizeof(size_t));
memcpy(send + sizeof(int) + sizeof(size_t),buf_datatype,buf_datatype_size);
memcpy(send + sizeof(int) + sizeof(size_t)+ buf_datatype_size ,(char*)&attr_size,sizeof(int));
memcpy(send + (2*sizeof(int)) + sizeof(size_t)+ buf_datatype_size ,(char*)&attr_name_size,sizeof(int));
strcpy(send + (3*sizeof(int)) + sizeof(size_t)+ buf_datatype_size,attr_name.c_str());
memcpy(send + (3*sizeof(int)) + sizeof(size_t)+ buf_datatype_size + attr_name_size,attr,attr_size * H5Tget_size(dtype));
MPI_Bsend(send,send_size,MPI::CHAR,client_size_,GET_ATTR_TAG,client_comm_);
delete[] buf_datatype;
delete[] send;
//cout<<"sended attribute"<<endl;
}
}
void IOserver::sendDataset(ioserver_file file,string dset_name, char * dset, hsize_t dim, hsize_t * size, hid_t dtype)
{
if(file.type != UNSTRUCTURED_H5_FILE && file.type!=STRUCTURED_H5_FILE)
{
cout<<"IOserver::sendATTR: trying to add attribute to a non hdf5 file..."<<endl;
cout<<"IOserver::sendATTR: returning without adding the attribute"<<endl;
return;
}
if(isClientFileRoot_)
{
char * send;
char * buf_datatype;
size_t buf_datatype_size;
int ID = file.ID;
int dtype_size;
int send_size;
int dset_size;
int dset_name_size = dset_name.size();
hsize_t dimT = dim;
hsize_t sizeT[dim];
for(int i = 0 ; i<dim;i++)sizeT[i]=size[i];
dset_size = 1;
for(int i=0;i<dim;i++)dset_size*=size[i];
dtype_size = H5Tget_size(dtype);
dset_size *= dtype_size;
buf_datatype =NULL;
H5Tencode(dtype,buf_datatype,&buf_datatype_size);
buf_datatype = new char[buf_datatype_size];
H5Tencode(dtype,buf_datatype,&buf_datatype_size);
send_size= (3*sizeof(int)) + sizeof(size_t) + buf_datatype_size + ((1+dim)*sizeof(hsize_t)) + dset_name_size + dset_size;
send = new char[send_size];
memcpy(send,(char*)&ID,sizeof(int));
memcpy(send + sizeof(int),(char*)&buf_datatype_size,sizeof(size_t));
memcpy(send + sizeof(int) + sizeof(size_t),buf_datatype,buf_datatype_size);
memcpy(send + sizeof(int) + sizeof(size_t) + buf_datatype_size,(char*)&dimT,sizeof(hsize_t));
memcpy(send + sizeof(int) + sizeof(size_t) + buf_datatype_size + sizeof(hsize_t),(char*)&sizeT,dim * sizeof(hsize_t));
memcpy(send + sizeof(int) + sizeof(size_t) + buf_datatype_size + ((1+dim)*sizeof(hsize_t)),(char*)&dset_size,sizeof(int));
memcpy(send + (2*sizeof(int)) + sizeof(size_t) + buf_datatype_size + ((1+dim)*sizeof(hsize_t)),(char*)&dset_name_size,sizeof(int));
strcpy(send + (3*sizeof(int)) + sizeof(size_t) + buf_datatype_size + ((1+dim)*sizeof(hsize_t)),dset_name.c_str());
memcpy(send + (3*sizeof(int)) + sizeof(size_t) + buf_datatype_size + ((1+dim)*sizeof(hsize_t)) + dset_name_size ,dset,dset_size);
MPI_Bsend(send,send_size,MPI::CHAR,client_size_,GET_DSET_TAG,client_comm_);
//MPI_Bsend(dset,dset_size,MPI::CHAR,client_size_,file.ID + OFFSET_DSET_TAG,client_comm_);
delete[] buf_datatype;
delete[] send;
}
}
void IOserver::start()
{
MPI_Status status;
MPI_Request send_statut_request;
bool serverOn_flag = true;
int control;