Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions commonb.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ void SetPriority (
/* Set affinity for this thread to a specific CPU core */

if (bind_type == 0) {
#ifndef __APPLE__
int num_cores;
hwloc_obj_t obj;
num_cores = hwloc_get_nbobjs_by_type (hwloc_topology, HWLOC_OBJ_CORE);
Expand Down Expand Up @@ -630,6 +631,17 @@ void SetPriority (
sprintf (buf, "Error getting hwloc object for core #%d. Affinity not set.\n", core+1);
OutputStr (info->worker_num, buf);
}
#else
int merror = mach_set_thread_cpubind(pthread_self(), core);
if (merror) {
sprintf (buf, "Error setting affinity to affinity-set #%d: %s\n", core+1, strerror (merror));
OutputStr (info->worker_num, buf);
}
else if (info->verbosity >= 2) {
sprintf (buf, "Affinity set to affinity-set #%d\n", core+1);
OutputStr (info->worker_num, buf);
}
#endif
}

/* Set affinity for this thread to a specific logical CPU */
Expand Down
2 changes: 2 additions & 0 deletions commonc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,8 +1323,10 @@ void nameAndReadIniFiles (
{
const struct hwloc_topology_support *support;
OS_CAN_SET_AFFINITY = 1;
#ifndef __APPLE__
support = hwloc_topology_get_support (hwloc_topology);
if (support == NULL || ! support->cpubind->set_thread_cpubind) OS_CAN_SET_AFFINITY = 0;
#endif
}

/* Initialize mutexes */
Expand Down
59 changes: 59 additions & 0 deletions macosx64/aff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifdef __APPLE__
#include <sys/types.h>
#include <sys/sysctl.h>
#include <pthread.h>
#include <stdint.h>
#include <mach/thread_policy.h>
#include <mach/thread_act.h>
#include <errno.h>

/* Alternatively, this can be simplified to set-only without checking core count */
int32_t pu_per_core = 0;
int32_t ncore = 0;

__attribute__((constructor))
static inline void initialize_pu_per_core() {
int64_t cacheconfig[32];
int32_t *cacheconfig32 = cacheconfig;
size_t size = sizeof(cacheconfig);
sysctlbyname("hw.cacheconfig", &cacheconfig, &size, NULL, 0);
/* per hwloc, there are 32 and 64-bit configs... */
if (cacheconfig[0] <= 0xFFFFFFFFUL) {
pu_per_core = cacheconfig[2];
} else {
pu_per_core = cacheconfig32[2];
}
}

__attribute__((constructor))
static inline void initialize_ncore() {
size_t size = sizeof(ncore);
sysctlbyname("hw.ncpu", &ncore, &size, NULL, 0);
// assert: 0 == ncore % pu_per_core
ncore /= pu_per_core;
}

/* Index is 1-based! 0 means no binding. Return value is positive-errno. Most commonly you get EPFNOSUPPORT (46) on ARM */
int mach_set_thread_cpubind(pthread_t thread, int cpu) {
if (cpu < 0 || cpu > ncore) {
return EINVAL;
}

thread_act_t handle = pthread_mach_thread_np(thread);
thread_affinity_policy_data_t policy = {.affinity_tag = cpu};
return thread_policy_set(handle, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT);
}

/* Return value is tag or negative-errno */
int mach_get_thread_cpubind(pthread_t thread) {
thread_act_t handle = pthread_mach_thread_np(thread);
thread_affinity_policy_data_t policy;
mach_msg_type_number_t count = THREAD_AFFINITY_POLICY_COUNT;
kern_return_t kr = thread_policy_get(handle, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, &count, 0);
if (kr != KERN_SUCCESS) {
return -kr;
}

return policy.affinity_tag;
}
#endif
2 changes: 1 addition & 1 deletion macosx64/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LFLAGS = -m64 -Wl,-no_pie -L/usr/local/lib
LIBS = ../gwnum/gwnum.a ../gwnum/polymult.a -lm -lpthread /usr/local/lib/libhwloc.a /usr/local/lib/libgmp.a -lcurl -framework IOKit -framework CoreFoundation -lc++

FACTOROBJ = ../prime95/macosx64/factor64.o
OBJS = prime.o menu.o cJSON.o ecm.o exponentiate.o pair.o pm1prob.o
OBJS = aff.o prime.o menu.o cJSON.o ecm.o exponentiate.o pair.o pm1prob.o
EXE = mprime

#########################################################################
Expand Down
7 changes: 7 additions & 0 deletions macosx64/prime.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ void test_status(void);

void rangeStatus (void);
void options_cpu (void);

#ifdef __APPLE__
#include <pthread.h>
int mach_set_thread_cpubind(pthread_t thread, int cpu);
int mach_get_thread_cpubind(pthread_t thread);
#endif