codes-mapping-context.c 7.59 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 61
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);
}

62 63 64 65 66 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
static struct codes_mctx set_group_ratio_common(
        enum codes_mctx_type type,
        char const * annotation,
        bool ignore_annotations)
{
    struct codes_mctx rtn;
    rtn.type = type;
    if (ignore_annotations)
        rtn.u.group_ratio.anno.cid = -1;
    else
        rtn.u.group_ratio.anno.cid =
            codes_mapping_get_anno_cid_by_name(annotation);
    return rtn;
}

struct codes_mctx codes_mctx_set_group_ratio(
        char const * annotation,
        bool ignore_annotations)
{
    return set_group_ratio_common(CODES_MCTX_GROUP_RATIO, annotation,
            ignore_annotations);
}

struct codes_mctx codes_mctx_set_group_ratio_reverse(
        char const * annotation,
        bool ignore_annotations)
{
    return set_group_ratio_common(CODES_MCTX_GROUP_RATIO_REVERSE, annotation,
            ignore_annotations);
}

93 94 95 96 97 98 99 100
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;
101 102 103 104 105
    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);
106 107 108 109 110 111 112 113
    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,
114
        tw_lpid sender_gid)
115 116 117 118 119 120
{
    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;
121 122 123 124
        case CODES_MCTX_GROUP_RATIO:
        case CODES_MCTX_GROUP_RATIO_REVERSE:
            anno = &ctx->u.group_ratio.anno;
            break;
125
        case CODES_MCTX_GROUP_MODULO:
126
        case CODES_MCTX_GROUP_MODULO_REVERSE:
127 128 129 130 131 132 133 134 135
            anno = &ctx->u.group_modulo.anno;
            break;
        case CODES_MCTX_GROUP_DIRECT:
            anno = &ctx->u.group_direct.anno;
            break;
        default:
            assert(0);
    }

136 137 138
    char const *sender_group;
    char const *sender_lpname;
    int rep_id, offset;
139 140

    // get sender info
141 142
    codes_mapping_get_lp_info2(sender_gid, &sender_group, &sender_lpname, NULL,
            &rep_id, &offset);
143

144 145 146 147 148 149
    char const * anno_str;
    if (anno->cid < 0)
        anno_str = NULL;
    else
        anno_str = codes_mapping_get_anno_name_by_cid(anno->cid);

150
    int dest_offset;
151 152 153 154 155 156 157 158
    int is_group_modulo = (ctx->type == CODES_MCTX_GROUP_MODULO ||
            ctx->type == CODES_MCTX_GROUP_MODULO_REVERSE);
    int is_group_ratio = (ctx->type == CODES_MCTX_GROUP_RATIO ||
            ctx->type == CODES_MCTX_GROUP_RATIO_REVERSE);
    int is_group_reverse = (ctx->type == CODES_MCTX_GROUP_MODULO_REVERSE ||
            ctx->type == CODES_MCTX_GROUP_RATIO_REVERSE);

    if (is_group_modulo || is_group_ratio) {
159
        int num_dest_lps = codes_mapping_get_lp_count(sender_group, 1,
160
                dest_lp_name, anno_str, anno->cid == -1);
161 162 163 164
        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",
165
                    dest_lp_name, sender_group, sender_gid,
166 167
                    anno->cid == -1 ? "ignored" :
                    codes_mapping_get_anno_name_by_cid(anno->cid));
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182
        if (is_group_modulo)
            dest_offset = offset % num_dest_lps;
        else {
            int num_src_lps = codes_mapping_get_lp_count(sender_group, 1,
                    sender_lpname, NULL, 1);
            if (num_src_lps <= num_dest_lps)
                dest_offset = offset;
            else {
                dest_offset = offset * num_dest_lps / num_src_lps;
                if (dest_offset >= num_dest_lps)
                    dest_offset = num_dest_lps-1;
            }
        }
        if (is_group_reverse)
183
            dest_offset = num_dest_lps - 1 - dest_offset;
184 185 186 187 188 189 190 191
    }
    else if (ctx->type == CODES_MCTX_GROUP_DIRECT) {
        dest_offset = ctx->u.group_direct.offset;
    }
    else
        assert(0);

    tw_lpid rtn;
192 193
    codes_mapping_get_lp_id(sender_group, dest_lp_name, anno_str,
            anno->cid == -1, rep_id, dest_offset, &rtn);
194 195 196
    return rtn;
}

197 198 199 200 201 202 203 204
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);
205 206 207 208 209 210 211 212
        case CODES_MCTX_GROUP_RATIO:
        case CODES_MCTX_GROUP_RATIO_REVERSE:
            // if not ignoring the annotation, just return what's in the
            // context
            if (ctx->u.group_modulo.anno.cid >= 0)
                return codes_mapping_get_anno_name_by_cid(
                        ctx->u.group_modulo.anno.cid);
            break;
213
        case CODES_MCTX_GROUP_MODULO:
214
        case CODES_MCTX_GROUP_MODULO_REVERSE:
215 216
            // if not ignoring the annotation, just return what's in the
            // context
217 218 219
            if (ctx->u.group_modulo.anno.cid >= 0)
                return codes_mapping_get_anno_name_by_cid(
                        ctx->u.group_modulo.anno.cid);
220 221
            break;
        case CODES_MCTX_GROUP_DIRECT:
222 223 224
            if (ctx->u.group_direct.anno.cid >= 0)
                return codes_mapping_get_anno_name_by_cid(
                        ctx->u.group_direct.anno.cid);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            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);
}

242 243 244 245 246 247 248 249 250
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  indent-tabs-mode: nil
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */