mystor / rust-cpp

Embed C++ directly inside your rust code!
Apache License 2.0
795 stars 44 forks source link

member reference type '..... *const' #77

Closed ansjsun closed 4 years ago

ansjsun commented 4 years ago

I really can't find a way how to solve it

I binding class

cpp_class!(pub unsafe struct IndexIVFFlat as "faiss::IndexIVFFlat");

let index = unsafe {
            cpp!([dimension as "int",index_size as "size_t",train_size as "size_t", trainvecs as "std::vector<float>"] ->  IndexIVFFlat as "faiss::IndexIVFFlat"{
                size_t nhash = 2;
                size_t nbits_subq = int (log2 (index_size+1) / 2);
                size_t ncentroids = 1 << (nhash * nbits_subq);
                faiss::MultiIndexQuantizer coarse_quantizer (dimension, nhash, nbits_subq);
                faiss::MetricType metric = faiss::METRIC_L2;
                // faiss::IndexIVFFlat *index = new faiss::IndexIVFFlat(&coarse_quantizer, dimension, ncentroids, metric);
                faiss::IndexIVFFlat index (&coarse_quantizer, dimension, ncentroids, metric);
                index.quantizer_trains_alone = true;
                index.verbose = true;
                index.nprobe = 2048;
                printf("1111111333333") ;
                index.train(train_size, trainvecs.data());
                return index ;
            })
        };

it 's run ok .

but when I split train method

 let index = unsafe {
            cpp!([dimension as "int",index_size as "size_t",train_size as "size_t", trainvecs as "std::vector<float>"] ->  IndexIVFFlat as "faiss::IndexIVFFlat"{
                size_t nhash = 2;
                size_t nbits_subq = int (log2 (index_size+1) / 2);
                size_t ncentroids = 1 << (nhash * nbits_subq);
                faiss::MultiIndexQuantizer coarse_quantizer (dimension, nhash, nbits_subq);
                faiss::MetricType metric = faiss::METRIC_L2;
                // faiss::IndexIVFFlat *index = new faiss::IndexIVFFlat(&coarse_quantizer, dimension, ncentroids, metric);
                faiss::IndexIVFFlat index (&coarse_quantizer, dimension, ncentroids, metric);
                index.quantizer_trains_alone = true;
                index.verbose = true;
                index.nprobe = 2048;
                return index ;
            })
        };

        unsafe {
            cpp!([index as "faiss::IndexIVFFlat", train_size as "size_t", trainvecs as "std::vector<float>"] ->  IndexIVFFlat as "faiss::IndexIVFFlat"{
                index.train(train_size, trainvecs.data());
            })
        }

it has err

cargo:warning=src/lib.rs:45:17: error: 'this' argument to member function 'train' has type 'const faiss::IndexIVFFlat', but function is not marked const
cargo:warning=                index.train(train_size, trainvecs.data());
cargo:warning=                ^~~~~
cargo:warning=/usr/local/include/faiss/IndexIVF.h:136:10: note: 'train' declared here
cargo:warning=    void train(idx_t n, const float* x) override;

so i take by this

let index: IndexIVFFlat = unsafe {
            cpp!([dimension as "int",index_size as "size_t",train_size as "size_t", trainvecs as "std::vector<float>"] ->  IndexIVFFlat as "faiss::IndexIVFFlat"{
                size_t nhash = 2;
                size_t nbits_subq = int (log2 (index_size+1) / 2);
                size_t ncentroids = 1 << (nhash * nbits_subq);
                faiss::MultiIndexQuantizer coarse_quantizer (dimension, nhash, nbits_subq);
                faiss::MetricType metric = faiss::METRIC_L2;
                // faiss::IndexIVFFlat *index = new faiss::IndexIVFFlat(&coarse_quantizer, dimension, ncentroids, metric);
                faiss::IndexIVFFlat index (&coarse_quantizer, dimension, ncentroids, metric);
                index.quantizer_trains_alone = true;
                index.verbose = true;
                index.nprobe = 2048;
                return index ;
            })
        };

        let index = &index;

        unsafe {
            cpp!([index as "faiss::IndexIVFFlat *", train_size as "size_t", trainvecs as "std::vector<float>"] ->  IndexIVFFlat as "faiss::IndexIVFFlat"{
                index -> train(train_size, trainvecs.data());
            });
        }

it has err:

running 1 test
Training level-1 quantizer
IVF quantizer trains alone...
error: test failed, to rerun pass '--lib'

Caused by:
  process didn't exit successfully: `/Users/xx/Documents/rustworkspace/rust-faiss/target/debug/deps/faiss4rs-c78b110c672d2238` (signal: 10, SIGBUS: access to undefined memory)

or let index = &mut index; the error same .

what should I do! thanks very much

ogoffart commented 4 years ago

Well, C++ is unsafe. I'm afraid you will have to debug with the usual tools (gdb/valgrind/...)

ansjsun commented 4 years ago

thx very much . The problem has been solved

                faiss::MultiIndexQuantizer coarse_quantizer (dimension, nhash, nbits_subq);

need to new