Failure in 1D std::vector boost serialization

huangapple go评论69阅读模式
英文:

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&#39;m trying to serialize a `std::vector` as a member in the class, the attached code fails at throws segmentation fault that I&#39;ve not been able to resolve this issue for weeks now. It&#39;s weird as it goes well in smaller size of container, but even this size is very small for memory issues. 
#include &lt;iostream&gt;
#include &lt;boost/mpi/environment.hpp&gt;
#include &lt;boost/mpi/communicator.hpp&gt;
#include &lt;boost/serialization/vector.hpp&gt;
#include &lt;boost/mpi.hpp&gt;
#include &lt;boost/version.hpp&gt;
class Dummy
{
private:
std::vector&lt;unsigned int&gt; array;
friend class boost::serialization::access;
template &lt;class T&gt; 
inline void serialize(T &amp; ar, const unsigned int /*version*/)
{ar &amp; 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 &lt;&lt; &quot;Using Boost &quot;     
&lt;&lt; BOOST_VERSION / 100000     &lt;&lt; &quot;.&quot;  // major version
&lt;&lt; BOOST_VERSION / 100 % 1000 &lt;&lt; &quot;.&quot;  // minor version
&lt;&lt; BOOST_VERSION % 100                // patch level
&lt;&lt; std::endl;
int MASTER = 0; 
int tag    = 0;
std::size_t size = 10000;  
std::vector&lt;unsigned int&gt; neighbrs = {0, 1, 2, 3};
std::vector&lt;Dummy&gt; TobeSend (size, Dummy(2)); 
std::vector&lt;Dummy&gt; 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&#39;m building the code with: 
`mpic++ -g mpidatatype.cpp -o output -lboost_mpi -lboost_serialization`
I&#39;ve tried both version of 1.75 and 1.82. I&#39;d appreciate help on this problem.
Here&#39;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&lt;&gt; struct is_mpi_datatype&lt;Dummy&gt;
: public mpl::true_ { };
} }

添加这行代码解决了我的问题。

英文:

So, I resolved this finally. The problem was missing

  namespace boost { namespace mpi {
template&lt;&gt; struct is_mpi_datatype&lt;Dummy&gt;
: public mpl::true_ { };
} }

adding this line of code resolved my issue

huangapple
  • 本文由 发表于 2023年7月13日 13:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定