Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <mpi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define n 16
- int main(int argc, char *argv[])
- {
- MPI_Init(&argc, &argv);
- MPI_Comm comm = MPI_COMM_WORLD;
- int p,r;
- MPI_Comm_size(comm, &p);
- MPI_Comm_rank(comm, &r);
- int *arr;
- arr = NULL;
- if (r == 0){
- arr = (int *) malloc(n * n * sizeof(int));
- for (int i = 0; i < n * n; i++) arr[i] = i;
- for (int i = 0; i < n; i++){
- printf("n");
- for (int j = 0; j < n; j++)
- printf("%4d", arr[i * n + j]);
- }
- }
- printf("n");
- int ps = sqrt(p);
- int ns = n / ps;
- if (r == 0) {
- printf("ps: %d ns: %dn", ps, ns);
- }
- /* create datatype */
- MPI_Datatype block;
- MPI_Type_vector(ns, ns, n, MPI_INT, &block);
- int blocks[ps];
- MPI_Aint displs[ps];
- for (int i = 0; i < ps; i++) {
- blocks[i] = 1;
- displs[i] = i * sizeof(int);
- }
- MPI_Datatype types[ps];
- //for (int i = 0; i < ps - 1; i++) types[i] = block;
- //types[ps - 1] = MPI_UB;
- types[0] = block;
- for (int i = 1; i < ps; i++) types[i] = MPI_UB;
- //types[0] = block;
- //types[1] = MPI_UB;
- if (r == 0) {
- printf("displs:n");
- for(int i = 0; i < ps; i++) printf("%3ld", displs[i]);
- printf("n");
- }
- MPI_Datatype row;
- MPI_Type_struct(ps, blocks, displs, types, &row);
- MPI_Type_commit(&row);
- /* prepare scatter */
- int sdispl[p]; int sendcounts[p];
- for (int i = 0; i < p; i++) {
- sdispl[i] = (i % ps) + (i / ps) * (ns * ps);
- sendcounts[i] = 1;
- }
- if (r == 0) {
- printf("sdispl: n");
- for (int i = 0; i < 4; i++) printf("%3d", sdispl[i]);
- printf("n");
- }
- int rcv[ns * ns];
- MPI_Scatterv(arr, sendcounts, sdispl, row, rcv, ns * ns, MPI_INT, 0, comm);
- int result = 1;
- if (r == result) {
- printf("result for %d:n", result);
- for (int i = 0; i < ns * ns; i++) {
- printf("%4d", rcv[i]);
- if ((i+1) % ns == 0) printf("n");
- }
- }
- if (arr != NULL) free(arr);
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement