As an Amazon Associate I earn money from qualifying purchases.
Showing posts with label bfgminer. Show all posts
Showing posts with label bfgminer. Show all posts

Tuesday, December 24, 2013

Building a Linux Litecoin / Scrypt Miner

I've had a few people ask me about this, and while there are other walkthroughs of the process (I owe a serious debt of gratitude to Cryptobadger), many of those are a bit out of date and don't say much about the new R9 290/290X GPUs. So, here's my take on building a Linux system, as of December 24, 2013 -- Merry Christmas Eve!

Fundamentally, getting scrypt mining going under Linux requires a similar process to Windows:
  1. Install Linux (duh)
  2. Install AMD Radeon drivers
  3. Install cgminer or bfgminer (I have used cgminer 3.7.2)
  4. Configure cgminer and launch
  5. Create a startup script so that cgminer automatically launches (i.e. for occasions when the system crashes and restarts, or there's a power outage).
I might need to create a more detailed set of instructions, but if you're mostly familiar with Linux here's the short summary. Before you begin, set your PC (in the BIOS) to automatically turn on when power is restored. How you do this varies by motherboard, but usually press DELETE to get into the BIOS/UEFI screens and look in the Advanced settings. (Email me or post a comment with your specific motherboard model if you need help.) From there...

First, grab the Linux distro you want to use. I've used 64-bit Xubuntu 13.10 (Saucy Salamandar) as it's pretty simple, but others should work fine as well. (You're on your own for installing other distros!) Short summary: create a bootable device with the image (either a CD/DVD or a USB stick), then launch the installer and set things up. You can use a second USB stick as your OS device if you want, but they can really be slow so I recommend an inexpensive 60GB SSD or even a cheap 2.5"/3.5" HDD over a $5-$30 USB stick. (Seriously, you're spending $500 per GPU or whatever; go nuts and spend $50 on a decent boot device! You'll thank me in the long run, as every time you screw up and crash the OS and need to reboot can feel like an eternity...speaking from experience here.)

The setup process is pretty straightforward. About the only thing to note is that you'll want to automatically log in when the OS starts. (This is a total security no-no, but we're not worried about files on the PC -- we just want it to mine!) Once the OS is installed and you log in, you have a couple options. Either never worry about getting updates, or grab them all right now is my advice. (If something goes wrong, you want it to happen now rather than after everything is mining!) Now we're ready to install the AMD drivers and cgminer.

Open a terminal window. Now remove any old drivers:
sudo apt-get -y remove fglrx-updates fglrx-amdcccle-updates fglrx-updates-dev
(sudo will prompt you for the super user password the first time you run it in a terminal windows.)

Download the latest AMD Linux drivers and unpack to your user directory. Then from a terminal window:
sudo chmod +x ~/amd-catalyst*.run
sudo ~/amd-catalyst*.run
(Reboot)
sudo aticonfig --lsa
sudo rm -f /etc/x11/xorg.conf ~/.Xauthority
sudo aticonfig --adapter=all --initial
sudo reboot
(Wait for reboot again...)
At that point you should have all of the necessary AMD drivers installed. Now get cgminer (nothing after 3.8 supports GPUs, so get the last 3.7.2 build), then extract:
wget http://ck.kolivas.org/apps/cgminer/3.7/cgminer-3.7.2-x86_64-built.tar.bz2
tar jxvf cgminer-3.7.2-x86_64-built.tar.bz2
You're almost ready. Now set some items for future use so that you can mine properly. The sudo ln line is to create a link so cgminer knows where to find libudev. (This has been necessary on the latest 13.10 Xubuntu for me, at least.)
export DISPLAY=:0
export GPU_MAX_ALLOC_PERCENT=100
export GPU_USE_SYNC_OBJECTS=1
sudo ln -s /lib/x86_64-linux-gnu/libudev.so.0 /lib/x86_64-linux-gnu/libudev.so.1
exit
(Note that you may have to close and restart a new Terminal session to have the above stick, I think? In Windows, I know you need to open a new command prompt after setx; doesn't hurt to be safe.)

At this point, try running the following just to see if you can mine at all. (You can change to your own pool, or just test this and throw a few uLTC my way in the process):
export DISPLAY=:0
export GPU_MAX_ALLOC_PERCENT=100
export GPU_USE_SYNC_OBJECTS=1
cd ~/cgminer-3.7.2-x86_64-built
./cgminer --scrypt -I 20 --thread-concurrency 24000 -w 256 -g 1 --temp-target 85 --temp-overheat 95 --gpu-fan 40-95 --auto-fan --gpu-engine 875 --gpu-memclock 1425 --gpu-powertune 30 -o stratum01.hashco.ws:8888 -u trogdorjw73.tester -p tester
Assuming that all works okay, you should get around 725-850KHash/sec per GPU, depending on whether you're running R9 290 or R9 290X, as well as some "luck of the draw" on each card. You'll find that some GPUs will do better than others, and that's normal. From here you need to tune the clocks and thread-concurrency to get optimal performance. Note that if you want to exit cgminer, unless you can find a recompiled version to fix the problem, you do NOT want to press "Q" to quit -- it will crash your system and require a hard reset in my experience. Instead, close the terminal window manually to kill off cgminer.

For tuning, generally speaking go for max memclock first, then play with the core clock to find a good value -- often you'll find that if you push the GPU above 900MHz, it will actually be slower than at lower clocks, as AMD's hardware will throttle if it detects too much power or something going through the card(s). When you've got good RAM and GPU clocks, I tune the thread-concurrency in steps of 64. I haven't noticed a huge difference with changing TC, but you might get an extra 2% out of a GPU with the right value.

Of course, the tuning needs to be done for each GPU as well. One thing to try is once cgminer is running, press G (GPU Settings), the C (Change Settings), and from there you can tune the RAM clock and core clock in real-time, on each GPU. So start on GPU 0, and bump the RAM clock up in 25MHz increments from 1425MHz or so. You'll probably eventually cause the system to crash, so back the RAM clock down 25MHz and you're done on that card. Repeat for the other GPUs.

On the engine clocks, start probably closer to 800MHz and let cgminer run for maybe 5-10 minutes to stabilize. Look at the average hash rate for the GPU, and bump the core clock up 25MHz. Let things stabilize again for 5-10 minutes. If performance didn't go up, try the next step, and you'll probably see performance drop, which means you have reached the limits of that particular GPU for RAM and GPU clocks. Move on to the next GPU and repeat the process.

Tuning thread-concurrency unfortunately requires restarting cgminer each time, so I'd find good clocks first and then tune TC as a group. Start at 24000 or so, run cgminer for 5-10 minutes and note the averages for each card. Close out and bump TC up by 64 and run again. Repeat this for probably 10 iterations (20 at most) and you'll usually find a pattern where every five or so steps of 64 you'll see a peak and/or valley in hashing performance on each GPU.

Now just use the best result for each GPU in your cgminer.conf file and do a test run for a few hours. You may find that after the first 5-15 minutes performance will drop some and then stabilize. This is normal, and if you want you can see if you can tweak the settings a bit more to improve performance. (You might need to back off the RAM a bit more as well -- redlining any part of the GPU may end up in lowered performance over time, not to mention the potential for killing the hardware after six months or so.)

Another idea: having a large fan blow air across the GPUs may help out, though be careful with the potential for dust buildup if you do this. When everything is running well in cgminer, generate a conf file for future use. Press S (Settings) and then W (Write config file) and give your file a name (or use the default); I like to use something other than the default so that cgminer doesn't automatically load a config file when I don't expect it, so for my purposes I use cgminer-current.conf.

When everything is running smoothly, you'll also want to set Linux up to auto-launch cgminer on startup. Crytobadger has some scripts that work well enough, which I'll copy here with minor tweaking. First we make a script:
sudo nano mine_scrypt.sh
Paste the following into nano:
#!/bin/sh
export DISPLAY=:0
export GPU_MAX_ALLOC_PERCENT=100
export GPU_USE_SYNC_OBJECTS=1
cd /home/[LINUX USER NAME]/cgminer-3.7.2-x86_64-built
./cgminer --scrypt --auto-fan --failover-only --config cgminer-current.conf
Save and exit (CTRL+O, CTRL+X), then make the script executable:
sudo chmod +x mine_scrypt.sh
If you open a terminal and type "./mine_scrypt.sh" you'll start mining. (Note you can press [TAB] to auto-complete file and directory names, so typing "./mine[tab]" is usually sufficient.) First we need to make sure we have "screen" installed:
sudo apt-get install screen
Now we need to create a second script to start mining when Linux boots:
sudo nano start_mining.sh
Paste the following into nano:
#!/bin/bash
DEFAULT_DELAY=0
if [ "x$1" = "x" -o "x$1" = "xnone" ]; then
   DELAY=$DEFAULT_DELAY
else
   DELAY=$1
fi
sleep $DELAY
su [YOUR LINUX USERNAME] -c "screen -dmS cgm /home/[YOUR LINUX USERNAME]/mine_scrypt.sh"
Save and exit again (CTRL+O, CTRL+X), and make the script (scrypt? Hahaha) executable:
sudo chmod +x start_mining.sh
Now we edit the system config file to automatically call our script:
sudo nano /etc/rc.local
Add the following line just above the "exit 0" line; the 15 is a delay in seconds, so you can use more or less as you see fit:
/home/[YOUR LINUX USERNAME]/start_mining.sh 15 &
Save and exit (CTRL+O, CTRL+X) and you're done. The only catch is that now when you boot Linux, cgminer is running in the background and you can't see it. To bring it to the foreground, open a terminal window and type:
screen -x cgm
You can also make an alias (command shortcut) to open the cgminer window by editing your own .bashrc file ("sudo nano .bashrc") and add the following near the bottom:
alias cgm='screen -x cgm'
When you close the window showing cgminer now, however, cgminer will continue running. To actually kill and restart cgminer, type "ps -A" find the "cgminer" process, and then type "kill -9 [Process ID]". Now you can just type ./mine_scrypt.sh to restart cgminer if you want (e.g. after you tweak the config file).

Is Linux any faster at mining than Windows? I haven't noticed any major difference, so the main draw is that Linux is free. Personally, I continue to use Windows on my PCs, because I'm more familiar with the programs and things like MSI Afterburner are handy. Besides, again we're talking about $89 for Windows 7 64-bit (or $96 for Windows 8.1 64-bit) on a system that has $2000 worth of other hardware. It's less than 5% of the total cost and increases the resale value if you ever decide to stop mining. Anyway, YMMV.

LTC tips: LfCLyykrNFftzpdWejR73hf478ZtBzQ9jE
BTC tips: 1GGJUb1gFpydygpeKzd6oFoShLRUSyThV7

Sunday, December 22, 2013

Radeon R9 290/290X Litecoin Mining: New Settings and Revised Expectations

Over the past several weeks I've had a flood of comments and emails about R9 290/290X hashing performance when mining Litecoins (or any other scrypt-based cryptocurrency). Initial experience with the cards suggests that the Radeon R9 290 can hit up to 900KHash and the Radeon R9 290X can hit 1000KHash, but the key part of that is "up to". In practice, the cards vary, sometimes wildly.

There are a lot of 290X cards that seem to top out in the 800-850KHash range, and short of adding better cooling I'm not sure how much farther you can push them. R9 290 tends to be more like 750-800KHash on a lot of cards. I think if we could get voltage adjustments working, we might be able to improve on those results for a lot of cards, but we probably won't have that for a few more weeks if not months.

One of the keys to understanding what's required for tuning performance on the new Hawaii R9 290/290X cards is that AMD has built in safety protocols to keep the cards running "safely". A lot of people have tried my original settings and come up with far lower performance than expected. This is why I provided batch files to help you tune and tweak performance.

Scrypt hashing by design is far more memory intensive than SHA256 hashing. The idea was to make it impossible or at least impractical to do scrypt hashing on GPUs, let along FPGAs and ASICs. So far, no one has yet released any FPGA or ASIC hardware for scrypt devices, and I suspect we're probably still at least a year away from such hardware becoming publicly available. (There may be early hardware undergoing "testing" for month in China or elsewhere first, of course.) So when you're looking to fine-tune your GPUs, start with the memory clocks and push those as far as you can while maintaining stability -- and maybe add a few extra cooling fans to help out! If you want to hit extremely high hash rates, you'll want the memory clock at 1600MHz or higher, and most cards won't do that without some sort of additional cooling or other modification.

Once you've found the maximum reliable memory clocks for your GPUs, only then should you start tuning the GPU clock speed. What's interesting is that even though most cards will happily accept GPU engine clocks of 1000MHz or higher, in reality they'll often run at far lower clocks -- this is AMD's safety protocols kicking in. So if you start off at 950MHz core and 1350MHz RAM and a thread concurrency of 32000 (give or take), and you find your hash rates are closer to 600 or 700KHash rather than 800-900KHash, don't despair!

Start with a lower GPU clock of around 800MHz (on R9 290/290X) and then start bumping it up 25MHz after about 10 minutes or more testing at each clock speed. You'll most likely find that 850MHz ends up being the sweet spot, and then you can play with clocks a bit more bumping it up/down in 5MHz increments to see if you can squeeze a bit better performance out of your system. When all of that is done, only then should you bother with tweaking your thread concurrency.

And with that said, here are my revised settings for R9 290/290X graphics cards. I'm going to include cgminer settings for four cards as an example this time, just to illustrate what you would use. I've also included three pools, the first two being multi-coin pools, since that's what I use these days. I'm also going to try toying around with BFGminer to see if I can coax anything better out of it. For the time being, these settings are more or less "stable".

cgminer.conf settings for ~810 KHash R9 290X / ~770 KHash R9 290
{
"pools" : [
{
"url" : "http://stratum01.hashco.ws:8888",
"user" : "trogdorjw73.tester",
"pass" : "tester"
},
{
"url" : "stratum+tcp://middlecoin.com:3333",
"user" : "13saHMFfUAv1crYp3QLbFr9aP1JYnb89oh",
"pass" : "x"
},
{
"url" : "http://coinotron.com:3334",
"user" : "trogdorjw73.tester",
"pass" : "tester"
}
]
,
"intensity" : "20,20,20,20",
"worksize" : "256",
"kernel" : "scrypt",
"lookup-gap" : "2",
"thread-concurrency" : "24272,24272,24272,24272",
"gpu-engine" : "850-850,850-850,850-850,850-850",
"gpu-fan" : "45-85,45-85,45-85,45-85",
"gpu-memclock" : "1475,1475,1475,1475",
"gpu-memdiff" : "0,0,0,0",
"gpu-powertune" : "50,50,50,50",
"gpu-vddc" : "1.100,1.100,1.100,1.100",
"temp-cutoff" : "100,100,100,100",
"temp-overheat" : "95,95,95,95",
"temp-target" : "85,85,85,85",
"api-mcast-port" : "4028",
"api-port" : "4028",
"auto-fan" : true,
"expiry" : "120",
"failover-only" : true,
"gpu-dyninterval" : "7",
"gpu-platform" : "1",
"gpu-threads" : "1",
"hotplug" : "5",
"log" : "5",
"no-pool-disable" : true,
"queue" : "1",
"scan-time" : "60",
"scrypt" : true,
"temp-hysteresis" : "3",
"shares" : "0",
"kernel-path" : "/usr/local/bin"
}
And just in case this isn't clear, with the above settings you would launch cgminer.exe with the following command:
cgminer.exe --auto-fan --config cgminer.conf --failover-only