From e592d2026c7c295d97dff8be780d78795a72b785 Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Wed, 13 Aug 2003 19:04:38 +0000
Subject: [PATCH] Select nodes for a job step in a contiguous fashion if
 possible.

---
 src/srun/job.c | 40 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)

diff --git a/src/srun/job.c b/src/srun/job.c
index de8e644f510..6c66cd4dc8c 100644
--- a/src/srun/job.c
+++ b/src/srun/job.c
@@ -611,12 +611,46 @@ int    job_resp_hack_for_step(resource_allocation_response_msg_t *resp)
 static int 
 _job_resp_add_nodes(bitstr_t *req_bitmap, bitstr_t *exc_bitmap, int node_cnt)
 {
-	int inx;
+	int inx, offset;
 	int total = bit_set_count(req_bitmap);
 	int max_nodes = MAX(opt.min_nodes, opt.max_nodes);
 
-	for (inx=0; ((inx<node_cnt) && (total<max_nodes)); inx++) {
-		if (bit_test(exc_bitmap, inx) || bit_test(req_bitmap, inx))
+	/* work up from first allocated node to first excluded node */
+	offset = bit_ffs(req_bitmap);
+	if (offset == -1)	/* no specific required nodes)
+		offset = 0;	/* start at beginning */
+	for (inx=offset; inx<node_cnt; inx++) {
+		if (total >= max_nodes) 
+			break;
+		if (bit_test(exc_bitmap, inx))
+			break;
+		if (bit_test(req_bitmap, inx))
+			continue;
+		bit_set(req_bitmap, inx);
+		total++;
+	}
+
+	/* then work down from first allocated node to first excluded node */
+	for (inx=offset; inx>=0; inx--) {
+		if (total >= max_nodes) 
+			break;
+		if (bit_test(exc_bitmap, inx))
+			break;
+		if (bit_test(req_bitmap, inx))
+			continue;
+		bit_set(req_bitmap, inx);
+		total++;
+	}
+	if (opt.contiguous)
+		return total;
+
+	/* then get everything else */
+	for (inx=0; inx<node_cnt; inx++) {
+		if (total >= max_nodes) 
+			break;
+		if (bit_test(exc_bitmap, inx))
+			continue;
+		if (bit_test(req_bitmap, inx))
 			continue;
 		bit_set(req_bitmap, inx);
 		total++;
-- 
GitLab