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

标签 c linux file io mpi

对于我的并行计算类(class),我正在研究一个使用 MPI 并行化生命游戏的项目。我正在具体执行 Michael J. Quinn 的“使用 MPI 和 OpenMP 在 C 中进行并行编程”中的练习 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 的值不变。

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

最佳答案

这里有两个选择:

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

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

关于c - 文件读取问题 - 平行版生命游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28890559/

相关文章:

c - 是否有任何 C API 可以从 Linux 中的完整路径中提取基本文件名?

java - 从 Java 应用程序打印到 TSC 打印机

python - os.path.getsize 返回不正确的值?

c++ - 这个静态变量赋值给局部函数中的指针有什么问题?

c - C 中的递归函数(2 个子数组)

linux - python 3 没有名为 _tkinter 的模块

c - 如何在 C 中读取名称中包含空格的文件

c# - 寻找在 C# 中快速读取和搜索文件的方法

将结构数组的索引与 NULL 进行比较,而不是评估为 true;

c - 将两个数组附加到一个二进制文件中