如何在C代码上实现MPI过滤器? [英] How to implement a MPI filter on C code?

查看:70
本文介绍了如何在C代码上实现MPI过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下过滤器代码的MPI,但我在执行该操作时遇到了困难.应该怎么做?:

I am trying to implement a MPI of the filter code below, but I'm facing difficulties doing it. How should it be done?:

过滤器代码:

int A[100000][100000];
int B[100000][100000];

for (int i=1; i<(100000 - 1); i++)
 for (int i=1; j<(100000 - 1); j++)
  B[i][j] = A[i-1][j] + A[i+1][j] + A[i][j-1] + A[i][j+1] - 4*A[i][j];

这是我遵循MPI的六个功能时尝试过的方法:

This is what I have tried while following the six functions of MPI:

 int myrank; /* Rank of process */
    int numprocs; /* Number of processes */
    int source; /* Rank of sender */
    int dest; /* Rank of receiver */

    char message[100]; /* Storage for the message */
    MPI_Status status; /* Return status for receive */
    MPI_Init( & argc, & argv);
    MPI_Comm_size(MPI_COMM_WORLD, & numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, & myrank);

    if (myrank != 0)
    {
        dest = 0;
        MPI_Send(message, strlen(message) + 1,
          MPI_CHAR, dest, 15, MPI_COMM_WORLD);
      } else {
        for (source = 1; source < numprocs; source++) {
          MPI_Recv(message, 100, MPI_CHAR, source,
            15, MPI_COMM_WORLD, & status);
        }
      }
      MPI_Finalize();

推荐答案

我会这样.首先,我要有这段代码

I'd go like this. First of all, I'd have this code

int A[100000][100000];
int B[100000][100000];

已替换为动态分配.您不需要每个进程的所有内存.

replaced with dynamic allocations. You don't need all that memory for each and every process.

然后,我将数组A发送到不同的进程.按行.

Then, I'd send array A to different processes. By rows.

数据帧的高度"是多少(行数):

What is the "height" of data frame (number of rows):

delta = (100000 - 2) / (numprocs-1);     // we don't count first and last row
reminder = (100000 - 2) % (numprocs-1);  // it might be that we need to give 
                                         // little bit more to calculate
                                         // to one of the processes

// we are starting from row with idx=1 (second row) and we want to finish when
// we hit last row
if(myrank == 0) {
  for( int i=1; i < numprocs; i++ ) {
    // +100000 - we need two more rows to calculate data
    int how_many_bytes = delta * 100000 + 200000; 
    if(reminder != 0 && i == (numprocs-1)) {
      how_many_bytes += reminder * 100000;
    }
    MPI_Send(&(A[(i-1)*delta][0]), how_many_bytes, MPI_INT, i, 0,
                 MPI_COMM_WORLD);
  }
} else {
  // allocate memory for bytes
  int *local_array = NULL;
  int how_many_bytes = delta * 100000 + 200000; 
  if(reminder != 0 && i == (numprocs-1)) {
    how_many_bytes += reminder * 100000;
  }
  local_array = malloc(how_many_bytes * sizeof(int));
  MPI_Status status;

  MPI_Recv(
    local_array,
    how_many_bytes,
    MPI_INT,
    0,
    0,
    MPI_COMM_WORLD,
    &status);
} 

// perform calculations for each and every slice
// remembering that we always have on extra row on
// top and one at the bottom
// send data back to master (as above, but vice versa).

这篇关于如何在C代码上实现MPI过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆