MPI Data Type

I am trying to broadcast a used-defined data(boundingbox) but unfortunately, the data differ in different processors.

here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void Geometry::generatePointsInBoundingBox() {
    if (myrank == 0) {
        Point minPoint = { std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity() };
        Point maxPoint = { -std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity() };

        for (int t = 0; t < triangles.size(); ++t) {
            const auto& triangle = triangles[t];
            minPoint.x = std::min(minPoint.x, std::min(triangle.x1, std::min(triangle.x2, triangle.x3)));
            minPoint.y = std::min(minPoint.y, std::min(triangle.y1, std::min(triangle.y2, triangle.y3)));
            minPoint.z = std::min(minPoint.z, std::min(triangle.z1, std::min(triangle.z2, triangle.z3)));
            maxPoint.x = std::max(maxPoint.x, std::max(triangle.x1, std::max(triangle.x2, triangle.x3)));
            maxPoint.y = std::max(maxPoint.y, std::max(triangle.y1, std::max(triangle.y2, triangle.y3)));
            maxPoint.z = std::max(maxPoint.z, std::max(triangle.z1, std::max(triangle.z2, triangle.z3)));
        }

        boundingbox = { minPoint.x, minPoint.y, minPoint.z, maxPoint.x, maxPoint.y, maxPoint.z };
    }

    MPI_Datatype mpiBoundingBoxType = mpi_caller.createBoundingBoxMPIType();
    MPI_Bcast(&boundingbox, 1, mpiBoundingBoxType, 0, MPI_COMM_WORLD);

    std::cout << "Processor " << myrank << ": BoundingBox: "
        << "Min(" << boundingbox.min_x << ", " << boundingbox.min_y << ", " << boundingbox.min_z << ") "
        << "Max(" << boundingbox.max_x << ", " << boundingbox.max_y << ", " << boundingbox.max_z << ")"
        << std::endl;

    MPI_Type_free(&mpiBoundingBoxType);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	MPI_Datatype createBoundingBoxMPIType() {
		int blocklengths[6] = { 1, 1, 1, 1, 1, 1 };
		MPI_Datatype types[6] = { MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE };
		MPI_Aint displacements[6];

		displacements[0] = offsetof(BoundingBox, min_x);
		displacements[1] = offsetof(BoundingBox, max_x);
		displacements[2] = offsetof(BoundingBox, min_y);
		displacements[3] = offsetof(BoundingBox, max_y);
		displacements[4] = offsetof(BoundingBox, min_z);
		displacements[5] = offsetof(BoundingBox, max_z);

		MPI_Datatype mpiBoundingBoxType;
		MPI_Type_create_struct(6, blocklengths, displacements, types, &mpiBoundingBoxType);
		MPI_Type_commit(&mpiBoundingBoxType);

		return mpiBoundingBoxType;
	}


1
2
3
4
5
6
7
8
struct BoundingBox {
    double min_x;
    double max_x;
    double min_y;
    double max_y;
    double min_z;
    double max_z;
};  


I was initially guessing it has something to do with infinity. I canged it to max and min and the same result.
here is an example of output.

Processor 0: BoundingBox: Min(1.14, -179.87, -183.98) Max(-205.75, 17.83, -152.17)
Processor 1: BoundingBox: Min(1.13682, -179.867, -183.981) Max(-205.747, 17.8319, -152.17)


As you can see the data are not the same. any help is appreciated in advance.
Last edited on
Your data ARE the same.

It's just that processor 0 has been told somewhere in code that you haven't shown that floating point output should have precision 2 decimal places, whereas processor 1 hasn't been given any such instruction and so outputs doubles to the default 6 sig figs.
Thanks, you are right.
Last edited on
Topic archived. No new replies allowed.