OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [libgomp/] [parallel.c] - Diff between revs 816 and 826

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 826
/* Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
/* Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
   Contributed by Richard Henderson <rth@redhat.com>.
   Contributed by Richard Henderson <rth@redhat.com>.
 
 
   This file is part of the GNU OpenMP Library (libgomp).
   This file is part of the GNU OpenMP Library (libgomp).
 
 
   Libgomp is free software; you can redistribute it and/or modify it
   Libgomp is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
   any later version.
 
 
   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.
   more details.
 
 
   Under Section 7 of GPL version 3, you are granted additional
   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.
   3.1, as published by the Free Software Foundation.
 
 
   You should have received a copy of the GNU General Public License and
   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */
   <http://www.gnu.org/licenses/>.  */
 
 
/* This file handles the (bare) PARALLEL construct.  */
/* This file handles the (bare) PARALLEL construct.  */
 
 
#include "libgomp.h"
#include "libgomp.h"
#include <limits.h>
#include <limits.h>
 
 
 
 
/* Determine the number of threads to be launched for a PARALLEL construct.
/* Determine the number of threads to be launched for a PARALLEL construct.
   This algorithm is explicitly described in OpenMP 3.0 section 2.4.1.
   This algorithm is explicitly described in OpenMP 3.0 section 2.4.1.
   SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
   SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
   If the IF clause is false, SPECIFIED is forced to 1.  When NUM_THREADS
   If the IF clause is false, SPECIFIED is forced to 1.  When NUM_THREADS
   is not present, SPECIFIED is 0.  */
   is not present, SPECIFIED is 0.  */
 
 
unsigned
unsigned
gomp_resolve_num_threads (unsigned specified, unsigned count)
gomp_resolve_num_threads (unsigned specified, unsigned count)
{
{
  struct gomp_thread *thread = gomp_thread();
  struct gomp_thread *thread = gomp_thread();
  struct gomp_task_icv *icv;
  struct gomp_task_icv *icv;
  unsigned threads_requested, max_num_threads, num_threads;
  unsigned threads_requested, max_num_threads, num_threads;
  unsigned long remaining;
  unsigned long remaining;
 
 
  icv = gomp_icv (false);
  icv = gomp_icv (false);
 
 
  if (specified == 1)
  if (specified == 1)
    return 1;
    return 1;
  else if (thread->ts.active_level >= 1 && !icv->nest_var)
  else if (thread->ts.active_level >= 1 && !icv->nest_var)
    return 1;
    return 1;
  else if (thread->ts.active_level >= gomp_max_active_levels_var)
  else if (thread->ts.active_level >= gomp_max_active_levels_var)
    return 1;
    return 1;
 
 
  /* If NUM_THREADS not specified, use nthreads_var.  */
  /* If NUM_THREADS not specified, use nthreads_var.  */
  if (specified == 0)
  if (specified == 0)
    threads_requested = icv->nthreads_var;
    threads_requested = icv->nthreads_var;
  else
  else
    threads_requested = specified;
    threads_requested = specified;
 
 
  max_num_threads = threads_requested;
  max_num_threads = threads_requested;
 
 
  /* If dynamic threads are enabled, bound the number of threads
  /* If dynamic threads are enabled, bound the number of threads
     that we launch.  */
     that we launch.  */
  if (icv->dyn_var)
  if (icv->dyn_var)
    {
    {
      unsigned dyn = gomp_dynamic_max_threads ();
      unsigned dyn = gomp_dynamic_max_threads ();
      if (dyn < max_num_threads)
      if (dyn < max_num_threads)
        max_num_threads = dyn;
        max_num_threads = dyn;
 
 
      /* Optimization for parallel sections.  */
      /* Optimization for parallel sections.  */
      if (count && count < max_num_threads)
      if (count && count < max_num_threads)
        max_num_threads = count;
        max_num_threads = count;
    }
    }
 
 
  /* ULONG_MAX stands for infinity.  */
  /* ULONG_MAX stands for infinity.  */
  if (__builtin_expect (gomp_thread_limit_var == ULONG_MAX, 1)
  if (__builtin_expect (gomp_thread_limit_var == ULONG_MAX, 1)
      || max_num_threads == 1)
      || max_num_threads == 1)
    return max_num_threads;
    return max_num_threads;
 
 
#ifdef HAVE_SYNC_BUILTINS
#ifdef HAVE_SYNC_BUILTINS
  do
  do
    {
    {
      remaining = gomp_remaining_threads_count;
      remaining = gomp_remaining_threads_count;
      num_threads = max_num_threads;
      num_threads = max_num_threads;
      if (num_threads > remaining)
      if (num_threads > remaining)
        num_threads = remaining + 1;
        num_threads = remaining + 1;
    }
    }
  while (__sync_val_compare_and_swap (&gomp_remaining_threads_count,
  while (__sync_val_compare_and_swap (&gomp_remaining_threads_count,
                                      remaining, remaining - num_threads + 1)
                                      remaining, remaining - num_threads + 1)
         != remaining);
         != remaining);
#else
#else
  gomp_mutex_lock (&gomp_remaining_threads_lock);
  gomp_mutex_lock (&gomp_remaining_threads_lock);
  num_threads = max_num_threads;
  num_threads = max_num_threads;
  remaining = gomp_remaining_threads_count;
  remaining = gomp_remaining_threads_count;
  if (num_threads > remaining)
  if (num_threads > remaining)
    num_threads = remaining + 1;
    num_threads = remaining + 1;
  gomp_remaining_threads_count -= num_threads - 1;
  gomp_remaining_threads_count -= num_threads - 1;
  gomp_mutex_unlock (&gomp_remaining_threads_lock);
  gomp_mutex_unlock (&gomp_remaining_threads_lock);
#endif
#endif
 
 
  return num_threads;
  return num_threads;
}
}
 
 
void
void
GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
{
{
  num_threads = gomp_resolve_num_threads (num_threads, 0);
  num_threads = gomp_resolve_num_threads (num_threads, 0);
  gomp_team_start (fn, data, num_threads, gomp_new_team (num_threads));
  gomp_team_start (fn, data, num_threads, gomp_new_team (num_threads));
}
}
 
 
void
void
GOMP_parallel_end (void)
GOMP_parallel_end (void)
{
{
  if (__builtin_expect (gomp_thread_limit_var != ULONG_MAX, 0))
  if (__builtin_expect (gomp_thread_limit_var != ULONG_MAX, 0))
    {
    {
      struct gomp_thread *thr = gomp_thread ();
      struct gomp_thread *thr = gomp_thread ();
      struct gomp_team *team = thr->ts.team;
      struct gomp_team *team = thr->ts.team;
      if (team && team->nthreads > 1)
      if (team && team->nthreads > 1)
        {
        {
#ifdef HAVE_SYNC_BUILTINS
#ifdef HAVE_SYNC_BUILTINS
          __sync_fetch_and_add (&gomp_remaining_threads_count,
          __sync_fetch_and_add (&gomp_remaining_threads_count,
                                1UL - team->nthreads);
                                1UL - team->nthreads);
#else
#else
          gomp_mutex_lock (&gomp_remaining_threads_lock);
          gomp_mutex_lock (&gomp_remaining_threads_lock);
          gomp_remaining_threads_count -= team->nthreads - 1;
          gomp_remaining_threads_count -= team->nthreads - 1;
#endif
#endif
        }
        }
    }
    }
  gomp_team_end ();
  gomp_team_end ();
}
}
 
 


/* The public OpenMP API for thread and team related inquiries.  */
/* The public OpenMP API for thread and team related inquiries.  */
 
 
int
int
omp_get_num_threads (void)
omp_get_num_threads (void)
{
{
  struct gomp_team *team = gomp_thread ()->ts.team;
  struct gomp_team *team = gomp_thread ()->ts.team;
  return team ? team->nthreads : 1;
  return team ? team->nthreads : 1;
}
}
 
 
int
int
omp_get_thread_num (void)
omp_get_thread_num (void)
{
{
  return gomp_thread ()->ts.team_id;
  return gomp_thread ()->ts.team_id;
}
}
 
 
/* This wasn't right for OpenMP 2.5.  Active region used to be non-zero
/* This wasn't right for OpenMP 2.5.  Active region used to be non-zero
   when the IF clause doesn't evaluate to false, starting with OpenMP 3.0
   when the IF clause doesn't evaluate to false, starting with OpenMP 3.0
   it is non-zero with more than one thread in the team.  */
   it is non-zero with more than one thread in the team.  */
 
 
int
int
omp_in_parallel (void)
omp_in_parallel (void)
{
{
  return gomp_thread ()->ts.active_level > 0;
  return gomp_thread ()->ts.active_level > 0;
}
}
 
 
int
int
omp_get_level (void)
omp_get_level (void)
{
{
  return gomp_thread ()->ts.level;
  return gomp_thread ()->ts.level;
}
}
 
 
int
int
omp_get_ancestor_thread_num (int level)
omp_get_ancestor_thread_num (int level)
{
{
  struct gomp_team_state *ts = &gomp_thread ()->ts;
  struct gomp_team_state *ts = &gomp_thread ()->ts;
  if (level < 0 || level > ts->level)
  if (level < 0 || level > ts->level)
    return -1;
    return -1;
  for (level = ts->level - level; level > 0; --level)
  for (level = ts->level - level; level > 0; --level)
    ts = &ts->team->prev_ts;
    ts = &ts->team->prev_ts;
  return ts->team_id;
  return ts->team_id;
}
}
 
 
int
int
omp_get_team_size (int level)
omp_get_team_size (int level)
{
{
  struct gomp_team_state *ts = &gomp_thread ()->ts;
  struct gomp_team_state *ts = &gomp_thread ()->ts;
  if (level < 0 || level > ts->level)
  if (level < 0 || level > ts->level)
    return -1;
    return -1;
  for (level = ts->level - level; level > 0; --level)
  for (level = ts->level - level; level > 0; --level)
    ts = &ts->team->prev_ts;
    ts = &ts->team->prev_ts;
  if (ts->team == NULL)
  if (ts->team == NULL)
    return 1;
    return 1;
  else
  else
    return ts->team->nthreads;
    return ts->team->nthreads;
}
}
 
 
int
int
omp_get_active_level (void)
omp_get_active_level (void)
{
{
  return gomp_thread ()->ts.active_level;
  return gomp_thread ()->ts.active_level;
}
}
 
 
ialias (omp_get_num_threads)
ialias (omp_get_num_threads)
ialias (omp_get_thread_num)
ialias (omp_get_thread_num)
ialias (omp_in_parallel)
ialias (omp_in_parallel)
ialias (omp_get_level)
ialias (omp_get_level)
ialias (omp_get_ancestor_thread_num)
ialias (omp_get_ancestor_thread_num)
ialias (omp_get_team_size)
ialias (omp_get_team_size)
ialias (omp_get_active_level)
ialias (omp_get_active_level)
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.