代码之家  ›  专栏  ›  技术社区  ›  amischiefr

使用OpenMPI初始化一次数组

  •  1
  • amischiefr  · 技术社区  · 14 年前

    我试图通过将工作划分到多个节点(第二部分是矩阵)来使用OpenMPI处理数组中的数据来运行一些测试。我现在遇到了一些问题,因为数据数组每次都被初始化,我不知道如何防止这种情况发生。

    如何使用ansi c c创建一个可变长度数组,使用openmpi一次?我试着让它静态化和全局化,但什么也没有。

    #define NUM_THREADS 4
    #define NUM_DATA 1000
    
    static int *list = NULL;
    
    int main(int argc, char *argv[]) {
      int numprocs, rank, namelen;
      char processor_name[MPI_MAX_PROCESSOR_NAME];
      int n = NUM_DATA*NUM_DATA;
      printf("hi\n");
      int i;
      if(list == NULL)
      {
         printf("ho\n");
         list = malloc(n*sizeof(int));
    
        for(i = 0 ; i < n; i++)
        {
          list[i] = rand() % 1000;
        }
      }
    
      int position;
    
      MPI_Init(&argc, &argv);
      MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
      MPI_Comm_rank(MPI_COMM_WORLD, &rank);
      MPI_Get_processor_name(processor_name, &namelen);
      printf("Process %d on %s out of %d\n", rank,processor_name, numprocs);
    
      clock_t start = clock();
    
      position = n / NUM_THREADS * rank;
      search(list,position, n / NUM_THREADS * (rank + 1));
    
      printf("Time elapsed: %f seconds\n",  ((double)clock() - (double)start) /(double) CLOCKS_PER_SEC);
    
      free(list);
    
      MPI_Finalize();
      return 0;
    }
    
    1 回复  |  直到 9 年前
        1
  •  2
  •   Dusty    14 年前

    最简单的方法可能是让排名0的进程进行初始化,而其他进程阻塞。然后初始化完成后,让他们都开始工作。

    尝试调用搜索函数的一个基本示例(注意:它是干编码的):

    #define NUM_THREADS 4
    #define NUM_DATA 1000
    
    int main(int argc, char *argv[]) {
       int *list;
       int numprocs, rank, namelen, i, n;
       int chunksize,offset;
       char processor_name[MPI_MAX_PROCESSOR_NAME];
    
       n= NUM_DATA * NUM_DATA;
    
       MPI_Status stat;
       MPI_Init(&argc, &argv);
       MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
       MPI_Comm_rank(MPI_COMM_WORLD, &rank);
       MPI_Get_processor_name(processor_name, &namelen);
    
       //note you'll need to handle n%NUM_THREADS !=0, but i'm ignoring that for now
       chunksize = n / NUM_THREADS; 
    
       if (rank == 0) {
          //Think of this as a master process
          //Do your initialization in this process
          list = malloc(n*sizeof(int));
    
          for(i = 0 ; i < n; i++)
          {
             list[i] = rand() % 1000;
          }
    
          // Once you're ready, send each slave process a chunk to work on
          offset = chunksize;
          for(i = 1; i < numprocs; i++) {
             MPI_Send(&list[offset], chunksize, MPI_INT, i, 0, MPI_COMM_WORLD);
             offset += chunksize
          }
    
          search(list, 0, chunksize);
    
          //If you need some sort of response back from the slaves, do a recv loop here
       } else {
    
          // If you're not the master, you're a slave process, so wait to receive data
    
          list = malloc(chunksize*sizeof(int));  
          MPI_Recv(list, chunksize, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat);
    
          // Now you can do work on your portion
          search(list, 0, chunksize);
    
          //If you need to send something back to the master, do it here.
       }
    
       MPI_Finalize();
    }