// iterator over this cpu subset using lpt whole cell algorithm
// assuming a specified complexity for mitral and granule

cxmitral = 1675
cxgranule = 258

objref lptiter_gidvec, pc

iterator cell_gids() { local i
	if (object_id(lptiter_gidvec) == 0) { lptiter_set() }
	for i=0, lptiter_gidvec.size-1 {
		$&1 = lptiter_gidvec.x[i]
		$&2 = i
		iterator_statement
	}
}

proc lptiter_set() {local i  localobj ldbal, cx, p
	cx = new Vector(ncell)
	cx.fill(cxmitral, nmitral_begin, ngranule_begin-1)
	cx.fill(cxgranule, ngranule_begin, ncell-1)
	p = lptiter_lpt(cx, pc.nhost, 0)
	lptiter_gidvec = p.c.indvwhere("==", pc.id)
	lptiter_gidvec.add(nmitral_begin)
	//for i=0, lptiter_gidvec.size-1 { printf("%d %d\n", pc.id, lptiter_gidvec.x[i])}
}

// from loadbal.hoc
// least processing time algorithm
// $o1 is vector of weights  $2 is number of partitions
// return is vector of partition indices parallel to weights
obfunc lptiter_lpt() {local i, j  localobj wx, ix, pw
	if ($3) {
		print $o1.size, " piece weights"
		$o1.printf
	}
	wx = $o1.sortindex.reverse
	ix = new Vector($o1.size)
	pw = new Vector($2)
	for i=0, $o1.size-1 {
		j = wx.x[i]
		w = $o1.x[j]
		ip = pw.min_ind
		pw.x[ip] += w
		ix.x[j] = ip
	}
	if ($3) {
		print $2, " partition complexities"
		pw.printf
	}
	if (pw.mean) {
		thread_cxbal_ = pw.max/(pw.mean)
	}else{
		thread_cxbal_ = 1
	}
	return ix
}