affinity.c revision 282152
1/* Copyright (C) 2006, 2007 Free Software Foundation, Inc.
2   Contributed by Jakub Jelinek <jakub@redhat.com>.
3
4   This file is part of the GNU OpenMP Library (libgomp).
5
6   Libgomp is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14   more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with libgomp; see the file COPYING.LIB.  If not, write to the
18   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   MA 02110-1301, USA.  */
20
21/* As a special exception, if you link this library with other files, some
22   of which are compiled with GCC, to produce an executable, this library
23   does not by itself cause the resulting executable to be covered by the
24   GNU General Public License.  This exception does not however invalidate
25   any other reasons why the executable file might be covered by the GNU
26   General Public License.  */
27
28/* This is a Linux specific implementation of a CPU affinity setting.  */
29
30#ifndef _GNU_SOURCE
31#define _GNU_SOURCE 1
32#endif
33#include "libgomp.h"
34#include <sched.h>
35#include <stdlib.h>
36#include <unistd.h>
37
38#ifdef HAVE_PTHREAD_AFFINITY_NP
39
40static unsigned int affinity_counter;
41#ifndef HAVE_SYNC_BUILTINS
42static gomp_mutex_t affinity_lock;
43#endif
44
45void
46gomp_init_affinity (void)
47{
48  cpu_set_t cpuset;
49  size_t idx, widx;
50
51  if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
52    {
53      gomp_error ("could not get CPU affinity set");
54      free (gomp_cpu_affinity);
55      gomp_cpu_affinity = NULL;
56      gomp_cpu_affinity_len = 0;
57      return;
58    }
59
60  for (widx = idx = 0; idx < gomp_cpu_affinity_len; idx++)
61    if (gomp_cpu_affinity[idx] < CPU_SETSIZE
62        && CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
63      gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
64
65  if (widx == 0)
66    {
67      gomp_error ("no CPUs left for affinity setting");
68      free (gomp_cpu_affinity);
69      gomp_cpu_affinity = NULL;
70      gomp_cpu_affinity_len = 0;
71      return;
72    }
73
74  gomp_cpu_affinity_len = widx;
75  CPU_ZERO (&cpuset);
76  CPU_SET (gomp_cpu_affinity[0], &cpuset);
77  pthread_setaffinity_np (pthread_self (), sizeof (cpuset), &cpuset);
78  affinity_counter = 1;
79#ifndef HAVE_SYNC_BUILTINS
80  gomp_mutex_init (&affinity_lock);
81#endif
82}
83
84void
85gomp_init_thread_affinity (pthread_attr_t *attr)
86{
87  unsigned int cpu;
88  cpu_set_t cpuset;
89
90#ifdef HAVE_SYNC_BUILTINS
91  cpu = __sync_fetch_and_add (&affinity_counter, 1);
92#else
93  gomp_mutex_lock (&affinity_lock);
94  cpu = affinity_counter++;
95  gomp_mutex_unlock (&affinity_lock);
96#endif
97  cpu %= gomp_cpu_affinity_len;
98  CPU_ZERO (&cpuset);
99  CPU_SET (gomp_cpu_affinity[cpu], &cpuset);
100  pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), &cpuset);
101}
102
103#else
104
105#include "../posix/affinity.c"
106
107#endif
108