Created
March 9, 2012 05:42
-
-
Save acfoltzer/2005212 to your computer and use it in GitHub Desktop.
Haskell affinity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ForeignFunctionInterface #-} | |
module System.Posix.Affinity (setAffinityOS) where | |
import Control.Monad | |
import Foreign | |
import Foreign.C | |
import System.IO.Error | |
foreign import ccall unsafe "pin_pthread" pin_pthread :: CInt -> IO Errno | |
setAffinityOS :: Int -> IO () | |
setAffinityOS cpu = do | |
err <- pin_pthread $ fromIntegral cpu | |
when (err /= eOK) $ | |
ioError $ errnoToIOError "setAffinityOS" err Nothing Nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <pthread.h> | |
#include <sched.h> | |
#include <stdlib.h> | |
#include "pin.h" | |
int pin_pthread(int cpu) { | |
cpu_set_t cs; | |
pthread_t thread; | |
thread = pthread_self(); | |
CPU_ZERO(&cs); | |
CPU_SET(cpu, &cs); | |
return pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment