Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active February 8, 2024 19:07
Show Gist options
  • Save divinity76/ef1e297fc4ec8d53f5d4d64aeac16e21 to your computer and use it in GitHub Desktop.
Save divinity76/ef1e297fc4ec8d53f5d4d64aeac16e21 to your computer and use it in GitHub Desktop.
sets "compute mode" on AMD GPU's in linux
#!/usr/bin/env php
<?php
declare(strict_types = 1);
// this does NOT work on my ubuntu 20.04, look at the other files for a 20.04 version
if (posix_geteuid () !== 0) {
die ( "error: this script requires root privileges, re-run it as root." );
}
$amdgpu_dir = '/sys/bus/pci/drivers/amdgpu';
$dirs = array_filter ( array_map ( 'trim', glob ( $amdgpu_dir.DIRECTORY_SEPARATOR.'*', GLOB_NOSORT | GLOB_ONLYDIR | GLOB_MARK ) ), function (string $str) {
return (is_writable ( $str."power_dpm_force_performance_level" ) && is_writable ( $str.'pp_compute_power_profile' ));
} );
$max = count ( $dirs );
if ($max === 0) {
die ( "error: found 0 applicable devices in $amdgpu_dir\n" );
}
echo "found $max applicable amdgpu devices\n";
$i = 0;
foreach ( $dirs as $gpu ) {
++ $i;
echo "setting card {$i}/{$max}: {$gpu} ..";
my_write ( $gpu."power_dpm_force_performance_level", "auto" );
my_write ( $gpu."pp_compute_power_profile", "set" );
echo ". done\n";
}
die ( "finished!\n" );
function my_write(string $file, string $data, bool $append = false) {
if (! is_writable ( $file )) {
throw new \RuntimeException ( "$file is not writable!" );
}
$len = strlen ( $data );
$written = file_put_contents ( $file, $data, $append ? FILE_APPEND : 0 );
if ($written !== $len) {
throw new \RuntimeException ( "tried to write $len bytes, but could only write $written bytes, file: $file" );
}
return $written;
}
#!/usr/bin/env php
<?php
declare(strict_types = 1);
// this does NOT work on ubuntu18.04, look at other files for an 18.04 version
if (posix_geteuid () !== 0) {
die ( "error: this script requires root privileges, re-run it as root." );
}
$amdgpu_dir = '/sys/bus/pci/drivers/amdgpu';
$dirs = array_filter ( array_map ( 'trim', glob ( $amdgpu_dir.DIRECTORY_SEPARATOR.'*', GLOB_NOSORT | GLOB_ONLYDIR | GLOB_MARK ) ), function (string $str) {
return (is_writable ( $str."power_dpm_force_performance_level" ) && is_writable ( $str.'pp_power_profile_mode' ));
} );
$max = count ( $dirs );
if ($max === 0) {
die ( "error: found 0 applicable devices in $amdgpu_dir\n" );
}
echo "found $max applicable amdgpu devices\n";
$i = 0;
foreach ( $dirs as $gpu ) {
++ $i;
echo "setting card {$i}/{$max}: {$gpu} ..";
my_write ( $gpu."power_dpm_force_performance_level", "manual" );
my_write ( $gpu."pp_power_profile_mode", "5" );
echo ". done\n";
}
die ( "finished!\n" );
function my_write(string $file, string $data, bool $append = false) {
if (! is_writable ( $file )) {
throw new \RuntimeException ( "$file is not writable!" );
}
$len = strlen ( $data );
$written = file_put_contents ( $file, $data, $append ? FILE_APPEND : 0 );
if ($written !== $len) {
throw new \RuntimeException ( "tried to write $len bytes, but could only write $written bytes, file: $file" );
}
return $written;
}
@liolok
Copy link

liolok commented Feb 21, 2021

@divinity76
Copy link
Author

Will it work on today latest drivers

made a new ubuntu 20.04 version based on the link provided by liolok ^^ this version doesn't seem to work on 18.04 though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment