Friday, October 4, 2013

Minimal Headless OpenCL + cgminer on Ubuntu 13.04 Server

UPDATE 2015-05-12: This guide hasn't been touched in a really long time. It'll probably still work if you use the actual versions of everything listed, but no guarantees. I've noticed that Catalyst 14.12 has a "minimal" video driver now along with a "graphics accelerators devel files" download. Needless to say, the situation seems to have changed for the better, but I haven't needed to do any new OpenCL setups, so I don't know what's involved. If you've landed on this page from a search engine, I would suggest searching for a more up-to-date guide. Unfortunately, I do not have any recommendations at this time.

---------------------------------------------

Purpose of this Guide
I'm going to explain how to set up a minimal number of X dependencies required to run cgminer on Linux with AMD GPUs. This also potentially applies to other OpenCL tasks too.

Why is this Needed?
AMD makes decent Windows drivers, but unfortunately, AMD's proprietary Linux drivers are not very good. In order to do OpenCL, you must have some sort of X server running*.

*Update: there is a way to modify the drivers so that an X server isn't required: http://devgurus.amd.com/thread/168273
I could not get it to work, but one of the commenters reported success (but only with errors and no ADL)

Assumptions
I'm going to assume you've got a headless server with SSH access and that you've just done a default install of Ubuntu Server 13.04 64-bit.

Note about Ubuntu Desktop vs Ubuntu Server:
It's very likely that the instructions in this guide will not work with Ubuntu Desktop without a few modifications. This guide will install xdm and assume it's set as the default, but if you already have a graphical environment, you probably already have a display manager installed (probably lightdm).

Also, there have been varying levels of success with different software versions. Some combinations of fglrx and the APP-SDK result in 100% CPU usage, or just don't work at all.



Update the Server
It's very important that you run apt-get update otherwise the buildpkg may fail later. Also, upgrading now is a good idea unless you want to experience the pain of upgrading a kernel with fglrx right away.
sudo apt-get update
sudo apt-get dist-upgrade
sudo reboot

Download and Install the Drivers
Download the latest fglrx drivers (I'm using 13.8 beta in this guide):
(note: at least one commenter reported problems with the latest drivers. If you need 13.8 specifically, see Patrick's commentuse "wget --referer="http://www.amd.com" http://www2.ati.com/drivers/beta/amd-catalyst-13.8-beta2-linux-x86.x86_64.zip" to download because AMD blocks direct links)
(note: if you have an R9 290 or one of the other newer GPUs, please see this comment, there are several people having trouble with the newer cards, you might need to install a graphical environment first and run the manual installer rather than building a package)
http://support.amd.com/us/gpudownload/linux/Pages/radeon_linux.aspx

Install Some Necessary Packages
sudo apt-get install dh-make dh-modaliases execstack libxrandr2 libice6 libsm6 libfontconfig1 libxi6 libxcursor1 libgl1-mesa-glx libxinerama1 libqtgui4 unzip

Extract the Installer
unzip amd-catalyst-13.10-beta2-linux-x86.x86_64.zip
chmod +x amd-catalyst-13.10-beta2-linux-x86.x86_64.run

Build the fglrx .deb Packages
./amd-catalyst-13.10-beta2-linux-x86.x86_64.run --buildpkg Ubuntu/raring

(replace "raring" with the code name for your version according to "lsb_release -c")

Install fglrx
sudo dpkg -i fglrx*.deb
sudo apt-get -f install
sudo reboot
(it'll fail the first time, complaining about dependency problems; running apt-get -f install fixes it)

If you have multiple GPUs and want to run all of them, you might have to run something like: Update: it looks like this step is required. You can also run "aticonfig --list-adapters" if you'd like some extra information.
sudo aticonfig --initial --adapter=all

Install the APP SDK
Next, you'll need AMD's APP SDK:

Extract the APP SDK
tar xvf AMD-APP-SDK-v2.8.1.0-lnx64.tgz
sudo ./Install-AMD-APP.sh
sudo reboot

(you can do this in another directory if you want, these things produce lost of junk files)

Install the Minimum Required X Server Packages
sudo apt-get install xdm xorg

If you have a screen hooked up, you'll see an ugly XDM log in prompt. Don't use it (unless you install openbox or something). If you need console access to your server, switch to tty1 (ctrl+alt+f1).

Disable Authorization on xdm
(if you know of a better way to do this, please tell me...)

sudo nano /etc/X11/xdm/xdm-config

find the line
DisplayManager*authorize:       true

and make it
DisplayManager*authorize:       false

Save the file and reboot
sudo reboot

At this point, I'd recommend making sure xdm is actually running.

Compile cgminer and Test
Even if you don't plan to mine crypto-currencies, running cgminer is a good way to make sure stuff works. If you just want to run your own application, take a look at the cgminer sh towards the end and modify it to your needs.

Install Git
sudo apt-get install git

Download cgminer
git clone -b 3.7 https://github.com/ckolivas/cgminer
Update: It looks like cgminer 3.7.2 is the last version to support GPU mining (see: https://bitcointalk.org/index.php?topic=28402.msg3538435#msg3538435). Thank you to the Anonymous poster in the comments for this.

Alternatively, you could try sgminer, a scrypt-only fork of cgminer 3.7.2 with new bug fixes, as suggested by an anonymous comment.
http://www.reddit.com/r/litecoinmining/comments/1va8g2/ann_sgminer_400_release/
https://github.com/veox/sgminer

Download AMD ADL SDK (for temperature monitoring, fan control, etc)
unzip ADL_SDK_5.0.zip
cp include/* ./cgminer/ADL_SDK/
(you might want to do this in a temporary directory as the ADL zip has a bunch of other stuff that it extracts into the current directory)

Install the packages we'll need to compile cgminer
sudo apt-get install build-essential autoconf libtool libcurl4-openssl-dev libncurses5-dev pkg-config libudev-dev

Compile cgminer
cd cgminer
./autogen.sh
./configure --enable-opencl --enable-scrypt
Update: It seems that I now have to add --enable-opencl to compile on 3.7.2
Note: you only need "./configure" (without any options) if you're using sgminer

Make sure you see the following after configure:
OpenCL...............: FOUND. GPU mining support enabled
scrypt...............: Enabled
ADL..................: SDK found, GPU monitoring support enabled

If everything looks good, compile
make

Testing cgminer (or other OpenCL applications)
You'll need to set some environment variables to run OpenCL applications. I recommend you start them with a bash script like the following:
#!/bin/bash
export DISPLAY=:0
export GPU_MAX_ALLOC_PERCENT=100
export GPU_USE_SYNC_OBJECTS=1
./cgminer -n

Save it as something like "test.sh" and "chmod +x test.sh"

(note: in some cases, you may need to run the script as root)
When you run it, you should get something like:
 [2013-10-04 22:46:09] CL Platform 0 vendor: Advanced Micro Devices, Inc.                    
 [2013-10-04 22:46:09] CL Platform 0 name: AMD Accelerated Parallel Processing                    
 [2013-10-04 22:46:09] CL Platform 0 version: OpenCL 1.2 AMD-APP (1214.3)                    
 [2013-10-04 22:46:09] Platform 0 devices: 1                    
 [2013-10-04 22:46:09] 0 Tahiti                    
 [2013-10-04 22:46:09] GPU 0 AMD Radeon HD 7900 Series hardware monitoring enabled                
 [2013-10-04 22:46:09] 1 GPU devices max detected

If everything worked (hopefully it did, because if not, the suffering starts now), you can now run OpenCL applications. Yay!

If you found this guide helpful and want to donate:
BTC -- 1EBVjJSM7Qa1msYcU4AEFG3kLFimwXbAA9
LTC -- LQNDRwrk9H6nwU88Ka87XPqfiJ9AAqdxJR
BQC -- bXigfHpF6qsGYANjJUSc58bRWDAcruVEUE

Issues / Troubleshooting

If something in this guide didn't work for you, please leave a comment with your operating system and driver version. I ran through this guide start to finish on a fresh install on my hardware and it seemed to work, but I only have a 7950 to test.

Some people are having issues with R9 290s. It looks like there are problems getting cgminer to see the newer hardware in some cases. I don't have any new hardware to test unfortunately, but I linked a few of the comments above, it seems a few people managed to figure it out.

Also, since GPU mining isn't supported in the newer cgminer releases, this guide may no longer work when AMD releases new hardware.

Run cgminer at Boot with rc.local and screen

This is a simple workaround to get cgminer running at boot and allow you to see it when you log in remotely later. These are not complete instructions, but hopefully they might help.

First, install screen if you haven't already:

sudo apt-get install screen

If you use a script like my test.sh script above to start cgminer already, then take note of where that is located. Create a different script somewhere accessible (mine is ~/boot.sh in my home directory) and add this:

#!/bin/bash
cd /home/yourusername/cgminer/
/home/yourusername/cgminer/run.sh
(of course replace "yourusername" with your name, and "run.sh" with whatever script you use to start cgminer)

Then add something like this to your rc.local

/bin/su yourusername -c "/usr/bin/screen -dmS test bash -c '/home/yourusername/boot.sh; exec bash'"

To test, log in as the root user "sudo su" and run "/etc/rc.local". Exit the root user, and run "screen -r" and see if cgminer is working. To detach from screen, type Ctrl+a then type 'd' key. Then try it after a reboot.

Alternatively, one commenter mentioned using crontab, which may be better than the rc.local method.

Run "crontab -e" (as a regular user, not root) and add something like the following:

@reboot cd /home/yourusername/cgminer && screen -dmS test bash -c '/home/yourusername/cgminer/run.sh; exec bash'

That seemed to work for me. Probably a better solution since it doesn't require root.