For my homework I have to test for several large matrices using this Conjugate Gradient Program with MPI (see code below). I copied the program from my book and it is supposed to compile but I get the errors:
In function ‘main’:
37:warning: passing argument 1 of read_replicated_vector makes pointer from integer without a cast
37: warning: passing argument 2 of read_replicated_vector makes pointer from integer without a cast
37: warning: passing argument 3 of read_replicated_vector makes integer from pointer without a cast
37: warning: passing argument 4 of read_replicated_vector from incompatible pointer type
37: error: void value not ignored as it ought to be
44: warning: passing argument 1 of print_replicated_vector makes pointer from integer without a cast
44: warning: passing argument 3 of print_replicated_vector makes integer from pointer without a cast
44: error: too many arguments to function 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 and p are not pointers, so I do think I need to pass them by reference in the calls to MPI_Comm_size and MPI_Comm_rank, though I tried doing that.
Edit
//Input Function
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);
}
// Output Function
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");
}
}
Your warnings are because you’re calling the function
with a first parameter of type
int:C code will sometimes store a pointer in an
int, and that’s what the compiler is assuming you want to do and it’s doing the appropriate type conversions (but warning you of them). But to make the code correct you’d have to make the types match up. I.e. pass a pointer to id with&idor whatever the appropriate argument is (I don’t know whatprint_replicated_vectordoes or what you want it to do).