对于我的并行计算课程,我正在开发一个使用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)表示cols的数量,其余数据是值在矩阵中:
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的值不会改变。
有没有人知道为什么没有正确地从文本文件中读入前两个整数?提前致谢。
答案 0 :(得分:1)
这里有两个选项:
这个程序期待二进制输入。所以你需要以某种方式产生二进制输入。 &#34; 5&#34;是一个十六进制值为0x35(十进制53)的ascii字符。当你发现sizeof(int)时,你实际上会输入2个字符。
你可以编辑程序来解析ascii文本,但这有点烦人。首先,您读入文件的一行,然后对其进行标记,然后将每个标记转换为整数。你来自perl / python背景吗?这种文本转换的东西在脚本语言中几乎是自动的。 C
答案 1 :(得分:0)
看看你需要关闭库函数并重写它以读取和解释文本文件。目前它读取二进制数据,当它读入m和n时,它读取sizeof(int)(可能是4个字节)的十六进制数据,即00,00,00,05是预期在你的文件中但是前4个字节类似于76,32,76,32 编写一个文件编译器来读取文本文件作为输入并将其写为二进制数据
,而不是重写库函数。