source: XIOS/dev/branch_openmp/extern/ep_dev/ep_free.cpp @ 1515

Last change on this file since 1515 was 1515, checked in by yushan, 6 years ago

save dev

File size: 4.3 KB
RevLine 
[1381]1#include "ep_lib.hpp"
2#include <mpi.h>
3#include "ep_declaration.hpp"
4#include "ep_mpi.hpp"
5
6namespace ep_lib
7{
8
9  int MPI_Comm_free(MPI_Comm *comm)
10  {
[1500]11    if(! (*comm)->is_ep)
[1381]12    {
[1500]13      if((*comm)->mpi_comm != static_cast< ::MPI_Comm*>(MPI_COMM_NULL->mpi_comm))
[1381]14      {
[1500]15        ::MPI_Comm mpi_comm = to_mpi_comm((*comm)->mpi_comm);
[1381]16
17        ::MPI_Comm_free(&mpi_comm);
18        Debug("comm is MPI, freed\n");
19      }
20      return 0;
21    }
22
[1500]23    else if((*comm)->is_intercomm)
[1381]24    {
25      return MPI_Comm_free_intercomm(comm);
26    }
27
28    else
29    {
30      int ep_rank_loc, num_ep;
31
[1500]32      ep_rank_loc = (*comm)->ep_comm_ptr->size_rank_info[1].first;
33      num_ep = (*comm)->ep_comm_ptr->size_rank_info[1].second;
[1381]34
35      MPI_Barrier(*comm);
36
37      if(ep_rank_loc == 0)
38      {
39        Debug("comm is EP, mpi_comm_ptr != NULL\n");
40
[1500]41        delete (*comm)->my_buffer;
[1381]42
43
[1500]44        if((*comm)->ep_barrier != NULL)
[1381]45        {
[1500]46          (*comm)->ep_barrier->~ep_barrier();
[1381]47          Debug("ep_barrier freed\n");
48        }
49
50
[1503]51        if( ! (*comm)->ep_rank_map->empty() )
[1381]52        {
[1503]53          (*comm)->ep_rank_map->clear();
54          delete (*comm)->ep_rank_map;
55          Debug("ep_rank_map emptied and freed\n");
[1381]56        }
57
58        for(int i=0; i<num_ep; i++)
59        {
[1500]60          (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr->message_queue->clear();
61          delete (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr->message_queue;
[1381]62          Debug("message queue freed\n");
63         
64
[1500]65          if((*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr != NULL)
[1381]66          {
[1500]67            delete (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr;
[1381]68            Debug("ep_comm_ptr freed\n");
69          }
70        }
71
[1500]72        if( to_mpi_comm((*comm)->mpi_comm) != *static_cast< ::MPI_Comm*>(MPI_COMM_NULL->mpi_comm) 
73          && to_mpi_comm((*comm)->mpi_comm) != *static_cast< ::MPI_Comm*>(MPI_COMM_WORLD->mpi_comm))
[1381]74        {
[1500]75          ::MPI_Comm mpi_comm = to_mpi_comm((*comm)->mpi_comm);
[1381]76          ::MPI_Comm_free(&mpi_comm);
77          Debug("mpi_comm freed\n");
78        }
79
[1500]80       //if(comm != NULL) {delete[] comm->ep_comm_ptr->comm_list; Debug("comm freed\n");}
[1381]81
82      }
83
84      return 0;
85    }
86
87
88
89  }
90
91  int MPI_Comm_free_intercomm(MPI_Comm *comm)
92  {
93    int ep_rank_loc, num_ep;
[1515]94    //MPI_Barrier(*comm);
[1381]95
[1515]96    return 0;
97
[1500]98    ep_rank_loc = (*comm)->ep_comm_ptr->size_rank_info[1].first;
99    num_ep = (*comm)->ep_comm_ptr->size_rank_info[1].second;
[1381]100
[1515]101
102
[1381]103    //MPI_Barrier(*comm);
104
105    if(ep_rank_loc == 0)
106    {
107      Debug("comm is EP, mpi_comm_ptr != NULL\n");
108
109
[1500]110      if((*comm)->ep_barrier != NULL)
[1381]111      {
[1500]112        (*comm)->ep_barrier->~ep_barrier();
[1381]113        Debug("ep_barrier freed\n");
114      }
115
116
[1503]117      if( ! (*comm)->ep_rank_map->empty() )
[1381]118      {
[1503]119        (*comm)->ep_rank_map->clear();
120        delete (*comm)->ep_rank_map;
121        Debug("ep_rank_map emptied and freed\n");
[1381]122      }
123
124      for(int i=0; i<num_ep; i++)
125      {
[1500]126        (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr->message_queue->clear();
[1381]127        Debug("message queue freed\n");
128
129        #pragma omp critical (memory_free)
[1500]130        if((*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr->intercomm != NULL)
[1381]131        {
[1500]132          (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr->intercomm->local_rank_map->clear();
133          (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr->intercomm->remote_rank_map->clear();
134          (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr->intercomm->intercomm_rank_map->clear();
[1381]135          Debug("intercomm local/remote/intercomm_rank_map emptied\n");
136        } 
137
[1500]138        if((*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr != NULL)
[1381]139        {
[1500]140          delete (*comm)->ep_comm_ptr->comm_list[i]->ep_comm_ptr;
[1381]141          Debug("ep_comm_ptr freed\n");
142        }
143      }
144
[1500]145      if((*comm)->mpi_comm != static_cast< ::MPI_Comm*>(MPI_COMM_NULL->mpi_comm))
[1381]146      {
[1500]147        ::MPI_Comm mpi_comm = to_mpi_comm((*comm)->mpi_comm);
[1381]148        ::MPI_Comm_free(&mpi_comm);
149        Debug("mpi_comm freed\n");
150      }
151
[1500]152      if((*comm)->ep_comm_ptr->intercomm->mpi_inter_comm != static_cast< ::MPI_Comm*>(MPI_COMM_NULL->mpi_comm))
[1381]153      {
[1500]154        ::MPI_Comm mpi_comm = to_mpi_comm((*comm)->ep_comm_ptr->comm_list[0]->ep_comm_ptr->intercomm->mpi_inter_comm);
[1381]155        ::MPI_Comm_free(&mpi_comm);
156        Debug("mpi_intercomm freed\n");
157      }
158
[1500]159     if(comm != NULL) {delete[] (*comm)->ep_comm_ptr->comm_list; Debug("comm freed\n");}
[1381]160
161    }
162
163   
164   
165    return 0;
166  }
167
168
169
170}
171
172
Note: See TracBrowser for help on using the repository browser.