6

I have to test an embedded computer for the most extreme conditions of generated heat and current draw, and to do so I want to write a program that employs the CPU resource as much as possible of a quad core CPU (one thread per core). Can you suggest something that would be very CPU hungry?

I have to do this for Linux on a ARMv7 and the language is C or C++, the other examples I have found are either for Windows or not in C/C++.

I am trying something like this on my Windows computer and apparently it is working as it takes 12% of total CPU power (which is a i7 quad core 2 threads per core):

float x = 1.5f;
while (1)
{
    x *= sin(x) / atan(x) * tanh(x) * sqrt(x);
}

I don't know how to make it multi-thread.

Mark Miles
  • 706
  • 8
  • 20

3 Answers3

4

Your code is serial. You have eight available threads (4 cores * 2 threads per core = 8 total threads), and your current code uses one of them for 1 thread / 8 available = 12.5% of your CPU. If you have to write your own code (and not use a pre-existing intensive code as already suggested by others), I would recommend putting a #pragma omp parallel above your while loop and compiling with the -fopenmp flag (assuming you are using MinGW, if not, the exact option may vary) so that you use all of the available threads instead.

wolfPack88
  • 4,163
  • 4
  • 32
  • 47
  • 1
    Wow! Why didn't I think of OpenMP?! It did instantly use 100% of my i7 when I compiled this code with /openmp on MSVC2013. I think this will as well work with Linux... Going to try right now. – Mark Miles Jan 02 '15 at 22:17
  • @MarkMiles: Indeed it should work with Linux as well, unless you've changed the environment variable `OMP_MAX_NUM_THREADS` to be something less than the maximum number of threads available on the machine. – wolfPack88 Jan 02 '15 at 22:19
2

It depends a bit on what you mean by "maximum CPU load".

With regards to CPU usage, basically anything will work. Just keep in mind that you'll need as many threads (or as many instances of the executable) as your CPU has cores.

What you will need to keep in mind, though, is that CPU usage is not the be-all and end-all of power usage in a SoC. Other aspects which you will need to keep in mind include:

  • Memory access. The application you're currently using doesn't touch memory at all.

  • Other on-die peripherals, such as flash controllers, SPI/I2C/UART drivers, etc.

  • A GPU, if your SoC includes one. (This will easily dwarf the power usage of everything else I've mentioned so far!)

  • Off-die peripherals: flash, memory, display, battery chargers, whatever else is in your device.

  • It's all correct, but I have mentioned the CPU alone because the final application of this SoC will mostly employ the CPU and not all the rest. The GPU won't work at all since the OS is GUI-less; the disk will only be used for booting up; memory and other controllers will have a minor usage. The final application will make much use of the CPU as the program does mostly math operations and will write the result to a USB serial device. – Mark Miles Jan 02 '15 at 22:14
0

Does it have to be a program of your own? You could always just take some existing code burning a lot of cycles, such as a raytracer or Bitcoin, and run several of those.

Christopher Creutzig
  • 8,656
  • 35
  • 45
  • Well I was looking for the 'easiest' way. A complex math function should do the trick but it is important that it runs on all available cores of a multi-core CPU. – Mark Miles Jan 02 '15 at 21:57
  • Using readily existing code sounds easier than writing new code. Running four processes is the easiest thing I can think of to give work to all four cores. – Christopher Creutzig Jan 02 '15 at 22:03