コード例 #1
1
int testprog_set_affinity(int start_cpu, int end_cpu)
{
	cpu_set_t cpu_set;
	CPU_ZERO(&cpu_set);
	int i;
	int retval;

	int n_cpus = get_nprocs(); 
	int n_cpus_conf = get_nprocs_conf();

	if (n_cpus != n_cpus_conf) {
		printf("uh, ncpus=%d but ncpus_conf=%d\n", n_cpus, n_cpus_conf); 
	}

	if (start_cpu < 0) start_cpu=0;
	if (end_cpu < 0 || end_cpu > n_cpus_conf) {
		end_cpu = n_cpus_conf;
	}

	for (i=start_cpu; i<end_cpu; i++) {
		CPU_SET(i,&cpu_set);
	}
	if ((retval = sched_setaffinity(0, sizeof(cpu_set), &cpu_set)) == 0) {
		printf("Set affinity for CPUs %d-%d OK\n", start_cpu, end_cpu-1);
	}
	return retval; 
}
コード例 #2
0
ファイル: Pin.cpp プロジェクト: SeisSol/SeisSol
std::string seissol::parallel::maskToString(cpu_set_t const& set) {
  std::stringstream st;
  for (int cpu = 0; cpu < get_nprocs(); ++cpu) {
    if (cpu % 10 == 0 && cpu != 0 && cpu != get_nprocs()-1) {
      st << '|';
    }
    if (CPU_ISSET(cpu, &set)) {
      st << cpu % 10;
    } else {
      st << '-';
    }
  }
  return st.str();
}
コード例 #3
0
static void process_algo() {
	int procs_cnt = get_nprocs();
	threads = (pthread_t *) malloc(sizeof(pthread_t) * procs_cnt);
	int thr_cnt = 0;
	
	int step = (border - 1) / procs_cnt;
	if ( (border - 1) % procs_cnt != 0) {
		++step;
	}
	
	for (int i = 2; i <= border; i += step) {
		int start = i;
		int end = MIN(border, i + step - 1);
		prime_args_t * args = malloc(sizeof *args);
		args->start = start;
		args->end = end;
		int rc = pthread_create(&threads[thr_cnt++], NULL, &calculatePrime, args);

		if (rc) {
			printf("ERROR. failed to create thread\n");
			exit(-1);
		}
	}
	
	for (int i = 0; i < thr_cnt; ++i) {
		int rc = pthread_join(threads[i], NULL);
		printf("Thread %d exited\n", i);
	}

	free(threads);
}
コード例 #4
0
int main(int argc, char *argv[])
{
	int cpu_core_num = get_nprocs();

	printf("CPU has %d core\n", cpu_core_num);
	
	cpu_set_t mask;
	cpu_set_t get;

	int myid, i;
	for (myid = 0; myid < cpu_core_num; myid++) {
		CPU_ZERO(&mask);
		CPU_SET(myid, &mask);
		if (sched_setaffinity(0, sizeof(mask), &mask) == -1) {
			printf("warning: could not set CPU affinity, continuing\n");
		}

		CPU_ZERO(&get);
		if (sched_getaffinity(0, sizeof(get), &get) == -1) {
			printf("warning: cound not get cpu affinity\n");
		}
		for (i = 0; i < cpu_core_num; i++) {
			if (CPU_ISSET(i, &get)) {
				printf("this process %d is running processor : %d\n",getpid(), i);
			}
		}
	}

	return EXIT_SUCCESS;
}
コード例 #5
0
ファイル: os.c プロジェクト: vilie/javify
int nativeAvailableProcessors() {
#ifdef __UCLIBC__
    return 1;
#else
    return get_nprocs();
#endif
}
コード例 #6
0
ファイル: porting.cpp プロジェクト: WantedGames/freeminer
/*
	Multithreading support
*/
int getNumberOfProcessors() {
#if defined(_SC_NPROCESSORS_ONLN)

	return sysconf(_SC_NPROCESSORS_ONLN);

#elif defined(__FreeBSD__) || defined(__APPLE__)

	unsigned int len, count;
	len = sizeof(count);
	return sysctlbyname("hw.ncpu", &count, &len, NULL, 0);

#elif defined(_GNU_SOURCE)

	return get_nprocs();

#elif defined(_WIN32)

	SYSTEM_INFO sysinfo;
	GetSystemInfo(&sysinfo);
	return sysinfo.dwNumberOfProcessors;

#elif defined(PTW32_VERSION) || defined(__hpux)

	return pthread_num_processors_np();

#else

	return 1;

#endif
}
コード例 #7
0
ファイル: getarch.c プロジェクト: firelf/OpenBLAS
static int get_num_cores(void) {

#ifdef OS_WINDOWS
  SYSTEM_INFO sysinfo;
#elif defined(__FreeBSD__) || defined(__APPLE__)
  int m[2], count;
  size_t len;
#endif
  
#ifdef linux
  return get_nprocs();
  
#elif defined(OS_WINDOWS)

  GetSystemInfo(&sysinfo);
  return sysinfo.dwNumberOfProcessors;

#elif defined(__FreeBSD__) || defined(__APPLE__)
  m[0] = CTL_HW;
  m[1] = HW_NCPU;
  len = sizeof(int);
  sysctl(m, 2, &count, &len, NULL, 0);

  return count;
#else
  return 2;
#endif
}
コード例 #8
0
ファイル: main818.c プロジェクト: pcyin/md5test
int main()
{
    uint32_t seq = 0;
    unsigned int i=0;
    proc_num = get_nprocs();
    gap = 100000000 / proc_num;

    signal(SIGUSR1,sig_handler);
    /*read from stdin*/
    for(i=0; i<16; i++)
    {
        scanf("%2x",&crypto_word[i]);
    }

    //scanf("%s",BUFF);
    //to();

    for(i=0; i<proc_num; i++)
    {
        beginSeq[i] = gap * i;
    }

    createChildProcess(proc_num);

    int waitNum = 0;
    while(wait(NULL))
    {
        waitNum++;
        if(waitNum==proc_num)
            break;
    }

    return 0;
}
コード例 #9
0
ファイル: number_of_cpu.c プロジェクト: jeonghanlee/test
void
main()
{
  printf("get_nprocs_conf %d get_nproc %d\n",  get_nprocs_conf(), get_nprocs());
  printf("----     N_CONF %d ---N_ONLN %d\n",  NPCONF, NPONLN );
  return;
}
コード例 #10
0
int thread_binding_cpu(pthread_t _tid, int cpu_seq)
{
	int cpu_core_num = get_nprocs();
	printf("CPU has %d core\n", cpu_core_num);
	cpu_set_t mask; 
 	cpu_set_t get; 

	_tid = (_tid == 0 ? pthread_self() : _tid);
	/* 绑定 */
	cpu_seq = (cpu_seq >= (cpu_core_num - 1) ? (cpu_core_num - 1) : cpu_seq);
	CPU_ZERO(&mask); 
	CPU_SET(cpu_seq, &mask); 
	if (pthread_setaffinity_np(_tid , sizeof(mask), &mask) == -1)
	{ 
		printf("warning: could not set CPU affinity, continuing\n"); 
		return 1;
	} 
	
	/* 查询绑定结果 */
	CPU_ZERO(&get); 
	if (pthread_getaffinity_np(_tid, sizeof(get), &get) == -1) 
	{ 
		printf("warning: cound not get cpu affinity\n"); 
		return 1;
	} 
	for(int i = 0; i < cpu_core_num; i++) 
	{ 
		if (CPU_ISSET(i, &get)) 
		{ 
			printf("this thread %d is running processor : %d\n", (int)(_tid), i); 
		} 
	}
	return 0;
}
コード例 #11
0
ファイル: unistd.c プロジェクト: SpiritGun/OS-Zero
long
sysconfupd(int name)
{
    long *ptr = sysconftab - MINSYSCONF;
    long  ret = -1;
    
    switch (name) {
        case _SC_NPROCESSORS_ONLN:
            ret = ptr[_SC_NPROCESSORS_ONLN] = get_nprocs();

            break;
        case _SC_NPROCESSORS_CONF:
            ret = ptr[_SC_NPROCESSORS_CONF] = get_nprocs_conf();

            break;
        case _SC_AVPHYS_PAGES:
            ret = ptr[_SC_AVPHYS_PAGES] = get_avphys_pages();

            break;
        case _SC_PHYS_PAGES:
            ret = ptr[_SC_PHYS_PAGES] = get_phys_pages();

            break;
    }

    return ret; 
}
コード例 #12
0
ファイル: main_noq.c プロジェクト: pcyin/md5test
int main()
{
    uint32_t seq = 0;
    unsigned int i=0;
    proc_num = get_nprocs();

    if(proc_num==1)
        proc_num += 2;
    else if(proc_num==2)
        proc_num+=2;

    if(proc_num==3 || proc_num == 4)
        gap = 123000000 / proc_num;
    else
        gap = 100000000 / proc_num;

    /*read from stdin*/
    //scanf("%s",BUFF);
    //to();
    unsigned int temp;
    for(i=0;i<MD5_LEN;i++){
        scanf("%2x",&temp);
        crypto_word[i] = temp;
    }
    //if(proc_num == 1){
//
 //       one_proc();
  //      exit(0);
   // }
	signal(SIGUSR1,sig_handler);
    for(i=0;i<proc_num;i++){
        beginSeq[i] = gap * i;
    }

    createChildProcess(proc_num);

    /*mq_unlink(MSGQUEUE);
    struct mq_attr attr;
    attr.mq_maxmsg = 10000;
    attr.mq_msgsize = MSGSIZE;
    msgQueueId = mq_open(MSGQUEUE,O_RDWR|O_CREAT,0664,&attr);

    createChildProcess(proc_num);

    for(;seq<100000000;seq+=gap){
        mq_send(msgQueueId,&seq,4,0);
    }*/

    int waitNum = 0;
    while(wait(NULL)){
        waitNum++;
        if(waitNum==proc_num)
            break;
    }
    mq_close(msgQueueId);
    mq_unlink(MSGQUEUE);
    return 0;
}
コード例 #13
0
ファイル: ppg-util.c プロジェクト: chergert/perfkit
/**
 * ppg_get_num_cpus:
 *
 * Retrieves the number of CPUs available on the system.
 *
 * Returns: None.
 * Side effects: None.
 */
guint
ppg_get_num_cpus (void)
{
#if __linux__
	return get_nprocs();
#else
	return 1;
#endif
}
コード例 #14
0
ファイル: metrics.c プロジェクト: bdbaddog/monitor-core
g_val_t
cpu_num_func ( void )
{
   g_val_t val;

   /* Use _SC_NPROCESSORS_ONLN to get operating cpus */
   val.uint16 = get_nprocs();

   return val;
}
コード例 #15
0
ファイル: ilspire07.c プロジェクト: migumar2/uiHRDC
//returns the number of cores that will be used to run "child-LZMA-encoders"
int getNprocsToUse() {
	int coresAvailable = get_nprocs();	
	if (coresAvailable <1)  //just in case it fails
		coresAvailable=1;
	else if (coresAvailable > DEFAULT_NPROC)
		coresAvailable--;  //leaves one core unused.
	else 
		coresAvailable=DEFAULT_NPROC;
	
	return coresAvailable;	
}
コード例 #16
0
ファイル: nsp-jobs.c プロジェクト: Michael-Z/Nowspide
NspJobs *
nsp_jobs_new()
{
	NspJobs *jobs = malloc(sizeof(NspJobs));
	
	assert(jobs != NULL);
	
	jobs->pool = g_thread_pool_new ((GFunc)nsp_jobs_main_loop, NULL, get_nprocs()+1, FALSE, NULL );
	
	return jobs;
}
コード例 #17
0
ファイル: hvl.cpp プロジェクト: echmet/HVL_MT
size_t GetNumCPUs()
{
#ifdef LIBHVL_PLATFORM_WIN32
	SYSTEM_INFO sysinfo;
	GetSystemInfo(&sysinfo);
	return sysinfo.dwNumberOfProcessors;
#elif defined LIBHVL_PLATFORM_UNIX
	int cpus = get_nprocs();
	return cpus > 0 ? cpus : 1;
#endif //HVL_THREADING_
}
コード例 #18
0
ファイル: Pin.cpp プロジェクト: SeisSol/SeisSol
cpu_set_t seissol::parallel::getFreeCPUsMask() {
  cpu_set_t workerUnion = getWorkerUnionMask();
  cpu_set_t set;
  CPU_ZERO(&set);
  for (int i = 0; i < get_nprocs() ; ++i) {
    CPU_SET(i, &set);
  }
  CPU_XOR(&set, &set, &workerUnion);

  return set;
}
コード例 #19
0
ファイル: utils.cpp プロジェクト: MLDL/lightlda
int32_t get_CPU_core_num()
{
#if defined(WIN32)  
	SYSTEM_INFO info;
	GetSystemInfo(&info);
	return info.dwNumberOfProcessors;
#elif defined(LINUX) || defined(SOLARIS) || defined(AIX)  
	return get_nprocs();   //GNU fuction  
#else  
#error  unsupported system  
#endif  
}
コード例 #20
0
    void vogl_threading_init()
    {
#ifdef WIN32
        SYSTEM_INFO g_system_info;
        GetSystemInfo(&g_system_info);
        g_number_of_processors = math::maximum<uint>(1U, g_system_info.dwNumberOfProcessors);
#elif defined(__GNUC__)
        g_number_of_processors = math::maximum<int>(1, get_nprocs());
#else
        g_number_of_processors = 1;
#endif
    }
コード例 #21
0
   uint lzham_get_max_helper_threads()
   {
#if defined(__GNUC__)
      uint num_procs = get_nprocs();
      return num_procs ? (num_procs - 1) : 0;
#else
      printf("TODO: lzham_get_max_helper_threads(): Implement system specific func to determine the max # of helper threads\n");

      // Just assume a dual-core machine.
      return 1;
#endif
   }
コード例 #22
0
ファイル: glt.c プロジェクト: adcastel/GLT
GLT_func_prefix void glt_init(int argc, char * argv[]) {
    int num_threads = get_nprocs();
   
    main_team = (glt_team_t *) malloc(sizeof (glt_team_t));
    CHECK(ABT_init(argc, argv),ABT_SUCCESS);

    if (getenv("GLT_NUM_THREADS") != NULL) {
        num_threads = atoi(getenv("GLT_NUM_THREADS"));
    }

    int num_pools = num_threads;

    if (getenv("GLT_NUM_POOLS") != NULL) {
        num_pools = atoi(getenv("GLT_NUM_POOLS"));
    }

    CHECK(ABT_xstream_self(&main_team->master),ABT_SUCCESS);

    main_team->num_xstreams = num_threads;
    main_team->num_pools = num_pools;
    main_team->max_elem = get_nprocs();
    main_team->team = (ABT_xstream *) malloc(sizeof (ABT_xstream) * num_threads);//main_team->max_elem);
    main_team->pools = (ABT_pool *) malloc(sizeof (ABT_pool) * num_pools);//main_team->max_elem);

    for (int i = 0; i < num_pools; i++) {
        CHECK(ABT_pool_create_basic(ABT_POOL_FIFO, ABT_POOL_ACCESS_MPMC, ABT_TRUE,
                &main_team->pools[i]),ABT_SUCCESS);
    }

    CHECK(ABT_xstream_self(&main_team->team[0]),ABT_SUCCESS);
    CHECK(ABT_xstream_set_main_sched_basic(main_team->team[0], ABT_SCHED_DEFAULT,
            1, &main_team->pools[0]),ABT_SUCCESS);

    for (int i = 1; i < num_threads; i++) {
        CHECK(ABT_xstream_create_basic(ABT_SCHED_DEFAULT, 1,
                &main_team->pools[i % main_team->num_pools],
                ABT_SCHED_CONFIG_NULL, &main_team->team[i]),ABT_SUCCESS);
        CHECK(ABT_xstream_start(main_team->team[i]),ABT_SUCCESS);
    }
}
コード例 #23
0
ファイル: loadavg.c プロジェクト: troglobit/watchdogd
static void compensate(double load[])
{
	static double num = 0.0;

	if (num == 0.0) {
		num = (double)get_nprocs();
		if (num == 0.0)
			num = 1.0;
	}

	for (int i = 0; i < 3; i++)
		load[i] /= num;
}
コード例 #24
0
ファイル: core_set.c プロジェクト: danklmn/embb
unsigned int embb_core_count_available() {
#ifdef EMBB_PLATFORM_HAS_HEADER_SYSINFO
  return get_nprocs();
#elif defined EMBB_PLATFORM_HAS_HEADER_SYSCTL
  const size_t kBufferSize = sizeof(unsigned int);
  char buf[kBufferSize];
  size_t len = kBufferSize;
  sysctlbyname("hw.ncpu", buf, &len, NULL, 0);
  return *(unsigned int*)&buf;
#else
#error "No implementation for embb_core_count_available()!"
#endif
}
コード例 #25
0
ファイル: perfx-cpu.c プロジェクト: chergert/perfx
uint32_t
PerfxCpu_GetCount(void)
{
#if defined(__linux__)
   return get_nprocs();
#elif defined(__APPLE__)
   int count = 0;
   size_t size = sizeof(count);
   sysctlbyname("hw.ncpu", &count, &size, NULL, 0);
   return count;
#else
#error "You're platform is not supported."
#endif
}
コード例 #26
0
ファイル: http_server.cpp プロジェクト: moulo1982/MyWarehouse
void http_server::run()
{
    std::vector<std::shared_ptr<std::thread> > threads;
    for (std::size_t i = 0; i < get_nprocs(); ++i)
    {
        std::shared_ptr<std::thread> thread(new std::thread([this](){
            io_service_.run();
        }));
        threads.push_back(thread);
    }

    for (std::size_t i = 0; i < 2; ++i)
        threads[i]->join();
}
コード例 #27
0
ファイル: cpulimit.c プロジェクト: Miliox/android-cpulimit
/* Get the number of CPUs */
static int get_ncpu() {
	int ncpu;
#ifdef _SC_NPROCESSORS_ONLN
	ncpu = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined __APPLE__
	int mib[2] = {CTL_HW, HW_NCPU};
	size_t len = sizeof(ncpu);
	sysctl(mib, 2, &ncpu, &len, NULL, 0);
#elif defined _GNU_SOURCE
	ncpu = get_nprocs();
#else
	ncpu = -1;
#endif
	return ncpu;
}
コード例 #28
0
unsigned possibleThreadCount() {
  #if defined(PTW32_VERSION) || defined(__hpux)
  return pthread_num_processors_np();
  #elif defined(__APPLE__) || defined(__FreeBSD__)
  int count;
  size_t size = sizeof(count);
  return sysctlbyname("hw.ncpu", &count, &size, NULL, 0) ? 0 : count;
  #elif defined(BOOST_HAS_UNISTD_H) && defined(_SC_NPROCESSORS_ONLN)
  int const count = sysconf(_SC_NPROCESSORS_ONLN);
  return (count > 0) ? count : 1;
  #elif defined(_GNU_SOURCE)
  return get_nprocs();
  #else
  return 1;
  #endif
}
コード例 #29
0
int TestMain () {
#if _WIN32||_WIN64 || __linux__ || __FreeBSD_version >= 701000
#if _WIN32||_WIN64
    SYSTEM_INFO si;
    GetSystemInfo(&si);
    if ( si.dwNumberOfProcessors < 2 )
        return Harness::Skipped;
    int availableProcs = (int)si.dwNumberOfProcessors / 2;
    DWORD_PTR mask = 1;
    for ( int i = 1; i < availableProcs; ++i )
        mask |= mask << 1;
    bool err = !SetProcessAffinityMask( GetCurrentProcess(), mask );
#else /* !WIN */
#if __linux__
    int maxProcs = get_nprocs();
    typedef cpu_set_t mask_t;
#if __TBB_MAIN_THREAD_AFFINITY_BROKEN
    #define setaffinity(mask) sched_setaffinity(0 /*get the mask of the calling thread*/, sizeof(mask_t), &mask)
#else
    #define setaffinity(mask) sched_setaffinity(getpid(), sizeof(mask_t), &mask)
#endif
#else /* __FreeBSD__ */
    int maxProcs = sysconf(_SC_NPROCESSORS_ONLN);
    typedef cpuset_t mask_t;
#if __TBB_MAIN_THREAD_AFFINITY_BROKEN
    #define setaffinity(mask) cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask_t), &mask)
#else
    #define setaffinity(mask) cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(mask_t), &mask)
#endif
#endif /* __FreeBSD__ */
    if ( maxProcs < 2 )
        return Harness::Skipped;
    mask_t newMask;
    CPU_ZERO(&newMask);
    int availableProcs = min(maxProcs, (int)sizeof(mask_t) * CHAR_BIT) / 2;
    for ( int i = 0; i < availableProcs; ++i )
        CPU_SET( i, &newMask );
    int err = setaffinity( newMask );
#endif /* !WIN */
    ASSERT( !err, "Setting process affinity failed" );
    ASSERT( tbb::task_scheduler_init::default_num_threads() == availableProcs, NULL );
    ASSERT( (int)tbb::tbb_thread::hardware_concurrency() == availableProcs, NULL );
    return Harness::Done;
#else /* !(WIN || LIN || BSD) */
    return Harness::Skipped;
#endif /* !(WIN || LIN || BSD) */
}
コード例 #30
0
ファイル: thread.cpp プロジェクト: JeffreyZksun/pcassist
    unsigned thread::hardware_concurrency()
    {
#if defined(PTW32_VERSION) || defined(__hpux)
        return pthread_num_processors_np();
#elif defined(__APPLE__) || defined(__FreeBSD__)
        int count;
        size_t size=sizeof(count);
        return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count;
#elif defined(BOOST_HAS_UNISTD_H) && defined(_SC_NPROCESSORS_ONLN)
        int const count=sysconf(_SC_NPROCESSORS_ONLN);
        return (count>0)?count:0;
#elif defined(__GLIBC__)
        return get_nprocs();
#else
        return 0;
#endif
    }