source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_free.cpp @ 1379

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

fix small bug

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