/*
 *  test_gsd_distribution_threaded.sli
 *
 *  This file is part of NEST.
 *
 *  Copyright (C) 2004 The NEST Initiative
 *
 *  NEST is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  NEST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NEST.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

 /* BeginDocumentation
Name: testsuite::test_gsd_distribution_threaded- Test global spike detector with hybrid MPI and thread parallelism

Synopsis: nest_indirect test_gsd_distribution_threaded.sli -> -

Description:
Tests if global spike detectors are equally distributed among virtual
recording processes. The script creates 4 Spike detectors on 1x2 and
2x2 recording processes and checks if they are equally distributed.

Author: Kunkel, Schmidt
FirstVersion: April 2014
*/


(unittest) run
/unittest using

is_threaded not { exit_test_gracefully } if


[2 3]
{
    NumProcesses /num_mpi_procs Set
    num_mpi_procs 1 sub SetNumRecProcesses
    
    0 <<
        /total_num_virtual_procs num_mpi_procs 2 mul
    >> SetStatus
    
   
    /iaf_neuron 1 Create pop
    /spike_detector 2 Create pop
    /iaf_neuron 1 Create pop
    /spike_detector 2 Create pop 

    Rank 0 gt
    {
    	[2 3 5 6] /sd Set
	[6] 0 LayoutArray /num_sd Set
	
    	sd
    	{
    	    /n Set
    	    n GetStatus /local get
    	    {
		n GetStatus /vp get /vp Set
		num_sd {1 add} [vp 1 add] MapAt /num_sd Set
    	    } if
    	} forall
	
    	num_sd {0 neq} Select /reduced_num_sd Set

	% Test if number of VPs holding spike_detectors on this rank equals number of local threads
	reduced_num_sd length 2 eq

	% Test if these VPs hold the same number of spike detectors
	reduced_num_sd 0 get reduced_num_sd 1 get eq

	and
    }
    {
    	true
    } ifelse

} distributed_collect_assert_or_die
 
