Skip to content

Commit 52211c4

Browse files
committed
1. add ModuleBase::bcast_data_cereal() for bcasting arbitrary class
1 parent a6407ea commit 52211c4

File tree

3 files changed

+67
-58
lines changed

3 files changed

+67
-58
lines changed

source/src_io/read_txt_input_process.cpp

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
#include "src_io/read_txt_tools.h"
99
#include "module_base/global_variable.h"
1010

11-
#include <mpi.h>
12-
#include <sstream>
13-
1411
#ifdef USE_CEREAL_SERIALIZATION
1512
#include "src_lcao/serialization_cereal.h"
16-
#include <cereal/archives/binary.hpp>
1713
#endif
1814

1915
namespace Read_Txt_Input
@@ -130,30 +126,7 @@ namespace Read_Txt_Input
130126
void Input_Process::bcast()
131127
{
132128
#ifdef USE_CEREAL_SERIALIZATION
133-
if(GlobalV::MY_RANK==0)
134-
{
135-
std::stringstream ss;
136-
{
137-
cereal::BinaryOutputArchive ar(ss);
138-
ar(this->input.list);
139-
}
140-
const int size = ss.str().size();
141-
MPI_Bcast( const_cast<int*>(&size), 1, MPI_INT, 0, MPI_COMM_WORLD );
142-
MPI_Bcast( const_cast<char*>(ss.str().c_str()), size, MPI_CHAR, 0, MPI_COMM_WORLD );
143-
}
144-
else
145-
{
146-
int size;
147-
MPI_Bcast( &size, 1, MPI_INT, 0, MPI_COMM_WORLD );
148-
std::vector<char> c(size);
149-
MPI_Bcast( c.data(), size, MPI_CHAR, 0, MPI_COMM_WORLD );
150-
std::stringstream ss;
151-
ss.rdbuf()->pubsetbuf(c.data(),size);
152-
{
153-
cereal::BinaryInputArchive ar(ss);
154-
ar(this->input.list);
155-
}
156-
}
129+
ModuleBase::bcast_data_cereal(this->input.list, MPI_COMM_WORLD, 0);
157130
#else
158131
#error Input_Process::bcast() needs cereal
159132
#endif

source/src_io/read_txt_stru.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ namespace Read_Txt_Stru
7979
atoms_info.push_back(std::move(atom0));
8080
atoms_info.insert(atoms_info.end(), stru_one.begin()+1, stru_one.end());
8181
}
82-
8382
}
8483
else
8584
{

source/src_lcao/serialization_cereal.h

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,79 @@ template<class Archive, typename T> void serialize( Archive & ar, Abfs::Vector3_
2121

2222
namespace ModuleBase
2323
{
24+
template<class Archive, typename T> void serialize( Archive & ar, Vector3<T> & v ){ ar(v.x); ar(v.y); ar(v.z); }
2425

25-
template<class Archive, typename T> void serialize( Archive & ar, Vector3<T> & v ){ ar(v.x); ar(v.y); ar(v.z); }
26-
27-
template<class Archive> void save( Archive & ar, const matrix & m )
28-
{
29-
ar(m.nr); ar(m.nc);
30-
ar(cereal::binary_data(m.c, m.nr*m.nc*sizeof(double)));
31-
}
32-
template<class Archive> void load( Archive & ar, matrix & m )
33-
{
34-
int nr, nc;
35-
ar(nr); ar(nc);
36-
m.create(nr,nc);
37-
ar(cereal::binary_data(m.c, m.nr*m.nc*sizeof(double)));
38-
}
26+
template<class Archive> void save( Archive & ar, const matrix & m )
27+
{
28+
ar(m.nr); ar(m.nc);
29+
ar(cereal::binary_data(m.c, m.nr*m.nc*sizeof(double)));
30+
}
31+
template<class Archive> void load( Archive & ar, matrix & m )
32+
{
33+
int nr, nc;
34+
ar(nr); ar(nc);
35+
m.create(nr,nc);
36+
ar(cereal::binary_data(m.c, m.nr*m.nc*sizeof(double)));
37+
}
3938
}
4039

4140
namespace Read_Txt_Input
4241
{
43-
template<class Archive> void Input_Value::serialize( Archive & ar )
44-
{
45-
ar( this->b );
46-
ar( this->i );
47-
ar( this->d );
48-
ar( this->s );
42+
template<class Archive> void Input_Value::serialize( Archive & ar )
43+
{
44+
ar( this->b );
45+
ar( this->i );
46+
ar( this->d );
47+
ar( this->s );
48+
}
49+
template<class Archive> void Input_Item::serialize( Archive & ar )
50+
{
51+
ar( this->annotation );
52+
ar( this->values );
53+
ar( this->values_type );
54+
ar( this->label );
55+
ar( this->values_size_read );
56+
ar( this->values_size_lower_limit );
57+
ar( this->values_size_upper_limit );
58+
}
4959
}
50-
template<class Archive> void Input_Item::serialize( Archive & ar )
60+
61+
62+
63+
#include <mpi.h>
64+
#include <sstream>
65+
66+
namespace ModuleBase
5167
{
52-
ar( this->annotation );
53-
ar( this->values );
54-
ar( this->values_type );
55-
ar( this->label );
56-
ar( this->values_size_read );
57-
ar( this->values_size_lower_limit );
58-
ar( this->values_size_upper_limit );
59-
}
68+
template<typename T>
69+
void bcast_data_cereal(T &data, const MPI_Comm &mpi_comm, const int &rank_bcast)
70+
{
71+
int my_rank; MPI_Comm_rank( mpi_comm, &my_rank );
72+
if(my_rank==rank_bcast)
73+
{
74+
std::stringstream ss;
75+
{
76+
cereal::BinaryOutputArchive ar(ss);
77+
ar(data);
78+
}
79+
const int size = ss.str().size();
80+
MPI_Bcast( const_cast<int*>(&size), 1, MPI_INT, rank_bcast, mpi_comm );
81+
MPI_Bcast( const_cast<char*>(ss.str().c_str()), size, MPI_CHAR, rank_bcast, mpi_comm );
82+
}
83+
else
84+
{
85+
int size;
86+
MPI_Bcast( &size, 1, MPI_INT, rank_bcast, mpi_comm );
87+
std::vector<char> c(size);
88+
MPI_Bcast( c.data(), size, MPI_CHAR, rank_bcast, mpi_comm );
89+
std::stringstream ss;
90+
ss.rdbuf()->pubsetbuf(c.data(),size);
91+
{
92+
cereal::BinaryInputArchive ar(ss);
93+
ar(data);
94+
}
95+
}
96+
}
6097
}
6198

6299
#endif

0 commit comments

Comments
 (0)