From ed06372a815d73f60d345927fd40fe30eccc3a37 Mon Sep 17 00:00:00 2001
From: Morris Jette <jette@schedmd.com>
Date: Wed, 1 Feb 2012 14:51:51 -0800
Subject: [PATCH] Set CUDA_VISIBLE_DEVICES based upon device file names cached

Based upon work by Nicolas Bigaouette
---
 src/plugins/gres/gpu/gres_gpu.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/plugins/gres/gpu/gres_gpu.c b/src/plugins/gres/gpu/gres_gpu.c
index 31e1aa52441..b0b514697b0 100644
--- a/src/plugins/gres/gpu/gres_gpu.c
+++ b/src/plugins/gres/gpu/gres_gpu.c
@@ -109,7 +109,7 @@ const uint32_t	plugin_version		= 100;
 
 static char	gres_name[]		= "gpu";
 
-static int *gpu_devices;
+static int *gpu_devices = NULL;
 static int nb_available_files;
 
 /*
@@ -199,7 +199,10 @@ extern void job_set_env(char ***job_env_ptr, void *gres_ptr)
 				dev_list = xmalloc(128);
 			else
 				xstrcat(dev_list, ",");
-			xstrfmtcat(dev_list, "%d", i);
+			if (gpu_devices && (gpu_devices[i] >= 0))
+				xstrfmtcat(dev_list, "%d", gpu_devices[i]);
+			else
+				xstrfmtcat(dev_list, "%d", i);
 		}
 	}
 	if (dev_list) {
@@ -236,7 +239,10 @@ extern void step_set_env(char ***job_env_ptr, void *gres_ptr)
 				dev_list = xmalloc(128);
 			else
 				xstrcat(dev_list, ",");
-			xstrfmtcat(dev_list, "%d", i);
+			if (gpu_devices && (gpu_devices[i] >= 0))
+				xstrfmtcat(dev_list, "%d", gpu_devices[i]);
+			else
+				xstrfmtcat(dev_list, "%d", i);
 		}
 	}
 	if (dev_list) {
-- 
GitLab