GitLab maintenance scheduled for Today, 2019-09-24, from 12:00 to 13:00 CT - Services will be unavailable during this time.

mapping_test.c 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#include <ross.h>
#include "codes/configuration.h"
#include "codes/codes_mapping.h"
#include "codes/codes.h"

/* define three types of lps for mapping test */

14
typedef struct state_s{
15
    int id_global, id_by_group, id_by_anno, id_by_group_anno;
16 17 18 19 20 21 22 23 24 25 26 27 28
    char group_name[MAX_NAME_LENGTH], lp_name[MAX_NAME_LENGTH],
         anno[MAX_NAME_LENGTH];
} state;

static void init(state *ns, tw_lp *lp){
    int dummy;
    // only need the names
    codes_mapping_get_lp_info(lp->gid, ns->group_name, &dummy,
            ns->lp_name, &dummy, ns->anno, &dummy, &dummy);
    const char * anno = codes_mapping_get_annotation_by_lpid(lp->gid);
    // annotation check
    if (    (anno == NULL && ns->anno[0] != '\0') ||
            (anno != NULL && strcmp(anno, ns->anno) != 0)){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
29
        fprintf(stderr, "LP %llu: annotations don't match: "
30
                "_get_lp_info:\"%s\", _get_anno_by_lpid:\"%s\"\n",
Jonathan Jenkins's avatar
Jonathan Jenkins committed
31
                LLU(lp->gid), ns->anno, anno);
32
    }
33

34 35 36 37
    ns->id_global = codes_mapping_get_lp_relative_id(lp->gid, 0, 0);
    ns->id_by_group = codes_mapping_get_lp_relative_id(lp->gid, 1, 0);
    ns->id_by_anno = codes_mapping_get_lp_relative_id(lp->gid, 0, 1);
    ns->id_by_group_anno = codes_mapping_get_lp_relative_id(lp->gid, 1, 1);
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52
    // relative ID check - looking up the gid works correctly
    tw_lpid id_from_global_rel =
        codes_mapping_get_lpid_from_relative(ns->id_global, NULL,
                ns->lp_name, NULL, 0);
    tw_lpid id_from_group_rel =
        codes_mapping_get_lpid_from_relative(ns->id_by_group, ns->group_name,
                ns->lp_name, NULL, 0);
    tw_lpid id_from_anno_rel =
        codes_mapping_get_lpid_from_relative(ns->id_by_anno, NULL,
                ns->lp_name, anno, 1);
    tw_lpid id_from_group_anno_rel =
        codes_mapping_get_lpid_from_relative(ns->id_by_group_anno, ns->group_name,
                ns->lp_name, anno, 1);
    if (lp->gid != id_from_global_rel){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
53 54 55
        fprintf(stderr, "LP %llu (%s): "
                "global relative id %d doesn't match (got %llu)\n",
                LLU(lp->gid), ns->lp_name, ns->id_global, LLU(id_from_global_rel));
56 57
    }
    if (lp->gid != id_from_group_rel){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
58 59 60 61
        fprintf(stderr, "LP %llu (%s): "
                "group %s relative id %d doesn't match (got %llu)\n",
                LLU(lp->gid), ns->lp_name, ns->group_name, ns->id_by_group,
                LLU(id_from_group_rel));
62 63
    }
    if (lp->gid != id_from_anno_rel){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
64 65 66 67
        fprintf(stderr, "LP %llu (%s): "
                "anno \"%s\" relative id %d doesn't match (got %llu)\n",
                LLU(lp->gid), ns->lp_name, ns->anno, ns->id_by_group,
                LLU(id_from_anno_rel));
68 69
    }
    if (lp->gid != id_from_group_anno_rel){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
70 71 72 73
        fprintf(stderr, "LP %llu (%s): "
                "group %s anno \"%s\" relative id %d doesn't match (got %llu)\n",
                LLU(lp->gid), ns->lp_name, ns->group_name, ns->anno, ns->id_by_group,
                LLU(id_from_group_anno_rel));
74 75 76 77 78
    }


    // output-based check - print out IDs, compare against expected
    char tmp[128];
Jonathan Jenkins's avatar
Jonathan Jenkins committed
79
    if (ns->anno[0]=='\0')
80
        tmp[0] = '\0';
81
    else
82
        sprintf(tmp, "@%s", ns->anno);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
83
    printf("TEST2 %2llu %2d %2d %2d %2d %s%s\n", LLU(lp->gid), ns->id_global,
84 85
            ns->id_by_group, ns->id_by_anno, ns->id_by_group_anno, ns->lp_name,
            tmp);
86 87 88
}

tw_lptype a_lp = {
89 90 91 92
    (init_f) init,
    (pre_run_f) NULL,
    (event_f) NULL,
    (revent_f) NULL,
93
    (commit_f) NULL,
94 95 96
    (final_f)  NULL,
    (map_f) codes_mapping,
    sizeof(state),
97 98
};
tw_lptype b_lp = {
99 100 101 102
    (init_f) init,
    (pre_run_f) NULL,
    (event_f) NULL,
    (revent_f) NULL,
103
    (commit_f) NULL,
104 105 106
    (final_f) NULL,
    (map_f) codes_mapping,
    sizeof(state),
107 108
};
tw_lptype c_lp = {
109 110 111 112
    (init_f) init,
    (pre_run_f) NULL,
    (event_f) NULL,
    (revent_f) NULL,
113
    (commit_f) NULL,
114 115 116
    (final_f) NULL,
    (map_f) codes_mapping,
    sizeof(state),
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 162 163 164 165 166 167 168 169 170 171 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
#define TEST_TO_NM(_expected_nm, _input, _fn_type) \
    do { \
        char const * _anm = \
                codes_mapping_get_ ##_fn_type ##_name_by_cid(_input); \
        char const * _enm = \
                (_expected_nm) == NULL ? "" : (_expected_nm); \
        if ((_expected_nm) == NULL && _anm != NULL) { \
            fprintf(stderr, "%s:%d: " \
                    " cid->name check error (expected NULL, got %s)\n", \
                    __FILE__, __LINE__, _anm);\
            return 1; \
        } else if ((_expected_nm) != NULL && strcmp(_enm, _anm) != 0) { \
            fprintf(stderr, "%s:%d: " \
                    " cid->name check error (expected %s, got %s)\n", \
                    __FILE__, __LINE__, \
                    (_expected_nm) == NULL ? "null" : (_expected_nm), _anm);\
            return 1; \
        } \
    } while (0);

#define TEST_TO_CID(_expected_cid, _input, _name_type, _fn_type) \
    do { \
        int _ecid = (_expected_cid); \
        int _acid = \
            codes_mapping_get_ ##_fn_type ##_cid_by_ ##_name_type (_input); \
        if (_ecid != _acid) { \
            fprintf(stderr, "%s:%d: " #_name_type \
                    " name->cid check error (expected %d, got %d)\n", \
                    __FILE__, __LINE__, _ecid, _acid);\
            return 1; \
        } \
    } while (0);

#define TEST_TO_CID_NM(_expected_cid, _input, _fn_type) \
    TEST_TO_CID(_expected_cid, _input, name, _fn_type)
#define TEST_TO_CID_LP(_expected_cid, _input, _fn_type) \
    TEST_TO_CID(_expected_cid, _input, lpid, _fn_type)

static int test_cids()
{
    TEST_TO_CID_NM(0, "GRP1", group)
    TEST_TO_CID_NM(1, "GRP2", group)
    TEST_TO_NM("GRP1", 0, group)
    TEST_TO_NM("GRP2", 1, group)

    TEST_TO_CID_NM(0, "a", lp)
    TEST_TO_CID_NM(1, "b", lp)
    TEST_TO_CID_NM(2, "c", lp)
    TEST_TO_NM("a", 0, lp)
    TEST_TO_NM("b", 1, lp)
    TEST_TO_NM("c", 2, lp)

    TEST_TO_CID_NM(0, "foo", anno)
    TEST_TO_CID_NM(1, "bar", anno)
    TEST_TO_CID_NM(2, NULL, anno)
    TEST_TO_CID_NM(2, "", anno)
    TEST_TO_NM("foo", 0, anno)
    TEST_TO_NM("bar", 1, anno)
    TEST_TO_NM(NULL, 2, anno)

    TEST_TO_CID_LP(0, 0, group)
    TEST_TO_CID_LP(0, 1, group)
    TEST_TO_CID_LP(0, 9, group)
    TEST_TO_CID_LP(1, 10, group)
    TEST_TO_CID_LP(1, 11, group)
    TEST_TO_CID_LP(1, 24, group)

    TEST_TO_CID_LP(0, 0, lp)
    TEST_TO_CID_LP(1, 1, lp)
    TEST_TO_CID_LP(2, 9, lp)
    TEST_TO_CID_LP(2, 10, lp)
    TEST_TO_CID_LP(1, 11, lp)
    TEST_TO_CID_LP(0, 24, lp)

    TEST_TO_CID_LP(2, 0, anno)
    TEST_TO_CID_LP(2, 1, anno)
    TEST_TO_CID_LP(2, 9, anno)
    TEST_TO_CID_LP(0, 8, anno)
    TEST_TO_CID_LP(1, 10, anno)
    TEST_TO_CID_LP(0, 11, anno)
    TEST_TO_CID_LP(0, 24, anno)

    return 0;
}

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
static char conf_file_name[128] = {'\0'};
static const tw_optdef app_opt [] =
{
	TWOPT_GROUP("codes-mapping test case" ),
    TWOPT_CHAR("codes-config", conf_file_name, "name of codes configuration file"),
	TWOPT_END()
};

int main(int argc, char *argv[])
{
    tw_opt_add(app_opt);
    tw_init(&argc, &argv);

    if (!conf_file_name[0]){
        fprintf(stderr, "Expected \"codes-config\" option, please see --help.\n");
        MPI_Finalize();
        return 1;
    }
    if (configuration_load(conf_file_name, MPI_COMM_WORLD, &config)){
        fprintf(stderr, "Error loading config file %s.\n", conf_file_name);
        MPI_Finalize();
        return 1;
    }

    lp_type_register("a", &a_lp);
    lp_type_register("b", &b_lp);
    lp_type_register("c", &c_lp);

    codes_mapping_setup();

234 235
    printf("# test 2 format:\n"
           "# <lp> rel id <global <group-wise> <anno.-wise> <both> <lp name>\n");
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    const char * lps[]    = {"a", "b", "c"};
    const char * groups[] = {NULL, "GRP1", "GRP2"};
    const char * annos[]  = {NULL, "foo", "bar"};
    char lpnm[128];
    for (int l = 0; l < 3; l++){
        for (int g = 0; g < 3; g++){
            for (int a = 0; a < 3; a++){
                if (annos[a]==NULL)
                    sprintf(lpnm, "%s", lps[l]);
                else
                    sprintf(lpnm, "%s@%s", lps[l], annos[a]);
                printf("TEST1 %2d %6s %s\n",
                    codes_mapping_get_lp_count(groups[g], 0, lps[l],
                        annos[a], 0),
                    groups[g], lpnm);
            }
            printf("TEST1 %2d %6s %s ignore annos\n",
254
                codes_mapping_get_lp_count(groups[g], 0, lps[l], NULL, 2),
255 256 257
                groups[g], lps[l]);
        }
    }
258 259

    tw_run();
260 261 262

    int rc = test_cids();

263 264
    tw_end();

265
    return rc;
266 267 268 269 270 271 272 273 274 275
}

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */