config/linux/affinity.c

Go to the documentation of this file.
00001 /* Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
00002    Contributed by Jakub Jelinek <jakub@redhat.com>.
00003 
00004    This file is part of the GNU OpenMP Library (libgomp).
00005 
00006    Libgomp is free software; you can redistribute it and/or modify it
00007    under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3, or (at your option)
00009    any later version.
00010 
00011    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
00012    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00014    more details.
00015 
00016    Under Section 7 of GPL version 3, you are granted additional
00017    permissions described in the GCC Runtime Library Exception, version
00018    3.1, as published by the Free Software Foundation.
00019 
00020    You should have received a copy of the GNU General Public License and
00021    a copy of the GCC Runtime Library Exception along with this program;
00022    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023    <http://www.gnu.org/licenses/>.  */
00024 
00025 /* This is a Linux specific implementation of a CPU affinity setting.  */
00026 
00027 #ifndef _GNU_SOURCE
00028 #define _GNU_SOURCE 1
00029 #endif
00030 #include "libgomp.h"
00031 #include <sched.h>
00032 #include <stdlib.h>
00033 #include <unistd.h>
00034 
00035 #ifdef HAVE_PTHREAD_AFFINITY_NP
00036 
00037 static unsigned int affinity_counter;
00038 
00039 void
00040 gomp_init_affinity (void)
00041 {
00042   cpu_set_t cpuset, cpusetnew;
00043   size_t idx, widx;
00044   unsigned long cpus = 0;
00045 
00046   if (pthread_getaffinity_np (pthread_self (), sizeof (cpuset), &cpuset))
00047     {
00048       gomp_error ("could not get CPU affinity set");
00049       free (gomp_cpu_affinity);
00050       gomp_cpu_affinity = NULL;
00051       gomp_cpu_affinity_len = 0;
00052       return;
00053     }
00054 
00055   CPU_ZERO (&cpusetnew);
00056   for (widx = idx = 0; idx < gomp_cpu_affinity_len; idx++)
00057     if (gomp_cpu_affinity[idx] < CPU_SETSIZE
00058         && CPU_ISSET (gomp_cpu_affinity[idx], &cpuset))
00059       {
00060     if (! CPU_ISSET (gomp_cpu_affinity[idx], &cpusetnew))
00061       {
00062         cpus++;
00063         CPU_SET (gomp_cpu_affinity[idx], &cpusetnew);
00064       }
00065     gomp_cpu_affinity[widx++] = gomp_cpu_affinity[idx];
00066       }
00067 
00068   if (widx == 0)
00069     {
00070       gomp_error ("no CPUs left for affinity setting");
00071       free (gomp_cpu_affinity);
00072       gomp_cpu_affinity = NULL;
00073       gomp_cpu_affinity_len = 0;
00074       return;
00075     }
00076 
00077   gomp_cpu_affinity_len = widx;
00078   if (cpus < gomp_available_cpus)
00079     gomp_available_cpus = cpus;
00080   CPU_ZERO (&cpuset);
00081   CPU_SET (gomp_cpu_affinity[0], &cpuset);
00082   pthread_setaffinity_np (pthread_self (), sizeof (cpuset), &cpuset);
00083   affinity_counter = 1;
00084 }
00085 
00086 void
00087 gomp_init_thread_affinity (pthread_attr_t *attr)
00088 {
00089   unsigned int cpu;
00090   cpu_set_t cpuset;
00091 
00092   cpu = __sync_fetch_and_add (&affinity_counter, 1);
00093   cpu %= gomp_cpu_affinity_len;
00094   CPU_ZERO (&cpuset);
00095   CPU_SET (gomp_cpu_affinity[cpu], &cpuset);
00096   pthread_attr_setaffinity_np (attr, sizeof (cpu_set_t), &cpuset);
00097 }
00098 
00099 #else
00100 
00101 #include "../posix/affinity.c"
00102 
00103 #endif

Generated on Fri Apr 5 05:38:09 2013 for Libgomp by  doxygen 1.4.7