I'm trying to broadcast the pointer to x
to the other processors. It's declared as a global variable along with err
and num
. But after broadcasting, the contents of the array are incorrect for all processors except rank 0
while err
and num
are read correctly.
I've added comments in caps to point out what I'm referring to in the above. The bottom print loop gives the correct values of err
and num
after broadcasting regardless of rank but the contents of x
is only correct if the rank is 0
. Anyone know why?
Note: I'm not that familiar with C but the elements of the array seem to have been populated correctly as process 0 is able to access the array and read the correct elements from it.
Sample input...
3
5.05
2 3 4
Output if I set rank to 0...
3
0.010000
0 2.000000
1 3.000000
2 4.000000
Output if I set rank to 1...
3
0.010000
0 1209010463764815312582472227618816.000000
1 18988728894356880079764586496.000000
2 78657026497739634549916490384015360.000000
and code..
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
float err;
float *x; // DECLARING X HERE
int num;
void get_input();
void get_input(FILE* fp, int process_rank, int comm_size)
{
if(process_rank == 0)
{
x = (float *)malloc(num * sizeof(float));
for(int i = 0; i < num; i++) {
fscanf(fp, "%f", &x[i]);
}
// BROADCASTING X
MPI_Bcast(&x, num, MPI_FLOAT, 0, MPI_COMM_WORLD);
fclose(fp);
}
}
int main(int argc, char *argv[])
{
FILE* fp;
fp = fopen(argv[1], "r");
if (!fp)
{
printf("Cannot open file %s\n", argv[1]);
exit(1);
}
fscanf(fp, "%d ", &num);
fscanf(fp, "%f ", &err);
int comm_size;
int process_rank;
x = (float *)malloc(num * sizeof(float)); /* The unknowns */
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
MPI_Bcast(&num, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&err, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
get_input(fp, process_rank, comm_size);
// CHECKING X
if(process_rank == 1) {
printf("%d\n", num);
printf("%f\n", err);
for(int i = 0; i < num; i++) {
printf("%d %f\n", i, x[i]);
}
}
MPI_Finalize();
return(0);
}