简体   繁体   English

具有MPI的C中的结构数组

[英]Array of Structures in C with MPI

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,并且我希望结构数组具有此信息(处理器名称和处理器的计算时间),这是我的代码的一部分:

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) !!! 如果将其打印在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) 如果我打印if (process_id > 0)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? 实际上,我不知道如何将结构与MPI使用,谁能给我建议我如何生成具有处理器名称和时间的结构数组? Thank you in advance for your time. 预先感谢您的宝贵时间。

At this line: 在这一行:

processor_name[MPI_MAX_PROCESSOR_NAME];

you start using the array variable processor_name without defining it anywhere. 您开始使用数组变量processor_name而无需在任何地方定义它。

You're missing something like all_info[i]. 您缺少诸如all_info[i].类的东西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. 然后,要存储字符串,您的processor_name需要内存。 A single char is just one byte (ie one letter). 一个char只是一个字节(即一个字母)。 So let's assume these names are never longer than 255, you'd get: 因此,假设这些名称永远不会超过255,您将获得:

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. 您的代码中有太多基本问题,您的问题似乎表明您对C编程缺乏基本的了解。 Therefore my advice would be to take more time studying this language. 因此,我的建议是花更多时间学习这种语言。

The error occurs here because you have not defined any type processor name . 由于未定义任何类型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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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