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

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <abt.h>
Philip Carns's avatar
Philip Carns committed
11 12
#include <abt-snoozer.h>
#include <margo.h>
13 14 15

#include "my-rpc.h"

16 17
ABT_eventual* shutdown_eventual;

18 19 20 21 22 23 24 25
/* example server program.  Starts HG engine, registers the example RPC type,
 * and then executes indefinitely.
 */

int main(int argc, char **argv) 
{
    int ret;
    ABT_eventual eventual;
26
    margo_instance_id mid;
27 28 29 30
    ABT_xstream handler_xstream;
    ABT_pool handler_pool;
    ABT_xstream progress_xstream;
    ABT_pool progress_pool;
31 32 33 34
    na_class_t *network_class;
    na_context_t *na_context;
    hg_context_t *hg_context;
    hg_class_t *hg_class;
35
    int single_pool_mode = 0;
36
    
37 38
    shutdown_eventual = &eventual;

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    if(argc > 2)
    {
        fprintf(stderr, "Usage: ./server <single>\n");
        fprintf(stderr, "   Note: the optional \"single\" argument makes the server use a single ABT pool for both HG progress and RPC handlers.\n");
        return(-1);
    }

    if(argc == 2)
    {
        if(strcmp(argv[1], "single") == 0)
            single_pool_mode = 1;
        else
        {
            fprintf(stderr, "Usage: ./server <single>\n");
            fprintf(stderr, "   Note: the optional \"single\" argument makes the server use a single ABT pool for both HG progress and RPC handlers.\n");
            return(-1);
        }
    }

58
    /* boilerplate HG initialization steps */
59
    /***************************************/
60 61 62 63 64 65 66 67 68 69 70 71 72
    network_class = NA_Initialize("tcp://localhost:1234", NA_TRUE);
    if(!network_class)
    {
        fprintf(stderr, "Error: NA_Initialize()\n");
        return(-1);
    }
    na_context = NA_Context_create(network_class);
    if(!na_context)
    {
        fprintf(stderr, "Error: NA_Context_create()\n");
        NA_Finalize(network_class);
        return(-1);
    }
73
    hg_class = HG_Init(network_class, na_context);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    if(!hg_class)
    {
        fprintf(stderr, "Error: HG_Init()\n");
        NA_Context_destroy(network_class, na_context);
        NA_Finalize(network_class);
        return(-1);
    }
    hg_context = HG_Context_create(hg_class);
    if(!hg_context)
    {
        fprintf(stderr, "Error: HG_Context_create()\n");
        HG_Finalize(hg_class);
        NA_Context_destroy(network_class, na_context);
        NA_Finalize(network_class);
        return(-1);
    }

    /* set up argobots */
92
    /***************************************/
93 94 95 96 97 98 99
    ret = ABT_init(argc, argv);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_init()\n");
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
100 101
    /* set primary ES to idle without polling */
    ret = ABT_snoozer_xstream_self_set();
102 103
    if(ret != 0)
    {
Philip Carns's avatar
Philip Carns committed
104
        fprintf(stderr, "Error: ABT_snoozer_xstream_self_set()\n");
105 106 107
        return(-1);
    }

108 109 110 111 112 113 114 115 116 117 118 119 120 121
    /* Find primary pool to use for running rpc handlers */
    ret = ABT_xstream_self(&handler_xstream);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_xstream_self()\n");
        return(-1);
    }
    ret = ABT_xstream_get_main_pools(handler_xstream, 1, &handler_pool);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_xstream_get_main_pools()\n");
        return(-1);
    }

122
    if(!single_pool_mode)
123
    {
124 125 126 127 128 129 130
        /* create a dedicated ES drive Mercury progress */
        ret = ABT_snoozer_xstream_create(1, &progress_pool, &progress_xstream);
        if(ret != 0)
        {
            fprintf(stderr, "Error: ABT_snoozer_xstream_create()\n");
            return(-1);
        }
131 132
    }

133
    /* actually start margo */
134 135 136 137 138
    /* provide argobots pools for driving communication progress and
     * executing rpc handlers as well as class and context for Mercury
     * communication.
     */
    /***************************************/
139 140 141 142
    if(single_pool_mode)
        mid = margo_init(handler_pool, handler_pool, hg_context, hg_class);
    else
        mid = margo_init(progress_pool, handler_pool, hg_context, hg_class);
143
    assert(mid);
144 145

    /* register RPC */
146 147
    MERCURY_REGISTER(hg_class, "my_rpc", my_rpc_in_t, my_rpc_out_t, 
        my_rpc_ult_handler);
148 149
    MERCURY_REGISTER(hg_class, "my_shutdown_rpc", void, void, 
        my_rpc_shutdown_ult_handler);
150 151

    /* suspend this ULT until someone tells us to shut down */
152
    ret = ABT_eventual_create(0, &eventual);
153 154 155 156 157 158
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_eventual_create()\n");
        return(-1);
    }

159
    /* wait for shutdown (assume that margo will be finalized by an RPC) */
160
    ABT_eventual_wait(eventual, NULL);
161

162 163 164 165 166
    if(!single_pool_mode)
    {
        ABT_xstream_join(progress_xstream);
        ABT_xstream_free(&progress_xstream);
    }
167

168 169
    ABT_finalize();

170 171 172 173 174
    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);
    NA_Context_destroy(network_class, na_context);
    NA_Finalize(network_class);

175 176 177
    return(0);
}