Ignore:
Timestamp:
05/28/18 23:25:35 (6 years ago)
Author:
yushan
Message:

MPI_split can deal with discontinuous ranking within a process

Location:
XIOS/dev/branch_openmp/extern/ep_dev
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • XIOS/dev/branch_openmp/extern/ep_dev/ep_allgather.cpp

    r1503 r1504  
    1616namespace ep_lib 
    1717{ 
     18 
     19  int MPI_Allgather_local(const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, MPI_Comm comm) 
     20  { 
     21    assert(valid_type(datatype)); 
     22 
     23    ::MPI_Aint datasize, lb; 
     24    ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize); 
     25 
     26    int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first; 
     27    int num_ep = comm->ep_comm_ptr->size_rank_info[1].second; 
     28 
     29    #pragma omp critical (write_buffer) 
     30    comm->my_buffer->void_buffer[ep_rank_loc] = const_cast< void* >(sendbuf); 
     31 
     32    MPI_Barrier_local(comm); 
     33 
     34    #pragma omp critical (read_buffer) 
     35    { 
     36      for(int i=0; i<num_ep; i++) 
     37        memcpy(recvbuf + datasize * i * count, comm->my_buffer->void_buffer[i], datasize * count); 
     38    } 
     39  } 
    1840 
    1941  int MPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) 
  • XIOS/dev/branch_openmp/extern/ep_dev/ep_create.cpp

    r1503 r1504  
    6464    out_comm_hdls[0]->my_buffer = new BUFFER; 
    6565 
    66     //out_comm_hdls[0]->rank_map = new RANK_MAP; 
    67     //out_comm_hdls[0]->rank_map->resize(ep_size); 
    68  
    6966    out_comm_hdls[0]->ep_rank_map = new EP_RANK_MAP; 
    70     //out_comm_hdls[0]->ep_rank_map->resize(ep_size); 
    7167 
    7268 
     
    7571      out_comm_hdls[i]->ep_barrier = out_comm_hdls[0]->ep_barrier; 
    7672      out_comm_hdls[i]->my_buffer  = out_comm_hdls[0]->my_buffer; 
    77       //out_comm_hdls[i]->rank_map   = out_comm_hdls[0]->rank_map; 
    7873      out_comm_hdls[i]->ep_rank_map= out_comm_hdls[0]->ep_rank_map; 
    7974    } 
  • XIOS/dev/branch_openmp/extern/ep_dev/ep_lib_local.hpp

    r1381 r1504  
    1818   
    1919  int MPI_Gather_local      (const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, int local_root, MPI_Comm comm); 
     20   
     21  int MPI_Allgather_local      (const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, MPI_Comm comm); 
    2022 
    2123  int MPI_Gatherv_local       (const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, 
  • XIOS/dev/branch_openmp/extern/ep_dev/ep_split.cpp

    r1500 r1504  
    3030    } 
    3131    inout_vector->swap(out_vec); 
     32  } 
     33   
     34  void vec_simplify(std::vector<int> *in_vector, std::vector<int> *out_vector) 
     35  { 
     36    int found=false; 
     37    for(std::vector<int>::iterator it_in = in_vector->begin() ; it_in != in_vector->end(); ++it_in) 
     38    { 
     39      for(std::vector<int>::iterator it = out_vector->begin() ; it != out_vector->end(); ++it) 
     40      { 
     41        if(*it_in == *it) 
     42        { 
     43          found=true; 
     44          break; 
     45        } 
     46        else found=false; 
     47      } 
     48      if(found == false) 
     49      { 
     50        out_vector->push_back(*it_in); 
     51      } 
     52    } 
    3253  } 
    3354 
     
    5273    vector<int> matched_number; 
    5374    vector<int> matched_number_loc; 
    54  
    55  
     75     
    5676    vector<int> all_color(ep_size); 
    5777    vector<int> all_color_loc(num_ep); 
    5878 
    59     MPI_Gather_local(&color, 1, MPI_INT, all_color_loc.data(), 0, comm); 
    60     MPI_Bcast_local(all_color_loc.data(), num_ep, MPI_INT, 0, comm); 
    61  
    62  
    6379    MPI_Allgather(&color, 1, MPI_INT, all_color.data(), 1, MPI_INT, comm); 
     80    MPI_Allgather_local(&color, 1, MPI_INT, all_color_loc.data(), comm); 
    6481 
    6582    list<int> color_list(all_color.begin(), all_color.end()); 
    6683    list<int> color_list_loc(all_color_loc.begin(), all_color_loc.end()); 
    6784 
    68     vec_simplify(&all_color); 
    69  
    70     matched_number.resize(all_color.size(), 0); 
    71     matched_number_loc.resize(all_color.size(), 0); 
     85    vector<int> all_color_simplified; 
     86    vec_simplify(&all_color, &all_color_simplified); 
     87    int number_of_color; 
     88    for(int i=0; i<all_color_simplified.size(); i++) 
     89    { 
     90      if(color == all_color_simplified[i]) 
     91      { 
     92        number_of_color = i; 
     93        break; 
     94      } 
     95    } 
     96 
     97    matched_number.resize(all_color_simplified.size(), 0); 
     98    matched_number_loc.resize(all_color_simplified.size(), 0); 
    7299 
    73100 
     
    93120      num_color++; 
    94121    } 
    95  
    96  
     122         
     123 
     124    vector<int> all_key(ep_size); 
     125    vector<int> all_key_loc(num_ep); 
     126     
     127    vector<int> colored_key[num_color]; 
    97128    vector<int> colored_key_loc[num_color]; 
    98     vector<int> key_loc(num_ep); 
    99  
    100     MPI_Gather_local(&key, 1, MPI_INT, key_loc.data(), 0, comm); 
    101     MPI_Bcast_local(key_loc.data(), num_ep, MPI_INT, 0, comm); 
    102      
    103     std::sort(key_loc.begin(), key_loc.end()); 
     129     
     130 
     131    MPI_Allgather(&key, 1, MPI_INT, all_key.data(),1, MPI_INT, comm); 
     132    MPI_Allgather_local(&key, 1, MPI_INT, all_key_loc.data(), comm); 
    104133 
    105134    for(int i=0; i<num_ep; i++) 
     
    107136      for(int j = 0; j<num_color; j++) 
    108137      { 
    109         if(all_color_loc[i] == all_color[j]) 
    110         { 
    111           colored_key_loc[j].push_back(key_loc[i]); 
    112         } 
    113       } 
    114     } 
    115  
     138        if(all_color_loc[i] == all_color_simplified[j]) 
     139        { 
     140          colored_key_loc[j].push_back(all_key_loc[i]); 
     141        } 
     142      } 
     143    } 
     144     
     145    for(int i=0; i<ep_size; i++) 
     146    { 
     147      for(int j = 0; j<num_color; j++) 
     148      { 
     149        if(all_color[i] == all_color_simplified[j]) 
     150        { 
     151          colored_key[j].push_back(all_key[i]); 
     152        } 
     153      } 
     154    } 
     155     
     156    for(int i=0; i<num_color; i++) 
     157    { 
     158      std::sort(colored_key[i].begin(), colored_key[i].end()); 
     159      std::sort(colored_key_loc[i].begin(), colored_key_loc[i].end()); 
     160    } 
     161 
     162    int new_ep_rank; 
     163     
     164    for(int i=0; i<colored_key[number_of_color].size(); i++) 
     165    { 
     166      if(key == colored_key[number_of_color][i]) 
     167      { 
     168        new_ep_rank = i; 
     169        break; 
     170      } 
     171    } 
     172     
    116173    int new_ep_rank_loc; 
    117  
    118     for(int i=0; i<num_color; i++) 
    119     { 
    120       if(colored_key_loc[i].size()>1) 
    121       { 
    122         std::sort(colored_key_loc[i].begin(), colored_key_loc[i].end()); 
    123       } 
    124       if(color == all_color[i]) 
    125       { 
    126         for(int j=0; j<colored_key_loc[i].size(); j++) 
    127         { 
    128           if(key == colored_key_loc[i][j]) 
    129           { 
    130             new_ep_rank_loc = j; 
    131             break; 
    132           } 
    133         } 
    134       } 
    135     } 
     174     
     175    for(int i=0; i<colored_key_loc[number_of_color].size(); i++) 
     176    { 
     177      if(key == colored_key_loc[number_of_color][i]) 
     178      { 
     179        new_ep_rank_loc = i; 
     180        break; 
     181      } 
     182    } 
     183     
    136184 
    137185    ::MPI_Comm **split_mpi_comm; 
     
    151199        comm->ep_comm_ptr->comm_list[0]->mpi_bridge = split_mpi_comm[j]; 
    152200      } 
     201       
    153202      MPI_Barrier_local(comm); 
     203       
    154204      int num_new_ep = 0; 
    155205 
    156       if(new_ep_rank_loc == 0 && color == all_color[j]) 
     206      if(new_ep_rank_loc == 0 && color == all_color_simplified[j]) 
    157207      { 
    158208        num_new_ep = matched_number_loc[j]; 
     
    162212        MPI_Comm_create_endpoints(comm->ep_comm_ptr->comm_list[0]->mpi_bridge, num_new_ep, info, ep_comm); 
    163213 
    164  
    165214        comm->ep_comm_ptr->comm_list[0]->mem_bridge = ep_comm; 
    166       } 
     215         
     216        (*ep_comm)->ep_rank_map->clear(); 
     217         
     218        memcheck("in MPI_Split ep_rank="<< ep_rank <<" : *ep_comm = "<< *ep_comm); 
     219      } 
     220       
    167221      MPI_Barrier_local(comm); 
    168       if(color == all_color[j]) 
     222       
     223      if(color == all_color_simplified[j]) 
    169224      { 
    170225        *newcomm = comm->ep_comm_ptr->comm_list[0]->mem_bridge[new_ep_rank_loc]; 
     226        memcheck("in MPI_Split ep_rank="<< ep_rank <<" : *newcomm = "<< *newcomm); 
    171227 
    172228        (*newcomm)->ep_comm_ptr->comm_label = color; 
    173       } 
    174     } 
    175  
    176  
    177  
    178  
     229         
     230        (*newcomm)->ep_comm_ptr->size_rank_info[0].first = new_ep_rank; 
     231        (*newcomm)->ep_comm_ptr->size_rank_info[1].first = new_ep_rank_loc; 
     232         
     233        int my_triple[3]; 
     234        vector<int> my_triple_vector; 
     235        vector<int> my_triple_vector_recv; 
     236        my_triple[0] = new_ep_rank; 
     237        my_triple[1] = new_ep_rank_loc; 
     238        my_triple[2] = (*newcomm)->ep_comm_ptr->size_rank_info[2].first; // new_mpi_rank 
     239         
     240        int new_ep_size = (*newcomm)->ep_comm_ptr->size_rank_info[0].second; 
     241        int new_num_ep  = (*newcomm)->ep_comm_ptr->size_rank_info[1].second; 
     242         
     243        int new_mpi_size = (*newcomm)->ep_comm_ptr->size_rank_info[2].second; 
     244         
     245        if(new_ep_rank_loc == 0) my_triple_vector.resize(3*new_ep_size); 
     246        if(new_ep_rank_loc == 0) my_triple_vector_recv.resize(3*new_ep_size); 
     247         
     248        MPI_Gather_local(my_triple, 3, MPI_INT, my_triple_vector.data(), 0, *newcomm); 
     249         
     250        if(new_ep_rank_loc == 0) 
     251        { 
     252          int *recvcounts = new int[new_mpi_size]; 
     253          int *displs = new int[new_mpi_size]; 
     254          int new_num_epx3 = new_num_ep * 3; 
     255          ::MPI_Allgather(&new_num_epx3, 1, to_mpi_type(MPI_INT), recvcounts, 1, to_mpi_type(MPI_INT), to_mpi_comm((*newcomm)->mpi_comm)); 
     256          displs[0]=0; 
     257          for(int i=1; i<new_mpi_size; i++) 
     258            displs[i] = displs[i-1] + recvcounts[i-1]; 
     259              
     260          ::MPI_Allgatherv(my_triple_vector.data(), 3*new_num_ep, to_mpi_type(MPI_INT), my_triple_vector_recv.data(), recvcounts, displs, to_mpi_type(MPI_INT), to_mpi_comm((*newcomm)->mpi_comm)); 
     261           
     262          for(int i=0; i<new_ep_size; i++) 
     263          { 
     264            (*newcomm)->ep_comm_ptr->comm_list[0]->ep_rank_map->insert(std::pair< int, std::pair<int,int> >(my_triple_vector_recv[3*i], my_triple_vector_recv[3*i+1], my_triple_vector_recv[3*i+2])); 
     265          } 
     266           
     267          (*newcomm)->ep_rank_map = (*newcomm)->ep_comm_ptr->comm_list[0]->ep_rank_map; 
     268           
     269          delete recvcounts; 
     270          delete displs; 
     271        }  
     272      } 
     273    } 
     274     
     275    /*for(int i=0; i<ep_size; i++) 
     276    { 
     277      MPI_Barrier(comm); 
     278      MPI_Barrier(comm); 
     279      if(ep_rank==i)  
     280      { 
     281        printf("ep_rank_map for endpoint %d = \n", ep_rank); 
     282        for(std::map<int, std::pair<int, int> > :: iterator it = (*newcomm)->ep_rank_map->begin(); it != (*newcomm)->ep_rank_map->end(); it++) 
     283        { 
     284          printf("\t\t\t %d %d %d\n", it->first, it->second.first, it->second.second); 
     285        } 
     286        printf("\n"); 
     287      } 
     288      MPI_Barrier(comm); 
     289      MPI_Barrier(comm); 
     290    }*/    
     291     
    179292    return 0; 
    180293  } 
  • XIOS/dev/branch_openmp/extern/ep_dev/main.cpp

    r1503 r1504  
    687687 
    688688    //int color = rank%2; 
    689     int color, remote_leader; 
    690     if(rank<size-2) {color = 1; remote_leader = size-2;} 
    691     else {color = 0; remote_leader = 0;} 
    692      
    693     printf("rank = %d, color = %d, remote_leader = %d\n", rank, color, remote_leader); 
    694    
    695     MPI_Comm sub_comm; 
    696     MPI_Comm_split(comm, color, rank, &sub_comm); 
    697  
    698  
    699      
    700     int sub_rank; 
    701     MPI_Comm_rank(sub_comm, &sub_rank); 
     689    int tab_color[16]={2,2,2,3,0,1,1,3,2,1,3,0,0,2,0,0}; 
     690    int tab_key[16]={3,11,10,5,6,8,15,7,2,1,9,13,4,14,12,0}; 
     691    int color = tab_color[rank]; 
     692    int key = tab_key[rank]; 
     693   
     694    MPI_Comm split_comm; 
     695    MPI_Comm_split(comm, color, key, &split_comm); 
     696 
     697 
     698     
     699    int split_rank; 
     700    MPI_Comm_rank(split_comm, &split_rank); 
     701     
     702    printf("rank = %d, color = %d, key = %d, split_rank = %d\n", rank, color, key, split_rank); 
    702703 
    703704 
     
    733734*/ 
    734735    MPI_Barrier(comm); 
    735     MPI_Comm_free(&sub_comm); 
     736    MPI_Comm_free(&split_comm); 
    736737 
    737738 
Note: See TracChangeset for help on using the changeset viewer.