Klaus' Log

Fr 28 August 2020

Compile mfaktc and fix CUDA runtime error on Arch Linux

Posted by Klaus Eisentraut in howto   

Since around 2008 I have sporadically contributed to the Great Internet Mersenne Prime Search project. This project is hunting for Mersenne primes, i.e. primes which can be written as 2^n-1. Those have an easy primality test and are usually the largest known primes which is kind of cool. The first step in the hunt for the next Mersenne prime is to eliminate candidate exponents n by trial-factoring the number p=2^n-1 to small limits. Obviously, a number which has a small factor cannot be a prime number.

This trial factoring is a perfect job for a GPU and there is a small program mfaktc which can be used for CUDA-compatible NVIDIA graphic cards. This niche program is not in the Arch Linux repository nor in the Arch User repository. Therefore one needs to compile it from source.

Compiling mfaktc on Arch Linux

First, install the official cuda package for Arch Linux by running pacman -S cuda. This should install CUDA 11.0 which is the latest one. If your card is older, you might want to use an older CUDA package which is still compatible to your card. However, the grapic card in my laptop is a GeForce GTX 950M which has Compute Cabability 5.0 and is still supported by CUDA 11.0 according to https://en.wikipedia.org/wiki/CUDA#GPUs_supported.

Now, we need to download the source code of mfaktc from here. The latest version at the time of writing is 0.21.

$ wget https://download.mersenne.ca/mfaktc/mfaktc-0.21/mfaktc-0.21.tar.gz
$ tar xzvf mfaktc-0.21.tar.gz 
$ cd mfaktc-0.21/
$ vim src/Makefile 

After extracting it, we need to change two simple settings in the Makefile src/Makefile. We need to adjust the CUDA_DIR variable to the location where Arch Linux installs it (/opt/cuda) and we need to uncomment the correct line for our compute capability (mine has 5.0, see Wikipedia link above):

[...snip...]
# where is the CUDA Toolkit installed?
CUDA_DIR = /opt/cuda/
[...snip...]
# generate code for various compute capabilities
NVCCFLAGS += --generate-code arch=compute_50,code=sm_50 # CC 5.x GPUs will use this code

Now, we compile the program by simply running cd src/ && make -j5. If everything works out, we should now have a ./mfaktc.exe executable. Even if it is called mfaktc.exe it is an ELF executable and runs on Linux.

We should now edit mfaktc.ini and set at least the following variables:

  • V5UserID: set this to your mersenne.org username
  • ComputerID: set this to the name of your computer (can be anything)
  • PrintMode: set this to 1 in order to make mfaktc update the progress line and not writing to much stuff to stdout.

Now, we can get some trial-factoring work from the manual assignment form of GIMPS. You will get a list of trial factoring work units similiar to this:

$ cat worktodo.txt 
Factor=N/A,112233977,74,75
Factor=N/A,119160341,72,74
Factor=N/A,119160499,72,74

Then, start mfaktc.exe and wait until it has trial-factored at least one work unit. After it has completed at least one work unit, it will write the results to a file results.txt.

$ cat results.txt
UID: gLauss/klaus-laptop-gpu, no factor for M119160179 from 2^72 to 2^73 [mfaktc 0.21 barrett76_mul32_gs]
UID: gLauss/klaus-laptop-gpu, no factor for M119160179 from 2^73 to 2^74 [mfaktc 0.21 barrett76_mul32_gs]
UID: gLauss/klaus-laptop-gpu, no factor for M119160247 from 2^72 to 2^73 [mfaktc 0.21 barrett76_mul32_gs]
UID: gLauss/klaus-laptop-gpu, no factor for M119160247 from 2^73 to 2^74 [mfaktc 0.21 barrett76_mul32_gs]

The content of the file results.txt should be reported to the manual result form.

mfaktc CUDA runtime error

Every time mfaktc is started, it will run a short self-test. This worked for a few months without any problems, but then I suddenly got this error:

CUDA version info
  binary compiled for CUDA  11.0
  CUDA runtime version      32.67
  CUDA driver version       11.0
ERROR: CUDA runtime version must match the CUDA toolkit version used during compile!

I was confused why it reported CUDA runtime version 32.67 which does not even exist. So I asked in the mersenne forum for help.

If this happens to you, too, then you should simply ignore the reported CUDA runtime version. mfaktc reports a bogus version number if the NVIDIA driver is not loaded properly.

Simply double-check that the NVIDIA driver is loaded properly: run nvidia-smi which should report something like below.

$ nvidia-smi
Fri Aug 28 21:21:56 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.66       Driver Version: 450.66       CUDA Version: 11.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce GTX 950M    Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   91C    P0    N/A /  N/A |     50MiB /  4046MiB |     99%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A       496      G   /usr/lib/Xorg                       3MiB |
|    0   N/A  N/A       621      C   ./mfaktc.exe                       44MiB |
+-----------------------------------------------------------------------------+

If nvidia-smi doesn't work, please double-check your cuda installation and try to load the kernel module manually by running modprobe nvidia.