Rayon, Rust/C bindings and involuntary context switches

Hi everyone,

I'm writing a program that uses a C library that was binded to Rust. I am also using Rayon to call some functions from that library in parallel. While it works fine, I've noticed there is a huge amount of involuntary context switches that make the multithreaded version much slower than the standard one.

Does anybody know what could be causing that?

Here's the result of the time command with multiple threads:

	User time (seconds): 13.13
	System time (seconds): 5.44
	Percent of CPU this job got: 582%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.18
	Maximum resident set size (kbytes): 1968684
	Minor (reclaiming a frame) page faults: 491505
	Voluntary context switches: 196762
	Involuntary context switches: 402217

Here's the same but with a single thread:

	User time (seconds): 1.53
	System time (seconds): 0.72
	Percent of CPU this job got: 100%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:02.25
	Maximum resident set size (kbytes): 116376
	Minor (reclaiming a frame) page faults: 483165
	Voluntary context switches: 338
	Involuntary context switches: 37

Thanks in advance!

Out of curiosity, what sorts of jobs are you parallelising?

It sounds like the jobs are small enough that rayon spends more time starting jobs and stealing work from other threads than it spends actually doing the work.

2 Likes