00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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