如何从一个C程序100%的CPU使用率 [英] How to get 100% CPU usage from a C program

查看:292
本文介绍了如何从一个C程序100%的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常有趣的问题,所以让我设置了现场。我在计算国家博物馆工作,我们刚刚成功地从1992年的运行得到一台Cray Y型MP EL超级计算机,我们真的希望看到它的速度有多快!

This is quite an interesting question so let me set the scene. I work at The National Museum of Computing, and we have just managed to get a Cray Y-MP EL super computer from 1992 running, and we really want to see how fast it can go!

我们决定这样做的最好办法是写一个简单的C程序,它会计算质数,并显示它花了多长时间这样做,然后运行一个快速现代化的台式电脑上的程序和结果进行比较。

We decided the best way to do this was to write a simple C program that would calculate prime numbers and show how long it took to do so, then run the program on a fast modern desktop PC and compare the results.

我们很快想出了这个code来算质数:

We quickly came up with this code to count prime numbers:

#include <stdio.h>
#include <time.h>

void main() {
    clock_t start, end;
    double runTime;
    start = clock();
    int i, num = 1, primes = 0;

    while (num <= 1000) { 
        i = 2; 
        while (i <= num) { 
            if(num % i == 0)
                break;
            i++; 
        }
        if (i == num)
            primes++;

        system("clear");
        printf("%d prime numbers calculated\n",primes);
        num++;
    }

    end = clock();
    runTime = (end - start) / (double) CLOCKS_PER_SEC;
    printf("This machine calculated all %d prime numbers under 1000 in %g seconds\n", primes, runTime);
}

这对我们的双核笔记本电脑运行Ubuntu(克雷运行UNICOS),完美工作,让100%的CPU使用率,并采取大约10分钟左右。当我回到家,我决定尝试一下我的六角核心的现代游戏的PC上,这是我们得到我们的第一个问题。

Which on our dual core laptop running Ubuntu (The Cray runs UNICOS), worked perfectly, getting 100% CPU usage and taking about 10 minutes or so. When I got home I decided to try it on my hex-core modern gaming PC, and this is where we get our first issues.

我第一次改编code在Windows上运行,因为这是游戏PC是使用什么,而是伤心地发现,这个过程只是得到了CPU的功率的15%左右。我计算过,必须是Windows的Windows是,所以我启动到Ubuntu的思维,Ubuntu的允许的过程与充分发挥其潜力运行,因为它已经在我的笔记本电脑前面做一个Live CD。

I first adapted the code to run on Windows since that is what the gaming PC was using, but was saddened to find that the process was only getting about 15% of the CPU's power. I figured that must be Windows being Windows, so I booted into a Live CD of Ubuntu thinking that Ubuntu would allow the process to run with its full potential as it had done earlier on my laptop.

不过,我只得到了5%的使用率!所以我的问题是,如何能适应我的程序在任何的Windows 7我的游戏机上运行,​​或在100%的CPU利用率住Linux呢?这将是伟大的,但不是必需的另一件事是,如果最终产品可以是一个.exe文件,可以很容易地分布在Windows机器上运行。

However I only got 5% usage! So my question is, how can I adapt the program to run on my gaming machine in either Windows 7 or live Linux at 100% CPU utilisation? Another thing that would be great but not necessary is if the end product can be one .exe that could be easily distributed and ran on Windows machines.

非常感谢!

P.S。当然,这个方案并没有真正与Crays 8专业处理器的工作,这是一个整体的另一个问题......如果你知道关于优化code至90的克雷超级计算机工作的事情给我们留言呢!

P.S. Of course this program didn't really work with the Crays 8 specialist processors, and that is a whole other issue... If you know anything about optimising code to work on 90's Cray super computers give us a shout too!

推荐答案

如果你想100%的CPU,你需要使用超过1个核心。要做到这一点,你需要多线程。

If you want 100% CPU, you need to use more than 1 core. To do that, you need multiple threads.

这里的水货版本使用OpenMP:

我不得不加大限百万,使其采取1秒以上,我的机器上。

I had to increase the limit to 1000000 to make it take more than 1 second on my machine.

#include <stdio.h>
#include <time.h>
#include <omp.h>

int main() {
    double start, end;
    double runTime;
    start = omp_get_wtime();
    int num = 1,primes = 0;

    int limit = 1000000;

#pragma omp parallel for schedule(dynamic) reduction(+ : primes)
    for (num = 1; num <= limit; num++) { 
        int i = 2; 
        while(i <= num) { 
            if(num % i == 0)
                break;
            i++; 
        }
        if(i == num)
            primes++;
//      printf("%d prime numbers calculated\n",primes);
    }

    end = omp_get_wtime();
    runTime = end - start;
    printf("This machine calculated all %d prime numbers under %d in %g seconds\n",primes,limit,runTime);

    return 0;
}

输出:

本机29.753秒计算百万下的所有78498素数

This machine calculated all 78498 prime numbers under 1000000 in 29.753 seconds

这是你的100%的CPU:

这篇关于如何从一个C程序100%的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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