Failure in 1D std::vector boost serialization

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

Failure in 1D std::vector boost serialization

问题

  1. 我尝试将`std::vector`序列化为类的成员,附加的代码失败并且抛出了分段错误,我已经无法解决这个问题几周了。奇怪的是,在较小的容器大小下运行良好,但即使对于内存问题来说,这个大小也很小。
  2. ```cpp
  3. #include <iostream>
  4. #include <boost/mpi/environment.hpp>
  5. #include <boost/mpi/communicator.hpp>
  6. #include <boost/serialization/vector.hpp>
  7. #include <boost/mpi.hpp>
  8. #include <boost/version.hpp>
  9. class Dummy
  10. {
  11. private:
  12. std::vector<unsigned int> array;
  13. friend class boost::serialization::access;
  14. template <class T>
  15. inline void serialize(T & ar, const unsigned int /*version*/)
  16. {ar & array;}
  17. public:
  18. Dummy()
  19. {};
  20. Dummy(const int nDivs)
  21. {array.resize(nDivs);}
  22. ~Dummy() {};
  23. };
  24. int main(int argc, char* const argv[])
  25. {
  26. boost::mpi::environment env;
  27. boost::mpi::communicator world;
  28. boost::mpi::request req;
  29. std::cout << "Using Boost "
  30. << BOOST_VERSION / 100000 << "." // major version
  31. << BOOST_VERSION / 100 % 1000 << "." // minor version
  32. << BOOST_VERSION % 100 // patch level
  33. << std::endl;
  34. int MASTER = 0;
  35. int tag = 0;
  36. std::size_t size = 10000;
  37. std::vector<unsigned int> neighbrs = {0, 1, 2, 3};
  38. std::vector<Dummy> TobeSend (size, Dummy(2));
  39. std::vector<Dummy> TobeRecvd (size ,Dummy(1));
  40. for(auto itri:neighbrs)
  41. {
  42. int target = itri;
  43. if(world.rank()!= target)
  44. {
  45. world.isend(target, tag, TobeSend);
  46. }
  47. }
  48. for(auto isource:neighbrs)
  49. {
  50. int source = isource;
  51. if(world.rank()!= source)
  52. {
  53. req= world.irecv(source, tag, TobeRecvd);
  54. req.test();
  55. }
  56. }
  57. return 0;
  58. }

我使用以下命令构建代码:
mpic++ -g mpidatatype.cpp -o output -lboost_mpi -lboost_serialization

我尝试过1.75和1.82的两个版本。对于这个问题,我会很感激帮助。

以下是程序在发送之前的起始调用堆栈:[![enter image description here][1]][1]

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. #include &lt;iostream&gt;
  5. #include &lt;boost/mpi/environment.hpp&gt;
  6. #include &lt;boost/mpi/communicator.hpp&gt;
  7. #include &lt;boost/serialization/vector.hpp&gt;
  8. #include &lt;boost/mpi.hpp&gt;
  9. #include &lt;boost/version.hpp&gt;
  10. class Dummy
  11. {
  12. private:
  13. std::vector&lt;unsigned int&gt; array;
  14. friend class boost::serialization::access;
  15. template &lt;class T&gt;
  16. inline void serialize(T &amp; ar, const unsigned int /*version*/)
  17. {ar &amp; array;}
  18. public:
  19. Dummy()
  20. {};
  21. Dummy(const int nDivs)
  22. {array.resize(nDivs);}
  23. ~Dummy() {};
  24. };
  25. int main(int argc, char* const argv[])
  26. {
  27. boost::mpi::environment env;
  28. boost::mpi::communicator world;
  29. boost::mpi::request req;
  30. std::cout &lt;&lt; &quot;Using Boost &quot;
  31. &lt;&lt; BOOST_VERSION / 100000 &lt;&lt; &quot;.&quot; // major version
  32. &lt;&lt; BOOST_VERSION / 100 % 1000 &lt;&lt; &quot;.&quot; // minor version
  33. &lt;&lt; BOOST_VERSION % 100 // patch level
  34. &lt;&lt; std::endl;
  35. int MASTER = 0;
  36. int tag = 0;
  37. std::size_t size = 10000;
  38. std::vector&lt;unsigned int&gt; neighbrs = {0, 1, 2, 3};
  39. std::vector&lt;Dummy&gt; TobeSend (size, Dummy(2));
  40. std::vector&lt;Dummy&gt; TobeRecvd (size ,Dummy(1));
  41. for(auto itri:neighbrs)
  42. {
  43. int target = itri;
  44. if(world.rank()!= target)
  45. {
  46. world.isend(target, tag, TobeSend);
  47. }
  48. }
  49. for(auto isource:neighbrs)
  50. {
  51. int source = isource;
  52. if(world.rank()!= source)
  53. {
  54. req= world.irecv(source, tag, TobeRecvd);
  55. req.test();
  56. }
  57. }
  58. return 0;
  59. }
  60. I&#39;m building the code with:
  61. `mpic++ -g mpidatatype.cpp -o output -lboost_mpi -lboost_serialization`
  62. I&#39;ve tried both version of 1.75 and 1.82. I&#39;d appreciate help on this problem.
  63. Here&#39;s the call stack as start of the program before sending occurs: [![enter image description here][1]][1]
  64. [1]: https://i.stack.imgur.com/0OkPt.png
  65. </details>
  66. # 答案1
  67. **得分**: 1
  68. 我最终解决了这个问题。问题是缺少以下代码行:
  69. ```cpp
  70. namespace boost { namespace mpi {
  71. template&lt;&gt; struct is_mpi_datatype&lt;Dummy&gt;
  72. : public mpl::true_ { };
  73. } }

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

英文:

So, I resolved this finally. The problem was missing

  1. namespace boost { namespace mpi {
  2. template&lt;&gt; struct is_mpi_datatype&lt;Dummy&gt;
  3. : public mpl::true_ { };
  4. } }

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:

确定