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?
|
|
|
|
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
|
|
|
princec
|
 |
«
Reply #2 - Posted
2013-10-16 14:10:54 » |
|
fork()! Cas 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
SHC
|
 |
«
Reply #3 - Posted
2013-10-16 14:20:20 » |
|
@princecI don't know what 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?
|
|
|
|
SHC
|
 |
«
Reply #4 - Posted
2013-10-16 14:27:58 » |
|
Adding that 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.
|
|
|
|
Riven
|
 |
«
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!
|
|
|
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 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.
|
|
|
|
Riven
|
 |
«
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!
|
|
|
SHC
|
 |
«
Reply #8 - Posted
2013-10-16 14:39:00 » |
|
Oh I see. How can I prevent it to fork on the childs?
|
|
|
|
Riven
|
 |
«
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) { 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!
|
|
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.
|
|
|
|
Riven
|
 |
«
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!
|
|
|
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.
|
|
|
|
Riven
|
 |
«
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!
|
|
|
SHC
|
 |
«
Reply #14 - Posted
2013-10-16 16:03:50 » |
|
Oops, a typo. Now it works perfectly.
|
|
|
|
ags1
|
 |
«
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.
|
|
|
|
Riven
|
 |
«
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!
|
|
|
ags1
|
 |
«
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.
|
|
|
|
Riven
|
 |
«
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!
|
|
|
ags1
|
 |
«
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.
|
|
|
|
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.
|
|
|
|
princec
|
 |
«
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 
|
|
|
|
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.
|
|
|
|
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. 
|
|
|
|
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.  What do you mean?
|
|
|
|
|