使用线程发现CPU内核数 [英] Discover number of CPU cores using threads

查看:132
本文介绍了使用线程发现CPU内核数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项工作,我必须在Linux上用C编写一个程序(我使用CentOS),该程序使用线程/进程来确定CPU的内核数量. 首先,我尝试以毫秒/微秒为单位显示当前时间,因为我知道可以运行1thread/core(或2并运行HT).但是,到毫秒级,超过10个线程打印了相同的时间,而到微秒级,则没有相同的时间. 其次,我尝试用时钟来测量线程的执行时间,考虑到我有4个内核,同时执行4个线程的执行时间几乎要与执行1一样长. CPU的数量. 你能帮我一些建议吗?

I have an assignment in which i have to write a program in C on Linux(i use CentOS), which uses threads/processes to determine the number of cores from the CPU. Firstly I tried to print the current time in mili/microseconds, because I know that there can run 1thread/core(or 2 with HT). But by miliseconds more than 10 threads printed the same amount of time and by microseconds none were identical. Secondly I tried to measure the execution time of threads with clock, given I have 4 cores, the execution time of 4 threads at the same time should take almost as long as executing 1.But none of my programs could bring me closer to the number of CPU's. Could you help me with some suggestions please?

程序打印当前时间:

pthread_t th[N];     

void* afis ()
{
    //time_t now;
    //time(&now);
    //printf("%s", ctime(&now));
    struct timeval start, end;
    long mtime, seconds, useconds;    

    gettimeofday(&start, NULL);
    // usleep(2000);
    gettimeofday(&end, NULL);

    seconds  = end.tv_sec  - start.tv_sec;
    useconds = end.tv_usec - start.tv_usec;

    mtime = seconds + useconds;

    printf("Thread with TID:%d   Elapsed time: %ld microsecons\n",(unsigned int)pthread_self(), mtime);
}     

int main()
{
    int i;
    for (i=0;i<N;i++)
    {
        pthread_create(&th[i],NULL,afis,NULL);
    }
    for(i=0;i<N;i++)
    {
        pthread_join(th[i],NULL);
    }
    return 0;
}

程序测量处理时间:

pthread_t th[N];    

void* func(void* arg)
{
    int x;
    int k;
    int n=(int)arg;
    for(k=0;k<10000000;k+=n)
    {
        x=0;
    }
}


int main()
{
    int i,j;
    for (i=0;i<N;i++)
    {
        clock_t start, end, total;
        start=clock();
        for(j=0;j<i;j++)
        {
            printf("execution nr: %d\n",i);
            pthread_create(&th[j],NULL,func,(int*)i);
        }
        for(j=0;j<i;j++)
        {
            pthread_join(th[j],NULL);
        }
        end=clock();
        printf("start = %ld, end = %ld\n", start, end);
        total=((double)(end-start) )/ CLOCKS_PER_SEC;
        printf("total=%ld\n",total);
    }

    return 0;
}

推荐答案

您可能应该做的是(用伪代码):

What you should probably do is (in pseudo-code):

get the actual time (start time)
start 40 threads
    each busy for one second;
wait for all of them to stop
get the actual time (stop time)

如果您分析执行40个线程所花费的时间,您将知道内核数,或者至少可以做出一个假设:

If you analyze the time it took for the 40 threads to execute, you will know the number of cores, or at least you can make an assumption:

if the time was around 40s: you have one core
else if the time was around 20s: you have two
and so on

您当然可以调整启动线程的数量以及让它们进入睡眠状态的时间,但是我猜想,如果只睡眠一毫秒,由于上下文切换和后台任务,您获得的时间将不具有代表性.

You could of course adapt the number of threads you start, aswell as the time you let them sleep, but I guess that if you sleep for a millisecond only you could get times that are not representative due to context switches and background tasks.

与其在线程中休眠,不如执行以下操作:

Instead of sleeping in the thread do something like:

highlyCpuIntensiveTask()
{
    //calculate something that takes up the time x (best would be above 1 second)
}

您只执行一次就无需启动线程,这将花费时间x.那个时间就是参考时间.

You execute it once without starting a thread, what would take up the time x. That time will be the reference time.

如果通过添加越来越多的pthread(y)来执行相同的功能,而您没有比x花费更多的时间,那么您就知道您至少有y核心.在某些时候,使用z线程,时间将在2x左右,此时您将知道拥有z-1内核.

If, by adding more and more pthreads (y), executing the same function, you do not use up considerably more time than x, then you know that you do most probably have at least y cores. At some point, with z threads, the time will be around 2x, at which point you'll know that you have z-1 cores.

这篇关于使用线程发现CPU内核数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆