【发布时间】:2011-05-01 20:19:31
【问题描述】:
对于我的家庭作业,我必须使用这个带有 MPI 的共轭梯度程序来测试几个大型矩阵(参见下面的代码)。我从我的书中复制了该程序,它应该可以编译但我得到了错误:
在函数'main'中:
37:警告:传递 read_replicated_vector 的参数 1 使指针从整数而不进行强制转换
37: 警告:传递 read_replicated_vector 的参数 2 使指针从整数而不进行强制转换
37: 警告:传递 read_replicated_vector 的参数 3 从指针生成整数而不进行强制转换
37:警告:从不兼容的指针类型传递 read_replicated_vector 的参数 4
37: 错误: void 值没有被忽略,因为它应该被忽略
44: 警告:传递 print_replicated_vector 的参数 1 使指针从整数而不进行强制转换
44: 警告:传递 print_replicated_vector 的参数 3 从指针生成整数而不进行强制转换
44: 错误:函数 print_replicated_vector 的参数过多
#include <stdlib.h>
#include <stdio.h>
#include "mpi.h"
#include "MyMPI.h"
main (int argc, char *argv[])
{
double **a; /* Solving Ax = b for x */
double *astorage; /* Holds elements of A */
double *b; /* Constant vector */
double *x; /* Solution vector */
int p; /* MPI Processes */
int id; /* Process rank */
int m; /* Rows in A */
int n; /* Columns in A */
int n1; /* Elements in b */
/* Initialize a and b so that solution is x[i] = i */
MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &p);
MPI_Comm_rank (MPI_COMM_WORLD, &id);
read_block_row_matrix (id, p, argv[1], (void *) &a,
(void *) &astorage, MPI_DOUBLE, &m, &n);
n1 = read_replicated_vector (id, p, argv[2], (void **) &b, MPI_DOUBLE);
if ((m != n) || (n != n1))
{
if (!id)
printf ("Incompatible dimensions (%d x %d) x (%d)\n", m, n, n1);
}
else {
x = (double *) malloc (n * sizeof(double));
cg (p, id, a, b, x, n);
print_replicated_vector (id, p, x, MPI_DOUBLE, n); // here
}
MPI_Finalize();
}
id 和 p 不是指针,所以我认为我需要在对 MPI_Comm_size 和 MPI_Comm_rank 的调用中通过引用传递它们,尽管我尝试这样做。
编辑
//输入函数
void read_replicated_vector (
char *s, /* IN - File name */
void **v, /* OUT - Vector */
MPI_Datatype dtype, /* IN - Vector type */
int *n, /* OUT - Vector length */
MPI_Comm comm) /* IN - Communicator */
{
int datum_size; /* Bytes per vector element */
int i;
int id; /* Process rank */
FILE *infileptr; /* Input file pointer */
int p; /* Number of processes */
MPI_Comm_rank (comm, &id);
MPI_Comm_size (comm, &p);
datum_size = get_size (dtype);
if (id == (p-1))
{
infileptr = fopen (s, "r");
if (infileptr == NULL) *n = 0;
else fread (n, sizeof(int), 1, infileptr);
}
MPI_Bcast (n, 1, MPI_INT, p-1, MPI_COMM_WORLD);
if (! *n) terminate (id, "Cannot open vector file");
*v = my_malloc (id, *n * datum_size);
if (id == (p-1))
{
fread (*v, datum_size, *n, infileptr);
fclose (infileptr);
}
MPI_Bcast (*v, *n, dtype, p-1, MPI_COMM_WORLD);
}
// 输出函数
void print_replicated_vector (
void *v, /* IN - Address of vector */
MPI_Datatype dtype, /* IN - Vector element type */
int n, /* IN - Elements in vector */
MPI_Comm comm) /* IN - Communicator */
{
int id; /* Process rank */
MPI_Comm_rank (comm, &id);
if (!id)
{
print_subvector (v, dtype, n);
printf ("\n\n");
}
}
【问题讨论】:
-
错误出现在
read_replicated_vector和print_replicated_vector的参数中;我们需要查看这两个函数的原型以了解问题所在。 -
@Jonathan Dursi 这些函数直接来自 MPI 标准库。
-
不,他们不是。 MPI 函数以
MPI_开头。我用谷歌搜索了你的东西,看起来你的作业包括一个声明这些的 MyMPI.h 文件。这是你的任务fac-staff.seattleu.edu/quinnm/web/education/ParallelProgramming/… 吗?void print_replicated_vector (void *, MPI_Datatype, int, MPI_Comm); -
@Adam 我也有 MPI.h 文件。我不应该在编译时包含它。现在,当我编译时,我使用 mpicc MyMPI.c -o cgradient cgradient.c。你是说我也应该包含头文件。该链接类似于我们在本书附录中的链接。当然,它们都是由作者“Micheal J. Quinn”编写的。
-
你需要 MPI.h。那是系统头文件。
mpicc的工作是确保 MPI 的包含/库路径是应有的(事实上,mpicc只是一个非常薄的包装器,它调用您的实际编译器,如gcc,添加的那些包括/库开关)。我想说的是,就问题而言,MyMPI.c/h 是用户代码。它们并非来自系统,只有您拥有该代码。
标签: mpi