简体   繁体   中英

mpicc and matrix

I have a code

distance = (double**)malloc(city_count*sizeof(double*));
for(i=0; i<city_count; i++) 
{
distance[i] = (double*)malloc(city_count*sizeof(double));
}

for(i=0; i<city_count; i++)
{
  for(j=0; j<city_count; j++) 
      {
         distance[i][j] = 1; // fscanf(fp, "%d", &tmp); distance[i][j] = tmp; EDITED

  }
}

Then I debug it in Visual Studio it works just fine. But on a real cluster it always filled with zeros. Can anybody help me?

Problem with assignment to matrix, not in reading files.

But on a real cluster it always filled with zeros.

for(i=0; i<city_count; i++)
{
    for(j=0; j<city_count; j++) 
    {
        fscanf(fp, "%d", &tmp);

Update

With your simplified problem statement (thanks for that) it pretty much leaves checking the pointer values returned by malloc to make sure they're not NULL (which would be very surprising unless city_count is really large.)

Old Update

Doh! I just noticed. %d reads an int which doesn't match the type of &tmp. Should be:

        fscanf(fp, "%lf", &tmp);

fscanf isn't typesafe so this type of mismatch usually causes a problem.

Post Facto Edit

And make sure you're not making the same mistake passing "%d" to printf . ;-)

End Update

Make sure city_count isn't 0 .

Check fscanf 's return value (and the value of tmp). Are you sure this file read is succeeding? From the docs at cplusplus.com

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM