MPI Reading from a text file

Text isn’t a great format for parallel processing exactly because you don’t know ahead of time where (say) line 25001 begins. So these sorts of problems are often dealt with ahead of time through some preprocessing step, either building an index or partitioning the file into the appropriate number of chunks for each process to … Read more

Sending columns of a matrix using MPI_Scatter

There’s a long description of this issue in my answer to this question: the fact that many people have these questions is proof that it’s not obvious and the ideas take some getting used to. The important thing to know is what memory layout the MPI datatype describes. The calling sequence to MPI_Type_vector is: int … Read more

MPI partition matrix into blocks

What you’ve got is pretty much “best practice”; it’s just a bit confusing until you get used to it. Two things, though: First, be careful with this: sizeof(MPI_CHAR) is, I assume, 4 bytes, not 1. MPI_CHAR is an (integer) constant that describes (to the MPI library) a character. You probably want sizeof(char), or SIZE/2*sizeof(char), or … Read more

Understanding concurrent file writes from multiple processes

Atomicity of writes less than PIPE_BUF applies only to pipes and FIFOs. For file writes, POSIX says: This volume of POSIX.1-2008 does not specify behavior of concurrent writes to a file from multiple processes. Applications should use some form of concurrency control. …which means that you’re on your own – different UNIX-likes will give different … Read more

Trouble Understanding MPI_Type_create_struct

The purpose of MPI_Type_create_struct() is, as you know, to provide a way to create user’s MPI_Datatypes mapping his structured types. These new types will subsequently be usable for MPI communications and other calls just as the default types, allowing for example to transfer arrays of structures the same way you would transfer arrays of ints … Read more

Using MPI_Bcast for MPI communication

This is a common source of confusion for people new to MPI. You don’t use MPI_Recv() to receive data sent by a broadcast; you use MPI_Bcast(). Eg, what you want is this: #include <mpi.h> #include <stdio.h> int main(int argc, char** argv) { int rank; int buf; const int root=0; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if(rank == … Read more