File: test_parallel.R

package info (click to toggle)
r-cran-rcpphnsw 0.3.0.9001%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 316 kB
  • sloc: cpp: 386; ansic: 58; sh: 13; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,003 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
library(RcppHNSW)
context("test parallel")

set.seed(1)

x <- matrix(rnorm(n = 1280 * 10), ncol = 10)

ind_0 <- hnsw_build(
  x,
  distance = "euclidean",
  M = 16,
  ef = 200,
  verbose = FALSE,
  progress = "bar",
  n_threads = 0
)

ind_1 <- hnsw_build(
  x,
  distance = "euclidean",
  M = 16,
  ef = 200,
  verbose = FALSE,
  progress = "bar",
  n_threads = 1
)

ind_2 <- hnsw_build(
  x,
  distance = "euclidean",
  M = 16,
  ef = 200,
  verbose = FALSE,
  progress = "bar",
  n_threads = 2
)

knn_0 <- hnsw_search(x, ind_0, k = 5)
knn_1 <- hnsw_search(x, ind_1, k = 5)
knn_2 <- hnsw_search(x, ind_2, k = 5)

# Seems index which was built using more than 1 thread is not deterministic
# but in general the difference between index built with 1 thread and
# many threads should be small
expect_lt(mean(knn_0$dist - knn_2$dist), 1e-4)
expect_lt(mean(knn_1$dist - knn_2$dist), 1e-4)

# same check for indices
expect_lt(mean(knn_0$idx != knn_2$idx), 1e-2)
expect_lt(mean(knn_1$idx != knn_2$idx), 1e-2)