Skip to content

Instantly share code, notes, and snippets.

@floli
Created February 14, 2018 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floli/310980790d5d76caac0b19a937e2a502 to your computer and use it in GitHub Desktop.
Save floli/310980790d5d76caac0b19a937e2a502 to your computer and use it in GitHub Desktop.
MPI_ERR_TRUNCATE in MPI_Unpack
#include <vector>
#include <mpi.h>
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
{
MPI_Request req1, req2;
std::vector<int> vec = {1, 2, 3};
int packSize = sizeof(int) * vec.size();
int position = 0;
std::vector<char> packSendBuf(packSize);
int vecSize = vec.size();
MPI_Pack(vec.data(), vec.size(), MPI_INT, packSendBuf.data(), packSize, &position, MPI_COMM_WORLD);
int estimatedPackSize = 0;
MPI_Pack_size(vec.size(), MPI_INT, MPI_COMM_WORLD, &estimatedPackSize);
std::cout << "packSize = " << packSize << ", estimatedPackSize = " << estimatedPackSize << std::endl;
std::cout << "position after pack = " << position << std::endl;
MPI_Isend(&vecSize, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &req1);
MPI_Isend(packSendBuf.data(), position, MPI_PACKED, 0, 0, MPI_COMM_WORLD, &req2);
}
{
int vecSize, msgSize;
int packSize = 0, position = 0;
MPI_Recv(&vecSize, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Status status;
MPI_Probe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_PACKED, &packSize);
char packBuffer[packSize];
std::cout << "packSize from get_count = " << packSize << std::endl;
std::vector<int> vec(vecSize);
MPI_Recv(packBuffer, packSize, MPI_PACKED, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Unpack(packBuffer, vecSize, &position, vec.data(), vecSize, MPI_INT, MPI_COMM_WORLD);
}
MPI_Finalize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment