margo.h 18 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#ifndef __MARGO
#define __MARGO

10 11 12 13
#ifdef __cplusplus
extern "C" {
#endif

14 15 16 17 18 19
/* This is to prevent the user from usin HG_Register_data
 * and HG_Registered_data, which are replaced with
 * margo_register_data and margo_registered_data
 * respecively.
 */

20 21 22 23 24
#include <mercury_bulk.h>
#include <mercury.h>
#include <mercury_macros.h>
#include <abt.h>

25 26
#undef MERCURY_REGISTER

27 28
struct margo_instance;
typedef struct margo_instance* margo_instance_id;
29
typedef struct margo_data* margo_data_ptr;
30

31
#define MARGO_INSTANCE_NULL ((margo_instance_id)NULL)
32
#define MARGO_DEFAULT_MPLEX_ID 0
33
#define MARGO_RPC_ID_IGNORE ((hg_id_t*)NULL)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
34 35 36

/**
 * Initializes margo library.
Shane Snyder's avatar
Shane Snyder committed
37 38
 * @param [in] addr_str            Mercury host address with port number
 * @param [in] listen_flag         Boolean flag to listen for incoming connections
Jonathan Jenkins's avatar
Jonathan Jenkins committed
39 40
 * @param [in] use_progress_thread Boolean flag to use a dedicated thread for
 *                                 running Mercury's progress loop. If false,
41
 *                                 it will run in the caller's thread context.
Jonathan Jenkins's avatar
Jonathan Jenkins committed
42 43
 * @param [in] rpc_thread_count    Number of threads to use for running RPC
 *                                 calls. A value of 0 directs Margo to execute
44 45 46 47 48 49
 *                                 RPCs in the caller's thread context.
 *                                 Clients (i.e processes that will *not* 
 *                                 service incoming RPCs) should use a value 
 *                                 of 0. A value of -1 directs Margo to use 
 *                                 the same execution context as that used 
 *                                 for Mercury progress.
Jonathan Jenkins's avatar
Jonathan Jenkins committed
50
 * @returns margo instance id on success, MARGO_INSTANCE_NULL upon error
51 52 53 54 55
 *
 * NOTE: Servers (processes expecting to service incoming RPC requests) must
 * specify non-zero values for use_progress_thread and rpc_thread_count *or*
 * call margo_wait_for_finalize() after margo_init() to relinguish control to 
 * Margo.
Jonathan Jenkins's avatar
Jonathan Jenkins committed
56
 */
Shane Snyder's avatar
Shane Snyder committed
57 58 59 60 61
margo_instance_id margo_init(
    const char *addr_str,
    int listen_flag,
    int use_progress_thread,
    int rpc_thread_count);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
62

63
/**
Jonathan Jenkins's avatar
doc fix  
Jonathan Jenkins committed
64
 * Initializes margo library from given argobots and Mercury instances.
Philip Carns's avatar
Philip Carns committed
65 66 67
 * @param [in] progress_pool Argobots pool to drive communication
 * @param [in] handler_pool Argobots pool to service RPC handlers
 * @param [in] hg_context Mercury context
Jonathan Jenkins's avatar
fixupme  
Jonathan Jenkins committed
68
 * @returns margo instance id on success, MARGO_INSTANCE_NULL upon error
69
 */
Shane Snyder's avatar
Shane Snyder committed
70 71 72
margo_instance_id margo_init_pool(
    ABT_pool progress_pool,
    ABT_pool handler_pool,
Jonathan Jenkins's avatar
Jonathan Jenkins committed
73
    hg_context_t *hg_context);
74 75

/**
Philip Carns's avatar
Philip Carns committed
76
 * Shuts down margo library and its underlying abt and mercury resources
Philip Carns's avatar
Philip Carns committed
77
 * @param [in] mid Margo instance
78
 */
79
void margo_finalize(margo_instance_id mid);
80 81 82 83 84 85 86 87 88 89 90 91

/**
 * Suspends the caller until some other entity (e.g. an RPC, thread, or
 * signal handler) invokes margo_finalize().
 *
 * NOTE: This informs Margo that the calling thread no longer needs to be 
 * scheduled for execution if it is sharing an Argobots pool with the
 * progress engine.
 *
 * @param [in] mid Margo instance
 */
void margo_wait_for_finalize(margo_instance_id mid);
92

93 94
/** 
 * Registers an RPC with margo
Philip Carns's avatar
Philip Carns committed
95
 * @param [in] mid Margo instance
96
 * @param [in] id Mercury RPC identifier
97
 */
98
int margo_register(margo_instance_id mid, hg_id_t id);
99

100 101
/** 
 * Registers an RPC with margo that is associated with a multiplexed service
102
 * @param [in] mid Margo instance
103 104 105
 * @param [in] id Mercury RPC identifier
 * @param [in] mplex_id multiplexing identifier
 * @param [in] pool Argobots pool the handler will execute in
106
 */
107
int margo_register_mplex(margo_instance_id mid, hg_id_t id, uint32_t mplex_id, ABT_pool pool);
108

109 110 111 112 113 114 115 116 117 118
/*
 * Indicate whether HG_Register_name() has been called for the RPC specified by
 * func_name.
 *
 * \param hg_class [IN]         pointer to HG class
 * \param func_name [IN]        function name
 * \param id [OUT]              registered RPC ID
 * \param flag [OUT]            pointer to boolean
 *
 * \return HG_SUCCESS or corresponding HG error code
119
 */
120 121 122 123 124 125 126 127 128
/* XXX
HG_EXPORT hg_return_t
HG_Registered_name(
        hg_class_t *hg_class,
        const char *func_name,
        hg_id_t *id,
        hg_bool_t *flag
        );
*/
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/**
 * Register and associate user data to registered function.
 * When HG_Finalize() is called free_callback (if defined) is called 
 * to free the registered data.
 *
 * \param [in] mid            Margo instance
 * \param [in] id             registered function ID
 * \param [in] data           pointer to data
 * \param [in] free_callback  pointer to free function
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
hg_return_t margo_register_data(
    margo_instance_id mid,
    hg_id_t id,
    void *data,
    void (*free_callback)(void *));

/**
 * Indicate whether margo_register_data() has been called and return associated
 * data.
 *
 * \param [in] mid        Margo instance 
 * \param [in] id         registered function ID
 *
 * \return Pointer to data or NULL
 */
void* margo_registered_data(margo_instance_id mid, hg_id_t id);

/**
160 161 162 163 164
 * Disable response for a given RPC ID. This allows an origin process to send an
 * RPC to a target without waiting for a response. The RPC completes locally and
 * the callback on the origin is therefore pushed to the completion queue once
 * the RPC send is completed. By default, all RPCs expect a response to
 * be sent back.
165
 *
166 167 168 169 170 171
 * \param hg_class [IN]         pointer to HG class
 * \param id [IN]               registered function ID
 * \param disable [IN]          boolean (HG_TRUE to disable
 *                                       HG_FALSE to re-enable)
 *
 * \return HG_SUCCESS or corresponding HG error code
172
 */
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/* XXX
HG_EXPORT hg_return_t
HG_Registered_disable_response(
        hg_class_t *hg_class,
        hg_id_t id,
        hg_bool_t disable
        );
*/

/**
 * Lookup an addr from a peer address/name.
 * @param [in] name             lookup name
 * @param [out] addr            return address
 * @returns HG_SUCCESS on success
 */
hg_return_t margo_addr_lookup(
    margo_instance_id mid,
    const char   *name,
    hg_addr_t    *addr);

/**
 * Free the addr from the list of peers.
 *
 * \param hg_class [IN]         pointer to HG class
 * \param addr [IN]             abstract address
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Addr_free(
        hg_class_t *hg_class,
        hg_addr_t   addr
        );
*/

/**
 * Access self address. Address must be freed with HG_Addr_free().
 *
 * \param hg_class [IN]         pointer to HG class
 * \param addr [OUT]            pointer to abstract address
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX 
HG_EXPORT hg_return_t
HG_Addr_self(
        hg_class_t *hg_class,
        hg_addr_t  *addr
        );
*/

/**
 * Duplicate an existing HG abstract address. The duplicated address can be
 * stored for later use and the origin address be freed safely. The duplicated
 * address must be freed with HG_Addr_free().
 *
 * \param hg_class [IN]         pointer to HG class
 * \param addr [IN]             abstract address
 * \param new_addr [OUT]        pointer to abstract address
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Addr_dup(
        hg_class_t *hg_class,
        hg_addr_t   addr,
        hg_addr_t  *new_addr
        );
*/

/**
 * Convert an addr to a string (returned string includes the terminating
 * null byte '\0'). If buf is NULL, the address is not converted and only
 * the required size of the buffer is returned. If the input value passed
 * through buf_size is too small, HG_SIZE_ERROR is returned and the buf_size
 * output is set to the minimum size required.
 *
 * \param hg_class [IN]         pointer to HG class
 * \param buf [IN/OUT]          pointer to destination buffer
 * \param buf_size [IN/OUT]     pointer to buffer size
 * \param addr [IN]             abstract address
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Addr_to_string(
        hg_class_t *hg_class,
        char       *buf,
        hg_size_t  *buf_size,
        hg_addr_t   addr
        );
*/

/**
 * Initiate a new HG RPC using the specified function ID and the local/remote
 * target defined by addr. The HG handle created can be used to query input
 * and output, as well as issuing the RPC by calling HG_Forward().
 * After completion the handle must be freed using HG_Destroy().
 *
 * \param context [IN]          pointer to HG context
 * \param addr [IN]             abstract network address of destination
 * \param id [IN]               registered function ID
 * \param handle [OUT]          pointer to HG handle
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Create(
        hg_context_t *context,
        hg_addr_t addr,
        hg_id_t id,
        hg_handle_t *handle
        );
*/

/**
 * Destroy HG handle. Decrement reference count, resources associated to the
 * handle are freed when the reference count is null.
 *
 * \param handle [IN]           HG handle
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Destroy(
        hg_handle_t handle
        );
*/

/**
 * Increment ref count on handle.
 *
 * \param handle [IN]           HG handle
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Ref_incr(
        hg_handle_t hg_handle
        );
*/

/**
 * Get info from handle.
 *
 * \remark Users must call HG_Addr_dup() to safely re-use the addr field.
 *
 * \param handle [IN]           HG handle
 *
 * \return Pointer to info or NULL in case of failure
 */
/* XXX
HG_EXPORT const struct hg_info *
HG_Get_info(
        hg_handle_t handle
        );
*/

/**
 * Get input from handle (requires registration of input proc to deserialize
 * parameters). Input must be freed using HG_Free_input().
 *
 * \remark This is equivalent to:
 *   - HG_Core_get_input()
 *   - Call hg_proc to deserialize parameters
 *
 * \param handle [IN]           HG handle
 * \param in_struct [IN/OUT]    pointer to input structure
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Get_input(
        hg_handle_t handle,
        void *in_struct
        );
*/

/**
 * Free resources allocated when deserializing the input.
 * User may copy parameters contained in the input structure before calling
 * HG_Free_input().
 *
 * \param handle [IN]           HG handle
 * \param in_struct [IN/OUT]    pointer to input structure
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Free_input(
        hg_handle_t handle,
        void *in_struct
        );
*/

/**
 * Get output from handle (requires registration of output proc to deserialize
 * parameters). Output must be freed using HG_Free_output().
 *
 * \remark This is equivalent to:
 *   - HG_Core_get_output()
 *   - Call hg_proc to deserialize parameters
 *
 *
 * \param handle [IN]           HG handle
 * \param out_struct [IN/OUT]   pointer to output structure
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Get_output(
        hg_handle_t handle,
        void *out_struct
        );
*/

/**
 * Free resources allocated when deserializing the output.
 * User may copy parameters contained in the output structure before calling
 * HG_Free_output().
 *
 * \param handle [IN]           HG handle
 * \param out_struct [IN/OUT]   pointer to input structure
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Free_output(
        hg_handle_t handle,
        void *out_struct
        );
*/
415

416 417
/**
 * Forward an RPC request to a remote host
Philip Carns's avatar
Philip Carns committed
418
 * @param [in] mid Margo instance
419 420 421 422 423
 * @param [in] handle identifier for the RPC to be sent
 * @param [in] in_struct input argument struct for RPC
 * @returns 0 on success, hg_return_t values on error
 */
hg_return_t margo_forward(
424
    margo_instance_id mid,
425 426 427
    hg_handle_t handle,
    void *in_struct);

428 429 430 431 432 433 434 435 436 437 438 439 440 441
/**
 * Forward an RPC request to a remote host with a user-defined timeout
 * @param [in] mid Margo instance
 * @param [in] handle identifier for the RPC to be sent
 * @param [in] in_struct input argument struct for RPC
 * @param [in] timeout_ms timeout in milliseconds
 * @returns 0 on success, hg_return_t values on error
 */
hg_return_t margo_forward_timed(
    margo_instance_id mid,
    hg_handle_t handle,
    void *in_struct,
    double timeout_ms);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/**
 * Send an RPC response, waiting for completion before returning
 * control to the calling ULT.
 * Note: this call is typically not needed as RPC listeners need not concern
 * themselves with what happens after an RPC finishes. However, there are cases
 * when this is useful (deferring resource cleanup, calling margo_finalize()
 * for e.g. a shutdown RPC).
 * @param [in] mid Margo instance
 * @param [in] handle identifier for the RPC for which a response is being
 * sent
 * @param [in] out_struct output argument struct for response
 * @return HG_SUCCESS on success, hg_return_t values on error. See HG_Respond.
 */
hg_return_t margo_respond(
    margo_instance_id mid,
    hg_handle_t handle,
    void *out_struct);

460 461 462 463 464 465 466 467 468 469 470 471 472 473
/**
 * Cancel an ongoing operation.
 *
 * \param handle [IN]           HG handle
 *
 * \return HG_SUCCESS or corresponding HG error code
 */
/* XXX
HG_EXPORT hg_return_t
HG_Cancel(
        hg_handle_t handle
        );
*/

474 475
/** 
 * Perform a bulk transfer
Philip Carns's avatar
Philip Carns committed
476
 * @param [in] mid Margo instance
477 478 479 480 481 482 483 484 485 486
 * @param [in] op type of operation to perform
 * @param [in] origin_addr remote Mercury address
 * @param [in] origin_handle remote Mercury bulk memory handle
 * @param [in] origin_offset offset into remote bulk memory to access
 * @param [in] local_handle local bulk memory handle
 * @param [in] local_offset offset into local bulk memory to access
 * @param [in] size size (in bytes) of transfer
 * @returns 0 on success, hg_return_t values on error
 */
hg_return_t margo_bulk_transfer(
487
    margo_instance_id mid,
488
    hg_bulk_op_t op,
489
    hg_addr_t origin_addr,
490 491 492 493 494 495
    hg_bulk_t origin_handle,
    size_t origin_offset,
    hg_bulk_t local_handle,
    size_t local_offset,
    size_t size);

496 497
/* XXX BULK */

498
/**
499 500 501
 * Retrieve the abt_handler pool that was associated with the instance at 
 *    initialization time
 * @param [in] mid Margo instance
502
 */
503
ABT_pool* margo_get_handler_pool(margo_instance_id mid);
504

505
/**
506 507
 * Retrieve the Mercury context that was associated with this instance at
 *    initialization time
508
 * @param [in] mid Margo instance
509
 * @return the Mercury context used in margo_init
510
 */
511
hg_context_t* margo_get_context(margo_instance_id mid);
512

513 514 515
/**
 * Retrieve the Mercury class that was associated with this instance at
 *    initialization time
516
 * @param [in] mid Margo instance
517
 * @return the Mercury class used in margo_init
518
 */
519
hg_class_t* margo_get_class(margo_instance_id mid);
520

521 522 523 524 525 526 527 528 529 530 531
/**
 * Get the margo_instance_id from a received RPC handle.
 *
 * \param [in] h          RPC handle
 * 
 * \return Margo instance
 */
margo_instance_id margo_hg_handle_get_instance(hg_handle_t h);

/**
 * Suspends the calling ULT for a specified time duration
Philip Carns's avatar
Philip Carns committed
532
 * @param [in] mid Margo instance
533
 * @param [in] timeout_ms timeout duration in milliseconds
Philip Carns's avatar
Philip Carns committed
534
 */
535 536 537
void margo_thread_sleep(
    margo_instance_id mid,
    double timeout_ms);
Philip Carns's avatar
Philip Carns committed
538

Philip Carns's avatar
Philip Carns committed
539 540 541 542 543 544 545 546 547
/**
 * Maps an RPC id and mplex id to the pool that it should execute on
 * @param [in] mid Margo instance
 * @param [in] id Mercury RPC identifier
 * @param [in] mplex_id multiplexing identifier
 * @param [out] pool Argobots pool the handler will execute in
 */
int margo_lookup_mplex(margo_instance_id mid, hg_id_t id, uint32_t mplex_id, ABT_pool *pool);

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

/**
 * macro that registers a function as an RPC.
 */
#define MARGO_REGISTER(__mid, __func_name, __in_t, __out_t, __handler, __rpc_id_ptr) do { \
    hg_return_t __hret; \
    hg_id_t __id; \
    hg_bool_t __flag; \
    int __ret; \
    __hret = HG_Registered_name(margo_get_class(__mid), __func_name, &__id, &__flag); \
    assert(__hret == HG_SUCCESS); \
    if(!__flag) \
        __id = HG_Register_name(margo_get_class(__mid), __func_name,\
                   BOOST_PP_CAT(hg_proc_, __in_t),\
                   BOOST_PP_CAT(hg_proc_, __out_t),\
                   __handler##_handler); \
    __ret = margo_register(__mid, __id); \
    assert(__ret == 0); \
    if(__rpc_id_ptr != MARGO_RPC_ID_IGNORE) { \
        *(__rpc_id_ptr) = __id; \
    } \
} while(0)

#define MARGO_REGISTER_MPLEX(__mid, __func_name, __in_t, __out_t, __handler, __mplex_id, __pool, __rpc_id_ptr) do { \
572 573 574 575 576 577 578
    hg_return_t __hret; \
    hg_id_t __id; \
    hg_bool_t __flag; \
    int __ret; \
    __hret = HG_Registered_name(margo_get_class(__mid), __func_name, &__id, &__flag); \
    assert(__hret == HG_SUCCESS); \
    if(!__flag) \
579 580 581 582
        __id = HG_Register_name(margo_get_class(__mid), __func_name,\
                   BOOST_PP_CAT(hg_proc_, __in_t),\
                   BOOST_PP_CAT(hg_proc_, __out_t),\
                   __handler##_handler); \
583
    __ret = margo_register_mplex(__mid, __id, __mplex_id, __pool); \
Philip Carns's avatar
typo  
Philip Carns committed
584
    assert(__ret == 0); \
585 586 587
    if(__rpc_id_ptr != MARGO_RPC_ID_IGNORE) { \
        *(__rpc_id_ptr) = __id; \
    } \
588 589
} while(0)

590 591
#define NULL_handler NULL

592
/**
Philip Carns's avatar
Philip Carns committed
593
 * macro that defines a function to glue an RPC handler to a ult handler
594 595
 * @param [in] __name name of handler function
 */
596
#define DEFINE_MARGO_RPC_HANDLER(__name) \
597
hg_return_t __name##_handler(hg_handle_t handle) { \
598
    int __ret; \
Philip Carns's avatar
Philip Carns committed
599
    ABT_pool __pool; \
600
    margo_instance_id __mid; \
Shane Snyder's avatar
Shane Snyder committed
601
    const struct hg_info *__hgi; \
602
    __hgi = HG_Get_info(handle); \
603 604 605 606
	__mid = margo_hg_handle_get_instance(handle); \
    if(__mid == MARGO_INSTANCE_NULL) { \
        return(HG_OTHER_ERROR); \
    } \
607
    __ret = margo_lookup_mplex(__mid, __hgi->id, __hgi->target_id, (&__pool)); \
Philip Carns's avatar
Philip Carns committed
608 609 610 611
    if(__ret != 0) { \
        return(HG_INVALID_PARAM); \
    }\
    __ret = ABT_thread_create(__pool, (void (*)(void *))__name, handle, ABT_THREAD_ATTR_NULL, NULL); \
612 613 614 615 616 617
    if(__ret != 0) { \
        return(HG_NOMEM_ERROR); \
    } \
    return(HG_SUCCESS); \
}

Philip Carns's avatar
Philip Carns committed
618 619
/**
 * macro that declares the prototype for a function to glue an RPC 
Philip Carns's avatar
Philip Carns committed
620
 * handler to a ult
Philip Carns's avatar
Philip Carns committed
621 622
 * @param [in] __name name of handler function
 */
623
#define DECLARE_MARGO_RPC_HANDLER(__name) hg_return_t __name##_handler(hg_handle_t handle);
624

625 626 627 628
#ifdef __cplusplus
}
#endif

629
#endif /* __MARGO */