GitLab maintenance scheduled for Today, 2019-12-05, from 17:00 to 18:00 CT - Services will be unavailable during this time.

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 {
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;
struct jobmap_list *lst = malloc(sizeof(*lst));
assert(lst);
char * buf = *line_buf;
int cap = *line_cap;
int pos = 0;
FILE *alloc_file_name = fopen(p->alloc_file, "r");
if(!alloc_file_name) {
ERR("Could not open file %s\n ", p->alloc_file);
*num_ranks = 0;
for (int c = fgetc(f); c != EOF && c != '\n'; c = fgetc(f)) {
buf[pos++] = (char)c;
COND_REALLOC(pos, cap, buf);
}
else{
lst->num_jobs = 0;
while(!feof(alloc_file_name)) {
char ch = (char)fgetc(alloc_file_name);
if(ch == '\n')
lst->num_jobs++;//how many jobs
}
if (ferror(f)) {
*num_ranks = -1;
goto end;
}
else {
buf[pos]='\0';
}
lst->num_rank_job = malloc(sizeof(lst->num_rank_job)*lst->num_jobs);
assert(lst->num_rank_job);
for(int i=0; i<lst->num_jobs; i++)
lst->num_rank_job[i]=0;
rewind(alloc_file_name);
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 == ' '){
lst->num_rank_job[job_id]++;//how many ranks in each job
int list_cap = 8;
int *lst = malloc(list_cap * sizeof(*lst));
assert(lst);
int rank;
*num_ranks = 0;
for (char * tok = strtok(buf, " \t\r"); tok != NULL;
tok = strtok(NULL, " \t\r")) {
int rc = sscanf(tok, "%d", &rank);
if (rc != 1) {
fprintf(stderr,
"jobmap-list: unable to read alloc file - bad rank (%s)\n",
tok);
*num_ranks = -1;
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);
for(int i=0; i<lst->num_jobs; i++){
lst->lp_arrays[i] = (int *)malloc(sizeof(int)*lst->num_rank_job[i]);
if (*num_ranks <= 0) {
*rank_list = NULL;
free(lst);
}
else
*rank_list = lst;
end:
*line_buf = buf;
*line_cap = cap;
return (*num_ranks < 0) ? -1 : 0;
}
rewind(alloc_file_name);
for(int i=0; i < lst->num_jobs; i++) {
for(int j=0; j < lst->num_rank_job[i]; j++) {
fscanf(alloc_file_name, "%d", &lst->lp_arrays[i][j]);
}
static int jobmap_list_configure(void const * params, void ** ctx)
{
struct codes_jobmap_params_list const * p = params;
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);
*ctx = lst;
// job storage
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);
for(int i=0; i < lst->num_jobs; i++) {
dprintf(" job %d contains %d ranks, LP list is:\n ",
i, lst->num_rank_job[i]);
for(int j=0; j < lst->num_rank_job[i]; j++) {
dprintf(" %d,", lst->lp_arrays[i][j]);
if (rc == 0) {
fclose(f);
free(line_buf);
*ctx = lst;
return 0;
}
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)
......
......@@ -14,7 +14,7 @@ TESTS += tests/lp-io-test.sh \
tests/lsm-test.sh \
tests/rc-stack-test \
tests/resource-test.sh \
tests/jobmap-test
tests/jobmap-test.sh
EXTRA_DIST += tests/lp-io-test.sh \
tests/workload/codes-workload-test.sh \
......@@ -23,6 +23,8 @@ EXTRA_DIST += tests/lp-io-test.sh \
tests/mapping_test.sh \
tests/lsm-test.sh \
tests/resource-test.sh \
tests/jobmap-test.sh \
tests/conf/jobmap-test-list.conf \
tests/conf/buffer_test.conf \
tests/conf/lsm-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 @@
return 1; \
} 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)
{
struct codes_jobmap_ctx *c;
......@@ -67,9 +104,12 @@ static int test_jobmap_dummy(int num_jobs)
int main(int argc, char *argv[])
{
if (argc != 2)
ERR("usage: jobmap-test <jobmap-list alloc file>");
int rc;
rc = test_jobmap_dummy(10);
if (rc) return rc;
rc = test_jobmap_list(argv[1]);
if (rc) return rc;
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