#include <v_lib.h>
#include "config.h"
#include "phasesp.h"
#include "ctrl.h"
#include "psys.h"

#ifdef		PS_PARALLEL

volatile int spinlock_init;
volatile int thread_num = 0;
volatile int all_jobs_done = 0;
volatile int ready_count;

void
apply_binary_forces_partial(particle_system_p ps,int *ptr, int *sentinel);

void V_CALL
ps_thread_proc(void *arg)
{
	int thread_idx = thread_num++;
	int *istart, *sentinel;
	particle_system_p ps = (particle_system_p)arg;
	istart = ps->ij_start[thread_idx];
	sentinel = ps->ij_start[thread_idx + 1];
	conPrintf("Thread %i initialized (sentinel - istart == %i).\n",
		thread_idx, (sentinel - istart) >> 1);
	spinlock_init = 0;
do_job:;
/*
	printf("Thread %i: waiting for job.\n", thread_idx);
*/
	ready_count++;
	condWait(ps->job_ready);
	if(all_jobs_done)
	{
		conPrintf("Thread %i: exiting...\n", thread_idx);
		threadExit(0);
	}
	/*
	 * do the job
	 */
/*
	for(int *ptr = istart;ptr < sentinel;ptr+=2)
	{
		printf("(%i,%i);",ptr[0],ptr[1]);
	}
*/
	apply_binary_forces_partial(ps,istart,sentinel);
/*
	printf("JOB finished for thread %i.\n", thread_idx);
*/
	goto do_job;
}

void do_job_parallel(particle_system_p ps)
{
/*
	puts("do_job_parallel(): waiting for completion of all threads");
*/
	while(ready_count < ps->num_slices);
	ready_count = 0;
/*
	puts("do_job_parallel(): starting threads.");
*/
	condPulse(ps->job_ready);
	while(ready_count < ps->num_slices);
/*
	puts("do_job_parallel(): all threads idle.");
*/
}

#endif	/* PS_PARALLEL */

particle_system_p
alloc_particle_system(u32_t num_particles)
{
	particle_system_p ps = NULL;
	ps = memAllocZ(sizeof(particle_system_t));

	ps->num_particles = num_particles;
	ps->particle_radius = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_density = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_mass = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_reciprocal_mass = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->particle_6_pi_eta_r = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t));
	ps->phase = 
		(vec_p)memAllocZ(num_particles * sizeof(vec_t) * PHASESPACE_DIM);
	ps->force = 
		(vec3_t*)memAllocZ(num_particles * sizeof(vec3_t));
	ps->saves_per_second = 10;
	ps->solution_RK_tolerance = 1e-2;
	ps->solution_RK_timeout = 10;
	ps->solution_maximal_step = MAXFLOAT;
	ps->rt_timer = timerCreate();
#ifdef	PS_PARALLEL
#define	NUM_SLICES		4
	{
		int i, j, *pij, slice_len2;
		int inner_limit, outer_limit;
		int num_ij = (num_particles * num_particles - num_particles);
		ps->ij = pij = (int*)memAlloc(num_ij * sizeof(int));
		num_ij = num_ij >> 1;
		inner_limit = num_particles;
		outer_limit = num_particles - 1;
		for(i = 0;i < outer_limit;i++)
			for(j = i+1;j < inner_limit;j++)
			{
				pij[0] = i;
				pij[1] = j;
				pij += 2;
			}
		/* TODO */
		ps->num_slices = NUM_SLICES;
		ps->ij_start = (int**)memAlloc((ps->num_slices + 1) * sizeof(int*));
		/*
		 * Split the job.
		 * pij now points at the 2nd element after end of ij.
		 */
		slice_len2 = (num_ij / ps->num_slices) << 1;
		ps->ij_start[ps->num_slices] = pij;
		pij = ps->ij;
		for(i = 0;i < ps->num_slices;i++)
		{
			ps->ij_start[i] = pij;
			pij += slice_len2;
		}
		ps->job_ready = condCreate(false);
		condReset(ps->job_ready);
		conPuts("Thread initialization...");
		ps->threads = (vhandle_t*)memAlloc(ps->num_slices * sizeof(vhandle_t));
		ready_count = 0;
		for(i = 0;i < ps->num_slices;i++)
		{
			spinlock_init = 1;
			ps->threads[i] = threadCreate(ps_thread_proc, ps);
			while(spinlock_init);
		}
		conPrintf("Thread initialization complete, ready_count %i\n", ready_count);
	}
#endif	/* PS_PARALLEL */

	return ps;
}

void
free_particle_system(particle_system_p ps)
{
	int i;
	if(!ps)
		return;
#ifdef	PS_PARALLEL
/*
	puts("free_particle_system() waiting for completion of all threads...");
*/
	while(ready_count < ps->num_slices);
/*
	puts("free_particle_system() all threads idle.");
*/
#endif	/* PS_PARALLEL */
	if(ps->rt_timer != LIBV_INVALID_HANDLE)
		timerDestroy(ps->rt_timer);
	if(ps->particle_radius)
		memFree(ps->particle_radius);
	if(ps->particle_density)
		memFree(ps->particle_density);
	if(ps->particle_mass)
		memFree(ps->particle_mass);
	if(ps->particle_reciprocal_mass)
		memFree(ps->particle_reciprocal_mass);
	if(ps->particle_6_pi_eta_r)
		memFree(ps->particle_6_pi_eta_r);
	if(ps->phase)
		memFree(ps->phase);
	if(ps->force)
		memFree(ps->force);
#ifdef	PS_PARALLEL
	if(ps->ij)
		memFree(ps->ij);
	if(ps->ij_start)
		memFree(ps->ij_start);
	all_jobs_done = 1;
	condPulse(ps->job_ready);
	for(i = 0;i < ps->num_slices;i++)
	{
		threadJoin(ps->threads[i], NULL);
		conPrintf("Thread %i finished.\n", i);
	}
	memFree(ps->threads);
	condDestroy(ps->job_ready);
#endif	/* PS_PARALLEL */
	memFree(ps);
}
