Index: src/plugins/task/affinity/dist_tasks.c
===================================================================
RCS file: /cvsroot/slurm/slurm/src/plugins/task/affinity/dist_tasks.c,v
retrieving revision 1.1.1.27
diff -u -r1.1.1.27 dist_tasks.c
--- src/plugins/task/affinity/dist_tasks.c	29 Jul 2011 15:50:48 -0000	1.1.1.27
+++ src/plugins/task/affinity/dist_tasks.c	12 Sep 2011 20:18:34 -0000
@@ -340,6 +340,7 @@
 		      "nodes:%d sockets:%d:%d cores:%d:%d threads:%d",
 		      max_tasks, whole_nodes, whole_sockets ,part_sockets,
 		      whole_cores, part_cores, whole_threads);
+
 		if ((max_tasks == whole_sockets) && (part_sockets == 0)) {
 			req->cpu_bind_type |= CPU_BIND_TO_SOCKETS;
 			goto make_auto;
@@ -672,10 +673,11 @@
 	}
 }
 
-/* helper function for _expand_masks() 
- * foreach task, consider which other tasks have set bits on the same socket */
+/* helper function for _expand_masks()
+ * foreach task, consider which other bits are set in avail_map
+ * on the same socket */
 static void _blot_mask_sockets(const uint32_t maxtasks, const uint32_t task,
-			       bitstr_t **masks, uint16_t blot)
+			       bitstr_t **masks, uint16_t blot, bitstr_t *avail_map)
 {
         uint16_t i, j, size = 0;
         uint32_t q;
@@ -685,26 +687,22 @@
 
         size = bit_size(masks[task]);
         for (i = 0; i < size; i++) {
-                if (bit_test(masks[task], i)) {
-			/* check if other tasks have set bits on this socket */
-                        uint16_t start = (i / blot) * blot;
-                        for (j = start; j < start+blot; j++) {
-                                for (q = 0; q < maxtasks; q++) {
-                                        if ((q != task) &&
-					    bit_test(masks[q], j)) {
-						bit_set(masks[task], j);
-					}
-				}
-			}
-		}
-	}
+            if (bit_test(masks[task], i)) {
+			/* check if other bits are set in avail_map on this socket */
+                uint16_t start = (i / blot) * blot;
+                for (j = start; j < start+blot; j++) {
+                    if (bit_test(avail_map, j))
+				        bit_set(masks[task], j);
+			    }
+		    }
+	    }
 }
 
 /* foreach mask, expand the mask around the set bits to include the
  * complete resource to which the set bits are to be bound */
 static void _expand_masks(uint16_t cpu_bind_type, const uint32_t maxtasks,
 			  bitstr_t **masks, uint16_t hw_sockets,
-			  uint16_t hw_cores, uint16_t hw_threads)
+			  uint16_t hw_cores, uint16_t hw_threads, bitstr_t *avail_map)
 {
 	uint32_t i;
 
@@ -722,7 +720,8 @@
 		if (hw_threads*hw_cores < 2)
 			return;
 		for (i = 0; i < maxtasks; i++) {
-			_blot_mask_sockets(maxtasks, i, masks, hw_threads*hw_cores);
+   			_blot_mask_sockets(maxtasks, i, masks, hw_threads*hw_cores,
+   					           avail_map);
 		}
 		return;
 	}
@@ -801,12 +800,12 @@
 				break;
 		}
 	}
-	FREE_NULL_BITMAP(avail_map);
 
 	/* last step: expand the masks to bind each task
 	 * to the requested resource */
 	_expand_masks(req->cpu_bind_type, max_tasks, masks,
-			hw_sockets, hw_cores, hw_threads);
+			hw_sockets, hw_cores, hw_threads, avail_map);
+	FREE_NULL_BITMAP(avail_map);
 
 	return SLURM_SUCCESS;
 }
@@ -891,6 +890,7 @@
 								  block_map_size);
 					}
 					bit_set(masks[taskcount], bit);
+
 					if (++i < req->cpus_per_task)
 						continue;
 					i = 0;
@@ -904,12 +904,12 @@
 				break;
 		}
 	}
-	FREE_NULL_BITMAP(avail_map);
 
 	/* last step: expand the masks to bind each task
 	 * to the requested resource */
 	_expand_masks(req->cpu_bind_type, max_tasks, masks,
-			hw_sockets, hw_cores, hw_threads);
+			hw_sockets, hw_cores, hw_threads, avail_map);
+	FREE_NULL_BITMAP(avail_map);
 
 	return SLURM_SUCCESS;
 }
@@ -1048,12 +1048,12 @@
 	}
 
 	xfree(task_array);
-	FREE_NULL_BITMAP(avail_map);
 
 	/* last step: expand the masks to bind each task
 	 * to the requested resource */
 	_expand_masks(req->cpu_bind_type, max_tasks, masks,
-			hw_sockets, hw_cores, hw_threads);
+			hw_sockets, hw_cores, hw_threads, avail_map);
+	FREE_NULL_BITMAP(avail_map);
 
 	return SLURM_SUCCESS;
 }
