0
|
1 /* Copyright (C) 2006, 2007, 2008, 2009 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 General Public License as published by
|
|
8 the Free Software Foundation; either version 3, or (at your option)
|
|
9 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 General Public License for
|
|
14 more details.
|
|
15
|
|
16 Under Section 7 of GPL version 3, you are granted additional
|
|
17 permissions described in the GCC Runtime Library Exception, version
|
|
18 3.1, as published by the Free Software Foundation.
|
|
19
|
|
20 You should have received a copy of the GNU General Public License and
|
|
21 a copy of the GCC Runtime Library Exception along with this program;
|
|
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
23 <http://www.gnu.org/licenses/>. */
|
|
24
|
|
25 /* This is a Linux specific implementation of a CPU affinity setting. */
|
|
26
|
|
27 #ifndef _GNU_SOURCE
|
|
28 #define _GNU_SOURCE 1
|
|
29 #endif
|
|
30 #include "libgomp.h"
|
|
31 #include <sched.h>
|
|
32 #include <stdlib.h>
|
|
33 #include <unistd.h>
|
|
34
|
|
35 #ifdef HAVE_PTHREAD_AFFINITY_NP
|
|
36
|
|
37 static unsigned int affinity_counter;
|
|
38
|
|
39 void
|
|
40 gomp_init_affinity (void)
|
|
41 {
|
|
42 cpu_set_t cpuset;
|
|
43 size_t idx, widx;
|
|
44
|
|
45 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
|
|
46 {
|
|
47 gomp_error ("could not get CPU affinity set");
|
|
48 free (gomp_cpu_affinity);
|
|
49 gomp_cpu_affinity = NULL;
|
|
50 gomp_cpu_affinity_len = 0;
|
|
51 return;
|
|
52 }
|
|
53
|
|
54 for (widx = idx = 0; idx < gomp_cpu_affinity_len; idx++)
|
|
55 if (gomp_cpu_affinity[idx] < CPU_SETSIZE
|
|
56 && CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
|
|
57 gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
|
|
58
|
|
59 if (widx == 0)
|
|
60 {
|
|
61 gomp_error ("no CPUs left for affinity setting");
|
|
62 free (gomp_cpu_affinity);
|
|
63 gomp_cpu_affinity = NULL;
|
|
64 gomp_cpu_affinity_len = 0;
|
|
65 return;
|
|
66 }
|
|
67
|
|
68 gomp_cpu_affinity_len = widx;
|
|
69 CPU_ZERO (&cpuset);
|
|
70 CPU_SET (gomp_cpu_affinity[0], &cpuset);
|
|
71 pthread_setaffinity_np (pthread_self (), sizeof (cpuset), &cpuset);
|
|
72 affinity_counter = 1;
|
|
73 }
|
|
74
|
|
75 void
|
|
76 gomp_init_thread_affinity (pthread_attr_t *attr)
|
|
77 {
|
|
78 unsigned int cpu;
|
|
79 cpu_set_t cpuset;
|
|
80
|
|
81 cpu = __sync_fetch_and_add (&affinity_counter, 1);
|
|
82 cpu %= gomp_cpu_affinity_len;
|
|
83 CPU_ZERO (&cpuset);
|
|
84 CPU_SET (gomp_cpu_affinity[cpu], &cpuset);
|
|
85 pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), &cpuset);
|
|
86 }
|
|
87
|
|
88 #else
|
|
89
|
|
90 #include "../posix/affinity.c"
|
|
91
|
|
92 #endif
|