2

I am having a relatively a problem,I have defined struct and I want the array of structure has this information (processor name and the computation time for the processor) this is part of my code :

struct stru
{
   double  arr_time[50];
   char pname[50];   
};
int main (int argc, char *argv[])
{

struct stru all_info[50];

   MPI_Status status;
   MPI_Init(&argc,&argv);
   MPI_Comm_rank(MPI_COMM_WORLD,&process_id);
   MPI_Comm_size(MPI_COMM_WORLD,&num_of_processes);

    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);
if (process_id == 0)
{  //do somthing
}
if (process_id > 0)
{  
    double start = MPI_Wtime();
    for (k=0; k<array_size; k++)
      for (i=0; i<rows; i++)
      {
          c[i][k]=0.0;                     
          for (j=0; j<array_size; j++)
           c[i][k] = c[i][k] + a[i][j] * b[j][k];
      }      
      end_time = MPI_Wtime() - start;  
        all_info[i].arr_time[i] = end_time;
      for (int i=1 ;i <= numworkers ;i++)
            strcpy( all_info[i].pname, processor_name);

         printf(" time  = %f  for processor %s 
         \n",all_info[i].arr_time, all_info[i].pname);

}


      MPI_Gather( &end_time, 1, MPI_DOUBLE, &all_info[i].arr_time, 1, 
       MPI_DOUBLE, 0, MPI_COMM_WORLD);

if (process_id == 0){

      for(i = 1; i <= numworkers; i++ )
      {
         printf("  time   %f  for processor %s 
         \n",all_info[i].arr_time , all_info[i].pname);
 }     }

I have no result if I print it in if (process_id == 0) !!! the out put is

 time   0.000000  for processor  
 time   0.000000  for processor  
 time   0.000000  for processor 

and just the time printed if I printting in if (process_id > 0)

In fact I don't know how can I use Structure with MPI can anyone give me advice how can I generate array of structure that has processor name and his time? Thank you in advance for your time.

9
  • 2
    Please indent & format your code properly, it's hard to read now. Commented Dec 23, 2017 at 19:06
  • this is part of my code I'm just asking how can I putting (end_time and processor_name ) in array of structure ? Commented Dec 23, 2017 at 19:11
  • 1
    It's a matter of being nice and formatting code well prevents mistakes. Commented Dec 23, 2017 at 19:20
  • Meanwhile, please make the effort to post a correctly indented code that compiles. Commented Dec 24, 2017 at 2:20
  • @Gilles Gouaillardet , all the idea I want the array of structure must has the name and time for each processor that work in parallel (each processor has time and name) Commented Dec 24, 2017 at 11:42

2 Answers 2

3

At this line:

processor_name[MPI_MAX_PROCESSOR_NAME];

you start using the array variable processor_name without defining it anywhere.

You're missing something like all_info[i]. in front of it. Like you have a bit lower:

all_info[i].processor_name;

Then, for storing a string your processor_name needs memory. A single char is just one byte (i.e. one letter). So let's assume these names are never longer than 255, you'd get:

struct stru
{
   double end_time;
   char   processor_name[256];   
};

There are so many basic things wrong in your code and your questions seem to indicate that you lack basic understanding of C programming. Therefore my advice would be to take more time studying this language.

Sign up to request clarification or add additional context in comments.

4 Comments

I defined it in structure, but I don't know if this true or no ?
I do your update and this error I have ( error: ‘processor_name’ undeclared (first use in this function) processor_name[MPI_MAX_PROCESSOR_NAME]; )
I update my code and I got rid of the mistake , but I have no result when printing the result ... thank you and Merry Christmas! :)
for this hint "char processor_name[256]; " plus one
1

The error occurs here because you have not defined any type processor name. If I understand what you're trying to correctly, it seems like you were trying to access the attribute of the structures. For doing that, you might need to use the . operator. For that you might need to define an array

struct stru all_info[MPI_MAX_PROCESSOR_NAME];

instead of

struct stru all_info[50];

3 Comments

in this case I reache just for the processor name ?? is that correct ??
I want all info array must contain the processor name and the time that I compute
I update my code and I got rid of the mistake , but I have no result when printing the result .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.