Exemplo n.º 1
0
void *thread_routine( void *pthread_id )
{
    int thread_id = *(int*)pthread_id;
    int num_cells = cells.size();

    int particles_per_thread = (n + n_threads - 1) / n_threads;
    int rows_per_thread = (num_cells + n_threads - 1) / n_threads;

    int first_row = min(  thread_id     * rows_per_thread, num_cells);
    int last_row  = min( (thread_id+1)  * rows_per_thread, num_cells);

    int first = min(  thread_id    * particles_per_thread, n );
    int last  = min( (thread_id+1) * particles_per_thread, n );

    Particles my_particles;
    my_particles.clear();
    get_particles_from_rows(first_row, last_row, &my_particles, cells);

 
    //
    //  simulate a number of time steps
    //
    for( int step = 0; step < NSTEPS; step++ )
    {
        my_particles.clear();
        get_particles_from_rows(first_row, last_row, &my_particles, cells);
        
        //
        //  compute forces
        //
        for(int i = 0; i < my_particles.size(); i++)
        {
            particle_t *curr_particle = my_particles[i];
            curr_particle->ax = 0;
            curr_particle->ay = 0;
            apply_force(curr_particle, cells);            
        }

        
        pthread_barrier_wait( &barrier );
        
        //
        //  move particles
        //
        for(int i = 0; i < my_particles.size(); i++)
        {
            particle_t *curr_particle = my_particles[i];
            move(*my_particles[i]);            
        }
        
        pthread_barrier_wait( &barrier );
        clear_cells(first_row, last_row, cells);

        pthread_barrier_wait( &barrier );
        update_cells_parallel(first_row, last_row, my_particles, cells);
    
        pthread_barrier_wait( &barrier );
        
        //
        //  save if necessary
        //
        if( thread_id == 0 && fsave && (step%SAVEFREQ) == 0 )
            save( fsave, n, particles );
    }
    
    return NULL;
}