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

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
10 11
#include <stdlib.h>
#include <ctype.h>
12 13 14 15
#include <abt.h>
#include <abt-snoozer.h>
#include <margo.h>

16 17
static int use_abt_sleep = 0;
static int sleep_seconds = 2;
18
static margo_instance_id mid;
19 20 21 22 23 24 25 26 27 28 29 30 31 32

static void sleep_fn(void *arg);

int main(int argc, char **argv) 
{
    ABT_thread threads[4];
    int t_ids[4];
    int i;
    int ret;
    ABT_xstream xstream;
    ABT_pool pool;
    hg_context_t *hg_context;
    hg_class_t *hg_class;

33 34
    int arg_ndx = 1;
    if(argc > 1)
35
    {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        if(isdigit(argv[1][0]))
        {
            sleep_seconds = atoi(argv[1]);
            arg_ndx++;
        }
        if((arg_ndx < argc) && (strcmp(argv[arg_ndx], "ABT") == 0))
        {
            use_abt_sleep = 1;
            arg_ndx++;
        }
        if(arg_ndx != argc)
        {
            fprintf(stderr, "Usage: %s [sleep_seconds] [ABT]\n", argv[0]);
            fprintf(stderr, "\tsleep_seconds: number of seconds for each thread to sleep.\n");
            fprintf(stderr, "\tABT: use ABT sleep mechanism, rather than POSIX sleep.\n");
            return(-1);
        }
53 54 55 56 57 58 59
    }

    /* boilerplate HG initialization steps */
    /***************************************/
    hg_class = HG_Init("tcp://localhost:1234", HG_FALSE);
    if(!hg_class)
    {
60 61 62 63 64 65 66
        /* if tcp didn't work, try sm */
        hg_class = HG_Init("sm://13/13", HG_FALSE);
        if(!hg_class)
        {
            fprintf(stderr, "Error: HG_Init()\n");
            return(-1);
        }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    }
    hg_context = HG_Context_create(hg_class);
    if(!hg_context)
    {
        fprintf(stderr, "Error: HG_Context_create()\n");
        HG_Finalize(hg_class);
        return(-1);
    }

    /* set up argobots */
    /***************************************/
    ret = ABT_init(argc, argv);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_init()\n");
        return(-1);
    }

    /* set primary ES to idle without polling */
    ret = ABT_snoozer_xstream_self_set();
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_snoozer_xstream_self_set()\n");
        return(-1);
    }

    /* retrieve current pool to use for ULT creation */
    ret = ABT_xstream_self(&xstream);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_xstream_self()\n");
        return(-1);
    }
    ret = ABT_xstream_get_main_pools(xstream, 1, &pool);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_xstream_get_main_pools()\n");
        return(-1);
    }

    /* actually start margo */
108 109
    /* use a single pool for progress and sleeper threads */
    /* NOTE: we don't use RPC handlers, so no need for an RPC pool */
110
    /***************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
111
    mid = margo_init_pool(pool, ABT_POOL_NULL, hg_context);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 160 161
    for(i=0; i<4; i++)
    {
        t_ids[i] = i;

        /* start up the sleeper threads */
        ret = ABT_thread_create(pool, sleep_fn, &t_ids[i],
            ABT_THREAD_ATTR_NULL, &threads[i]);
        if(ret != 0)
        {
            fprintf(stderr, "Error: ABT_thread_create()\n");
            return(-1);
        }

    }

    /* yield to one of the threads */
    ABT_thread_yield_to(threads[0]);

    for(i=0; i<4; i++)
    {
        ret = ABT_thread_join(threads[i]);
        if(ret != 0)
        {
            fprintf(stderr, "Error: ABT_thread_join()\n");
            return(-1);
        }
        ret = ABT_thread_free(&threads[i]);
        if(ret != 0)
        {
            fprintf(stderr, "Error: ABT_thread_join()\n");
            return(-1);
        }
    }

    /* shut down everything */
    margo_finalize(mid);
    
    ABT_finalize();

    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);

    return(0);
}

static void sleep_fn(void *arg)
{
    int my_tid = *(int *)arg;

    if(use_abt_sleep)
162
        margo_thread_sleep(mid, sleep_seconds*1000.0);
163
    else
164
        sleep(sleep_seconds);
165 166 167 168 169

    printf("TID: %d sleep end\n", my_tid);

    return;
}