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

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

EP update part 2

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