Mottl / lightgbm3-rs

LightGBM Rust library
MIT License
11 stars 6 forks source link

Is `Booster` thread safe? #6

Closed tokahuke closed 2 months ago

tokahuke commented 3 months ago

So, I have run into the problem that Booster is neither Send nor Sync, since it contains some naughty pointers inside. However, most of the operations it seems to be doing look inherently immutable. Would it be sound to add unsafe impl's for Send and Sync for Booster?

Mottl commented 2 months ago

Hi! I'm not sure that Booster is thread safe (at minimum, while training). If you believe it's absolutely necessary for you to have Send or Sync, and your use case is thread safe, you can just implement a wrapper for Booster.

struct WBooster(Booster);
unsafe impl Send for WBooster {}
unsafe impl Sync for WBooster {}

p.s. Even if we imagine, that Booster is thread safe right now, we can't assume it will be thread safe in the future, cause it completely depends on the underlying C++ implementation, which maybe changed.

tokahuke commented 2 months ago

Well, that is what I am doing at the moment. Fortunately, for my use case, I only need the evaluation part (an immutable operation). I can see, however, that it is has no reason to be threadsafe for training.