guessserializer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file guessserializer.cpp
  3. /// \brief Classes which handle (de)serializing LPJ-GUESS state
  4. ///
  5. /// $Date: 2013-10-10 10:20:33 +0200 (Thu, 10 Oct 2013) $
  6. ///
  7. ///////////////////////////////////////////////////////////////////////////////////////
  8. #include "config.h"
  9. #include "guessserializer.h"
  10. #include "archive.h"
  11. #include "partitionedmapserializer.h"
  12. #include "driver.h"
  13. ///////////////////////////////////////////////////////////////////////////////////////
  14. // Functors used by PartidionedMap(De)serializer to read/write coordinates
  15. // and grid cells.
  16. //
  17. // Pretty much all of Guess(De)serializer is implemented by
  18. // PartitionedMap(De)serializer. These functors supply the domain specific
  19. // knowledge about how and what we're serializing (coordinates and gridcells,
  20. // of which PartitionedMap(De)serializer knows nothing).
  21. class CoordSerializer {
  22. public:
  23. void operator()(std::ostream& os, const std::pair<double, double>& coord) {
  24. os.write((char*)(&coord.first), sizeof(coord.first));
  25. os.write((char*)(&coord.second), sizeof(coord.second));
  26. }
  27. };
  28. class GridcellSerializer {
  29. public:
  30. void operator()(std::ostream& os, const Gridcell& gridcell) {
  31. ArchiveOutStream aos(os);
  32. const_cast<Gridcell&>(gridcell).serialize(aos);
  33. }
  34. };
  35. class CoordDeserializer {
  36. public:
  37. void operator()(std::istream& is, std::pair<double, double>& coord) {
  38. double a,b;
  39. is.read((char*)&a, sizeof(double));
  40. is.read((char*)&b, sizeof(double));
  41. coord = std::make_pair(a,b);
  42. }
  43. };
  44. class GridcellDeserializer {
  45. public:
  46. void operator()(std::istream& is, Gridcell& gridcell) {
  47. ArchiveInStream ais(is);
  48. gridcell.serialize(ais);
  49. }
  50. };
  51. ///////////////////////////////////////////////////////////////////////////////////////
  52. // Functions for dealing with the meta data file
  53. //
  54. namespace {
  55. std::string meta_file_path(const char* directory) {
  56. return std::string(directory)+"/meta.bin";
  57. }
  58. /// Creates the meta data file
  59. /** The meta data file contains information about the simulation
  60. * for which we're saving the state, so that we can do some
  61. * basic checking when we restart and fail if for instance
  62. * new instruction file has different PFTs
  63. */
  64. void create_meta_data(const char* directory, int num_processes) {
  65. // Create the file
  66. std::ofstream file(meta_file_path(directory).c_str(),
  67. std::ios::binary | std::ios::trunc);
  68. if (file.fail()) {
  69. fail("Failed to open meta data file for writing");
  70. }
  71. // Write number of processes involved,
  72. // we need to know this when we restart so we know how many
  73. // state files to try and open
  74. file.write((const char*)&num_processes, sizeof(num_processes));
  75. // Write out the vegetation mode and number of PFTs
  76. file.write((const char*)&vegmode, sizeof(vegmode));
  77. file.write((const char*)&npft, sizeof(npft));
  78. // Write out the names of the PFTs
  79. for (int i = 0; i < npft; i++) {
  80. xtring name = pftlist[i].name;
  81. unsigned long length = name.len();
  82. file.write((const char*)&length, sizeof(length));
  83. file.write(name, name.len());
  84. }
  85. }
  86. /// Reads in the meta data and checks it
  87. /** This is only some basic checking, there are probably
  88. * a lot of other things that are unwise to change
  89. * before restarting from state files.
  90. */
  91. void verify_meta_data(const char* directory, int& num_processes) {
  92. // Open the file
  93. std::ifstream file(meta_file_path(directory).c_str(),
  94. std::ios::binary | std::ios::in);
  95. if (file.fail()) {
  96. dprintf("Failed to open meta data file %s",meta_file_path(directory).c_str());
  97. fail(" for reading");
  98. }
  99. // Read number of processes involved in the old simulation
  100. // (not necessarily the same number as in the current job)
  101. file.read((char*)&num_processes, sizeof(num_processes));
  102. // Verify vegetation mode
  103. vegmodetype vegmode_from_file;
  104. file.read((char*)&vegmode_from_file, sizeof(vegmode_from_file));
  105. if (vegmode != vegmode_from_file) {
  106. fail("State file has incompatible vegetation mode");
  107. }
  108. // Verify that the number of PFTs is the same
  109. int npft_from_file;
  110. file.read((char*)&npft_from_file, sizeof(npft_from_file));
  111. if (npft != npft_from_file) {
  112. fail("State file has different number of PFTs");
  113. }
  114. // Verify that the PFTs have the same names
  115. for (int i = 0; i < npft; i++) {
  116. const size_t PFT_NAME_MAX_SIZE = 256;
  117. unsigned long length;
  118. file.read((char*)&length, sizeof(length));
  119. if (length > PFT_NAME_MAX_SIZE) {
  120. fail("PFT name too long");
  121. }
  122. char buffer[PFT_NAME_MAX_SIZE+1];
  123. file.read(buffer, length);
  124. buffer[length] = '\0';
  125. if (xtring(buffer) != pftlist[i].name) {
  126. fail("PFT list changed, expected %s, got %s",
  127. (char*)pftlist[i].name,
  128. buffer);
  129. }
  130. }
  131. }
  132. }
  133. ///////////////////////////////////////////////////////////////////////////////////////
  134. // GuessSerializer
  135. //
  136. // Contains members of GuessSerializer which we don't want in the header
  137. struct GuessSerializer::Impl {
  138. Impl(const char* directory, int my_rank)
  139. : pms(directory, my_rank, GridcellSerializer(), CoordSerializer()) {
  140. }
  141. PartitionedMapSerializer<Gridcell,
  142. std::pair<double, double>,
  143. GridcellSerializer,
  144. CoordSerializer> pms;
  145. };
  146. GuessSerializer::GuessSerializer(const char* directory, int my_rank, int num_processes) {
  147. try {
  148. pimpl = new Impl(directory, my_rank);
  149. // In a parallel job, only the first process creates the meta data
  150. if (my_rank == 0) {
  151. create_meta_data(directory, num_processes);
  152. }
  153. }
  154. catch (const PartitionedMapSerializerError& e) {
  155. fail(e.what());
  156. }
  157. }
  158. GuessSerializer::~GuessSerializer() {
  159. delete pimpl;
  160. }
  161. void GuessSerializer::serialize_gridcell(const Gridcell& gridcell) {
  162. try {
  163. pimpl->pms.serialize_element(std::make_pair(gridcell.get_lon(), gridcell.get_lat()),
  164. gridcell);
  165. }
  166. catch (const PartitionedMapSerializerError& e) {
  167. fail(e.what());
  168. }
  169. }
  170. ///////////////////////////////////////////////////////////////////////////////////////
  171. // GuessDeserializer
  172. //
  173. // Contains members of GuessDeserializer which we don't want in the header
  174. struct GuessDeserializer::Impl {
  175. Impl(const char* directory, int max_rank)
  176. : pmd(directory,
  177. max_rank,
  178. GridcellDeserializer(),
  179. CoordDeserializer()) {
  180. }
  181. PartitionedMapDeserializer<Gridcell,
  182. std::pair<double,double>,
  183. GridcellDeserializer,
  184. CoordDeserializer,
  185. sizeof(double)*2> pmd;
  186. };
  187. GuessDeserializer::GuessDeserializer(const char* directory) {
  188. try {
  189. int num_processes;
  190. verify_meta_data(directory, num_processes);
  191. pimpl = new Impl(directory, num_processes - 1);
  192. }
  193. catch (const PartitionedMapSerializerError& e) {
  194. fail(e.what());
  195. }
  196. }
  197. GuessDeserializer::~GuessDeserializer() {
  198. delete pimpl;
  199. }
  200. void GuessDeserializer::deserialize_gridcell(Gridcell& gridcell) {
  201. try {
  202. pimpl->pmd.deserialize_element(std::make_pair(gridcell.get_lon(), gridcell.get_lat()),
  203. gridcell);
  204. }
  205. catch (const PartitionedMapSerializerError& e) {
  206. fail(e.what());
  207. }
  208. }
  209. void GuessDeserializer::deserialize_gridcells(const std::vector<Gridcell*>& gridcells) {
  210. try {
  211. std::vector<std::pair<double, double> > coords;
  212. for (size_t i = 0; i < gridcells.size(); i++) {
  213. coords.push_back(std::make_pair(gridcells[i]->get_lon(), gridcells[i]->get_lat()));
  214. }
  215. pimpl->pmd.deserialize_elements(coords, gridcells);
  216. }
  217. catch (const PartitionedMapSerializerError& e) {
  218. fail(e.what());
  219. }
  220. }