1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2015      Los Alamos National Security, LLC.  All rights
4  *                         reserved.
5  * $COPYRIGHT$
6  *
7  * Additional copyrights may follow
8  *
9  * $HEADER$
10  */
11 
12 #include "osc_pt2pt.h"
13 #include "osc_pt2pt_sync.h"
14 
ompi_osc_pt2pt_sync_constructor(ompi_osc_pt2pt_sync_t * sync)15 static void ompi_osc_pt2pt_sync_constructor (ompi_osc_pt2pt_sync_t *sync)
16 {
17     sync->type = OMPI_OSC_PT2PT_SYNC_TYPE_NONE;
18     sync->eager_send_active = false;
19     sync->epoch_active = false;
20     OBJ_CONSTRUCT(&sync->lock, opal_mutex_t);
21     OBJ_CONSTRUCT(&sync->cond, opal_condition_t);
22 }
23 
ompi_osc_pt2pt_sync_destructor(ompi_osc_pt2pt_sync_t * sync)24 static void ompi_osc_pt2pt_sync_destructor (ompi_osc_pt2pt_sync_t *sync)
25 {
26     OBJ_DESTRUCT(&sync->lock);
27     OBJ_DESTRUCT(&sync->cond);
28 }
29 
30 OBJ_CLASS_INSTANCE(ompi_osc_pt2pt_sync_t, opal_free_list_item_t,
31                    ompi_osc_pt2pt_sync_constructor,
32                    ompi_osc_pt2pt_sync_destructor);
33 
ompi_osc_pt2pt_sync_allocate(struct ompi_osc_pt2pt_module_t * module)34 ompi_osc_pt2pt_sync_t *ompi_osc_pt2pt_sync_allocate (struct ompi_osc_pt2pt_module_t *module)
35 {
36     ompi_osc_pt2pt_sync_t *sync;
37 
38     /* module is not used yet */
39     (void) module;
40 
41     sync = OBJ_NEW (ompi_osc_pt2pt_sync_t);
42     if (OPAL_UNLIKELY(NULL == sync)) {
43         return NULL;
44     }
45 
46     sync->module = module;
47     return sync;
48 }
49 
ompi_osc_pt2pt_sync_return(ompi_osc_pt2pt_sync_t * sync)50 void ompi_osc_pt2pt_sync_return (ompi_osc_pt2pt_sync_t *sync)
51 {
52     OBJ_RELEASE(sync);
53 }
54 
ompi_osc_pt2pt_sync_array_peer(int rank,ompi_osc_pt2pt_peer_t ** peers,size_t nranks,struct ompi_osc_pt2pt_peer_t ** peer)55 static inline bool ompi_osc_pt2pt_sync_array_peer (int rank, ompi_osc_pt2pt_peer_t **peers, size_t nranks,
56                                                    struct ompi_osc_pt2pt_peer_t **peer)
57 {
58     int mid = nranks / 2;
59 
60     /* base cases */
61     if (0 == nranks || (1 == nranks && peers[0]->rank != rank)) {
62         if (peer) {
63             *peer = NULL;
64         }
65         return false;
66     } else if (peers[0]->rank == rank) {
67         if (peer) {
68             *peer = peers[0];
69         }
70         return true;
71     }
72 
73     if (peers[mid]->rank > rank) {
74         return ompi_osc_pt2pt_sync_array_peer (rank, peers, mid, peer);
75     }
76 
77     return ompi_osc_pt2pt_sync_array_peer (rank, peers + mid, nranks - mid, peer);
78 }
79 
ompi_osc_pt2pt_sync_pscw_peer(ompi_osc_pt2pt_module_t * module,int target,struct ompi_osc_pt2pt_peer_t ** peer)80 bool ompi_osc_pt2pt_sync_pscw_peer (ompi_osc_pt2pt_module_t *module, int target, struct ompi_osc_pt2pt_peer_t **peer)
81 {
82     ompi_osc_pt2pt_sync_t *pt2pt_sync = &module->all_sync;
83 
84     /* check synchronization type */
85     if (OMPI_OSC_PT2PT_SYNC_TYPE_PSCW != pt2pt_sync->type) {
86         if (peer) {
87             *peer = NULL;
88         }
89         return false;
90     }
91 
92     return ompi_osc_pt2pt_sync_array_peer (target, pt2pt_sync->peer_list.peers, pt2pt_sync->num_peers, peer);
93 }
94