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

#include <stdio.h>

#include "codes/codes-jobmap.h"

#define ERR(str, ...) \
    do { \
Jonathan Jenkins's avatar
Jonathan Jenkins committed
13
        fprintf(stderr, "ERROR at %s:%d: " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
14 15 16
        return 1; \
    } while(0)

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static int test_jobmap_identity(int num_ranks)
{
    struct codes_jobmap_ctx *c;
    struct codes_jobmap_params_identity p;

    c = codes_jobmap_configure(CODES_JOBMAP_IDENTITY, &p);
    if (!c) ERR("jobmap-identity: configure failure");

    int num_jobs = codes_jobmap_get_num_jobs(c);
    if (1 != num_jobs)
        ERR("jobmap-identity: expected exactly 1 job, got %d\n", num_jobs);

    struct codes_jobmap_id lid;
    int gid;
    for (int i = 0; i < num_ranks; i++) {
        lid.job = -1; lid.rank = -1;
        lid = codes_jobmap_to_local_id(i, c);
        if (lid.job != 0 || lid.rank != i)
            ERR("jobmap-identity: expected lid (%d,%d), got (%d,%d) for gid %d",
                    0,i, lid.job,lid.rank, i);
        gid = codes_jobmap_to_global_id(lid, c);
        if (gid != i)
            ERR("jobmap-identity: expected gid %d, got %d for lid (%d,%d)",
                    i, gid, lid.job, lid.rank);
    }
    return 0;
}
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* THIS TEST IS HARDCODED AGAINST jobmap-test-list.conf */
static int test_jobmap_list(char * fname)
{
    struct codes_jobmap_ctx *c;
    struct codes_jobmap_params_list p;
    p.alloc_file = fname;

    c = codes_jobmap_configure(CODES_JOBMAP_LIST, &p);
    if (!c) ERR("jobmap-list: configure failure");

    int rank_count_per_job[] = {10, 5, 2, 1, 1, 1, 1, 1, 1};

    int num_jobs = codes_jobmap_get_num_jobs(c);
    if (num_jobs != 9)
        ERR("jobmap-list: expected %d jobs, got %d", 9, num_jobs);

    int gid, gid_expected = 0;
    struct codes_jobmap_id lid_expected, lid;
    for (int i = 0; i < num_jobs; i++) {
        for (int j = 0; j < rank_count_per_job[i]; j++) {
            lid_expected.job = i;
            lid_expected.rank = j;
            gid = codes_jobmap_to_global_id(lid_expected, c);
            if (gid != gid_expected)
                ERR("jobmap-list: expected gid %d for lid (%d,%d), got %d",
                        gid_expected, lid_expected.job, lid_expected.rank, gid);
            lid = codes_jobmap_to_local_id(gid_expected, c);
            if (lid.job != lid_expected.job || lid.rank != lid_expected.rank)
                ERR("jobmap-list: expected lid (%d,%d) for gid %d, got (%d,%d)",
                        lid_expected.job, lid_expected.rank, gid_expected,
                        lid.job, lid.rank);
            gid_expected++;
        }
    }
    codes_jobmap_destroy(c);
    return 0;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
81 82 83 84 85 86
static int test_jobmap_dummy(int num_jobs)
{
    struct codes_jobmap_ctx *c;
    struct codes_jobmap_params_dummy p;
    p.num_jobs = num_jobs;

87 88
    /* initialize */
    c = codes_jobmap_configure(CODES_JOBMAP_DUMMY, &p);
89
    if (!c) ERR("jobmap configure failure");
90

Jonathan Jenkins's avatar
Jonathan Jenkins committed
91 92 93 94 95 96
    struct codes_jobmap_id lid;

    /* successful local lookups */
    for (int i = 0; i < num_jobs; i++) {
        lid = codes_jobmap_to_local_id(i, c);
        if (lid.job != i || lid.rank != 0)
97
            ERR("lookup failure for %d: expected (%d,%d), got (%d,%d)",
Jonathan Jenkins's avatar
Jonathan Jenkins committed
98
                    i, i, 0, lid.job, lid.rank);
99
        else {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
100 101
            lid.job = -1;
            lid.rank = -1;
102
        }
103
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    /* bad local lookup */
    lid = codes_jobmap_to_local_id(num_jobs, c);
    if (lid.job != -1 || lid.rank != -1)
        ERR("lookup expected failure for %d: expected (%d,%d), got (%d,%d)",
                num_jobs, -1,-1, lid.job,lid.rank);

    /* successful global lookups */
    int gid;
    lid.rank = 0;
    for (lid.job = 0; lid.job < num_jobs; lid.job++) {
        gid = codes_jobmap_to_global_id(lid, c);
        if (gid != lid.job)
            ERR("lookup failure for (%d,%d): expected %d, got %d",
                    lid.job, lid.rank, lid.job, gid);
    }
    /* bad global lookup */
    lid.job = num_jobs;
    gid = codes_jobmap_to_global_id(lid, c);
    if (gid != -1)
        ERR("lookup expected failure for (%d,0): expected -1, got %d",
                num_jobs, gid);
125 126 127 128

    /* cleanup */
    codes_jobmap_destroy(c);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
129 130 131 132 133
    return 0;
}

int main(int argc, char *argv[])
{
134 135
    if (argc != 2)
        ERR("usage: jobmap-test <jobmap-list alloc file>");
Jonathan Jenkins's avatar
Jonathan Jenkins committed
136 137 138
    int rc;
    rc = test_jobmap_dummy(10);
    if (rc) return rc;
139 140
    rc = test_jobmap_identity(10);
    if (rc) return rc;
141 142
    rc = test_jobmap_list(argv[1]);
    if (rc) return rc;
143 144
    return 0;
}