@@ -481,3 +481,40 @@ def test_reader(self):
481481 finally :
482482 if os .path .exists (fname ):
483483 os .unlink (fname )
484+
485+
486+ class TestIOFlatMMap (unittest .TestCase ):
487+
488+ def test_mmap (self ):
489+ xt , xb , xq = get_dataset_2 (32 , 0 , 100 , 50 )
490+ index = faiss .index_factory (32 , "SQfp16" , faiss .METRIC_L2 )
491+ # does not need training
492+ index .add (xb )
493+ Dref , Iref = index .search (xq , 10 )
494+
495+ fd , fname = tempfile .mkstemp ()
496+ os .close (fd )
497+ try :
498+ faiss .write_index (index , fname )
499+ index2 = faiss .read_index (fname , faiss .IO_FLAG_MMAP_IFC )
500+ Dnew , Inew = index2 .search (xq , 10 )
501+ np .testing .assert_array_equal (Iref , Inew )
502+ np .testing .assert_array_equal (Dref , Dnew )
503+ finally :
504+ if os .path .exists (fname ):
505+ os .unlink (fname )
506+
507+ def test_zerocopy (self ):
508+ xt , xb , xq = get_dataset_2 (32 , 0 , 100 , 50 )
509+ index = faiss .index_factory (32 , "SQfp16" , faiss .METRIC_L2 )
510+ # does not need training
511+ index .add (xb )
512+ Dref , Iref = index .search (xq , 10 )
513+
514+ serialized_index = faiss .serialize_index (index )
515+ reader = faiss .ZeroCopyIOReader (
516+ faiss .swig_ptr (serialized_index ), serialized_index .size )
517+ index2 = faiss .read_index (reader )
518+ Dnew , Inew = index2 .search (xq , 10 )
519+ np .testing .assert_array_equal (Iref , Inew )
520+ np .testing .assert_array_equal (Dref , Dnew )
0 commit comments