| 1 | /* -*- Mode: C; c-basic-offset:4 ; -*- */ |
|---|
| 2 | /* |
|---|
| 3 | * (C) 2001 by Argonne National Laboratory. |
|---|
| 4 | * See COPYRIGHT in top-level directory. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #ifdef HAVE_UNISTD_H |
|---|
| 8 | #include <unistd.h> |
|---|
| 9 | #endif |
|---|
| 10 | #ifdef HAVE_SYS_PARAM_H |
|---|
| 11 | #include <sys/param.h> |
|---|
| 12 | #endif |
|---|
| 13 | |
|---|
| 14 | #include "mpid_nem_impl.h" |
|---|
| 15 | |
|---|
| 16 | void *MPIDI_CH3_packet_buffer = NULL; |
|---|
| 17 | int MPIDI_CH3I_my_rank = -1; |
|---|
| 18 | MPIDI_PG_t *MPIDI_CH3I_my_pg = NULL; |
|---|
| 19 | |
|---|
| 20 | static int nemesis_initialized = 0; |
|---|
| 21 | |
|---|
| 22 | /* MPIDI_CH3_Init(): Initialize the nemesis channel */ |
|---|
| 23 | #undef FUNCNAME |
|---|
| 24 | #define FUNCNAME MPIDI_CH3_Init |
|---|
| 25 | #undef FCNAME |
|---|
| 26 | #define FCNAME MPIDI_QUOTE(FUNCNAME) |
|---|
| 27 | int MPIDI_CH3_Init(int has_parent, MPIDI_PG_t *pg_p, int pg_rank) |
|---|
| 28 | { |
|---|
| 29 | int mpi_errno = MPI_SUCCESS; |
|---|
| 30 | int i; |
|---|
| 31 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_INIT); |
|---|
| 32 | |
|---|
| 33 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_INIT); |
|---|
| 34 | |
|---|
| 35 | MPIU_Assert(sizeof(MPIDI_CH3I_VC) <= sizeof(((MPIDI_VC_t*)0)->channel_private)); |
|---|
| 36 | |
|---|
| 37 | /* There are hard-coded copy routines that depend on the size of the mpich2 header |
|---|
| 38 | We only handle the 32- and 40-byte cases. |
|---|
| 39 | */ |
|---|
| 40 | MPIU_Assert (sizeof(MPIDI_CH3_Pkt_t) >= 32 && sizeof(MPIDI_CH3_Pkt_t) <= 40); |
|---|
| 41 | |
|---|
| 42 | mpi_errno = MPID_nem_init (pg_rank, pg_p, has_parent); |
|---|
| 43 | if (mpi_errno) MPIU_ERR_POP (mpi_errno); |
|---|
| 44 | |
|---|
| 45 | nemesis_initialized = 1; |
|---|
| 46 | |
|---|
| 47 | MPIDI_CH3I_my_rank = pg_rank; |
|---|
| 48 | MPIDI_CH3I_my_pg = pg_p; |
|---|
| 49 | |
|---|
| 50 | /* |
|---|
| 51 | * Initialize Progress Engine |
|---|
| 52 | */ |
|---|
| 53 | mpi_errno = MPIDI_CH3I_Progress_init(); |
|---|
| 54 | if (mpi_errno) MPIU_ERR_SETFATALANDJUMP (mpi_errno, MPI_ERR_OTHER, "**init_progress"); |
|---|
| 55 | |
|---|
| 56 | for (i = 0; i < pg_p->size; i++) |
|---|
| 57 | { |
|---|
| 58 | mpi_errno = MPIDI_CH3_VC_Init(&pg_p->vct[i]); |
|---|
| 59 | if (mpi_errno) MPIU_ERR_POP(mpi_errno); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | mpi_errno = MPID_nem_coll_barrier_init(); |
|---|
| 63 | if (mpi_errno) MPIU_ERR_POP (mpi_errno); |
|---|
| 64 | |
|---|
| 65 | fn_exit: |
|---|
| 66 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_INIT); |
|---|
| 67 | return mpi_errno; |
|---|
| 68 | fn_fail: |
|---|
| 69 | goto fn_exit; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /* This function simply tells the CH3 device to use the defaults for the |
|---|
| 73 | MPI Port functions */ |
|---|
| 74 | int MPIDI_CH3_PortFnsInit( MPIDI_PortFns *portFns ) |
|---|
| 75 | { |
|---|
| 76 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_PORTFNSINIT); |
|---|
| 77 | |
|---|
| 78 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_PORTFNSINIT); |
|---|
| 79 | |
|---|
| 80 | MPIU_UNREFERENCED_ARG(portFns); |
|---|
| 81 | |
|---|
| 82 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_PORTFNSINIT); |
|---|
| 83 | return 0; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /* This function simply tells the CH3 device to use the defaults for the |
|---|
| 87 | MPI-2 RMA functions */ |
|---|
| 88 | int MPIDI_CH3_RMAFnsInit( MPIDI_RMAFns *a ) |
|---|
| 89 | { |
|---|
| 90 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_RMAFNSINIT); |
|---|
| 91 | |
|---|
| 92 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_RMAFNSINIT); |
|---|
| 93 | |
|---|
| 94 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_RMAFNSINIT); |
|---|
| 95 | return 0; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /* Perform the channel-specific vc initialization */ |
|---|
| 99 | #undef FUNCNAME |
|---|
| 100 | #define FUNCNAME MPIDI_CH3_VC_Init |
|---|
| 101 | #undef FCNAME |
|---|
| 102 | #define FCNAME MPIDI_QUOTE(FUNCNAME) |
|---|
| 103 | int MPIDI_CH3_VC_Init( MPIDI_VC_t *vc ) |
|---|
| 104 | { |
|---|
| 105 | int mpi_errno = MPI_SUCCESS; |
|---|
| 106 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_VC_INIT); |
|---|
| 107 | |
|---|
| 108 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_VC_INIT); |
|---|
| 109 | |
|---|
| 110 | /* FIXME: Circular dependency. Before calling MPIDI_CH3_Init, |
|---|
| 111 | MPID_Init calls InitPG which calls MPIDI_PG_Create which calls |
|---|
| 112 | MPIDI_CH3_VC_Init. But MPIDI_CH3_VC_Init needs nemesis to be |
|---|
| 113 | initialized. We can't call MPIDI_CH3_Init before initializing |
|---|
| 114 | the PG, because it needs the PG. |
|---|
| 115 | |
|---|
| 116 | There is a hook called MPIDI_CH3_PG_Init which is called from |
|---|
| 117 | MPIDI_PG_Create before MPIDI_CH3_VC_Init, but we don't have |
|---|
| 118 | the pg_rank in that function. |
|---|
| 119 | |
|---|
| 120 | Maybe this shouldn't really be a FIXME, since I believe that |
|---|
| 121 | this issue will be moot once nemesis is a device. |
|---|
| 122 | |
|---|
| 123 | So what we do now, is do nothing if MPIDI_CH3_VC_Init is |
|---|
| 124 | called before MPIDI_CH3_Init, and call MPIDI_CH3_VC_Init from |
|---|
| 125 | inside MPIDI_CH3_Init after initializing nemesis |
|---|
| 126 | */ |
|---|
| 127 | if (!nemesis_initialized) |
|---|
| 128 | goto fn_exit; |
|---|
| 129 | |
|---|
| 130 | /* no need to initialize vc to self */ |
|---|
| 131 | if (vc->pg == MPIDI_CH3I_my_pg && vc->pg_rank == MPIDI_CH3I_my_rank) |
|---|
| 132 | goto fn_exit; |
|---|
| 133 | |
|---|
| 134 | ((MPIDI_CH3I_VC *)vc->channel_private)->recv_active = NULL; |
|---|
| 135 | |
|---|
| 136 | mpi_errno = MPID_nem_vc_init (vc); |
|---|
| 137 | if (mpi_errno) MPIU_ERR_POP (mpi_errno); |
|---|
| 138 | |
|---|
| 139 | fn_exit: |
|---|
| 140 | fn_fail: |
|---|
| 141 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_VC_INIT); |
|---|
| 142 | return mpi_errno; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | #undef FUNCNAME |
|---|
| 146 | #define FUNCNAME MPIDI_CH3_VC_Destroy |
|---|
| 147 | #undef FCNAME |
|---|
| 148 | #define FCNAME MPIDI_QUOTE(FUNCNAME) |
|---|
| 149 | int MPIDI_CH3_VC_Destroy(MPIDI_VC_t *vc ) |
|---|
| 150 | { |
|---|
| 151 | int mpi_errno = MPI_SUCCESS; |
|---|
| 152 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_VC_DESTROY); |
|---|
| 153 | |
|---|
| 154 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_VC_DESTROY); |
|---|
| 155 | |
|---|
| 156 | /* no need to destroy vc to self, this corresponds to the optimization above |
|---|
| 157 | * in MPIDI_CH3_VC_Init */ |
|---|
| 158 | if (vc->pg == MPIDI_CH3I_my_pg && vc->pg_rank == MPIDI_CH3I_my_rank) { |
|---|
| 159 | MPIU_DBG_MSG_P(NEM_SOCK_DET, VERBOSE, "skipping self vc=%p", vc); |
|---|
| 160 | goto fn_exit; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | mpi_errno = MPID_nem_vc_destroy(vc); |
|---|
| 164 | |
|---|
| 165 | fn_exit: |
|---|
| 166 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_VC_DESTROY); |
|---|
| 167 | return mpi_errno; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /* MPIDI_CH3_Connect_to_root() create a new vc, and connect it to the process listening on port_name */ |
|---|
| 171 | #undef FUNCNAME |
|---|
| 172 | #define FUNCNAME MPIDI_CH3_Connect_to_root |
|---|
| 173 | #undef FCNAME |
|---|
| 174 | #define FCNAME MPIDI_QUOTE(FUNCNAME) |
|---|
| 175 | int MPIDI_CH3_Connect_to_root (const char *port_name, MPIDI_VC_t **new_vc) |
|---|
| 176 | { |
|---|
| 177 | int mpi_errno = MPI_SUCCESS; |
|---|
| 178 | MPIDI_VC_t * vc; |
|---|
| 179 | MPIU_CHKPMEM_DECL(1); |
|---|
| 180 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_CONNECT_TO_ROOT); |
|---|
| 181 | |
|---|
| 182 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_CONNECT_TO_ROOT); |
|---|
| 183 | |
|---|
| 184 | *new_vc = NULL; /* so that the err handling knows to cleanup */ |
|---|
| 185 | |
|---|
| 186 | MPIU_CHKPMEM_MALLOC (vc, MPIDI_VC_t *, sizeof(MPIDI_VC_t), mpi_errno, "vc"); |
|---|
| 187 | /* FIXME - where does this vc get freed? |
|---|
| 188 | ANSWER (goodell@) - ch3u_port.c FreeNewVC |
|---|
| 189 | (but the VC_Destroy is in this file) */ |
|---|
| 190 | |
|---|
| 191 | /* init ch3 portion of vc */ |
|---|
| 192 | MPIDI_VC_Init (vc, NULL, 0); |
|---|
| 193 | |
|---|
| 194 | /* init channel portion of vc */ |
|---|
| 195 | MPIU_ERR_CHKANDJUMP (!nemesis_initialized, mpi_errno, MPI_ERR_OTHER, "**intern"); |
|---|
| 196 | ((MPIDI_CH3I_VC *)vc->channel_private)->recv_active = NULL; |
|---|
| 197 | MPIDI_CHANGE_VC_STATE(vc, ACTIVE); |
|---|
| 198 | |
|---|
| 199 | *new_vc = vc; /* we now have a valid, disconnected, temp VC */ |
|---|
| 200 | |
|---|
| 201 | mpi_errno = MPID_nem_connect_to_root (port_name, vc); |
|---|
| 202 | if (mpi_errno) MPIU_ERR_POP (mpi_errno); |
|---|
| 203 | |
|---|
| 204 | MPIU_CHKPMEM_COMMIT(); |
|---|
| 205 | fn_exit: |
|---|
| 206 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_CONNECT_TO_ROOT); |
|---|
| 207 | return mpi_errno; |
|---|
| 208 | fn_fail: |
|---|
| 209 | /* freeing without giving the lower layer a chance to cleanup can lead to |
|---|
| 210 | leaks on error */ |
|---|
| 211 | if (*new_vc) |
|---|
| 212 | MPIDI_CH3_VC_Destroy(*new_vc); |
|---|
| 213 | MPIU_CHKPMEM_REAP(); |
|---|
| 214 | goto fn_exit; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | #ifndef MPIDI_CH3_HAS_NO_DYNAMIC_PROCESS |
|---|
| 218 | #ifdef USE_DBG_LOGGING |
|---|
| 219 | const char * MPIDI_CH3_VC_GetStateString( struct MPIDI_VC *vc ) |
|---|
| 220 | { |
|---|
| 221 | const char *name = "unknown"; |
|---|
| 222 | static char asdigits[20]; |
|---|
| 223 | MPIDI_CH3I_VC *vcch = (MPIDI_CH3I_VC *)vc->channel_private; |
|---|
| 224 | int state = vcch->state; |
|---|
| 225 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_VC_GETSTATESTRING); |
|---|
| 226 | |
|---|
| 227 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_VC_GETSTATESTRING); |
|---|
| 228 | |
|---|
| 229 | switch (state) { |
|---|
| 230 | case MPIDI_CH3I_VC_STATE_UNCONNECTED: name = "CH3I_VC_STATE_UNCONNECTED"; break; |
|---|
| 231 | case MPIDI_CH3I_VC_STATE_CONNECTING: name = "CH3I_VC_STATE_CONNECTING"; break; |
|---|
| 232 | case MPIDI_CH3I_VC_STATE_CONNECTED: name = "CH3I_VC_STATE_CONNECTED"; break; |
|---|
| 233 | case MPIDI_CH3I_VC_STATE_FAILED: name = "CH3I_VC_STATE_FAILED"; break; |
|---|
| 234 | default: |
|---|
| 235 | MPIU_Snprintf( asdigits, sizeof(asdigits), "%d", state ); |
|---|
| 236 | asdigits[20-1] = 0; |
|---|
| 237 | name = (const char *)asdigits; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_VC_GETSTATESTRING); |
|---|
| 241 | return name; |
|---|
| 242 | } |
|---|
| 243 | #endif |
|---|
| 244 | #endif |
|---|
| 245 | |
|---|
| 246 | /* We don't initialize before calling MPIDI_CH3_VC_Init */ |
|---|
| 247 | int MPIDI_CH3_PG_Init(MPIDI_PG_t *pg_p) |
|---|
| 248 | { |
|---|
| 249 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_PG_INIT); |
|---|
| 250 | |
|---|
| 251 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_PG_INIT); |
|---|
| 252 | MPIU_UNREFERENCED_ARG(pg_p); |
|---|
| 253 | |
|---|
| 254 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_PG_INIT); |
|---|
| 255 | return MPI_SUCCESS; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | int MPIDI_CH3_PG_Destroy(MPIDI_PG_t *pg_p) |
|---|
| 259 | { |
|---|
| 260 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_PG_DESTROY); |
|---|
| 261 | |
|---|
| 262 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_PG_DESTROY); |
|---|
| 263 | MPIU_UNREFERENCED_ARG(pg_p); |
|---|
| 264 | |
|---|
| 265 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_PG_DESTROY); |
|---|
| 266 | return MPI_SUCCESS; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | typedef struct initcomp_cb |
|---|
| 271 | { |
|---|
| 272 | int (* callback)(void); |
|---|
| 273 | struct initcomp_cb *next; |
|---|
| 274 | } initcomp_cb_t; |
|---|
| 275 | |
|---|
| 276 | static struct {initcomp_cb_t *top;} initcomp_cb_stack = {0}; |
|---|
| 277 | |
|---|
| 278 | #define INITCOMP_S_TOP() GENERIC_S_TOP(initcomp_cb_stack) |
|---|
| 279 | #define INITCOMP_S_PUSH(ep) GENERIC_S_PUSH(&initcomp_cb_stack, ep, next) |
|---|
| 280 | |
|---|
| 281 | /* register a function to be called when all initialization is finished */ |
|---|
| 282 | #undef FUNCNAME |
|---|
| 283 | #define FUNCNAME MPID_nem_register_initcomp_cb |
|---|
| 284 | #undef FCNAME |
|---|
| 285 | #define FCNAME MPIDI_QUOTE(FUNCNAME) |
|---|
| 286 | int MPID_nem_register_initcomp_cb(int (* callback)(void)) |
|---|
| 287 | { |
|---|
| 288 | int mpi_errno = MPI_SUCCESS; |
|---|
| 289 | initcomp_cb_t *ep; |
|---|
| 290 | MPIU_CHKPMEM_DECL(1); |
|---|
| 291 | MPIDI_STATE_DECL(MPID_STATE_MPID_NEM_REGISTER_INITCOMP_CB); |
|---|
| 292 | |
|---|
| 293 | MPIDI_FUNC_ENTER(MPID_STATE_MPID_NEM_REGISTER_INITCOMP_CB); |
|---|
| 294 | MPIU_CHKPMEM_MALLOC(ep, initcomp_cb_t *, sizeof(*ep), mpi_errno, "initcomp callback element"); |
|---|
| 295 | |
|---|
| 296 | ep->callback = callback; |
|---|
| 297 | INITCOMP_S_PUSH(ep); |
|---|
| 298 | |
|---|
| 299 | MPIU_CHKPMEM_COMMIT(); |
|---|
| 300 | fn_exit: |
|---|
| 301 | MPIDI_FUNC_EXIT(MPID_STATE_MPID_NEM_REGISTER_INITCOMP_CB); |
|---|
| 302 | return mpi_errno; |
|---|
| 303 | fn_fail: |
|---|
| 304 | MPIU_CHKPMEM_REAP(); |
|---|
| 305 | goto fn_exit; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | #undef FUNCNAME |
|---|
| 309 | #define FUNCNAME MPIDI_CH3_InitCompleted |
|---|
| 310 | #undef FCNAME |
|---|
| 311 | #define FCNAME MPIDI_QUOTE(FUNCNAME) |
|---|
| 312 | int MPIDI_CH3_InitCompleted(void) |
|---|
| 313 | { |
|---|
| 314 | int mpi_errno = MPI_SUCCESS; |
|---|
| 315 | initcomp_cb_t *ep; |
|---|
| 316 | MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3_INITCOMPLETED); |
|---|
| 317 | |
|---|
| 318 | MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3_INITCOMPLETED); |
|---|
| 319 | ep = INITCOMP_S_TOP(); |
|---|
| 320 | while (ep) |
|---|
| 321 | { |
|---|
| 322 | mpi_errno = ep->callback(); |
|---|
| 323 | if (mpi_errno) MPIU_ERR_POP(mpi_errno); |
|---|
| 324 | ep = ep->next; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | fn_exit: |
|---|
| 328 | MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3_INITCOMPLETED); |
|---|
| 329 | return mpi_errno; |
|---|
| 330 | fn_fail: |
|---|
| 331 | goto fn_exit; |
|---|
| 332 | } |
|---|