Dear deal.II community,
For a distributed implementation, we would like to broadcast packed data
objects.
For the beginning, we would like to understand how the serialize function
for packing an object is working in the sense of the deal.II way, before it
is getting more complicated.
I created a small test with a class, which should be broadcasted. As a
comparison, I took the dealii:Point<dim>, which can obviously be packed and
broadcasted without any issues.
Unfortunately, the broadcast for our class does not work. We assume that
our serialize function is not in the correct manner. Is it possible to pack
own objects using the dealii::Utilities:pack() function? What is the proper
way defining the serialize function?
Attached you'll find our small example code. We are using deal.II 9.0.1.
Thank you very much in advance.
Best regards,
Maurice Rohracker
Master Student Computational Engineering
FAU Erlange-Nürnberg
--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see
https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to the Google Groups
"deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/dealii/cc9c1b57-3c12-4417-9935-6e65f569aeb6o%40googlegroups.com.
cmake_minimum_required(VERSION 3.10)
set(CMAKE_BUILD_TYPE Release)
find_package(MPI)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
# Ensure dealii is present
SET(DEAL_II_DIR /usr/local/bin/dealii/)
FIND_PACKAGE(deal.II 9.0.0 QUIET
HINTS ${deal.II_DIR} ${DEAL_II_DIR} $ENV{DEAL_II_DIR}
)
IF(NOT ${deal.II_FOUND})
MESSAGE(FATAL_ERROR "\n"
"*** Could not locate a (sufficiently recent) version of deal.II.
***\n\n"
"You may want to either pass a flag -DDEAL_II_DIR=/path/to/deal.II
to cmake\n"
"or set an environment variable \"DEAL_II_DIR\" that contains this
path."
)
ENDIF()
SET(CMAKE_CXX_COMPILER mpicxx)
add_executable(serializationTest serializationTestMain.cpp)
target_link_libraries(serializationTest ${MPI_CPP_LIBRARIES})
DEAL_II_SETUP_TARGET(serializationTest)
//
// Created by Maurice on 04.06.2020.
//
#ifndef SERIALIZATIONTEST_ROOM_H
#define SERIALIZATIONTEST_ROOM_H
#include <string>
class Room {
private:
int nWindows;
int nDoors;
public:
Room() : nWindows(0), nDoors(0) { }
Room(int nW, int nD) : nWindows(nW), nDoors(nD) { }
~Room() { }
Room(const Room &other) : nWindows(other.nWindows), nDoors(other.nDoors) { }
Room& operator=(const Room &other) {
if (this != &other) {
nWindows = other.nWindows;
nDoors = other.nDoors;
}
return *this;
}
void setWindows(int n) {
nWindows = n;
}
void setDoors(int n) {
nDoors = n;
}
std::string print() const {
return "number of windows: " + std::to_string(nWindows) + " | number of doors: " + std::to_string(nDoors);
}
template<class Archive>
void serialize(Archive &ar, const unsigned int version);
};
template<class Archive>
inline
void
Room::serialize(Archive & ar, const unsigned int version) {
ar & nWindows;
ar & nDoors;
}
#endif //SERIALIZATIONTEST_ROOM_H
//
// Created by Maurice on 03.06.2020.
//
#include "mpi.h"
#include <deal.II/base/point.h>
#include <deal.II/base/mpi.h>
#include <string>
#include <iostream>
#include "Room.h"
int main(int argc, char *argv[]) {
dealii::Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
MPI_Comm const & mpi_communicator(MPI_COMM_WORLD);
Room r1;
const unsigned dim = 5;
dealii::Point<dim> p1;
if (dealii::Utilities::MPI::this_mpi_process(mpi_communicator) == 0) {
r1.setWindows(4);
r1.setDoors(6);
for(int i = 0; i < dim; ++i)
p1[i] = i + 2;
}
// pack objects
auto buffer = dealii::Utilities::pack(r1);
auto bufferPoint = dealii::Utilities::pack(p1);
// broadcast objects
MPI_Bcast(&buffer[0], buffer.size(), MPI_CHAR, 0, mpi_communicator); // with commenting this line the code works without errors
MPI_Bcast(&bufferPoint[0], bufferPoint.size(), MPI_CHAR, 0, mpi_communicator);
// unpack objects (if not root process)
if (dealii::Utilities::MPI::this_mpi_process(mpi_communicator) != 0) {
r1 = dealii::Utilities::unpack<Room>(buffer);
p1 = dealii::Utilities::unpack<dealii::Point<5> >(bufferPoint);
}
// print to see, if all worked
std::cout << "Hello form "
<< dealii::Utilities::MPI::this_mpi_process(mpi_communicator)
<< " of "
<< dealii::Utilities::MPI::n_mpi_processes(mpi_communicator)
<< std::endl;
std::cout << "Process: "
<< dealii::Utilities::MPI::this_mpi_process(mpi_communicator) << " "
<< r1.print() << std::endl;
std::cout << "Process: "
<< dealii::Utilities::MPI::this_mpi_process(mpi_communicator) << std::endl;
for(int i = 0; i < dim; ++i)
std::cout << p1[i] << " ";
std::cout << std::endl;
}