Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How can I get full CPU usage with C?  (Read 2946 times)
0 Members and 1 Guest are viewing this topic.
Offline SHC
« Posted 2013-10-16 13:55:23 »

Today is our first class with C in college. After introducing the language, our lecturer said us to write some programs and we did that. She also asked me to try to write a program that uses 100% CPU. My initial thought was using a non-termination while loop, so I wrote this program.

1  
2  
3  
4  
int main()
{
    while (1){}
}

This only produced 50% of load on my dual core cpu. So I think that it is using only core. So I need a way to make it work on all the cores at the same time. How can I achieve this?

Offline xsvenson
« Reply #1 - Posted 2013-10-16 13:57:35 »

Run it in parallel, how to do that in C, no idea.

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Online princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2013-10-16 14:10:54 »

fork()!

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-10-16 14:20:20 »

@princec
I don't know what
fork()
does. Can you explain that a bit? I now changed the code to

1  
2  
3  
4  
5  
6  
7  
8  
#include <stdio.h>

int main()
{
    int pid = fork();

    while (1){}
}

and gcc is saying that

1  
2  
3  
4  
5  
6  
localhost:Desktop sriharshachilakapati$ gcc fullCpu.c
fullCpu.c:5:15: warning: implicit declaration of function 'fork' is invalid in
      C99 [-Wimplicit-function-declaration]
    int pid = fork();
              ^
1 warning generated.

What does this mean?

Offline SHC
« Reply #4 - Posted 2013-10-16 14:27:58 »

Adding that
fork()
two times worked, but it still prints that warning. What should I explain to my lecturer about that function? I'm now searching for it.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2013-10-16 14:30:47 »

This may help to define the fork function
1  
2  
3  
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>


Mind you, don't just fork, as that will spawn new child processes recursively, until the OS crashes.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SHC
« Reply #6 - Posted 2013-10-16 14:35:37 »

Thanks princec for your help. It's working fully and I found a documentation for the
fork()
function here. I changed my final code to this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
#include <stdio.h>
#define NUM_CORES = 2

int main()
{
    for (int i=0; i<NUM_CORES-1; i++)
    {
        fork();
    }

    while (1){}
}

Thanks again princec.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2013-10-16 14:36:53 »

when NUM_CORES > 2, this will spawn way too many processes. Remember that the forked processes will start to fork too.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SHC
« Reply #8 - Posted 2013-10-16 14:39:00 »

Oh I see. How can I prevent it to fork on the childs?

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2013-10-16 14:44:52 »

Oh I see. How can I prevent it to fork on the childs?

By handling control flow appropriately, based on the returned value, like:
1  
2  
3  
4  
5  
    for (int i=0; i<n; i++) {
        if(fork() == 0) { // is child process? then perform busy-loop
            while(1){}
        }
    }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #10 - Posted 2013-10-16 14:51:53 »

This created exactly two processes in activity monitor.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
#include <stdio.h>

int main()
{
    int ret = -1;
    int numCores = 2;

    if (ret != 0)
    {
        for (int i=0; i<numCores-1; i++)
        {
            ret = fork();
        }
    }

    while (1){}
}

Thanks princec and Riven.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2013-10-16 14:55:44 »

try setting numCores to 8 then, and be surprised

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SHC
« Reply #12 - Posted 2013-10-16 15:02:44 »

Ghosh! It created 128 processes. I changed it to this and it creates exactly the same number of processes for each core.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
#include <stdio.h>

int main()
{
    int numCores = 8;

    for (int i=0; i<numCores; i++)
    {
        if (fork() == 0)
        {
            while (1){}
        }
    }
}

Thanks again.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2013-10-16 15:30:29 »

Your code doesn't work for 1 core

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SHC
« Reply #14 - Posted 2013-10-16 16:03:50 »

Oops, a typo. Now it works perfectly.

Offline ags1

JGO Wizard


Medals: 72
Projects: 3
Exp: 5 years


Make code not war!


« Reply #15 - Posted 2013-10-16 20:58:59 »

I find that running threads equal to the number of cores does not necessarily mean you will get full CPU usage. The OS will try to favor a single core if possible (as it can be clocked higher) so if you spawn, say, three threads on an 8 thread CPU, you might find only one core in action. It's all a bit murky. If you spawn enough threads, and that might mean a few more than eight in this case, then you will shoot up in a nonlinear fashion to full CPU usage.

At least that's true with Java and using default thread priority - perhaps C has more luck.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2013-10-16 21:05:52 »

if you spawn, say, three threads on an 8 thread CPU, you might find only one core in action.
Eh, it's actually quite the opposite. If you even spawn 1 thread, it will be moved among all cores. Adding more threads will spread the load even better over all cores.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ags1

JGO Wizard


Medals: 72
Projects: 3
Exp: 5 years


Make code not war!


« Reply #17 - Posted 2013-10-16 21:25:17 »

Not sure what you mean. If I have one thread on an i7, I get x performance, if I have 10 threads I get 6x performance (and flattens out at that point). However the increase is not linear and often you get no more than x performance, even at, say, 4 threads. The performance never goes lower than x, so I'm guessing that the threads are sometimes pushed onto a single core.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2013-10-16 21:31:18 »

Contention and hyper threading muddy the waters, but you can be rest assured that the OS will not try to push more than 1 thread on 1 core exclusively. As said, it's actually the opposite: 1 thread will hop between all cores.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ags1

JGO Wizard


Medals: 72
Projects: 3
Exp: 5 years


Make code not war!


« Reply #19 - Posted 2013-10-16 22:02:51 »

Yes, I know the thread moves around between cores, but a single thread is never simultaneously on more than one core. Apparently if the cores are in any way busy (as they very likely are) windows is not too smart about what core to pick for a new thread.

In the i7 case, it only takes 4 threads to occupy all the (real) cores, and then apparently windows gets a bit random about where it allocates new threads.

EDIT: I've tried to find a reference for my initial assertion that the scheduler would favor a single core wrt turbo boost, but i can't find one. Looks like I must have dreamed that one.

Offline Roquen
« Reply #20 - Posted 2013-10-17 09:24:27 »

In my experience the windows scheduler is quite good.  Remember if you have hyperthreading then 2 threads will be running on a given core.
Online princec

« JGO Spiffy Duke »


Medals: 422
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #21 - Posted 2013-10-17 09:57:53 »

In general schedulers will try to keep a thread on a particular core if they can (but there are many reasons that they can't), one reason being that hopping between cores trashes the L1 cache for the thread. There are so many ways for something to get in the way of this happening though that it rarely happens unless you force it by setting affinity and other wizardry.

Cas Smiley

Offline SHC
« Reply #22 - Posted 2013-10-17 13:15:53 »

Thanks again princec and Riven. I'm the only one in the class to complete the problem. My lecturer actually said that it is not possible to get full cpu usage, the cpu moves it among cores, but I said I've seen a thread on the net that showed a program that actually did that. So she asked me to write a program. Writing this program in a computer in the lab has hanged that cpu and we've to restart it to get it working again and I won in this case. And today, I got the full marks in the lab session. I'm called by the HOD and our HOD asked me the details and I showed her my GL Tutorial Series. She made some good points from that and I'm going to give a tutorial lecture the next monday in the college.

Thanks again.

Offline relminator
« Reply #23 - Posted 2013-10-17 15:59:13 »

Thanks again princec and Riven. I'm the only one in the class to complete the problem. My lecturer actually said that it is not possible to get full cpu usage, the cpu moves it among cores, but I said I've seen a thread on the net that showed a program that actually did that. So she asked me to write a program. Writing this program in a computer in the lab has hanged that cpu and we've to restart it to get it working again and I won in this case. And today, I got the full marks in the lab session. I'm called by the HOD and our HOD asked me the details and I showed her my GL Tutorial Series. She made some good points from that and I'm going to give a tutorial lecture the next monday in the college.

Thanks again.

Which only means, that if you knew Eintein's age at the time of his death, you are likely a bad-ass coder. Smiley
Offline SHC
« Reply #24 - Posted 2013-10-17 16:00:09 »

Which only means, that if you knew Eintein's age at the time of his death, you are likely a bad-ass coder. Smiley

What do you mean?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (22 views)
2014-11-25 12:00:59

SHC (20 views)
2014-11-25 11:53:45

Norakomi (18 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (67 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!