OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
HDF5_writer_vd.hpp
1 /*
2  * HDF5_loader_vector_dist.hpp
3  *
4  * Created on: May 1, 2017
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_IO_SRC_HDF5_WR_HDF5_WRITER_VD_HPP_
9 #define OPENFPM_IO_SRC_HDF5_WR_HDF5_WRITER_VD_HPP_
10 
11 #include "Packer_Unpacker/Pack_selector.hpp"
12 #include "Packer_Unpacker/Packer.hpp"
13 #include "Packer_Unpacker/Unpacker.hpp"
14 
15 template <>
16 class HDF5_writer<VECTOR_DIST>
17 {
18 public:
19 
20  template<unsigned int dim, typename St, typename prp>
21  inline void save(const std::string & filename,
22  const openfpm::vector<Point<dim,St>> & v_pos,
23  const openfpm::vector<prp> & v_prp) const
24  {
25  Vcluster & v_cl = create_vcluster();
26 
27  //Pack_request vector
28  size_t req = 0;
29 
30  //std::cout << "V_pos.size() before save: " << v_pos.size() << std::endl;
31 
32  //Pack request
35 
36  // allocate the memory
37  HeapMemory pmem;
38  //pmem.allocate(req);
39  ExtPreAlloc<HeapMemory> & mem = *(new ExtPreAlloc<HeapMemory>(req,pmem));
40  mem.incRef();
41 
42  //Packing
43 
44  Pack_stat sts;
45 
48 
49  /*****************************************************************
50  * Create a new file with default creation and access properties.*
51  * Then create a dataset and write data to it and close the file *
52  * and dataset. *
53  *****************************************************************/
54 
55  int mpi_rank = v_cl.getProcessUnitID();
56  int mpi_size = v_cl.getProcessingUnits();
57 
58  MPI_Comm comm = v_cl.getMPIComm();
59  MPI_Info info = MPI_INFO_NULL;
60 
61  // Set up file access property list with parallel I/O access
62 
63  hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
64  H5Pset_fapl_mpio(plist_id, comm, info);
65 
66  // Create a new file collectively and release property list identifier.
67  hid_t file = H5Fcreate (filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
68  H5Pclose(plist_id);
69 
70  size_t sz = pmem.size();
71  //std::cout << "Pmem.size: " << pmem.size() << std::endl;
72  openfpm::vector<size_t> sz_others;
73  v_cl.allGather(sz,sz_others);
74  v_cl.execute();
75 
76  size_t sum = 0;
77 
78  for (size_t i = 0; i < sz_others.size(); i++)
79  sum += sz_others.get(i);
80 
81  //Size for data space in file
82  hsize_t fdim[1] = {sum};
83 
84  //Size for data space in file
85  hsize_t fdim2[1] = {(size_t)mpi_size};
86 
87  //Create data space in file
88  hid_t file_dataspace_id = H5Screate_simple(1, fdim, NULL);
89 
90  //Create data space in file
91  hid_t file_dataspace_id_2 = H5Screate_simple(1, fdim2, NULL);
92 
93  //Size for data space in memory
94  hsize_t mdim[1] = {pmem.size()};
95 
96  //Create data space in memory
97  hid_t mem_dataspace_id = H5Screate_simple(1, mdim, NULL);
98 
99  //Create data set in file
100  hid_t file_dataset = H5Dcreate (file, "vector_dist", H5T_NATIVE_CHAR, file_dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
101 
102  //Create data set 2 in file
103  hid_t file_dataset_2 = H5Dcreate (file, "metadata", H5T_NATIVE_INT, file_dataspace_id_2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
104 
105  //H5Pclose(plist_id);
106  H5Sclose(file_dataspace_id);
107  H5Sclose(file_dataspace_id_2);
108 
109  hsize_t block[1] = {pmem.size()};
110 
111  //hsize_t stride[1] = {1};
112 
113  hsize_t count[1] = {1};
114 
115  hsize_t offset[1] = {0};
116 
117  for (int i = 0; i < mpi_rank; i++)
118  {
119  if (mpi_rank == 0)
120  {
121  /* coverity[dead_error_line] */
122  offset[0] = 0;
123  }
124  else
125  {offset[0] += sz_others.get(i);}
126  }
127 
128  int metadata[mpi_size];
129 
130  for (int i = 0; i < mpi_size; i++)
131  metadata[i] = sz_others.get(i);
132 
133  //Select hyperslab in the file.
134  file_dataspace_id = H5Dget_space(file_dataset);
135  H5Sselect_hyperslab(file_dataspace_id, H5S_SELECT_SET, offset, NULL, count, block);
136 
137  file_dataspace_id_2 = H5Dget_space(file_dataset_2);
138 
139 
140  //Create property list for collective dataset write.
141  plist_id = H5Pcreate(H5P_DATASET_XFER);
142  H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
143 
144  //Write a data set to a file
145  H5Dwrite(file_dataset, H5T_NATIVE_CHAR, mem_dataspace_id, file_dataspace_id, plist_id, (const char *)pmem.getPointer());
146 
147  //Write a data set 2 to a file
148  H5Dwrite(file_dataset_2, H5T_NATIVE_INT, H5S_ALL, file_dataspace_id_2, plist_id, metadata);
149 
150 
151  //Close/release resources.
152  H5Dclose(file_dataset);
153  H5Sclose(file_dataspace_id);
154  H5Dclose(file_dataset_2);
155  H5Sclose(file_dataspace_id_2);
156  H5Sclose(mem_dataspace_id);
157  H5Pclose(plist_id);
158  H5Fclose(file);
159  }
160 
161 };
162 
163 
164 #endif /* OPENFPM_IO_SRC_HDF5_WR_HDF5_WRITER_VD_HPP_ */
size_t getProcessUnitID()
Get the process unit id.
void execute()
Execute all the requests.
MPI_Comm getMPIComm()
Get the MPI_Communicator (or processor group) this VCluster is using.
size_t size()
Stub size.
Definition: map_vector.hpp:70
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
virtual size_t size() const
the the size of the allocated memory
Definition: HeapMemory.cpp:157
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
Implementation of VCluster class.
Definition: VCluster.hpp:36
virtual void * getPointer()
get a readable pointer with the data
Definition: HeapMemory.cpp:237
Packing class.
Definition: Packer.hpp:44
virtual void incRef()
Increment the reference counter.
Definition: ExtPreAlloc.hpp:69
It model an expression expr1 + ... exprn.
Definition: sum.hpp:92
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
Packing status object.
Definition: Pack_stat.hpp:51
size_t getProcessingUnits()
Get the total number of processors.
bool allGather(T &send, openfpm::vector< T, Mem, gr > &v)
Gather the data from all processors.