jobmap-list.c 3.89 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
#define ERR(str, ...)\
    do{\
        fprintf(stderr, "ERROR at %s:%d: " str "\n", __FILE__, __LINE__, ##__VA_ARGS__);\
        return -1; \
    }while(0)
17
18
19
20
21
22
23

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

24
static int jobmap_list_configure(void const * params, void ** ctx)
25
26
{
    struct codes_jobmap_params_dumpi const * p = params;
27
28
29
30
31
32
    struct workload_params *wp = malloc(sizeof(*wp));
    assert(wp);

    FILE *alloc_file_name = fopen(p->alloc_file, "r");
    if(!alloc_file_name)
    {
33
        ERR( "Coudld not open file %s\n ", p->alloc_file);
34
35
36
37
38
39
40
41
42
43
    }
    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
        }
    }
44

45
46
47
48
49
    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;

50
51

    rewind(alloc_file_name);
52
    {
53
54
55
56
57
58
59
60
61
62
63
        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
            }
64
        }
65

66
    }
67

68
69
70
71
    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]);
    }
72

73
    rewind(alloc_file_name);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    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++)
        {
91
            printf("%d,", wp->lp_arrays[i][j]);
92
93
94
95
96
97
        }
        printf("\n==========\n");
    }

    return 0;
}
98

99
static struct codes_jobmap_id jobmap_list_to_local(int id, void const * ctx)
100
101
{
    struct codes_jobmap_id rtn;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    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;
            }
        }
119
    }
120

121
122
123
    return rtn;
}

124
static int jobmap_list_to_global(struct codes_jobmap_id id, void const * ctx)
125
126
{

127
128
129
130
    struct workload_params *wp = (struct workload_params*)ctx;

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

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

}


143
static void jobmap_list_destroy(void * ctx)
144
{
145
146
147
148
149
150
151
152
153
    struct workload_params *wp = (struct workload_params*)ctx;
    for(int i=0; i<wp->num_jobs; i++){
        free(wp->lp_arrays[i]);
    }

    free(wp->lp_arrays);

    free(wp->num_rank_job);

154
    free(ctx);
155
156
}

157

158
159
160
161
162
163
struct codes_jobmap_impl jobmap_list_impl = {
    jobmap_list_configure,
    jobmap_list_destroy,
    jobmap_list_to_local,
    jobmap_list_to_global,
    jobmap_list_get_num_jobs
164
165
166
167
168
169
170
171
172
173
174
};

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