1 /* Copyright (C) 2008-2013 Free Software Foundation, Inc.
2 This file is part of the UPC runtime library.
3 Written by Gary Funck <gary@intrepid.com>
4 and Nenad Vukicevic <nenad@intrepid.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
29 #include "upc_config.h"
30 #include "upc_sysdep.h"
33 #include "upc_affinity.h"
36 __upc_numa_supported (void)
42 __upc_numa_init (const upc_info_p u
, const char **ARG_UNUSED (err_msg
))
44 u
->num_nodes
= numa_max_node () + 1;
49 __upc_numa_allocate (const upc_info_p u
, const int thread_id
,
50 int *sched_affinity
, int *mem_affinity
,
51 const char **ARG_UNUSED (err_msg
))
54 /* schedule threads over nodes */
55 pelem
= thread_id
% u
->num_nodes
;
56 *sched_affinity
= pelem
;
57 *mem_affinity
= pelem
;
61 /* Set node scheduling policy. */
64 __upc_numa_sched_set (const upc_info_p u
, const int thread_id
)
66 #if defined(LIBNUMA_API_VERSION) && (LIBNUMA_API_VERSION==2)
67 struct bitmask
*set
= numa_allocate_cpumask();
68 numa_node_to_cpus (u
->thread_info
[thread_id
].sched_affinity
, set
);
69 if (numa_sched_setaffinity (0, set
))
71 __upc_fatal ("Scheduling cannot be set");
76 numa_node_to_cpus (u
->thread_info
[thread_id
].sched_affinity
,
77 (unsigned long *) &set
, sizeof (set
));
78 if (sched_setaffinity (0, sizeof (set
), &set
))
80 __upc_fatal ("Scheduling cannot be set");
85 /* Set memory allocation policy */
88 __upc_numa_memory_affinity_set (const upc_info_p u
, const int thread_id
)
90 if (u
->mem_policy
== GUPCR_MEM_POLICY_NODE
)
92 numa_set_preferred (u
->thread_info
[thread_id
].mem_affinity
);
94 else if (u
->mem_policy
== GUPCR_MEM_POLICY_STRICT
)
96 #if defined(LIBNUMA_API_VERSION) && (LIBNUMA_API_VERSION==2)
97 struct bitmask
*nodemask
= numa_bitmask_alloc(u
->num_nodes
);
98 numa_bitmask_setbit (nodemask
, u
->thread_info
[thread_id
].mem_affinity
);
99 numa_set_membind (nodemask
);
100 numa_bitmask_free (nodemask
);
103 nodemask_zero (&nodemask
);
104 nodemask_set (&nodemask
, u
->thread_info
[thread_id
].mem_affinity
);
105 numa_set_membind (&nodemask
);
110 /* Set affinity for memory region */
113 __upc_numa_memory_region_affinity_set (const upc_info_p u
,
115 const void *region
, const size_t size
)
117 if ((u
->sched_policy
!= GUPCR_SCHED_POLICY_AUTO
) &&
118 (u
->mem_policy
!= GUPCR_MEM_POLICY_AUTO
))
120 /* memory is being allocated with affinity to "thread" */
121 if (thread_id
== MYTHREAD
)
122 numa_tonode_memory ((void *) region
, size
,
123 u
->thread_info
[thread_id
].mem_affinity
);