Skip to content

Commit

Permalink
Merge pull request #49 from climbfuji/macosx-portability
Browse files Browse the repository at this point in the history
Remove logic to get and set CPU affinity for Darwin (MacOSX) platforms
  • Loading branch information
wrongkindofdoctor authored Jul 19, 2019
2 parents db20e25 + 4e381fb commit f2e2c86
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mpp/affinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@
#include <sys/resource.h>
#include <sys/syscall.h>

#ifndef __APPLE__
static pid_t gettid(void)
{
return syscall(__NR_gettid);
}
#endif

/*
* Returns this thread's CPU affinity, if bound to a single core,
* or else -1.
*/
int get_cpu_affinity(void)
{
#ifndef __APPLE__
cpu_set_t coremask; /* core affinity mask */

CPU_ZERO(&coremask);
Expand All @@ -60,6 +63,7 @@ int get_cpu_affinity(void)

if (last_cpu != -1) {return (first_cpu);}
return (last_cpu == -1) ? first_cpu : -1;
#endif
}

int get_cpu_affinity_(void) { return get_cpu_affinity(); } /* Fortran interface */
Expand All @@ -70,13 +74,15 @@ int get_cpu_affinity_(void) { return get_cpu_affinity(); } /* Fortran interface
*/
void set_cpu_affinity( int cpu )
{
#ifndef __APPLE__
cpu_set_t coremask; /* core affinity mask */

CPU_ZERO(&coremask);
CPU_SET(cpu,&coremask);
if (sched_setaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
fprintf(stderr,"Unable to set thread %d affinity. %s\n",gettid(),strerror(errno));
}
#endif
}

void set_cpu_affinity_(int *cpu) { set_cpu_affinity(*cpu); } /* Fortran interface */

0 comments on commit f2e2c86

Please sign in to comment.