Commit cb3aed49 authored by Jonathan Jenkins's avatar Jonathan Jenkins

clean up implementation and add tests

parent 73b5dc42
...@@ -33,69 +33,137 @@ struct jobmap_list { ...@@ -33,69 +33,137 @@ struct jobmap_list {
int **lp_arrays; int **lp_arrays;
}; };
static int jobmap_list_configure(void const * params, void ** ctx) #define COND_REALLOC(_len_expr, _cap_var, _buf_var) \
do { \
if ((_len_expr) == _cap_var) { \
_buf_var = realloc(_buf_var, (_cap_var)*2*sizeof(*(_buf_var))); \
assert(_buf_var); \
_cap_var *= 2; \
} \
} while (0)
static int parse_line(
FILE *f,
char **line_buf,
int *line_cap,
int *num_ranks,
int **rank_list)
{ {
struct codes_jobmap_params_list const * p = params; char * buf = *line_buf;
struct jobmap_list *lst = malloc(sizeof(*lst));
assert(lst); int cap = *line_cap;
int pos = 0;
FILE *alloc_file_name = fopen(p->alloc_file, "r"); *num_ranks = 0;
if(!alloc_file_name) {
ERR("Could not open file %s\n ", p->alloc_file); for (int c = fgetc(f); c != EOF && c != '\n'; c = fgetc(f)) {
buf[pos++] = (char)c;
COND_REALLOC(pos, cap, buf);
} }
else{ if (ferror(f)) {
lst->num_jobs = 0; *num_ranks = -1;
while(!feof(alloc_file_name)) { goto end;
char ch = (char)fgetc(alloc_file_name); }
if(ch == '\n') else {
lst->num_jobs++;//how many jobs buf[pos]='\0';
}
} }
lst->num_rank_job = malloc(sizeof(lst->num_rank_job)*lst->num_jobs); int list_cap = 8;
assert(lst->num_rank_job); int *lst = malloc(list_cap * sizeof(*lst));
for(int i=0; i<lst->num_jobs; i++) assert(lst);
lst->num_rank_job[i]=0; int rank;
rewind(alloc_file_name); *num_ranks = 0;
int job_id = 0; for (char * tok = strtok(buf, " \t\r"); tok != NULL;
while(!feof(alloc_file_name)) { tok = strtok(NULL, " \t\r")) {
char ch = (char)fgetc(alloc_file_name); int rc = sscanf(tok, "%d", &rank);
if(ch == '\n'){ if (rc != 1) {
job_id++;//how many jobs fprintf(stderr,
continue; "jobmap-list: unable to read alloc file - bad rank (%s)\n",
} tok);
if(ch == ' '){ *num_ranks = -1;
lst->num_rank_job[job_id]++;//how many ranks in each job break;
} }
COND_REALLOC(*num_ranks, list_cap, lst);
lst[*num_ranks] = rank;
*num_ranks += 1;
} }
lst->lp_arrays = (int **)malloc(sizeof(int *)*lst->num_jobs); if (*num_ranks <= 0) {
for(int i=0; i<lst->num_jobs; i++){ *rank_list = NULL;
lst->lp_arrays[i] = (int *)malloc(sizeof(int)*lst->num_rank_job[i]); free(lst);
} }
else
*rank_list = lst;
end:
*line_buf = buf;
*line_cap = cap;
return (*num_ranks < 0) ? -1 : 0;
}
rewind(alloc_file_name); static int jobmap_list_configure(void const * params, void ** ctx)
for(int i=0; i < lst->num_jobs; i++) { {
for(int j=0; j < lst->num_rank_job[i]; j++) { struct codes_jobmap_params_list const * p = params;
fscanf(alloc_file_name, "%d", &lst->lp_arrays[i][j]); struct jobmap_list *lst = malloc(sizeof(*lst));
} assert(lst);
FILE *f = fopen(p->alloc_file, "r");
if(!f) {
ERR("Could not open file %s", p->alloc_file);
} }
fclose(alloc_file_name); // job storage
*ctx = lst; lst->num_jobs = 0;
int job_cap = 8;
lst->num_rank_job = calloc(job_cap, sizeof(*lst->num_rank_job));
assert(lst->num_rank_job);
lst->lp_arrays = calloc(job_cap, sizeof(*lst->lp_arrays));
assert(lst->lp_arrays);
// line storage
int line_cap = 1<<10;
char *line_buf = malloc(line_cap);
assert(line_buf);
int rc = 0;
do {
rc = parse_line(f, &line_buf, &line_cap,
&lst->num_rank_job[lst->num_jobs],
&lst->lp_arrays[lst->num_jobs]);
if (rc == -1) {
// error and exit
if (ferror(f)) {
perror("fgets");
break;
}
}
else if (lst->num_rank_job[lst->num_jobs] > 0) {
lst->num_jobs++;
}
// resize if needed
if (!feof(f) && lst->num_jobs == job_cap) {
int tmp = job_cap;
COND_REALLOC(lst->num_jobs, tmp, lst->num_rank_job);
COND_REALLOC(lst->num_jobs, job_cap, lst->lp_arrays);
}
} while (!feof(f));
dprintf("read %d jobs\n", lst->num_jobs); if (rc == 0) {
for(int i=0; i < lst->num_jobs; i++) { fclose(f);
dprintf(" job %d contains %d ranks, LP list is:\n ", free(line_buf);
i, lst->num_rank_job[i]); *ctx = lst;
for(int j=0; j < lst->num_rank_job[i]; j++) { return 0;
dprintf(" %d,", lst->lp_arrays[i][j]); }
else {
for (int i = 0; i < job_cap; i++) {
free(lst->lp_arrays[i]);
} }
dprintf("\n==========\n"); free(lst->lp_arrays);
free(lst->num_rank_job);
free(lst);
*ctx = NULL;
return -1;
} }
return 0;
} }
static struct codes_jobmap_id jobmap_list_to_local(int id, void const * ctx) static struct codes_jobmap_id jobmap_list_to_local(int id, void const * ctx)
......
...@@ -14,7 +14,7 @@ TESTS += tests/lp-io-test.sh \ ...@@ -14,7 +14,7 @@ TESTS += tests/lp-io-test.sh \
tests/lsm-test.sh \ tests/lsm-test.sh \
tests/rc-stack-test \ tests/rc-stack-test \
tests/resource-test.sh \ tests/resource-test.sh \
tests/jobmap-test tests/jobmap-test.sh
EXTRA_DIST += tests/lp-io-test.sh \ EXTRA_DIST += tests/lp-io-test.sh \
tests/workload/codes-workload-test.sh \ tests/workload/codes-workload-test.sh \
...@@ -23,6 +23,8 @@ EXTRA_DIST += tests/lp-io-test.sh \ ...@@ -23,6 +23,8 @@ EXTRA_DIST += tests/lp-io-test.sh \
tests/mapping_test.sh \ tests/mapping_test.sh \
tests/lsm-test.sh \ tests/lsm-test.sh \
tests/resource-test.sh \ tests/resource-test.sh \
tests/jobmap-test.sh \
tests/conf/jobmap-test-list.conf \
tests/conf/buffer_test.conf \ tests/conf/buffer_test.conf \
tests/conf/lsm-test.conf \ tests/conf/lsm-test.conf \
tests/conf/mapping_test.conf \ tests/conf/mapping_test.conf \
......
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14
15 16
17
18
19
20
21
22
...@@ -14,6 +14,43 @@ ...@@ -14,6 +14,43 @@
return 1; \ return 1; \
} while(0) } while(0)
/* 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;
}
static int test_jobmap_dummy(int num_jobs) static int test_jobmap_dummy(int num_jobs)
{ {
struct codes_jobmap_ctx *c; struct codes_jobmap_ctx *c;
...@@ -67,9 +104,12 @@ static int test_jobmap_dummy(int num_jobs) ...@@ -67,9 +104,12 @@ static int test_jobmap_dummy(int num_jobs)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2)
ERR("usage: jobmap-test <jobmap-list alloc file>");
int rc; int rc;
rc = test_jobmap_dummy(10); rc = test_jobmap_dummy(10);
if (rc) return rc; if (rc) return rc;
rc = test_jobmap_list(argv[1]);
if (rc) return rc;
return 0; return 0;
} }
#!/bin/sh
if [[ -z $srcdir ]] ; then
echo srcdir variable not set
exit 1
fi
tests/jobmap-test $srcdir/tests/conf/jobmap-test-list.conf
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment