文件读取问题 - 平行版生命游戏

File read issues - Parallel version of Game of Life

对于我的并行计算 class,我正在开发一个使用 MPI 并行化生命游戏的项目。我正在具体执行 Michael J. Quinn 在 "Parallel Programming in C with MPI and OpenMP" 中的练习 6.13。

我正在使用作者预先编写的库函数,"read_row_striped_matrix"。以下是该函数的代码:

/*
 *   Process p-1 opens a file and inputs a two-dimensional
 *   matrix, reading and distributing blocks of rows to the
 *   other processes.
 */

void read_row_striped_matrix (
   char        *s,        /* IN - File name */
   void      ***subs,     /* OUT - 2D submatrix indices */
   void       **storage,  /* OUT - Submatrix stored here */
   MPI_Datatype dtype,    /* IN - Matrix element type */
   int         *m,        /* OUT - Matrix rows */
   int         *n,        /* OUT - Matrix cols */
   MPI_Comm     comm)     /* IN - Communicator */
{
   int          datum_size;   /* Size of matrix element */
   int          i;
   int          id;           /* Process rank */
   FILE        *infileptr;    /* Input file pointer */
   int          local_rows;   /* Rows on this proc */
   void       **lptr;         /* Pointer into 'subs' */
   int          p;            /* Number of processes */
   void        *rptr;         /* Pointer into 'storage' */
   MPI_Status   status;       /* Result of receive */
   int          x;            /* Result of read */

   MPI_Comm_size (comm, &p);
   MPI_Comm_rank (comm, &id);
   datum_size = get_size (dtype);

   /* Process p-1 opens file, reads size of matrix,
      and broadcasts matrix dimensions to other procs */

   if (id == (p-1)) {
      infileptr = fopen (s, "r");
      if (infileptr == NULL) *m = 0;
      else {
         fread (m, sizeof(int), 1, infileptr);
         fread (n, sizeof(int), 1, infileptr);
      }      
   }
   MPI_Bcast (m, 1, MPI_INT, p-1, comm);

   if (!(*m)) MPI_Abort (MPI_COMM_WORLD, OPEN_FILE_ERROR);

   MPI_Bcast (n, 1, MPI_INT, p-1, comm);

   local_rows = BLOCK_SIZE(id,p,*m);

   /* Dynamically allocate matrix. Allow double subscripting
      through 'a'. */

   *storage = (void *) my_malloc (id,
       local_rows * *n * datum_size);
   *subs = (void **) my_malloc (id, local_rows * PTR_SIZE);

   lptr = (void *) &(*subs[0]);
   rptr = (void *) *storage;
   for (i = 0; i < local_rows; i++) {
      *(lptr++)= (void *) rptr;
      rptr += *n * datum_size;
   }

   /* Process p-1 reads blocks of rows from file and
      sends each block to the correct destination process.
      The last block it keeps. */

   if (id == (p-1)) {
      for (i = 0; i < p-1; i++) {
         x = fread (*storage, datum_size,
            BLOCK_SIZE(i,p,*m) * *n, infileptr);
         MPI_Send (*storage, BLOCK_SIZE(i,p,*m) * *n, dtype,
            i, DATA_MSG, comm);
      }
      x = fread (*storage, datum_size, local_rows * *n,
         infileptr);
      fclose (infileptr);
   } else
      MPI_Recv (*storage, local_rows * *n, dtype, p-1,
         DATA_MSG, comm, &status);
}

在我的代码开头,我这样调用 "read_row_striped_matrix":

#include <stdio.h>
#include <mpi.h>
#include "MyMPI.h"

typedef int dtype;
#define MPI_TYPE MPI_INT

int main(int argc, char *argv[]) {

  dtype** matrix;         /* Doubly-subscripted array */
  dtype*  storage;        /* Local portion of array elements */
  int     proc_id;        /* Process Rank */
  int     row_count;      /* Number of rows in matrix */
  int     col_count;      /* Number of columns in matrix */
  int     proc_count;     /* Number of processes */ 
  int     i;              /* Used with for loop */

  MPI_Init                (&argc, &argv);
  MPI_Comm_rank           (MPI_COMM_WORLD, &proc_id);
  MPI_Comm_size           (MPI_COMM_WORLD, &proc_count); 


  read_row_striped_matrix (argv[3], (void *) &matrix, (void *) &storage, MPI_TYPE, 
    &row_count, &col_count, MPI_COMM_WORLD);

  ....

问题是,我的实现陷入了无限循环。因此,我开始通过测试进行调试,以查看是否正确地从文本文件中读取了数据。我的名为 "file_input.txt" 的文本文件包含以下输入,其中第一个数字 (5) 代表行数,第二个数字(也是 5)代表列数,其余数据为值在矩阵中:

5 5 0 0 1 0 1 0 0 1 ...

我在从文本文件中读取长度和高度的库代码部分插入了以下 printf 语句:

if (id == (p-1))    
    printf("The name of the file is %s\n", s);
    infileptr = fopen (s, "r");
    if (infileptr == NULL) *m = 0;
    else {
        printf("The value of m is %d\n", *m);

        size_t ret_val      = fread (m, sizeof(int), 1, infileptr);
        size_t next_ret_val = fread (n, sizeof(int), 1, infileptr);

        printf("The total # of elements successfully read is: %d\n", ret_val);
        printf("The total # of elements successfully read is: %d\n", next_ret_val);

        printf("The value of m is %d\n", *m);
        printf("The value of n is %d\n", *n);
   }
}

对于执行"project_3 5 5 file_input.txt",程序的输出是:

The name of the file is: file_input.txt
The value of m is 0
The total number of elements successfully read is: 1
The total number of elements successfully read is: 1
The value of m is: 540549176
The value of n is: 540090416
...

据我观察,文件名被正确读入,调用fread前m(0)的值是正确的。 fread 正在读取 m 和 n 的正确元素数量,但值为“540549176”和“540090416”而不是 5 和 5。当我尝试将文本文件开头的数字更改为“3和 4" 为例,m 和 n 的值不变。

有人知道为什么前两个整数没有从文本文件中正确读入吗?提前致谢。

看来您需要剥离库函数并重写它以读取和解释文本文件。目前它读取二进制数据,当它读入 m 和 n 时,它读取 sizeof(int) (可能是 4 个字节)的十六进制数据,即 00,00,00,05 是您文件中预期的内容,但前 4 个字节是像 76,32,76,32 这样的东西 与其重写库函数,编写一个文件编译器可能更有意义,该编译器读取文本文件作为输入并将其写入二进制数据

这里有两个选择:

  • 此程序需要二进制输入。所以你需要以某种方式产生二进制输入。 “5”是一个十六进制值为 0x35(十进制 53)的 ascii 字符。当您害怕 sizeof(int) 时,您实际上将输入 2 个字符。

  • 您可以编辑程序来解析 ascii 文本,但这有点烦人。首先,您读入文件的一行,然后对其进行标记化,然后将每个标记转换为整数。您来自 perl/python 背景吗?这种文本转换在脚本语言中几乎是自动的。 C

  • 中没有什么是自动的