Assignment-04/Question-05/matvec.c
// Slave process computes inner product and returns result to master process
Slave process computes inner product and returns result to master process
Slave process computes inner product and returns result to master process
void slave(int proc_id, int n_rows, int n_cols) {
    double *x = (double *) malloc(n_cols*sizeof(double));
    double *my_row = (double *) malloc(n_cols*sizeof(double));
    double ans = 0.0;
    int row = 0;
    MPI_Status status;
    MPI_Bcast(x, n_cols, MPI_DOUBLE, MASTER, MPI_COMM_WORLD);
    MPI_Recv(my_row, n_cols, MPI_DOUBLE, MASTER, MPI_ANY_TAG, 
             MPI_COMM_WORLD, &status);
    row = status.MPI_TAG;
    while (row < n_rows) {
        ans = cblas_ddot(n_cols, my_row, 1, x, 1);
        MPI_Send(&ans, 1, MPI_DOUBLE, MASTER, row, MPI_COMM_WORLD);
        MPI_Recv(my_row, n_cols, MPI_DOUBLE, MASTER, MPI_ANY_TAG, 
                 MPI_COMM_WORLD, &status);
        row = status.MPI_TAG;
    }
}
void slave ( int proc_id , int n_rows , int n_cols ) { double * x = ( double * ) malloc ( n_cols * sizeof ( double ) ) ; double * my_row = ( double * ) malloc ( n_cols * sizeof ( double ) ) ; double ans = 0.0 ; int row = 0 ; MPI_Status status ; MPI_Bcast ( x , n_cols , MPI_DOUBLE , MASTER , MPI_COMM_WORLD ) ; MPI_Recv ( my_row , n_cols , MPI_DOUBLE , MASTER , MPI_ANY_TAG , MPI_COMM_WORLD , & status ) ; row = status . MPI_TAG ; while ( row < n_rows ) { ans = cblas_ddot ( n_cols , my_row , 1 , x , 1 ) ; MPI_Send ( & ans , 1 , MPI_DOUBLE , MASTER , row , MPI_COMM_WORLD ) ; MPI_Recv ( my_row , n_cols , MPI_DOUBLE , MASTER , MPI_ANY_TAG , MPI_COMM_WORLD , & status ) ; row = status . MPI_TAG ; } }
Slave process computes inner product and returns result to master process
Slave process computes inner product and returns result to master process
// Allocate arrays for receiving and sending rows & dot products // 1. Receive vector x from master process // Receive row to calculate from master process // Compute inner product and return result to master process // Receive new row from master