How to speed up this problem by MPI

前端 未结 4 1199
时光取名叫无心
时光取名叫无心 2020-12-11 09:06

(1). I am wondering how I can speed up the time-consuming computation in the loop of my code below using MPI?

 int main(int argc, char ** argv)   
 {   
 //          


        
相关标签:
4条回答
  • 2020-12-11 09:48

    Quick edit (because I either can't figure out how to leave comments, or I'm not allowed to leave comments yet) -- 3lectrologos is incorrect about the parallel part of MPI programs. You cannot do serial work before MPI_Init and after MPI_Finalize and expect it to actually be serial -- it will still be executed by all MPI threads.

    I think part of the issue is that the "parallel part" of an MPI program is the entire program. MPI will start executing the same program (your main function) on each node you specify at approximately the same time. The MPI_Init call just sets certain things up for the program so it can use the MPI calls correctly.

    The correct "template" (in pseudo-code) for what I think you want to do would be:

    int main(int argc, char *argv[]) {
        MPI_Init(&argc, &argv);  
        MPI_Comm_size(MPI_COMM_WORLD,&numprocs);  
        MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    
        if (myid == 0) { // Do the serial part on a single MPI thread
            printf("Performing serial computation on cpu %d\n", myid);
            PreParallelWork();
        }
    
        ParallelWork();  // Every MPI thread will run the parallel work
    
        if (myid == 0) { // Do the final serial part on a single MPI thread
            printf("Performing the final serial computation on cpu %d\n", myid);
            PostParallelWork();
        }
    
        MPI_Finalize();  
        return 0;  
    }  
    
    0 讨论(0)
  • 2020-12-11 09:50

    The MPI_Init (with args of &argc and &argv. It is the requirement of MPI implementations) must be really the first executed statement of MAIN. And Finalize must be the very last executed statement.

    main() will be started on every node in MPI environment. Parameters like number of nodes, node_id, and master node address may be passed via argc and argv.

    It is framework:

    #include "mpi.h"  
    #include <stdio.h>  
    #include <string.h>  
    
    void f();
    
    int numprocs; int myid; 
    
    int main(int argc, char **argv)  
    {  
    
    MPI_Init(&argc, &argv);  
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);  
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);  
    
    if(myid == 0)  
    {  /* main process. user interaction is ONLY HERE */
    
        printf("%s\n", "Start running!");  
    
        MPI_Send ... requests with job
        /*may be call f in main too*/
        MPU_Reqv ... results..
        printf("%s\n", "End running!");  
    }
    else
    {
    
      /* Slaves. Do sit here and wait a job from main process */
      MPI_Recv(.input..);  
      /* dispatch input by parsing it 
        (if there can be different types of work)
        or just do the work */    
      f(..)
      MPI_Send(.results..);  
    }
    
    MPI_Finalize();  
    
    return 0;  
    }  
    
    0 讨论(0)
  • 2020-12-11 09:58

    The easiest migration to cluster form OpenMP can be "Cluster OpenMP" from intel.

    For MPI you need to completely rewrite dispatching of work.

    0 讨论(0)
  • 2020-12-11 10:03

    If all the values in the array are independent, then it should be trivially parallelizable. Split the array into chunks of roughly equal size, give each chunk to a node, and then compile the results back together.

    0 讨论(0)
提交回复
热议问题