英文:
Failure in 1D std::vector boost serialization
问题
我尝试将`std::vector`序列化为类的成员,附加的代码失败并且抛出了分段错误,我已经无法解决这个问题几周了。奇怪的是,在较小的容器大小下运行良好,但即使对于内存问题来说,这个大小也很小。
```cpp
#include <iostream>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/mpi.hpp>
#include <boost/version.hpp>
class Dummy
{
private:
std::vector<unsigned int> array;
friend class boost::serialization::access;
template <class T>
inline void serialize(T & ar, const unsigned int /*version*/)
{ar & array;}
public:
Dummy()
{};
Dummy(const int nDivs)
{array.resize(nDivs);}
~Dummy() {};
};
int main(int argc, char* const argv[])
{
boost::mpi::environment env;
boost::mpi::communicator world;
boost::mpi::request req;
std::cout << "Using Boost "
<< BOOST_VERSION / 100000 << "." // major version
<< BOOST_VERSION / 100 % 1000 << "." // minor version
<< BOOST_VERSION % 100 // patch level
<< std::endl;
int MASTER = 0;
int tag = 0;
std::size_t size = 10000;
std::vector<unsigned int> neighbrs = {0, 1, 2, 3};
std::vector<Dummy> TobeSend (size, Dummy(2));
std::vector<Dummy> TobeRecvd (size ,Dummy(1));
for(auto itri:neighbrs)
{
int target = itri;
if(world.rank()!= target)
{
world.isend(target, tag, TobeSend);
}
}
for(auto isource:neighbrs)
{
int source = isource;
if(world.rank()!= source)
{
req= world.irecv(source, tag, TobeRecvd);
req.test();
}
}
return 0;
}
我使用以下命令构建代码:
mpic++ -g mpidatatype.cpp -o output -lboost_mpi -lboost_serialization
我尝试过1.75和1.82的两个版本。对于这个问题,我会很感激帮助。
以下是程序在发送之前的起始调用堆栈:[![enter image description here][1]][1]
<details>
<summary>英文:</summary>
I'm trying to serialize a `std::vector` as a member in the class, the attached code fails at throws segmentation fault that I've not been able to resolve this issue for weeks now. It's weird as it goes well in smaller size of container, but even this size is very small for memory issues.
#include <iostream>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/mpi.hpp>
#include <boost/version.hpp>
class Dummy
{
private:
std::vector<unsigned int> array;
friend class boost::serialization::access;
template <class T>
inline void serialize(T & ar, const unsigned int /*version*/)
{ar & array;}
public:
Dummy()
{};
Dummy(const int nDivs)
{array.resize(nDivs);}
~Dummy() {};
};
int main(int argc, char* const argv[])
{
boost::mpi::environment env;
boost::mpi::communicator world;
boost::mpi::request req;
std::cout << "Using Boost "
<< BOOST_VERSION / 100000 << "." // major version
<< BOOST_VERSION / 100 % 1000 << "." // minor version
<< BOOST_VERSION % 100 // patch level
<< std::endl;
int MASTER = 0;
int tag = 0;
std::size_t size = 10000;
std::vector<unsigned int> neighbrs = {0, 1, 2, 3};
std::vector<Dummy> TobeSend (size, Dummy(2));
std::vector<Dummy> TobeRecvd (size ,Dummy(1));
for(auto itri:neighbrs)
{
int target = itri;
if(world.rank()!= target)
{
world.isend(target, tag, TobeSend);
}
}
for(auto isource:neighbrs)
{
int source = isource;
if(world.rank()!= source)
{
req= world.irecv(source, tag, TobeRecvd);
req.test();
}
}
return 0;
}
I'm building the code with:
`mpic++ -g mpidatatype.cpp -o output -lboost_mpi -lboost_serialization`
I've tried both version of 1.75 and 1.82. I'd appreciate help on this problem.
Here's the call stack as start of the program before sending occurs: [![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/0OkPt.png
</details>
# 答案1
**得分**: 1
我最终解决了这个问题。问题是缺少以下代码行:
```cpp
namespace boost { namespace mpi {
template<> struct is_mpi_datatype<Dummy>
: public mpl::true_ { };
} }
添加这行代码解决了我的问题。
英文:
So, I resolved this finally. The problem was missing
namespace boost { namespace mpi {
template<> struct is_mpi_datatype<Dummy>
: public mpl::true_ { };
} }
adding this line of code resolved my issue
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论