4

I have a Raspberry Pi 4, running the latest Raspberry Pi OS version. I have a C++ program running on the Raspberry Pi and i want to distribute the execution of this program on cores 1+2+3, while Raspberry Pi OS runs on core 0.

Down below is a picture of the htop-command:

enter image description here

As you can see, my program is relatively CPU intensive, which is why I want to distribute the execution of the program on core 1+2+3 (core 2+3+4 on the picture above). Core 0 (core 1 on the picture above) runs Raspberry Pi OS. Right now, the execution of my C++ program is only happening on core 2, and I don't know why...

Below is the systemd startup-service:

[Unit]
Description=Automatically start software
After=graphical.target

[Service] ExecStart=sudo ./path/to/program Nice=-20 LimitNice=-20 CPUAffinity=1 2 3 Restart=always

[Install] WantedBy=graphical.target

The program is set to highest priority (nice=-20) and i'm using CPUAffinity=1 2 3 to let core 1+2+3 work on the program.

Below i the cmdline.txt file:

console=serial0,115200 console=tty1 root=PARTUUID=97f8b39e-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles isolcpus=1,2,3

Here I isolate CPU core 1+2+3.

Below is a response when i run the taskset -cp<PID>-command:

pid 651's current affinity list: 1-3

The affinity list is correct = core 1-3.

My problem: When running the htop command, the program is still only executed on core 1 (core 2 in htop). How do I distribute the execution of the program so it is run on core 1+2+3?

If i run the exact same program, with the same setup in the systemd startup-service and cmdline.txt files, but on a dietPi OS, the workload of the program is distributed among core 1+2+3, where core 0 runs the dietPi OS:

enter image description here

Here, the workload per core is ~30%, and in Raspberry Pi OS it is ~90% on core 1... (core 2 in the first htop picture).

jrn6270
  • 143
  • 1
  • 10

1 Answers1

6

Check your program is actually multi-threaded enough to run in parallel.

(Also you shouldn't need the sudo - IIRC systemd will run your command as root by default)

Douglas Leeder
  • 341
  • 1
  • 6
  • My program only has 1 main thread. And the startup-service doesn't work if I omit sudo – jrn6270 Jun 24 '20 at 11:27
  • 3
    If your program only has 1 computing thread, it can only use one core. – Douglas Leeder Jun 24 '20 at 13:39
  • The program is written using C++. But how can dietPi then use 3 cores on the same program? – jrn6270 Jun 24 '20 at 13:46
  • 2
    DietPi is an operating system, just as Raspberry OS is. So it has complete control of all the hardware. What I suspect is happening is that it is switching the cores, only one core at a time is running your code but because it switches quick enough it appears to be using all the cores. – Nick Jun 24 '20 at 14:18
  • Does running your program on DietPi actually have 3x the through-put? If it takes about the same time for given input, then it might just be the way it's being reported. If you are computing on one thread, only one CPU core can be used. – Douglas Leeder Jun 24 '20 at 19:50
  • Ahh okay. I see your point now. My C++ program only has 1 main()-thread, so my RPi 4 can only utilize 1 core to run it. DietPi uses some quick switching to use core 1+2+3 to run the same program, but as you, @Douglas Leeder, pointed out, I don't think it hsa 3 times the through-put. Thanks for the answers :-) – jrn6270 Jun 25 '20 at 06:34