1 /* Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
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)
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
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.
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/>. */
25 /* This is a Linux specific implementation of a CPU affinity setting. */
35 #ifdef HAVE_PTHREAD_AFFINITY_NP
37 static unsigned int affinity_counter
;
40 gomp_init_affinity (void)
42 cpu_set_t cpuset
, cpusetnew
;
44 unsigned long cpus
= 0;
46 if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset
), &cpuset
))
48 gomp_error ("could not get CPU affinity set");
49 free (gomp_cpu_affinity
);
50 gomp_cpu_affinity
= NULL
;
51 gomp_cpu_affinity_len
= 0;
55 CPU_ZERO (&cpusetnew
);
56 for (widx
= idx
= 0; idx
< gomp_cpu_affinity_len
; idx
++)
57 if (gomp_cpu_affinity
[idx
] < CPU_SETSIZE
58 && CPU_ISSET (gomp_cpu_affinity
[idx
], &cpuset
))
60 if (! CPU_ISSET (gomp_cpu_affinity
[idx
], &cpusetnew
))
63 CPU_SET (gomp_cpu_affinity
[idx
], &cpusetnew
);
65 gomp_cpu_affinity
[widx
++] = gomp_cpu_affinity
[idx
];
70 gomp_error ("no CPUs left for affinity setting");
71 free (gomp_cpu_affinity
);
72 gomp_cpu_affinity
= NULL
;
73 gomp_cpu_affinity_len
= 0;
77 gomp_cpu_affinity_len
= widx
;
78 if (cpus
< gomp_available_cpus
)
79 gomp_available_cpus
= cpus
;
81 CPU_SET (gomp_cpu_affinity
[0], &cpuset
);
82 pthread_setaffinity_np (pthread_self (), sizeof (cpuset
), &cpuset
);
87 gomp_init_thread_affinity (pthread_attr_t
*attr
)
92 cpu
= __sync_fetch_and_add (&affinity_counter
, 1);
93 cpu
%= gomp_cpu_affinity_len
;
95 CPU_SET (gomp_cpu_affinity
[cpu
], &cpuset
);
96 pthread_attr_setaffinity_np (attr
, sizeof (cpu_set_t
), &cpuset
);
101 #include "../posix/affinity.c"