Use all cpu linux

Compile a C program with GCC, so that it can use all cpu cores in linux

I have a sample C program for addition. When I compile and run it with GCC it is using only one CPU core. Is there any way to compile a C program so that it can use all CPU cores in Linux. I used to compile like gcc -O3 malloc.c Code:

#include #include #include int main() < float *ptr; unsigned long long i; ptr = (float*) malloc(8000000000 * sizeof(float)); for(i=0; iclock_t tic = clock(); for(i=0; i clock_t toc = clock(); printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); return 0; > 

If you use the make buildtool then you can use -j flag, where you can job simultaneously. For 4 cores you can give like make -j4 , is that what your asking?

@BasileStarynkevitch, I wasn’t sure what OP asked for, that’s why I just raised the question for clarification. If he wants to do this at runtime then I agree with you no point in using make .

4 Answers 4

Is there any way to compile a C program so that it can use all CPU cores in Linux.

No, not as magically as you want it to happen. Parallelization of programs is a very difficult subject and in general cannot be done automagically. BTW, parallel programs might not be as efficient as you wish them to be (be aware of Amdahl’s law).

However, you could design and code a parallel program. You might for example use posix threads. Beware, it is tricky! Read first some Pthread tutorial. You won’t be sure that all cores would be used (since they are managed by the kernel), but that is in practice very likely. Read also about processor affinity.

You could also use OpenMP or OpenACC. You could code some of your numerical kernels using OpenCL. You could have a multi-processing approach (e.g. forking several processes, using inter-process communications), perhaps using MPI. Look also into the MapReduce approach, the 0mq library (and many others).

Читайте также:  Decoding base64 in linux

You could read something on OSes, e.g. Operating Systems: Three Easy Pieces. You could also read something on Linux system programming, e.g. Advanced Linux Programming (or some newer book). See also intro(2) and syscalls(2) & pthreads(7).

Be aware that designing, coding and debugging a parallel (or concurrent, or distributed) application is very difficult. Take into account the cost of development time (and the time, probably years, needed to acquire the relevant skills). There is No Silver Bullet!

(it is not very realistic to transform an existing real-life sequential application into a parallel one; you usually have to design a parallel program from scratch)

Источник

Use all cpu linux

increase Ubuntu booting time (reduce Ubuntu boot time)

This another post how to make Ubuntu boot faster, exactly increase Ubuntu booting time (reduce Ubuntu boot time) by using all cores during startup. We’ll make a little tweak and test it using GeekBench scores to know the results before and after modification.

There are various factors come into play for a computer to boot up fast some of them: hardware spec, number services and programs run (at system startup) during booting time. Let’s go to the topic: Make Linux Ubuntu use all cpu cores to speed up booting.

Before make any modification, how to check how many cores your processor is running and get memory info on your Linux Ubuntu? i use Geekbench test to gathering these information, please do this test as comparative material.

If you don’t know yet how to use Geekbench, please read get memory info, how to check how many processors are running in linux Ubuntu. i have the result see image which i mark as before. Next, make modification in config file then run Geekbench once more to compare both result before and after we made modification.

1. First open your terminal (ctrl +alt + T)
use this command to open and modify /etc/init.d/rc using gedit GUI text editor as root.

You can replace gedit with other GUI text editor you like. gksudo is not installed by default, if you don’t have gksudo installed yet, install gksudo using command : sudo apt-get install gksu .

Читайте также:  Creating file systems on linux

2. Editing configuration file
Configuration file has open by execute the first command above, now find word «Concurrency» (without quote) for faster result use shortcut keys (Ctrl + F) then type «Concurrency» in search box. ex result:

#Concurrency=makefile # disable startpar, incompatible with "task" upstart jobs Concurrency=none

modify «CONCURRENCY=makefile» and «Concurrency=none» (see image before & after), it should be look like this:

CONCURRENCY=makefile # disable startpar, incompatible with "task" upstart jobs #CONCURRENCY=none

Save and close your Gedit text editor.

Now you can run Geekbench test once more then you’ll see the difference of Geekbench score for your CPU cores before and after modify file above.

Another way to make Ubuntu boot faster, We’ve posted how to disable unwanted startup services & applications on Ubuntu to increase booting time, you can read detail article here.

Источник

Making sort use all CPU CORES

enter image description here

htop is showing that the «sort» command only uses 1 CPU CORE (on an RPM based Linux distro). How can I tell it to use all the available CPU CORES?

2 Answers 2

—parallel=N change the number of sorts run concurrently to N

This was introduced in coreutils 8.6, with a lot of bugfixes in version 8.8. Older distros (e.g. RHEL6) don’t have this.

If you know the PID of the process, you could set the number of CPU cores as discussed here.

    If you do not have taskset , you could install it as,

sudo yum install util-linux 

You could also launch the program mentioning specific CPU cores. So all these details are present in the above referenced link.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Читайте также:  Сколько работает сервер linux

Источник

How can I speed up C program in terminal (use all of CPU speed)

I am using Ubuntu 18.10 and running my own program using terminal, which requires many calculations, but when I open task manager, terminal CPU usage (including running program) is never taking more than 26%. Here is very simple program, that behaves the same way. I would like to know, how can I force my program (or terminal) to use all of my CPU speed. I also tried to run multiple terminals and start program, or use threads in my code, but it appears that those 26% splits into these terminals, and run slowly. Is it terminal limit? How can I fix it and make program run faster? Thank you

#include int main() < int a=0; while(1)< printf("%d\n",a); >return 0; > 

The program you show is mostly dependent on output to the terminal, which is relatively slow. I/O bound processes will have a hard time utilizing the CPU in full. Remove the printf call, and only do a++ and you should see the CPU utilization skyrocket (but only for the core the process is running on).

Your program has undefined behavior due to overflow of signed type. You got to correct that before trying anything else.

That’s still I/O bound. If you redirect to /dev/null the output will be discarded and you might get higher utilization, but it’s still an I/O bound process. To stop it you should not have any output (or input) at all.

“Speed up program” and “use all of CPU speed” are completely different things. Car trips are not faster when you take a long roundabout highway route that lets you race your engine instead of a much shorter route with a few stop signs.

The “25%” statistic in a system with four processors suggests CPU use is being reported as a percentage of all available processor time, not a percentage of one processor’s time. In this case, a single-threaded program cannot use more than 25% of CPU, because it can execute on only one processor at a time. To use more CPU, you would have to write a multithreaded program that executed several things simultaneously.

Источник

Оцените статью
Adblock
detector