[deal.II] Re: Broadcasting packed objects

2020-06-09 Thread Maurice Rohracker
Thanks, Peter, that helped so far!

Another question would be, how would then the serialize function look like 
if one has another class as a member of a class. So, in this case, how 
would the serialize function look like if one has a class House and two of 
its member are of the type Room?
A serializing of nested objects so to speak.


Am Montag, 8. Juni 2020 14:19:25 UTC+2 schrieb peterrum:
>
> Dear Maurice,
>
> The problem is that the size of `auto buffer = 
> dealii::Utilities::pack(r1);` is not the same on all processes, which is a 
> requirement if you use `MPI_Bcast`. My suggestion would to split the 
> procedure into two steps: 1) bcast the size on rank 1; 2) bcast the actual 
> data.
>
> Peter
>
> On Monday, 8 June 2020 12:46:53 UTC+2, Maurice Rohracker wrote:
>>
>> 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, 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 dealii+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/dealii/31811f32-55a7-41d6-8a71-376e2a07cd76o%40googlegroups.com.


[deal.II] Broadcasting packed objects

2020-06-08 Thread Maurice Rohracker
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, 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 dealii+unsubscr...@googlegroups.com.
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 

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 ) : nWindows(other.nWindows), nDoors(other.nDoors) { }

Room& operator=(const Room ) {
if (this != ) {
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
void serialize(Archive , const unsigned int version);
};

template
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 
#include 
#include 
#include 
#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 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([0], buffer.size(), MPI_CHAR, 0, mpi_communicator); // with commenting this line the code works without errors
  MPI_Bcast([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(buffer);
  p1 = dealii::Utilities::unpack >(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_communi