Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code for one dimentional matrix multiplication


C++
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define SIZE 1024
#define FROM MASTER 1 
#define FROM WORKER 2
#define DEBUG 0 /∗ 1 = debug on, 0 = debug off ∗/
MPI Status status;
static double a[SIZE][SIZE];
static double b[SIZE][SIZE];
static double c[SIZE][SIZE];
static void
init matrix(void)
{
    int i, j;
    for (i = 0; i < SIZE; i++)
        for (j = 0; j < SIZE; j++) {
            a[i][j] = 1.0;
            b[i][j] = 1.0;
        }
}
static void
print matrix(void)
{
    int i, j;
    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++)
            printf(" %f", c[i][j]);
        printf("\n");
    }
}
int
main(int argc, char ∗∗argv)
{
    int myrank, nproc;
    int rows;
    int mtype;
    int dest, src, offset;
    double start time, end time;
    int i, j, k;
    MPI Init(&argc, &argv);
    MPI Comm size(MPI COMM WORLD, &nproc);
    MPI Comm rank(MPI COMM WORLD, &myrank);
    if (myrank == 0) {
        printf("SIZE = %d, number of nodes = %d\n", SIZE, nproc);
        init matrix();
        start time = MPI Wtime();
        rows = SIZE / nproc;
        mtype = FROM MASTER;
        offset = rows;
        for (dest = 1; dest < nproc; dest++) {
            if (DEBUG)
                printf(" sending %d rows to task %d\n", rows, dest);
            MPI Send(&offset, 1, MPI INT, dest, mtype, MPI_COMM_WORLD);
            MPI Send(&rows, 1, MPI INT, dest, mtype, MPI_COMM_WORLD);
            MPI Send(&a[offset][0], rows∗SIZE, MPI DOUBLE, dest, mtype,
                MPI_COMM_WORLD);
            MPI Send(&b, SIZE∗SIZE, MPI DOUBLE, dest, mtype, MPI_COMM_WORLD);
            offset += rows;
        }
        for (i = 0; i < rows; i++) {
            for (j = 0; j < SIZE; j++) {
                c[i][j] = 0.0;
                for (k = 0; k < SIZE; k++)
                    c[i][j] = c[i][j] + a[i][k] ∗ b[k][j];
            }
        }
        mtype = FROM WORKER;
        for (src = 1; src < nproc; src++) {
            MPI Recv(&offset, 1, MPI INT, src, mtype, MPI_COMM_WORLD, &status);
            MPI Recv(&rows, 1, MPI INT, src, mtype, MPI_COMM_WORLD, &status);
            MPI Recv(&c[offset][0], rows∗SIZE, MPI_DOUBLE, src, mtype,
                MPI_COMM_WORLD, &status);
            if (DEBUG)
                printf(" recvd %d rows from task %d, offset = %d\n",
                rows, src, offset);
        }
        end time = MPI_Wtime();
        if (DEBUG)
            print matrix();
        printf("Execution time on % 2d nodes: %f\n", nproc, end time−start time);
    } else {
        mtype = FROM MASTER;
        MPI Recv(&offset, 1, MPI INT, 0, mtype, MPI COMM WORLD, &status);
        MPI Recv(&rows, 1, MPI INT, 0, mtype, MPI COMM WORLD, &status);
        MPI Recv(&a[offset][0], rows∗SIZE, MPI DOUBLE, 0, mtype,
            MPI COMM WORLD, &status);
        MPI Recv(&b, SIZE∗SIZE, MPI DOUBLE, 0, mtype, MPI COMM WORLD,
            &status);
        if (DEBUG)
            printf("Rank = %d, offset = %d, row = %d, a[offset][0] = %e, b[0][0] = %e\n",
            myrank, offset, rows, a[offset][0], b[0][0]);
        for (i = offset; i < offset + rows; i++)
            for (j = 0; j < SIZE; j++) {
            c[i][j] = 0.0;
            for (k = 0; k < SIZE; k++)
                c[i][j] = c[i][j] + a[i][k] ∗ b[k][j];
            }
        if (DEBUG)
            printf("Rank = %d, offset = %d, row = %d, c[offset][0] = %e\n",
            myrank, offset, rows, a[offset][0]);
        mtype = FROM WORKER;
        MPI Send(&offset, 1, MPI INT, 0, mtype, MPI COMM WORLD);
        MPI Send(&rows, 1, MPI INT, 0, mtype, MPI COMM WORLD);
        MPI Send(&c[offset][0], rows∗SIZE, MPI DOUBLE, 0, mtype,
            MPI COMM WORLD);
    }
    MPI_Finalize();
    return 0;
}



What I have tried:

I want tips writing two dimentional matrix multiplication in mpi
Posted
Updated 11-Apr-16 14:31pm
Comments
CHill60 11-Apr-16 18:48pm    
And what have you tried to achieve two dimensional matrix multiplication? What went wrong?
Sergey Alexandrovich Kryukov 11-Apr-16 20:27pm    
You code does not contain a function which calculate matrix product. That is, you did not even show what you have tried. It looks like you know who to write code. (Well, static data declarations outside functions are bad, but I like that you use separately defined SIZE; it makes your code maintainable.) So, you don't need programming tips, you only need mathematical definitions of these very simple operations.
—SA

1 solution

Please see my comment to the question. You don't need programming tips much, and the mathematical definitions you can find in many places. Matrix product is a very simple operation (in contrast to division, or inversion, complicated and time-taking). See, for example:
Matrix (mathematics) — Wikipedia, the free encyclopedia[^],
Matrix multiplication — Wikipedia, the free encyclopedia[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900