diff --git a/mpi/image_parts_gather.c b/mpi/image_parts_gather.c new file mode 100644 index 0000000..854301b --- /dev/null +++ b/mpi/image_parts_gather.c @@ -0,0 +1,41 @@ +#include +#include + +#define image_size 1024 + +int main(int argc, char *argv[]) { + int myrank, mysize; + int * imgPart; + int * buf; + int i; + int partSize; + int count; + + MPI_Request request; + MPI_Status status; + MPI_Status s; + + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &myrank); + MPI_Comm_size(MPI_COMM_WORLD, &mysize); + + imgPart = (int*)malloc(image_size/mysize * sizeof(int)); + +/* Generate image parts */ + for (i=0; i < (image_size/mysize) ; i++){ + imgPart[i] = myrank * 1; + } + partSize = image_size/mysize; + +/* Process 0 is the root of the collective, i.e. the receiver of all parts */ + if (myrank == 0) /* Only the root must allocate buf */ + buf = (int*)malloc(image_size* sizeof(int)); + + MPI_Gather(imgPart, partSize, MPI_INT, buf, partSize, MPI_INT, 0, MPI_COMM_WORLD); + + free(imgPart); + if (myrank==0) free(buf); + + MPI_Finalize(); +} + diff --git a/mpi/image_parts_in_order.c b/mpi/image_parts_in_order.c new file mode 100644 index 0000000..2cab710 --- /dev/null +++ b/mpi/image_parts_in_order.c @@ -0,0 +1,44 @@ +#include +#include + +#define image_size 1024 + +int main(int argc, char *argv[]) { + int myrank, mysize; + int * imgPart; + int * buf; + int i; + int partSize; + + MPI_Request request; + MPI_Status status; + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &myrank); + MPI_Comm_size(MPI_COMM_WORLD, &mysize); + + imgPart = malloc(image_size/mysize * sizeof(int)); + +/* Generate image parts */ + for (i=0; i < (image_size/mysize) ; i++){ + imgPart[i] = myrank * 1; + } + partSize = image_size/mysize; +/* Each process sends */ + MPI_Isend(imgPart, partSize, MPI_INT, 0, 0, MPI_COMM_WORLD, &request); + +// Process 0 receives all parts into buf + if (myrank == 0){ + buf = malloc(image_size* sizeof(int)); + for (i=0; i +#include + +#define image_size 1024 + +int main(int argc, char *argv[]) { + int myrank, mysize; + int * imgPart; + int * buf; + int i; + int partSize; + int count; + + MPI_Request request; + MPI_Status status; + MPI_Status s; + + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &myrank); + MPI_Comm_size(MPI_COMM_WORLD, &mysize); + + imgPart = (int*)malloc(image_size/mysize * sizeof(int)); + +/* Generate image parts */ + for (i=0; i < (image_size/mysize) ; i++){ + imgPart[i] = myrank * 1; + } + partSize = image_size/mysize; + +/* Each process sends */ + MPI_Isend(imgPart, partSize, MPI_INT, 0, 0, MPI_COMM_WORLD, &request); +// Process 0 receives all parts into buf + if (myrank == 0) { + buf = (int*)malloc(image_size* sizeof(int)); + for (i=0; i