I'm trying to use MPI to broadcast an array to nodes 1 and 2 from node 0. The array has values in it, however, I cannot seem to successfully broadcast the array despite trying a few different things based on suggestions found here and elsewhere on the net. When I run this, which asks for a file name within the same directory we're running the code from (that file is guaranteed to contain only integers, 1-per-line) I end up with a "Fatal error: glibc detected an invalid stdio handle", and it happens around my MPI_Bcast(M, N, MPI_INT, 0, MPI_COMM_WORLD) line, but I can't seem to pinpoint or correct the problem.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <mpi.h> int fuelCalc(int array[], int elementCount, int myrank, int worldsize){ int fuelSum = 0; int addlFuelReq = 0; printf("Rank %d has array[0] value %d\n", myrank, array[0]); for(int i = 0; i < elementCount; i++){ if( (i % worldsize) == myrank){ usleep(1000000); addlFuelReq = (array[i]/4) - 3; if(addlFuelReq < 1){addlFuelReq = 1;} fuelSum += addlFuelReq; } } return fuelSum; } int main(){ int i = 0, N = 0; char fuelFile[30]; char comp; int totalFuel; int myrank, worldsize; FILE *file; int mysum; MPI_Init(NULL, NULL); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm_size(MPI_COMM_WORLD, &worldsize); if(myrank == 0){ printf("What file should be used?\n"); scanf("%s", fuelFile); file = fopen(fuelFile, "r"); if(file == NULL){ printf("The file entered does not exist.\n"); return 0; } for(comp = getc(file); comp!=EOF;comp=getc(file)){ if(comp == '\n'){ N = N+1; } } printf("# of Lines: %d\n", N); } MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD); printf("rank %d has N value %d\n", myrank, N); int M[N]; fseek(file, 0, SEEK_SET); for( i=0; i<N; i++){ fscanf(file, "%d\n", &M[i]); } if(myrank==0){printf("M[0] = %d\n", M[0]);} if(myrank==0){printf("Successfully scanned file in to M\n");} MPI_Bcast(M, N, MPI_INT, 0, MPI_COMM_WORLD); printf("Rank %d has M[0] of %d\n", myrank, M[0]); mysum = fuelCalc(M, N, myrank, worldsize); if(myrank==0){printf("Successfully sent M, N, myrank, worldsize to other nodes\n");} MPI_Reduce(&mysum, &totalFuel,1,MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if(myrank==0){printf("Successfully reduced stuff\n");} if(myrank == 0){ totalFuel = (totalFuel+((totalFuel/100)*10)); printf("Total fuel required: %d\n", totalFuel); } MPI_Finalize(); }
mpiruncommand line and any additional values and/or files you are using.