codes-mapping-context.c 5.52 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#include <codes/codes-mapping-context.h>
#include <codes/codes_mapping.h>

10
static struct codes_mctx const CODES_MCTX_DEFAULT_VAL = {
11 12 13 14
    .type = CODES_MCTX_GROUP_MODULO,
    .u = {
        .group_modulo = {
            .anno = {
15
                .cid = -1,
16 17 18 19 20
            }
        }
    }
};

21 22
struct codes_mctx const * const CODES_MCTX_DEFAULT = &CODES_MCTX_DEFAULT_VAL;

23 24 25 26 27 28 29
struct codes_mctx codes_mctx_set_global_direct(tw_lpid lpid)
{
    struct codes_mctx rtn;
    rtn.type = CODES_MCTX_GLOBAL_DIRECT;
    rtn.u.global_direct.lpid = lpid;
    return rtn;
}
30 31 32

static struct codes_mctx set_group_modulo_common(
        enum codes_mctx_type type,
33 34 35 36
        char const * annotation,
        bool ignore_annotations)
{
    struct codes_mctx rtn;
37
    rtn.type = type;
38 39 40 41 42
    if (ignore_annotations)
        rtn.u.group_modulo.anno.cid = -1;
    else
        rtn.u.group_modulo.anno.cid =
            codes_mapping_get_anno_cid_by_name(annotation);
43 44
    return rtn;
}
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
struct codes_mctx codes_mctx_set_group_modulo(
        char const * annotation,
        bool ignore_annotations)
{
    return set_group_modulo_common(CODES_MCTX_GROUP_MODULO, annotation,
            ignore_annotations);
}

struct codes_mctx codes_mctx_set_group_modulo_reverse(
        char const * annotation,
        bool ignore_annotations)
{
    return set_group_modulo_common(CODES_MCTX_GROUP_MODULO_REVERSE, annotation,
            ignore_annotations);
}

61 62 63 64 65 66 67 68
struct codes_mctx codes_mctx_set_group_direct(
        int offset,
        char const * annotation,
        bool ignore_annotations)
{
    struct codes_mctx rtn;
    rtn.type = CODES_MCTX_GROUP_DIRECT;
    rtn.u.group_direct.offset = offset;
69 70 71 72 73
    if (ignore_annotations)
        rtn.u.group_direct.anno.cid = -1;
    else
        rtn.u.group_direct.anno.cid =
            codes_mapping_get_anno_cid_by_name(annotation);
74 75 76 77 78 79 80 81
    return rtn;
}

/* helper function to do a codes mapping - this function is subject to change
 * based on what types of ctx exist */
tw_lpid codes_mctx_to_lpid(
        struct codes_mctx const * ctx,
        char const * dest_lp_name,
82
        tw_lpid sender_gid)
83 84 85 86 87 88 89
{
    struct codes_mctx_annotation const *anno;
    // short circuit for direct mappings
    switch (ctx->type) {
        case CODES_MCTX_GLOBAL_DIRECT:
            return ctx->u.global_direct.lpid;
        case CODES_MCTX_GROUP_MODULO:
90
        case CODES_MCTX_GROUP_MODULO_REVERSE:
91 92 93 94 95 96 97 98 99 100 101 102 103
            anno = &ctx->u.group_modulo.anno;
            break;
        case CODES_MCTX_GROUP_DIRECT:
            anno = &ctx->u.group_direct.anno;
            break;
        default:
            assert(0);
    }

    char sender_group[MAX_NAME_LENGTH];
    int unused, rep_id, offset;

    // get sender info
104
    codes_mapping_get_lp_info(sender_gid, sender_group, &unused, NULL, &unused,
105 106
            NULL, &rep_id, &offset);

107 108 109 110 111 112
    char const * anno_str;
    if (anno->cid < 0)
        anno_str = NULL;
    else
        anno_str = codes_mapping_get_anno_name_by_cid(anno->cid);

113
    int dest_offset;
114 115
    if (ctx->type == CODES_MCTX_GROUP_MODULO ||
            ctx->type == CODES_MCTX_GROUP_MODULO_REVERSE) {
116
        int num_dest_lps = codes_mapping_get_lp_count(sender_group, 1,
117
                dest_lp_name, anno_str, anno->cid == -1);
118 119 120 121
        if (num_dest_lps == 0)
            tw_error(TW_LOC,
                    "ERROR: Found no LPs of type %s in group %s "
                    "(source lpid %lu) with annotation: %s\n",
122
                    dest_lp_name, sender_group, sender_gid,
123 124
                    anno->cid == -1 ? "ignored" :
                    codes_mapping_get_anno_name_by_cid(anno->cid));
125 126

        dest_offset = offset % num_dest_lps;
127 128
        if (ctx->type == CODES_MCTX_GROUP_MODULO_REVERSE)
            dest_offset = num_dest_lps - 1 - dest_offset;
129 130 131 132 133 134 135 136
    }
    else if (ctx->type == CODES_MCTX_GROUP_DIRECT) {
        dest_offset = ctx->u.group_direct.offset;
    }
    else
        assert(0);

    tw_lpid rtn;
137 138
    codes_mapping_get_lp_id(sender_group, dest_lp_name, anno_str,
            anno->cid == -1, rep_id, dest_offset, &rtn);
139 140 141
    return rtn;
}

142 143 144 145 146 147 148 149 150
char const * codes_mctx_get_annotation(
        struct codes_mctx const *ctx,
        char const * dest_lp_name,
        tw_lpid sender_id)
{
    switch(ctx->type) {
        case CODES_MCTX_GLOBAL_DIRECT:
            return codes_mapping_get_annotation_by_lpid(sender_id);
        case CODES_MCTX_GROUP_MODULO:
151
        case CODES_MCTX_GROUP_MODULO_REVERSE:
152 153
            // if not ignoring the annotation, just return what's in the
            // context
154 155 156
            if (ctx->u.group_modulo.anno.cid >= 0)
                return codes_mapping_get_anno_name_by_cid(
                        ctx->u.group_modulo.anno.cid);
157 158
            break;
        case CODES_MCTX_GROUP_DIRECT:
159 160 161
            if (ctx->u.group_direct.anno.cid >= 0)
                return codes_mapping_get_anno_name_by_cid(
                        ctx->u.group_direct.anno.cid);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
            break;
        default:
            tw_error(TW_LOC, "unrecognized or uninitialized context type: %d",
                    ctx->type);
            return NULL;
    }
    // at this point, we must be a group-wise mapping ignoring annotations

    char group[MAX_NAME_LENGTH];
    int dummy;
    // only need the group name
    codes_mapping_get_lp_info(sender_id, group, &dummy, NULL, &dummy, NULL,
            &dummy, &dummy);

    return codes_mapping_get_annotation_by_name(group, dest_lp_name);
}

179 180 181 182 183 184 185 186 187
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  indent-tabs-mode: nil
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */