First taste of MPI

Different with OpenMP which focuses on multiple threads in one process, MPI defines how multiple processes can collaborate with each other. In this post, I use Open MPI on Arch Linux to do a simple test.

The “Hello World” program is from here, build and run it on one node, not a cluster containing many nodes:

$ mpirun mpi_hello_world
Hello world from processor tesla-p100, rank 16 out of 52 processors
Hello world from processor tesla-p100, rank 34 out of 52 processors
Hello world from processor tesla-p100, rank 35 out of 52 processors

Check the CPU information:

$ lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              104
On-line CPU(s) list: 0-103
Thread(s) per core:  2
Core(s) per socket:  26
Socket(s):           2
NUMA node(s):        2

Although there are 2 physical CPUs in the system, the mpirun only utilizes 1 CPU. Modify the program to output process ID:

#include <unistd.h>
printf("Hello world from process %d, processor %s, rank %d out of %d processors\n",
         getpid(), processor_name, world_rank, world_size);

This time you can make sure different processes are spawned:

$ mpirun mpi_hello_world
Hello world from process 52528, processor tesla-p100, rank 21 out of 52 processors
Hello world from process 52557, processor tesla-p100, rank 31 out of 52 processors
Hello world from process 52597, processor tesla-p100, rank 43 out of 52 processors

P.S., if you run mpirun as root, please add --allow-run-as-root option:

# mpirun --allow-run-as-root mpi_hello_world


Build SPDZ-2 on Arch Linux

To build SPDZ-2 on Arch Linux, besides installing necessary packages (mpir, libsodium, etc.), You also need to do following steps:

(1) Add following line in CONFIG.mine:


Otherwise, you will meet errors when executing Scripts/

# Scripts/
terminate called after throwing an instance of 'std::runtime_error'
  what():  You are trying to use insecure benchmarking functionality for preprocessing.
You can activate this at compile time by adding -DINSECURE to the compiler options.
Make sure to run make clean as well.
Scripts/ line 33: 10355 Aborted                 (core dumped) $SPDZROOT/Fake-Offline.x ${players} -lgp ${bits} -lg2 ${g} --default ${default}
dd: failed to open 'Player-Data/Private-Input-0': No such file or directory
dd: failed to open 'Player-Data/Private-Input-1': No such file or directory

(2) Execute make command;

(3) Run Scripts/;

(4) SPDZ-2 requires python2, but the default python is python3 on Arch Linux. So you need to install python2 manually:

# pacman -S python2

Then modify

#!/usr/bin/env python2

Otherwise, you will encounter following errors when running ./ tutorial:

# ./ tutorial
Traceback (most recent call last):
  File "./", line 19, in <module>
    import Compiler
  File "/root/SPDZ-2/Compiler/", line 3, in <module>
    import compilerLib, program, instructions, types, library, floatingpoint
ModuleNotFoundError: No module named 'compilerLib'

Execute ./ tutorial.

Now you can play the example:

# ./Server.x 2 5000 &
# Scripts/ tutorial


A brief introduction of OpenBSD nohup command

When you execute command in terminal (not background mode), if the connection disconnects unexpectedly, the running process will be terminated by SIGHUP signal. nohup command can let process still keep running when this situation occurs.

OpenBSD‘s nohup implementation is neat. It actually only does 4 things:

(1) If stdout is terminal, redirect it to nohup.out file (created in current directory or specified by HOME environment variable):

if (isatty(STDOUT_FILENO))

In dofile option:

if (dup2(fd, STDOUT_FILENO) == -1)
    err(EXIT_MISC, NULL);

(2) If stderr is terminal, redirect it to stdout. In this case, stderr and stdout will point to same file:

if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {

(3) Ignore SIGHUP signal:

(void)signal(SIGHUP, SIG_IGN);

(4) Execute the intended command:

execvp(argv[1], &argv[1]);

That’s all!

If a project helps you, please give it a star!

I used to fork projects from github when I thought they are useful: even not currently but maybe I need it in the future, so I did a backup of it. However, I didn’t always give the project a star. Graudally, I found this is not a good habit. Currenlty, When I fork a project, I acknowledge it gives me valuable information, I always give this project a star.

The reason behind it is simple: for a repository in github, no matter it belongs to a company or an independent developer, is an outcome of hardworking. Especially for an individual, who mostly sacrifices his own spare time to foster the project and share it freely. You don’t need to pay even one penny to obatin knowledge from others’ IP, and only one click of mouse can express your gratitude and respect for original author. It is not too hard, correct?

Actually, the author can often get motivated by this simple action because it means his endeavour gets a recognization and he earns potential reputation in his career; this may drive he to polish project continuously. The users, including you, will get benefit from a high-qualified project with doubt, so there will be a “win-win” result finally.

Based on above exposition, maybe everyone should give a star for the projects which have helped you. This is the cheapest method to support Free& Open Source Projects, no matter it is software, hardware, and whatever.

Be aware of space when issuing HTTP request

Use netcat to issue HTTP request:

# nc -cv https
Connection to 443 port [tcp/https] succeeded!
GET / HTTP/1.1
 Connection: close

HTTP/1.1 400 Bad Request
Content-Length: 54
Content-Type: text/html; charset=UTF-8
Date: Mon, 27 Aug 2018 09:37:22 GMT
Connection: close

<html><title>Error 400 (Bad Request)!!1</title></html>

A superfluous space before “Connection: close” will cause error response. Be aware of it!