1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
4  *                         University Research and Technology
5  *                         Corporation.  All rights reserved.
6  * Copyright (c) 2004-2013 The University of Tennessee and The University
7  *                         of Tennessee Research Foundation.  All rights
8  *                         reserved.
9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
10  *                         University of Stuttgart.  All rights reserved.
11  * Copyright (c) 2004-2005 The Regents of the University of California.
12  *                         All rights reserved.
13  * Copyright (c) 2012-2013 Inria.  All rights reserved.
14  * Copyright (c) 2014-2016 Los Alamos National Security, LLC. All rights
15  *                         reserved.
16  * Copyright (c) 2016      Research Organization for Information Science
17  *                         and Technology (RIST). All rights reserved.
18  * $COPYRIGHT$
19  *
20  * Additional copyrights may follow
21  *
22  * $HEADER$
23  */
24 
25 #include "opal_config.h"
26 
27 #include <string.h>
28 #include <stdlib.h>
29 
30 #include "opal/class/opal_bitmap.h"
31 #include "opal/datatype/opal_convertor.h"
32 #include "btl_self.h"
33 #include "btl_self_frag.h"
34 #include "opal/util/proc.h"
35 
36 /**
37  * PML->BTL notification of change in the process list.
38  * PML->BTL Notification that a receive fragment has been matched.
39  * Called for message that is send from process with the virtual
40  * address of the shared memory segment being different than that of
41  * the receiver.
42  *
43  * @param btl (IN)
44  * @param proc (IN)
45  * @param peer (OUT)
46  * @return     OPAL_SUCCESS or error status on failure.
47  *
48  */
mca_btl_self_add_procs(struct mca_btl_base_module_t * btl,size_t nprocs,struct opal_proc_t ** procs,struct mca_btl_base_endpoint_t ** peers,opal_bitmap_t * reachability)49 static int mca_btl_self_add_procs (struct mca_btl_base_module_t *btl, size_t nprocs,
50                                    struct opal_proc_t **procs,
51                                    struct mca_btl_base_endpoint_t **peers,
52                                    opal_bitmap_t* reachability)
53 {
54     for (int i = 0; i < (int)nprocs; i++ ) {
55         if( 0 == opal_compare_proc(procs[i]->proc_name, OPAL_PROC_MY_NAME) ) {
56             opal_bitmap_set_bit( reachability, i );
57             /* need to return something to keep the bml from ignoring us */
58             peers[i] = (struct mca_btl_base_endpoint_t *) 1;
59             break;  /* there will always be only one ... */
60         }
61     }
62 
63     return OPAL_SUCCESS;
64 }
65 
66 /**
67  * PML->BTL notification of change in the process list.
68  *
69  * @param btl (IN)     BTL instance
70  * @param proc (IN)    Peer process
71  * @param peer (IN)    Peer addressing information.
72  * @return             Status indicating if cleanup was successful
73  *
74  */
mca_btl_self_del_procs(struct mca_btl_base_module_t * btl,size_t nprocs,struct opal_proc_t ** procs,struct mca_btl_base_endpoint_t ** peers)75 static int mca_btl_self_del_procs (struct mca_btl_base_module_t *btl, size_t nprocs,
76                                    struct opal_proc_t **procs,
77                                    struct mca_btl_base_endpoint_t **peers)
78 {
79     return OPAL_SUCCESS;
80 }
81 
82 
83 /**
84  * MCA->BTL Clean up any resources held by BTL module
85  * before the module is unloaded.
86  *
87  * @param btl (IN)   BTL module.
88  *
89  * Prior to unloading a BTL module, the MCA framework will call
90  * the BTL finalize method of the module. Any resources held by
91  * the BTL should be released and if required the memory corresponding
92  * to the BTL module freed.
93  *
94  */
95 
mca_btl_self_finalize(struct mca_btl_base_module_t * btl)96 static int mca_btl_self_finalize(struct mca_btl_base_module_t* btl)
97 {
98     return OPAL_SUCCESS;
99 }
100 
101 
102 /**
103  * Allocate a segment.
104  *
105  * @param btl (IN)      BTL module
106  * @param size (IN)     Request segment size.
107  */
mca_btl_self_alloc(struct mca_btl_base_module_t * btl,struct mca_btl_base_endpoint_t * endpoint,uint8_t order,size_t size,uint32_t flags)108 static mca_btl_base_descriptor_t *mca_btl_self_alloc (struct mca_btl_base_module_t *btl,
109                                                       struct mca_btl_base_endpoint_t *endpoint,
110                                                       uint8_t order, size_t size, uint32_t flags)
111 {
112     mca_btl_self_frag_t *frag = NULL;
113 
114     if (size <= MCA_BTL_SELF_MAX_INLINE_SIZE) {
115         MCA_BTL_SELF_FRAG_ALLOC_RDMA(frag);
116     } else if (size <= mca_btl_self.btl_eager_limit) {
117         MCA_BTL_SELF_FRAG_ALLOC_EAGER(frag);
118     } else if (size <= btl->btl_max_send_size) {
119         MCA_BTL_SELF_FRAG_ALLOC_SEND(frag);
120     }
121 
122     if( OPAL_UNLIKELY(NULL == frag) ) {
123         return NULL;
124     }
125 
126     frag->segments[0].seg_len = size;
127     frag->base.des_segment_count = 1;
128     frag->base.des_flags       = flags;
129 
130     return &frag->base;
131 }
132 
133 /**
134  * Return a segment allocated by this BTL.
135  *
136  * @param btl (IN)      BTL module
137  * @param segment (IN)  Allocated segment.
138  */
mca_btl_self_free(struct mca_btl_base_module_t * btl,mca_btl_base_descriptor_t * des)139 static int mca_btl_self_free (struct mca_btl_base_module_t *btl, mca_btl_base_descriptor_t *des)
140 {
141     MCA_BTL_SELF_FRAG_RETURN((mca_btl_self_frag_t *) des);
142 
143     return OPAL_SUCCESS;
144 }
145 
146 
147 /**
148  * Prepare data for send
149  *
150  * @param btl (IN)      BTL module
151  */
mca_btl_self_prepare_src(struct mca_btl_base_module_t * btl,struct mca_btl_base_endpoint_t * endpoint,struct opal_convertor_t * convertor,uint8_t order,size_t reserve,size_t * size,uint32_t flags)152 static struct mca_btl_base_descriptor_t *mca_btl_self_prepare_src (struct mca_btl_base_module_t* btl,
153                                                                    struct mca_btl_base_endpoint_t *endpoint,
154                                                                    struct opal_convertor_t *convertor,
155                                                                    uint8_t order, size_t reserve,
156                                                                    size_t *size, uint32_t flags)
157 {
158     bool inline_send = !opal_convertor_need_buffers(convertor);
159     size_t buffer_len = reserve + (inline_send ? 0 : *size);
160     mca_btl_self_frag_t *frag;
161 
162     frag = (mca_btl_self_frag_t *) mca_btl_self_alloc (btl, endpoint, order, buffer_len, flags);
163     if (OPAL_UNLIKELY(NULL == frag)) {
164         return NULL;
165     }
166 
167     /* non-contigous data */
168     if (OPAL_UNLIKELY(!inline_send)) {
169         struct iovec iov = {.iov_len = *size, .iov_base = (IOVBASE_TYPE *) ((uintptr_t) frag->data + reserve)};
170         size_t max_data = *size;
171         uint32_t iov_count = 1;
172         int rc;
173 
174         rc = opal_convertor_pack (convertor, &iov, &iov_count, &max_data);
175         if(rc < 0) {
176             mca_btl_self_free (btl, &frag->base);
177             return NULL;
178         }
179 
180         *size = max_data;
181         frag->segments[0].seg_len = reserve + max_data;
182     } else {
183         void *data_ptr;
184 
185         opal_convertor_get_current_pointer (convertor, &data_ptr);
186 
187         frag->segments[1].seg_addr.pval = data_ptr;
188         frag->segments[1].seg_len = *size;
189         frag->base.des_segment_count = 2;
190     }
191 
192     return &frag->base;
193 }
194 
195 /**
196  * Initiate a send to the peer.
197  *
198  * @param btl (IN)      BTL module
199  * @param peer (IN)     BTL peer addressing
200  */
201 
mca_btl_self_send(struct mca_btl_base_module_t * btl,struct mca_btl_base_endpoint_t * endpoint,struct mca_btl_base_descriptor_t * des,mca_btl_base_tag_t tag)202 static int mca_btl_self_send (struct mca_btl_base_module_t *btl,
203                               struct mca_btl_base_endpoint_t *endpoint,
204                               struct mca_btl_base_descriptor_t *des,
205                               mca_btl_base_tag_t tag)
206 {
207     mca_btl_active_message_callback_t* reg;
208     int btl_ownership = (des->des_flags & MCA_BTL_DES_FLAGS_BTL_OWNERSHIP);
209 
210     /* upcall */
211     reg = mca_btl_base_active_message_trigger + tag;
212     reg->cbfunc( btl, tag, des, reg->cbdata );
213 
214     /* send completion */
215     if( des->des_flags & MCA_BTL_DES_SEND_ALWAYS_CALLBACK ) {
216         des->des_cbfunc( btl, endpoint, des, OPAL_SUCCESS );
217     }
218     if( btl_ownership ) {
219         mca_btl_self_free( btl, des );
220     }
221     return 1;
222 }
223 
mca_btl_self_sendi(struct mca_btl_base_module_t * btl,struct mca_btl_base_endpoint_t * endpoint,struct opal_convertor_t * convertor,void * header,size_t header_size,size_t payload_size,uint8_t order,uint32_t flags,mca_btl_base_tag_t tag,mca_btl_base_descriptor_t ** descriptor)224 static int mca_btl_self_sendi (struct mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint,
225                                struct opal_convertor_t *convertor, void *header, size_t header_size,
226                                size_t payload_size, uint8_t order, uint32_t flags, mca_btl_base_tag_t tag,
227                                mca_btl_base_descriptor_t **descriptor)
228 {
229     mca_btl_base_descriptor_t *frag;
230 
231     if (!payload_size || !opal_convertor_need_buffers(convertor)) {
232         void *data_ptr = NULL;
233         if (payload_size) {
234             opal_convertor_get_current_pointer (convertor, &data_ptr);
235         }
236 
237         mca_btl_base_segment_t segments[2] = {{.seg_addr.pval = header, .seg_len = header_size},
238                                               {.seg_addr.pval = data_ptr, .seg_len = payload_size}};
239         mca_btl_base_descriptor_t des = {.des_segments = segments, .des_segment_count = payload_size ? 2 : 1,
240                                          .des_flags = 0};
241 
242         (void) mca_btl_self_send (btl, endpoint, &des, tag);
243         return OPAL_SUCCESS;
244     }
245 
246     frag = mca_btl_self_prepare_src (btl, endpoint, convertor, order, header_size, &payload_size,
247                                      flags | MCA_BTL_DES_FLAGS_BTL_OWNERSHIP);
248     if (NULL == frag) {
249         *descriptor = NULL;
250         return OPAL_ERR_OUT_OF_RESOURCE;
251     }
252 
253     memcpy (frag->des_segments[0].seg_addr.pval, header, header_size);
254     (void) mca_btl_self_send (btl, endpoint, frag, tag);
255     return OPAL_SUCCESS;
256 }
257 
mca_btl_self_put(mca_btl_base_module_t * btl,struct mca_btl_base_endpoint_t * endpoint,void * local_address,uint64_t remote_address,mca_btl_base_registration_handle_t * local_handle,mca_btl_base_registration_handle_t * remote_handle,size_t size,int flags,int order,mca_btl_base_rdma_completion_fn_t cbfunc,void * cbcontext,void * cbdata)258 static int mca_btl_self_put (mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, void *local_address,
259                              uint64_t remote_address, mca_btl_base_registration_handle_t *local_handle,
260                              mca_btl_base_registration_handle_t *remote_handle, size_t size, int flags,
261                              int order, mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, void *cbdata)
262 {
263     memcpy ((void *)(intptr_t) remote_address, local_address, size);
264 
265     cbfunc (btl, endpoint, local_address, NULL, cbcontext, cbdata, OPAL_SUCCESS);
266 
267     return OPAL_SUCCESS;
268 }
269 
mca_btl_self_get(mca_btl_base_module_t * btl,struct mca_btl_base_endpoint_t * endpoint,void * local_address,uint64_t remote_address,mca_btl_base_registration_handle_t * local_handle,mca_btl_base_registration_handle_t * remote_handle,size_t size,int flags,int order,mca_btl_base_rdma_completion_fn_t cbfunc,void * cbcontext,void * cbdata)270 static int mca_btl_self_get (mca_btl_base_module_t *btl, struct mca_btl_base_endpoint_t *endpoint, void *local_address,
271                              uint64_t remote_address, mca_btl_base_registration_handle_t *local_handle,
272                              mca_btl_base_registration_handle_t *remote_handle, size_t size, int flags,
273                              int order, mca_btl_base_rdma_completion_fn_t cbfunc, void *cbcontext, void *cbdata)
274 {
275     memcpy (local_address, (void *)(intptr_t) remote_address, size);
276 
277     cbfunc (btl, endpoint, local_address, NULL, cbcontext, cbdata, OPAL_SUCCESS);
278 
279     return OPAL_SUCCESS;
280 }
281 
mca_btl_self_ft_event(int state)282 static int mca_btl_self_ft_event(int state) {
283     return OPAL_SUCCESS;
284 }
285 
286 /* btl self module */
287 mca_btl_base_module_t mca_btl_self = {
288     .btl_component = &mca_btl_self_component.super,
289     .btl_add_procs = mca_btl_self_add_procs,
290     .btl_del_procs = mca_btl_self_del_procs,
291     .btl_finalize = mca_btl_self_finalize,
292     .btl_alloc = mca_btl_self_alloc,
293     .btl_free = mca_btl_self_free,
294     .btl_prepare_src = mca_btl_self_prepare_src,
295     .btl_send = mca_btl_self_send,
296     .btl_sendi = mca_btl_self_sendi,
297     .btl_put = mca_btl_self_put,
298     .btl_get = mca_btl_self_get,
299     .btl_dump = mca_btl_base_dump,
300     .btl_ft_event = mca_btl_self_ft_event,
301 };
302