Here is a small piece of C-code that I wrote for stress testing CPUs on an HPC cluster. This testing is important for various performance optimization studies. This code will maximize the CPU usage on the node that it is executed on by creating separate threads.
cpu_8core_c.c
: (code to utilize the full 8-cores)
/* derlemek için gcc -w -lpthread cpu_8core_c.c -o cpu_8core_c */
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void
wait(void)
{
int i = 0;
while (1 < 5) {
i++;
}
}
int
main(void)
{
pthread_t thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8;
int err1, err2, err3, err4, err5, err6, err7, err8;
err1 = pthread_create(&thread1, NULL, wait, NULL);
err2 = pthread_create(&thread2, NULL, wait, NULL);
err3 = pthread_create(&thread2, NULL, wait, NULL);
err4 = pthread_create(&thread2, NULL, wait, NULL);
err5 = pthread_create(&thread2, NULL, wait, NULL);
err6 = pthread_create(&thread2, NULL, wait, NULL);
err7 = pthread_create(&thread2, NULL, wait, NULL);
err8 = pthread_create(&thread2, NULL, wait, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
pthread_join(thread5, NULL);
pthread_join(thread6, NULL);
pthread_join(thread7, NULL);
pthread_join(thread8, NULL);
return 0;
}
to compile:
gcc -w -lpthread cpu_8core_c.c -o cpu_8core_c
Below is a 16-core version.
cpu_16core_c.c
:
/* derlemek için: gcc -w -lpthread cpu_16core_c.c -o cpu_16core_c */
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void
wait(void)
{
int i = 0;
while (1 < 5) {
i++;
}
}
int
main(void)
{
pthread_t thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8, thread9, thread10, thread11, thread12, thread13, thread14, thread15, thread16;
int err1, err2, err3, err4, err5, err6, err7, err8, err9, err10, err11, err12, err13, err14, err15, err16;
err1 = pthread_create(&thread1, NULL, wait, NULL);
err2 = pthread_create(&thread2, NULL, wait, NULL);
err3 = pthread_create(&thread2, NULL, wait, NULL);
err4 = pthread_create(&thread2, NULL, wait, NULL);
err5 = pthread_create(&thread2, NULL, wait, NULL);
err6 = pthread_create(&thread2, NULL, wait, NULL);
err7 = pthread_create(&thread2, NULL, wait, NULL);
err8 = pthread_create(&thread2, NULL, wait, NULL);
err9 = pthread_create(&thread1, NULL, wait, NULL);
err10 = pthread_create(&thread2, NULL, wait, NULL);
err11 = pthread_create(&thread2, NULL, wait, NULL);
err12 = pthread_create(&thread2, NULL, wait, NULL);
err13 = pthread_create(&thread2, NULL, wait, NULL);
err14 = pthread_create(&thread2, NULL, wait, NULL);
err15 = pthread_create(&thread2, NULL, wait, NULL);
err16 = pthread_create(&thread2, NULL, wait, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
pthread_join(thread5, NULL);
pthread_join(thread6, NULL);
pthread_join(thread7, NULL);
pthread_join(thread8, NULL);
pthread_join(thread8, NULL);
pthread_join(thread10, NULL);
pthread_join(thread11, NULL);
pthread_join(thread12, NULL);
pthread_join(thread13, NULL);
pthread_join(thread14, NULL);
pthread_join(thread15, NULL);
pthread_join(thread16, NULL);
return 0;
}
gcc -w -lpthread cpu_16core_c.c -o cpu_16core_c
Php version:
[root@login cpu]# cat cpu_multicore_php.php
<?php
$number_of_cores = 8;
for ($i=0; $i<$number_of_cores; $i++) {
// open 8 processes
for ($j = 0; $j < $number_of_cores; $j++) {
$pipe[$j] = popen('php cpu_singlecore_php.php', 'w');
}
// wait for them to finish
for ($j = 0; $j < $number_of_cores; ++$j) {
pclose($pipe[$j]);
}
}
[root@login cpu]# cat cpu_singlecore_php.php
<?php
$sonuc=0; $i=0;
for ($i=1; $i<999999999; $i++)
while (1 >0) {
$sonuc = $sonuc+ $i;
$sonuc2 = $sonuc2+ $i;
$i++;
}