objref idvec, spikevec
idvec = new Vector()
spikevec = new Vector()

iterator serialize_output() {local i
	if (pc.id == 0) {
		$o1.wopen()
		$o1.close()
	}
	pc.barrier()
	for i=0, pc.nhost-1 {
		if (i == pc.id) {
			$o1.aopen()
			iterator_statement
			$o1.close()
		}
		pc.barrier()
	}
}

proc want_all_spikes() {local i, gid  localobj mgr
	idvec.buffer_size(10000)
	spikevec.buffer_size(10000)
	for cell_gids(&gid, &i) if (gid < splitbit) {
		pc.spike_record(gid, spikevec, idvec)
	}
	for i=0, mgrs_list.count()-1 {
		mgr = mgrs_list.object(i)
		if (object_id(mgr.md)) { pc.spike_record(mgr.md_gid, spikevec, idvec) }
		if (object_id(mgr.gd)) { pc.spike_record(mgr.gd_gid, spikevec, idvec) }
	}
}

// replaced by version in spike2file.hoc
proc spike2file() { local i  localobj outf, s
	s = new String()
	//sprint(s.s, "out_%d_%d.dat", odor_idx, pnm.nhost)
	sprint(s.s, "out_%s_%d.dat", wbase.s, pnm.nhost)
        outf = new File(s.s)
	for serialize_output(outf) {
	        for i=0, idvec.size-1 {
        	        outf.printf("%.10g %d\n", spikevec.x[i], idvec.x[i])
		}
        }
}
{load_file("spike2file.hoc")} // replaces above (and different prototypw)

objref tdat_   
tdat_ = new Vector(7)
tdat_.x[5] = cxcpu // expected from lptiter.hoc
mindelay_ = 1e9
proc prun() {local told
	pc.setup_transfer()
        mindelay_ = pc.set_maxstep(10)
        runtime=startsw()
        tdat_.x[0] = pc.wait_time
        stdinit()
if (0) {
	if (0) {
		pc.psolve(tstop/2)
		savestate()
	}else{
		restorestate()
	}
}
//        pc.psolve(tstop)
	while(t < tstop) {
		told = t
		tnext = t + checkpoint_interval
		if (tnext > tstop) {
			tnext = tstop
		}
		if (pc.id == 0) printf("tnext = %g\n", tnext)
	        pc.psolve(tnext)
		// In case infinite loop due to round off error in fixed
		// step method.
		// May wish to use a dt which is a negative power of 2
		if (t == told) {
			if (pc.id == 0) {
printf("psolve did not advance time from t=%.20g to tnext=%.20g\n", t, tnext)
			}
			break
		}
		spike2file(spikevec, idvec, n_spkout_sort, n_spkout_files)
	}

        tdat_.x[0] = pc.wait_time - tdat_.x[0]
        runtime = startsw() - runtime
        tdat_.x[1] = pc.step_time
        tdat_.x[2] = pc.send_time
	tdat_.x[3] = pc.vtransfer_time(0) // for gaps
	tdat_.x[4] = pc.vtransfer_time(1) // for splitcells
//      printf("%d wtime %g\n", pc.id, waittime)
}

objref mxhist_
proc mkhist() {
	if (pc.id == 0) {
		mxhist_ = new Vector($1)
		pc.max_histogram(mxhist_)
	}
}
proc prhist() {local i, j
	if (pc.id == 0 && object_id(mxhist_)) {
		printf("histogram of #spikes vs #exchanges\n")
		j = 0
		for i=0, mxhist_.size-1 {
			if (mxhist_.x[i] != 0) { j = i }
		}
		for i = 0, j {
			printf("%d\t %d\n", i, mxhist_.x[i])
		}
		printf("end of histogram\n")
	}
}


func mindelay() {local i, md
	if (pc.nhost > 1) {
		pc.context("{pc.post(\"mindelay\", mindelay_)}")
		for i=1, pc.nhost-1 {
			pc.take("mindelay", &md)
			if (md < mindelay_) {
				mindelay_ = md
			}
		}		
	}
	return mindelay_ // see nc_append
}

objref tavg_stat, tmin_stat, tmax_stat, idmin_stat, idmax_stat
proc poststat() {
	pc.post("poststat", pc.id, tdat_)
}
proc getstat() {local i, j, id localobj tdat
	tdat = tdat_.c	tavg_stat = tdat_.c  tmin_stat = tdat_.c  tmax_stat = tdat_.c
	idmin_stat = tdat_.c.fill(0)  idmax_stat = tdat_.c.fill(0)
	if (pc.nhost > 1) {
		pc.context("poststat()\n")
		for i=0, pc.nhost-2 {
			pc.take("poststat", &id, tdat)
			tavg_stat.add(tdat)
			for j = 0, tdat_.size-1 {
				if (tdat.x[j] > tmax_stat.x[j]) {
					idmax_stat.x[j] = id
					tmax_stat.x[j] = tdat.x[j]
				}
				if (tdat.x[j] < tmin_stat.x[j]) {
					idmin_stat.x[j] = id
					tmin_stat.x[j] = tdat.x[j]
				}
			}
		}
	}
	tavg_stat.div(pc.nhost)
}

objref spstat_
proc postspstat() {local i
	spstat_ = new Vector()
	cvode.spike_stat(spstat_)
	i = spstat_.size
	spstat_.resize(spstat_.size + 4)
	spstat_.x[i] = pc.spike_statistics(&spstat_.x[i+1], &spstat_.x[i+2],\
		&spstat_.x[i+3])
	pc.post("postspstat", pc.id, spstat_)
}
proc print_spike_stat_info() {local i, j, id  localobj spstat, sum, min, max, idmin, idmax, label
	spstat = new Vector()
	spstat_ = new Vector()
	cvode.spike_stat(spstat_)
	i = spstat_.size
	spstat_.resize(spstat_.size + 4)
	spstat_.x[i] = pc.spike_statistics(&spstat_.x[i+1], &spstat_.x[i+2],\
		&spstat_.x[i+3])
	sum = spstat_.c
	min = spstat_.c
	max = spstat_.c
	idmin = spstat_.c.fill(0)
	idmax = spstat_.c.fill(0)
	if (pc.nhost > 1) {
		pc.context("postspstat()\n")
		for i=0, pc.nhost-2 {
			pc.take("postspstat", &id, spstat)
			sum.add(spstat)
			for j=0, spstat.size-1 {
				if (spstat.x[j] > max.x[j]) {
					idmax.x[j] = id
					max.x[j] = spstat.x[j]
				}
				if (spstat.x[j] < min.x[j]) {
					idmin.x[j] = id
					min.x[j] = spstat.x[j]
				}
			}
		}
	}
	label = new List()
	label.append(new String("eqn"))
	label.append(new String("NetCon"))
	label.append(new String("deliver"))
	label.append(new String("NC deliv"))
	label.append(new String("PS send"))
	label.append(new String("S deliv"))
	label.append(new String("S send"))
	label.append(new String("S move"))
	label.append(new String("Q insert"))
	label.append(new String("Q move"))
	label.append(new String("Q remove"))
	label.append(new String("max sent"))
	label.append(new String("sent"))
	label.append(new String("received"))
	label.append(new String("used"))

	printf("%10s %13s %10s %10s    %5s   %5s\n",\
		"", "total", "min", "max", "idmin", "idmax")
	for i=0, spstat_.size-1 {
		printf("%-10s %13.0lf %10d %10d    %5d   %5d\n",\
label.object(i).s, sum.x[i], min.x[i], max.x[i], idmin.x[i], idmax.x[i])
	}

	printf("\n%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",\
		"setup", "run", "avgspkxfr", "avgcomp", "avgx2q", "avgvxfr", "avgsplit", "avgcx", "avgactcx")
	printf("%-10.4g %-10.4g", setuptime, runtime)
	for i=0, tdat_.size-1 { printf(" %-10.4g", tavg_stat.x[i]) }

	printf("\n\n%5s %-15s %-15s %-15s %-15s %-15s %-15s %-15s\n", \
		"", "id   spkxfr", "id   com", "id   x2q", "id   vxfr", "id   split", "id   cx", "id   actcx")
	printf("%-5s", "min")
	for i=0, tdat_.size-1 { printf(" %-4d %-10.4g", idmin_stat.x[i], tmin_stat.x[i]) }
	printf("\n%-5s", "max")
	for i=0, tdat_.size-1 { printf(" %-4d %-10.4g", idmax_stat.x[i], tmax_stat.x[i]) }
	printf("\n")
}

proc savestate() {local i  localobj s, ss, f, rl
	s = new String()
	sprint(s.s, "svst.%04d", pnm.myid)
	f = new File(s.s)
	ss = new SaveState()
	ss.save()
	ss.fwrite(f, 0)

	rl = new List("Random")
	f.printf("Random %d\n", rl.count)
	for i=0, rl.count-1 {
		f.printf("%d\n", rl.object(i).seq())
	}
	f.close
}

proc restorestate() {local i  localobj s, ss, f, rl
	s = new String()
	sprint(s.s, "svst.%04d", pnm.myid)
	f = new File(s.s)
	ss = new SaveState()
	ss.fread(f, 0)
	rl = new List("Random")
	if (f.scanvar() != rl.count) {
		execerror("Random count unexpected", "")
	}
	for i=0, rl.count-1 {
		rl.object(i).seq(f.scanvar())
	}
	f.close
	ss.restore()
}