jobmap-local-global-mapping.c 3.87 KB
Newer Older
1 2 3 4 5
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */
6
#include <string.h>
7 8 9 10 11
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "src/util/codes-jobmap-method-impl.h"

12 13 14 15 16 17 18

struct workload_params {
    int num_jobs;
    int *num_rank_job;
    int **lp_arrays;
};

19 20 21
static int jobmap_dumpi_configure(void const * params, void ** ctx)
{
    struct codes_jobmap_params_dumpi const * p = params;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    struct workload_params *wp = malloc(sizeof(*wp));
    assert(wp);

    /*open first time, count No. lines, each line is the LP list for a job*/
    FILE *alloc_file_name = fopen(p->alloc_file, "r");
    if(!alloc_file_name)
    {
        printf("Coudld not open file %s ======\n ", p->alloc_file);
        exit(1);
    }
    else{
        wp->num_jobs = 0;
        while(!feof(alloc_file_name))
        {
            char ch = (char)fgetc(alloc_file_name);
            if(ch == '\n')
                wp->num_jobs++;//how many jobs
        }
        fclose(alloc_file_name);
    }
42 43


44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    wp->num_rank_job = malloc(sizeof(wp->num_rank_job)*wp->num_jobs);
    assert(wp->num_rank_job);
    for(int i=0; i<wp->num_jobs; i++)
        wp->num_rank_job[i]=0;

    /*open second time, read No. of LP id in each line, No. of LP id equals to No. of rank in each job*/
    alloc_file_name = fopen(p->alloc_file, "r");
    int job_id = 0;
    while(!feof(alloc_file_name))
    {
        char ch = (char)fgetc(alloc_file_name);
        if(ch == '\n'){
            job_id++;//how many jobs
            continue;
        }
        if(ch == ' '){
            wp->num_rank_job[job_id]++;//how many ranks in each job
        }
    }
    fclose(alloc_file_name);
64

65 66 67 68 69
    /*open third time, read in each LP id for each rank in every job*/
    wp->lp_arrays = (int **)malloc(sizeof(int *)*wp->num_jobs);
    for(int i=0; i<wp->num_jobs; i++){
        wp->lp_arrays[i] = (int *)malloc(sizeof(int)*wp->num_rank_job[i]);
    }
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
    alloc_file_name = fopen(p->alloc_file, "r");
    for(int i=0; i < wp->num_jobs; i++)
    {
        for(int j=0; j < wp->num_rank_job[i]; j++)
        {
            fscanf(alloc_file_name, "%d", &wp->lp_arrays[i][j]);
        }
    }

    fclose(alloc_file_name);
    *ctx = wp;

    printf("There are %d Jobs\n", wp->num_jobs);
    for(int i=0; i < wp->num_jobs; i++)
    {
        printf("\nIn Job %d, there are %d ranks, LP list is:\n", i, wp->num_rank_job[i]);
        for(int j=0; j < wp->num_rank_job[i]; j++)
        {
            printf(",%d,", wp->lp_arrays[i][j]);
        }
        printf("\n==========\n");
    }

    return 0;
}
96 97 98 99

static struct codes_jobmap_id jobmap_dumpi_to_local(int id, void const * ctx)
{
    struct codes_jobmap_id rtn;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    struct workload_params *wp = (struct workload_params*)ctx;

    for(int i=0; i<wp->num_jobs; i++)
    {
        for(int j=0; j < wp->num_rank_job[i]; j++)
        {
            if(id == wp->lp_arrays[i][j])
            {
                rtn.job = i;
                rtn.rank = j;
                return rtn;
            }
            else{
                rtn.job = -1;
                rtn.rank = -1;
            }
        }
117
    }
118

119 120 121 122 123 124
    return rtn;
}

static int jobmap_dumpi_to_global(struct codes_jobmap_id id, void const * ctx)
{

125 126 127 128
    struct workload_params *wp = (struct workload_params*)ctx;

    if (id.job < wp->num_jobs)
        return wp->lp_arrays[id.job][id.rank];
129 130 131 132 133 134
    else
        return -1;
}

int jobmap_dumpi_get_num_jobs(void const * ctx)
{
135 136 137 138 139 140 141 142 143
    struct workload_params *wp = (struct workload_params*)ctx;
    return wp->num_jobs;

}


static void jobmap_dumpi_destroy(void * ctx)
{
    free(ctx);
144 145
}

146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
struct codes_jobmap_impl jobmap_dumpi_impl = {
    jobmap_dumpi_configure,
    jobmap_dumpi_destroy,
    jobmap_dumpi_to_local,
    jobmap_dumpi_to_global,
    jobmap_dumpi_get_num_jobs
};

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