Sunday, August 21, 2016
For a couple months, I've been intending to do a blog post on mining with Ubuntu. Now that I've been able do make a static build of Genoil's ethminer, that process has become much easier. Since I have no Nvidia GPUs, this post will only cover how to mine with AMD GPUs like the R7 and R9 series.
The first step is to download a 64-bit Ubuntu 14.04 desktop release. I use the desktop distribution since it includes X11, although it is possible to use Ubuntu server and then install the X11 packages separately. I recommend installing Ubuntu without any GPU cards installed (use your motherboard's iGPU), in order to confirm the base system is working OK. Follow the installation instructions, and at step 7, choose "Log in automatically". This will make it easier to have your rig start mining automatically after reboot.
After the initial reboot, I recommend installing ssh server. It can be installed from the shell (terminal) with: "sudo apt-get install openssh-server -y". Ubuntu uses mDNS, so if you chose 'rig1' as the computer name during the installation, you can ssh to 'rig1.local' from other computers on your LAN.
Shutdown the computer and install the first GPU card, and plug your monitor into the GPU card instead of the iGPU video port. Most motherboards will default to using the GPU card when it is installed, and if not, there should be a BIOS setup option to choose between them. If you do not even see a boot screen, try plugging the card directly into the motherboard instead of using a riser. Also double-check your card's PCI-e power connections.
Once you are successfully booting into the Ubuntu desktop, edit /etc/init/gpu-manager.conf to keep gpu manager from modifying /etc/X11/xorg.conf. Then install the AMD fglrx video drivers: "sudo apt-get install fglrx -y". If the fglrx drivers installed successfully, running "sudo aticonfig --lsa" will show your installed card. Next, to set up your xorg.conf file, run "sudo rm /etc/X11/xorg.conf" and "sudo aticonfig --initial --adapter=all".
After rebooting, if the computer does not boot into the X11 desktop, ssh into the computer and verify that /etc/modprobe.d/fglrx-core.conf was created when the fglrx driver was installed. This keeps Ubuntu from loading the open-source radeon drivers, which will conflict with the proprietary fglrx drivers. For additional debugging, look at the /var/log/Xorg.0.log file.
Continue with installing the rest of your cards one at a time. Re-initialize your xorg.conf each time, by executing "sudo rm /etc/X11/xorg.conf" and "sudo aticonfig --initial --adapter=all". Reboot one more time, and then exeucte, "aticonfig --odgc --adapter=all". This will display all the cards and their core/memory clocks. If you are connecting remotely via ssh, you need to run, "export DISPLAY=:0" or you will get the "X needs to be running..." error. You can use aticonfig to change the clock speeds on your card. For example, "aticonfig --od-enable --adapter=2 --odsc 820,1500" will set card #2 to 820Mhz core and 1500Mhz memory (a good speed for most R9 380 cards). To simplify setting clock speeds on different cards, I created a script which reads a list of card types and clock rates from a clocks.txt file.
Once your cards are installed and configured, you can use my ethminer build:
tar xzf ethminer-1.1.9nr-OCL.tgz
Once you've confirmed that ethminer is working, you can edit the mine.sh script to use your own mining pool account. If you want your rig to start mining automatically on boot-up, edit your .bashrc and add "cd ethminer-nr" and "./mine.sh" to the end of the file.
Sunday, July 31, 2016
In my last post about mining ethereum, I explained why I preferred Genoil's fork of the Ethereum Foundation's ethminer. After that post, I started having stability problems with one of the newer releases of Genoil's miner. I suspected the problem was likely deadlocks with mutexes that had been added to the code. They had been added to reduce the chance of the miner submitting stale or invalid shares, but in this case the solution was worse than the problem, since there is no harm in submitting a small number of invalid shares to a pool. After taking some time to review the code and discuss my ideas with the author, I decided to make some improvements. The result is ethminer-nr.
A description of some of the changes can be found on the issues tracker for Genoil's miner, since I expect most of my changes to be merged upstream. The first thing I did was remove the mutexes. This does open the possibility of a rare race condition that could cause an invalid share submit when one thread processes a share from a GPU while another thread processes a new job from the pool. On Linux the threads can be serialized using the taskset command to pin the process to a single CPU. On a multi-CPU system, use "taskset 1 ./ethminer ..." to pin the process to the first CPU.
As described in the issues tracker, I added per-GPU reporting of hash rate. I also reduced the stats output to accepted (A) and rejected (R), including stales, since I have never seen a pool submit fail, and only some pools will report a rejected share. The more compact output helps the stats still fit on a single line, even with hashrate reporting from multiple GPUs:
m 13:28:46|ethminer 15099 24326 15099 =54525Khs A807+6:R0+0
To help detect when a pool connection has failed, instead of trying to manage timeouts in the code, I decided to rely on the TCP stack. The first thing I did was enable TCP keepalives on the stratum connection to the pool. If the pool server is alive but just didn't have any new jobs for a while, the socket connection will remain open. If the network connection to the pool fails, there will be no keepalive response and the socket will be closed. Since the default timeouts are rather long, I reduced them to make network failure detection faster:
sudo sysctl -w net.ipv4.tcp_keepalive_time=30
sudo sysctl -w net.ipv4.tcp_keepalive_intvl=5
sudo sysctl -w net.ipv4.tcp_keepalive_intvl=3
I wasn't certain if packets sent to the server will reset the keepalive timer, even if there is no response (even an ACK) from the server. Therefore I also reduced the default TCP retransmission count to 5, so the pool connection will close after a packet is sent (i.e. share submit) 5 times without an acknowledgement.
sudo sysctl -w net.ipv4.tcp_retries2=5
I was also able to make a stand-alone linux binary. Until now the Linux builds had made extensive use of shared libraries, so the binary could not be used without first installing several shared library dependencies like boost and json. I had to do some of the build manually, so to make your own static linked binary you'll have to wait a few days for some updates to the cmake build scripts. If you want to try it now anyway, you can add "-DETH_STATIC=1" to the cmake command line.
As for future improvements, since I've started learning OpenCL, I'm hoping to optimize the ethminer OpenCL kernel to improve hashing performance. Look for something in late August or early September.
Sunday, July 17, 2016
Programs for mining on GPUs are usually written in OpenCL. It's based on C, which I know well, so a few weeks ago I decided to try to improve some mining OpenCL code. My intention was to both learn OpenCL and better understand mining algorithims.
I started with simple changes to the OpenCL code for Genoil's ethminer. I then spent a lot of time reading GCN architecture and instruction set documents to understand how AMD GPUs run OpenCL code. Since I recently started mining Sia, I took a look at the gominer kernel code, and thought I might be able to optimize the performance. I tested with the AMD fglrx drivers under Ubuntu 14.04 (OpenGL version string: 4.5.13399) with a r9 290 card.
The first thing I tried was replacing the rotate code in the ror64 function to use amd_bitalign. The bitalign instruction (v_alignbit_b32) can do a 32-bit rotate in a single cycle, much like the ARM barrel shifter. I was surprised that the speed did not improve, which suggests the AMD OpenCL drivers are optimized to use the alignbit instruction. What was worse was that the kernel would calculate incorrect hash values. After double and triple-checking my code, I found a post indicating a bug with amd_bitalign when using values divisible by 8. I then tried amd_bytealign, and that didn't work either. I was able to confirm the bug when I found that a bitalign of 21 followed by 3 worked (albeit slower), while a single bitalign of 24 did not.
It would seem there is no reason to use the amd_bitalign any more. Relying on the driver to optimize the code makes it portable to other platforms. I couldn't find any documentation from AMD saying the bitalign and other media ops are deprected, but I did verify that the pragmas make no difference in kernel:
#pragma OPENCL EXTENSION cl_amd_media_ops : enable
#pragma OPENCL EXTENSION cl_amd_media_ops : disable
After finding a post stating the rotate() function is optimized to use alignbit, I tried changing the "ror64(x, y)" calls to "rotate(x, 64-y)". The code functioned properly but was actually slower. By using AMD_OCL_BUILD_OPTIONS_APPEND=-save-temps, I was able to view the assember .isa files, and could tell that the calls to rotate with 64-bit values were using v_lshlrev_b32, v_lshrrev_b64, and v_or_b32 instead of a pair of v_alignbit_b32 instructions. Besides using 1 additional instruction, the 64-bit shift instructions apparently take 2 or even 4 times longer to execute on some platforms.
In the end, I wasn't able to improve the kernel speed. I think re-writing the kernel in GCN assembler is probably the best way to get the maximum hashing performance.
Monday, July 11, 2016
Sia is a hot crypto-currency for miners. Just a week ago, the sia network hashrate was 6.5 Th/s, and the only way to mine was solo as there were no public pools. In the last three days, sia.nanopoool.org, and siamining.com started up and the network hashrate grew to 14.7 Th/s, with the two pools making up 80% of the total network hashrate.
Mining on Windows is relatively easy, with nanopool posting a binary build of siamining's gominer fork. For Ubuntu, you need to build it from the source. For that, you'll need to install go first. If you type 'go' in Ubuntu 14.04, you'll get the following message:
The program 'go' is currently not installed. You can install it by typing:
apt-get install gccgo-go
Even with the golang 1.5.1, building gominer wasn't as simple as "go get github.com/SiaMining/gominer". The reason is that the gominer modifications to support pooled mining are in the "poolmod3" branch, and there is no option to install directly from a branch. So I made my own fork of the poolmod3 branch, and added detailed install instructions for Ubuntu:
Once I got it running on a single GPU, I wanted to find out if it was worthwhile to switch my eth mining rigs to sia. I couldn't find a good sia mining calculator, so I pieced together some information about mining rewards and used the Sia Pulse calculator. I wanted to compare a single R9 290 clocked at 1050/1125, which gets about 29Mh/s mining eth, earning $2.17/day. For Sia, the R9 290 gets about 1100Mh, which if you put that into the Sia Pulse calculator along with the current difficulty of 4740Th, it will calculate daily earnings of 6015 SC/day. Multiplying by the 62c/1000SC shown on sia.nanopool.org will give you a total of $3.73/d, but that will be wrong. The Sia Pulse calculator defaults to a block reward of 300,000, but that goes down by 1 for each block. So at block 59,900, the block reward is 240,100. and the actual earnings would be $2.99/d.
add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update apt-get install -y git ocl-icd-libopencl1 opencl-headers golang go get github.com/nerdralph/gominer-nr
Since the earnings are almost 40% better than eth, I decided to switch my mining rigs from eth to sia. I had to adjust the overclocking settings, as sia is a compute-intensive algorithm instead of a memory-intensive algorithm like ethereum. After reducing the core clock of a couple cards from 1050 to 1025, the rigs were stable. When trying out nanopool, I was getting a lot of "ERROR fetching work;" and "Error submitting solution - Share rejected" messages. I think their servers may have been getting overloaded, as it worked fine when I switched to siamining.com. I also find siamining.com has more detailed stats, in particular % of rejected shares (all below 0.5% for me).
I may end up switching back to eth in the near future, since a doubling in network hashare for sia will eventually mean a doubling of the difficulty, cutting the amount of sia mined in half. In the process I'll at least have learned a bit about golang, and I can easily switch between eth and sia when one is more profitable than the other.
Friday, June 3, 2016
I recently bought some cheap molex to PCI-e power adapters from a seller on AliExpress. Although there are deals for quality goods on AliExpress, I was a bit suspicious when I ordered these given just how cheap they were. PCI-e power connectors are supposed to be rated for 75W of power carried over 2 conductors at 12V, which means 3.1A per conductor. In order to avoid a large voltage drop the wires used are usually 18AWG, although 20AWG wires (with 1.6x the resistance) would be reasonably safe.
When the package arrived, I inspected the adapter cables, which were labeled 18AWG. Despite the label, they didn't feel like 18AWG wires, which have a conductor diameter of 1mm. I decided to do a destructive test on one of the adapters by cutting and stripping one of the wires. The conductor measured only 0.4mm in diameter, which is actually 26AWG. The first photo above shows a real 18AWG wire taken from an old ATX PSU next to the fake 18AWG wire from the adapter cables.
When I opened a dispute through AliExpress, things got more amusing. I provided the photo, as well as an explanation that real 18AWG wire should be 1mm in diameter. The seller claimed "we never heard of this before", and after exchanging a couple more messages said, "you can't say it is fake just because it is thin". At that point I realized I was dealing with one of those "you can't fix stupid" situations.
So what would happen if I actually tried to use the adapter cables on a video card that pulls 75W on the PCI-e power connector? Well you can find posts on overclocking sites about cables that melted and burst into flames. If you have a cheap PSU without short-circuit protection, when the insulation melts and the wires short, your power supply could be destroyed. And if that happend I'm sure the AliExpress seller is not going to replace your power supply. How much hotter the cables would get compared to genuine 18AWG cables is a function of the resistance. Each gauge has 1.26 times more resistance than the previous, so 20AWG has 1.26^2 = 1.59 times the resistance of 18AWG. The 26AWG wire used in these cheap adapter cables would have 1.26^8 or just over 6 times the resistance of 18AWG wire, and would have a temperature increase 6 times greater than 18AWG for a given level of current.
It could make for a fun future project; create a resistive load of 75W, take an old ATX PSU, hook up the adapter cables, and see what happens. People do seem to like pictures and videos of things bursting into flames posted on the internet...
Thursday, May 26, 2016
Perl has been my go-to interpreted language for over 20 years now, but in the last few years I've been learning (and liking) python. Python 2.7 is a standard part of of Linux distributions, and while many recent distributions include Python 3.4, Python 3.5.1 is not so common. I'm working on some code that will use the new async and await primitives, which are new in Python 3.5. I've searched Extra Packages for Enterprise Linux and other repositories for Python 3.5 binaries, but the latest I can find is 3.4. That means I have to build it from source.
While the installation process isn't very complicated, it does require installing gcc and associated build tools first. Since I'm installing it on a couple servers (devel and prod), I wrote a short (10-line) install script for rpm-based Linux distributions. Download the script, then run "sh py35.sh". The python3.5 binary will be installed in /usr/local/bin/.
When installing pip packages for python3, use "pip3", while "pip" will install python2 packages. And speaking of pip, you may want to update it to the latest version:
sudo /usr/local/bin/pip3 install --upgrade pip
Friday, April 22, 2016
In my last post, I gave a basic introduction to ethereum mining. Since there is not much information available about eth mining compared to bitcoin mining, and some of the information I have found is even wrong, I decided to go into more detail on eth mining.
Comparing the bitcoin protocol to ethereum, one of the significant differences is the concept of uncle blocks. When two miners find a block at almost the same time, only one of them can be the next block in the chain, and the other will be an uncle. They are equivalent to stale blocks in bitcoin, but unlike bitcoin where the stale blocks go unrewarded, uncle blocks are rewarded based on how "fresh" they are, with the highest reward being 4.375 eth. An example of this can be found in block 1,378,035. Each additional generation that passes (i.e. each increment of the block count) before an uncle block gets included reduces the reward by .625 eth. An example of an uncle that was 2 generations late getting included in the blockchain can be found in block 1,378,048. The miner including the uncle in their block gets a bonus of .15625 eth on top of the normal 5 eth block reward.
Based on the current trend, I expect the uncle rate to be in the 6-7% range over the next few months. With the average uncle reward being around 3.5 eth (most uncles are more than one generation old), uncles provide a bonus income to miners of about 4%. Since uncles do not factor into ethereum's difficulty formula, when more uncles are mined the difficulty does not increase. The mining calculators I've looked at don't factor in uncle rewards, so real-world returns from mining in an optimal setup should be slightly higher than the estimates of the mining calculators.
Another thing the calculators do not factor is the .15625 eth uncle inclusion reward, but this is rather insignificant, and most pools do not share the uncle inclusion reward. Assuming a 6% uncle rate, the uncle inclusion reward increases mining returns by less than 0.2%. If your pool is down or otherwise unavailable for 3 minutes of the day, that would be a 0.21% loss in mining rewards. So a stable pool with good network connections is more important than a pool that shares the uncle inclusion reward. Transaction fees are also another source of mining revenue, but most pools do not share them, and they amount to even less than the uncle inclusion reward in any case.
Finding a good pool for ethereum mining has been much more difficult than bitcoin, where it is pretty hard to beat Antpool. For optimal mining returns, you need to use stratum mode, and there are two main variations of the stratum protocol for eth mining; dwarf and coinotron. Coinotron's stratum protocol is directly supported by Genoil's ethminer, which avoids the need to run eth-proxy in addition to the miner. Coinmine.pl and miningpoolhub.com support coinotron's stratum protocol, while nanopool, f2pool, and mininpoolhub support dwarf's protocol. Miningpoolhub is able to support both on the same port since the json connection string is different.
Coinmine.pl and coinotron only have servers in Europe, and half the time I've tried to go to coinotron's web site it doesn't even load after 15 seconds. Miningpoolhub has servers in the US, Europe, and Asia, and has had reasonable uptimes. As well, the admin responds adequately to issues, and speaks functional english. They have a status page that shows enough information to be able to confirm that your mining connection to the pool is working properly. I have a concern over how the pool reports rejected shares, but the impact on mining returns does not appear to be material. Rejected shares happens on other pools too, and since I am still investigating what is happening with rejected shares, there is not much useful information I can provide about it.
So for now my recommended pool is ethereum.miningpoolhub.com. My recommended mining progam is v1.0.7 of Genoil's ethminer, which added support for stratum connection failover where it can connect to a secondary pool server if the first goes down. The Ethereum Foundation is supporting the development of open-source mining pool software, so we may see an ideal eth mining pool in the near future, and maybe even improvements to the official ethminer supporting stratum protocol.