【问题标题】:Passing argument without a cast in MPI在 MPI 中传递参数而不进行强制转换
【发布时间】: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();
}

idp 不是指针,所以我认为我需要在对 MPI_Comm_sizeMPI_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_vectorprint_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


【解决方案1】:

您的警告是因为您正在调用该函数

void print_replicated_vector (void *, MPI_Datatype, int, MPI_Comm);

带有int类型的第一个参数:

print_replicated_vector (id, p, x, MPI_DOUBLE, n); // here

C 代码有时会在int 中存储一个指针,这就是编译器假设您想要做的并且它正在执行适当的类型转换(但会警告您)。但是要使代码正确,您必须使类型匹配。 IE。使用 &amp;id 或任何适当的参数传递指向 id 的指针(我不知道 print_replicated_vector 做什么或你想要它做什么)。

【讨论】:

  • @Adam 哦,我明白了。我将进行更改,看看是否仍然出现错误。谢谢
  • 现在我再看一遍,你将 5 个参数传递给一个只需要 4 个参数的函数......而且你的命令似乎是错误的。 MPI_DOUBLE 是一个 MPI_Datatype,所以它应该是第二个参数,而不是第四个。最后一个是沟通者,可能是 MPI_COMM_WORLD 除非你创建其他人(我不认为你这样做)。
  • @Adam 我也意识到 print_replicated_verctor 只需要 4 个参数。再说一次,这直接来自书中,所以我不想改变任何假设它应该运行的东西,但我想不会。
  • @Adam 有什么推荐吗??哪个参数是多余的?对不起,这是一个奇怪的问题,但线索会有所帮助..
  • @Adam btw 这里是在线完整程序的类似代码。唯一的区别是我不包括的 OpenMP 编译指示。 read_replicated_vector 和 print_replicated_vector 也丢失了,但我在我的问题中添加了这里。 fac-staff.seattleu.edu/quinnm/web/education/ParallelProgramming/…
猜你喜欢
  • 2011-07-05
  • 2019-02-18
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多