[在C中使用mpi的2D矩阵矢量乘法

问题描述 投票:0回答:1

我是并行编程的新手,我的项目是在C编程语言中使用mpi进行二维矩阵矢量乘法。我有这个乘法功能,但是我不知道如何使用mpi来运行它。你能帮我吗?

我写这些命令,

$ mpicc -g -Wall -o matrix2d matrix2d.c -lm -c
$ mpiexec  ./matrix2d.c

但是我遇到一些错误。

mpiexec was unable to launch the specified application as it could not access
or execute an executable:

Executable: ./matrix2d
Node: ubuntu

while attempting to start process rank 0.

2D矩阵-向量乘法功能在下面:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>


void* MatrixVectorMultiply_2D(int n, double *a, double *b, double *x, MPI_Comm comm)
 {
   int ROW=0, COL=1; /* Improve readability */
   int i, j, nlocal;
   double *px; /* Will store partial dot products */
   int npes, dims[2], periods[2], keep_dims[2];
   int myrank, my2drank, mycoords[2];
   int other_rank, coords[2];
   MPI_Status status;
   MPI_Comm comm_2d, comm_row, comm_col;

   /* Get information about the communicator */
   MPI_Comm_size(comm, &npes);
   MPI_Comm_rank(comm, &myrank);

   /* Compute the size of the square grid */
   dims[ROW] = dims[COL] = sqrt(npes);

  nlocal = n/dims[ROW];

 /* Allocate memory for the array that will hold the partial dot-products */
  px = malloc(nlocal*sizeof(double));

  /* Set up the Cartesian topology and get the rank & coordinates of the process in
  this topology */
  periods[ROW] = periods[COL] = 1; /* Set the periods for wrap-around connections */

  MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 1, &comm_2d);

  MPI_Comm_rank(comm_2d, &my2drank); /* Get my rank in the new topology */
  MPI_Cart_coords(comm_2d, my2drank, 2, mycoords); /* Get my coordinates */

  /* Create the row-based sub-topology */
  keep_dims[ROW] = 0;
  keep_dims[COL] = 1;
  MPI_Cart_sub(comm_2d, keep_dims, &comm_row);
  /* Create the column-based sub-topology */
  keep_dims[ROW] = 1;
  keep_dims[COL] = 0;
  MPI_Cart_sub(comm_2d, keep_dims, &comm_col);

 /* Redistribute the b vector. */
 /* Step 1. The processors along the 0th column send their data to the diagonal
  processors */
  if (mycoords[COL] == 0 && mycoords[ROW] != 0) { /* I'm in the first column */
    coords[ROW] = mycoords[ROW];
    coords[COL] = mycoords[ROW];
    MPI_Cart_rank(comm_2d, coords, &other_rank);
    MPI_Send(b, nlocal, MPI_DOUBLE, other_rank, 1, comm_2d);
    }
  if (mycoords[ROW] == mycoords[COL] && mycoords[ROW] != 0) {
     coords[ROW] = mycoords[ROW];
     coords[COL] = 0;
     MPI_Cart_rank(comm_2d, coords, &other_rank);
     MPI_Recv(b, nlocal, MPI_DOUBLE, other_rank, 1, comm_2d,
       &status);
    }

    /* Step 2. The diagonal processors perform a column-wise broadcast */
     coords[0] = mycoords[COL];
     MPI_Cart_rank(comm_col, coords, &other_rank);
     MPI_Bcast(b, nlocal, MPI_DOUBLE, other_rank, comm_col);

     /* Get into the main computational loop */
     for (i=0; i<nlocal; i++) {
     px[i] = 0.0;
     for (j=0; j<nlocal; j++) {
     px[i] += a[i*nlocal+j]*b[j];
    }
   }

   /* Perform the sum-reduction along the rows to add up the partial dot-products */
    coords[0] = 0;
    MPI_Cart_rank(comm_row, coords, &other_rank);
    MPI_Reduce(px, x, nlocal, MPI_DOUBLE, MPI_SUM, other_rank,
     comm_row);

    MPI_Comm_free(&comm_2d); /* Free up communicator */
    MPI_Comm_free(&comm_row); /* Free up communicator */
    MPI_Comm_free(&comm_col); /* Free up communicator */

    free(px);

  MPI_Finalize();
c parallel-processing mpi matrix-multiplication
1个回答
0
投票

我刚刚解决了之前的问题,但是现在我遇到了另一个错误,但仍然无法摆脱。拜托,有人可以帮我吗?

我运行此命令:

  mpiexec -np 7 ./matrix2d.c ./matrix2d

但是我遇到了另一个错误。

Open MPI tried to fork a new process via the "execve" system call but
failed.  Open MPI checks many things before attempting to launch a
child process, but nothing is perfect. This error may be indicative
 of another problem on the target host, or even something as silly as
 having specified a directory for your application. Your job will now
 abort.

 Local host:        ubuntu
 Application name:  ./matrix2d.c
 Error:             Exec format error
--------------------------------------------------------------------------
 7 total processes failed to start
[ubuntu:83710] 6 more processes have sent help message help-orte-odls- 
 default.txt / execve error
[ubuntu:83710] Set MCA parameter "orte_base_help_aggregate" to 0 to see all 
 help / error messages

是什么意思?我在哪里做错了?

谢谢你,谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.