/*
    Simulacia sirenia trhliny
    Copyright (C) 2008 Samuel Kupka

    This program 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 3 of the License, or
    (at your option) any later version.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/
__global__ void purge_kernel(dim3 gdim,int start,unsigned int *s_queue,unsigned int *s_grid)
{
    int poz=threadIdx.x+start;
    uint4 now,next;
    
    now=simulate_queue_get(poz,s_queue);
    for(int i=0;i<12;i++){
        next=simulate_translate(now,i);
        if(simulate_get_grid_bit(gdim,next,s_grid)==0) return;
    }

    int p=atomicSub(&n_queue,1);
    s_queue[poz]=s_queue[p-1];
}

void simulate_purge(dim3 gdim,int num_t)
{
    int p=0,l;
    while(1){
        simulate_get_state();
        l=(h_state.n_queue-p)/2;
        if(l<(num_t<<1)) break;
        if(l>num_t) l=num_t;
        if(l<1) break;
        purge_kernel<<<1,l>>>(gdim,p,s_queue,s_grid);
        main_check();
        p+=l;
    }
}
