c_api.cpp 74.3 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
Guolin Ke's avatar
Guolin Ke committed
5
#include <LightGBM/c_api.h>
Guolin Ke's avatar
Guolin Ke committed
6

Guolin Ke's avatar
Guolin Ke committed
7
8
#include <LightGBM/boosting.h>
#include <LightGBM/config.h>
9
10
11
#include <LightGBM/dataset.h>
#include <LightGBM/dataset_loader.h>
#include <LightGBM/metric.h>
12
#include <LightGBM/network.h>
13
14
15
16
17
18
19
#include <LightGBM/objective_function.h>
#include <LightGBM/prediction_early_stop.h>
#include <LightGBM/utils/common.h>
#include <LightGBM/utils/log.h>
#include <LightGBM/utils/openmp_wrapper.h>
#include <LightGBM/utils/random.h>
#include <LightGBM/utils/threading.h>
Guolin Ke's avatar
Guolin Ke committed
20
21

#include <string>
22
23
#include <cstdio>
#include <functional>
Guolin Ke's avatar
Guolin Ke committed
24
#include <memory>
wxchan's avatar
wxchan committed
25
#include <mutex>
26
27
#include <stdexcept>
#include <vector>
Guolin Ke's avatar
Guolin Ke committed
28

29
#include "application/predictor.hpp"
Guolin Ke's avatar
Guolin Ke committed
30

Guolin Ke's avatar
Guolin Ke committed
31
32
namespace LightGBM {

Guolin Ke's avatar
Guolin Ke committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
inline int LGBM_APIHandleException(const std::exception& ex) {
  LGBM_SetLastError(ex.what());
  return -1;
}
inline int LGBM_APIHandleException(const std::string& ex) {
  LGBM_SetLastError(ex.c_str());
  return -1;
}

#define API_BEGIN() try {
#define API_END() } \
catch(std::exception& ex) { return LGBM_APIHandleException(ex); } \
catch(std::string& ex) { return LGBM_APIHandleException(ex); } \
catch(...) { return LGBM_APIHandleException("unknown exception"); } \
return 0;

49
50
51
52
53
54
55
56
const int PREDICTOR_TYPES = 4;

// Single row predictor to abstract away caching logic
class SingleRowPredictor {
 public:
  PredictFunction predict_function;
  int64_t num_pred_in_one_row;

Guolin Ke's avatar
Guolin Ke committed
57
  SingleRowPredictor(int predict_type, Boosting* boosting, const Config& config, int iter) {
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    bool is_predict_leaf = false;
    bool is_raw_score = false;
    bool predict_contrib = false;
    if (predict_type == C_API_PREDICT_LEAF_INDEX) {
      is_predict_leaf = true;
    } else if (predict_type == C_API_PREDICT_RAW_SCORE) {
      is_raw_score = true;
    } else if (predict_type == C_API_PREDICT_CONTRIB) {
      predict_contrib = true;
    } else {
      is_raw_score = false;
    }
    early_stop_ = config.pred_early_stop;
    early_stop_freq_ = config.pred_early_stop_freq;
    early_stop_margin_ = config.pred_early_stop_margin;
    iter_ = iter;
Guolin Ke's avatar
Guolin Ke committed
74
    predictor_.reset(new Predictor(boosting, iter_, is_raw_score, is_predict_leaf, predict_contrib,
75
                                   early_stop_, early_stop_freq_, early_stop_margin_));
Guolin Ke's avatar
Guolin Ke committed
76
    num_pred_in_one_row = boosting->NumPredictOneRow(iter_, is_predict_leaf, predict_contrib);
77
    predict_function = predictor_->GetPredictFunction();
Guolin Ke's avatar
Guolin Ke committed
78
    num_total_model_ = boosting->NumberOfTotalModel();
79
  }
80

81
  ~SingleRowPredictor() {}
82

Guolin Ke's avatar
Guolin Ke committed
83
  bool IsPredictorEqual(const Config& config, int iter, Boosting* boosting) {
84
85
86
87
88
    return early_stop_ == config.pred_early_stop &&
      early_stop_freq_ == config.pred_early_stop_freq &&
      early_stop_margin_ == config.pred_early_stop_margin &&
      iter_ == iter &&
      num_total_model_ == boosting->NumberOfTotalModel();
89
  }
Guolin Ke's avatar
Guolin Ke committed
90

91
92
93
94
95
96
97
98
99
 private:
  std::unique_ptr<Predictor> predictor_;
  bool early_stop_;
  int early_stop_freq_;
  double early_stop_margin_;
  int iter_;
  int num_total_model_;
};

Guolin Ke's avatar
Guolin Ke committed
100
class Booster {
Nikita Titov's avatar
Nikita Titov committed
101
 public:
Guolin Ke's avatar
Guolin Ke committed
102
  explicit Booster(const char* filename) {
103
    boosting_.reset(Boosting::CreateBoosting("gbdt", filename));
104
105
  }

Guolin Ke's avatar
Guolin Ke committed
106
  Booster(const Dataset* train_data,
107
          const char* parameters) {
Guolin Ke's avatar
Guolin Ke committed
108
    auto param = Config::Str2Map(parameters);
wxchan's avatar
wxchan committed
109
    config_.Set(param);
110
111
112
    if (config_.num_threads > 0) {
      omp_set_num_threads(config_.num_threads);
    }
Guolin Ke's avatar
Guolin Ke committed
113
    // create boosting
Guolin Ke's avatar
Guolin Ke committed
114
    if (config_.input_model.size() > 0) {
115
116
      Log::Warning("Continued train from model is not supported for c_api,\n"
                   "please use continued train with input score");
Guolin Ke's avatar
Guolin Ke committed
117
    }
Guolin Ke's avatar
Guolin Ke committed
118

Guolin Ke's avatar
Guolin Ke committed
119
    boosting_.reset(Boosting::CreateBoosting(config_.boosting, nullptr));
Guolin Ke's avatar
Guolin Ke committed
120

121
122
    train_data_ = train_data;
    CreateObjectiveAndMetrics();
Guolin Ke's avatar
Guolin Ke committed
123
    // initialize the boosting
Guolin Ke's avatar
Guolin Ke committed
124
    if (config_.tree_learner == std::string("feature")) {
125
      Log::Fatal("Do not support feature parallel in c api");
126
    }
Guolin Ke's avatar
Guolin Ke committed
127
    if (Network::num_machines() == 1 && config_.tree_learner != std::string("serial")) {
128
      Log::Warning("Only find one worker, will switch to serial tree learner");
Guolin Ke's avatar
Guolin Ke committed
129
      config_.tree_learner = "serial";
130
    }
Guolin Ke's avatar
Guolin Ke committed
131
    boosting_->Init(&config_, train_data_, objective_fun_.get(),
132
                    Common::ConstPtrInVectorWrapper<Metric>(train_metric_));
wxchan's avatar
wxchan committed
133
134
135
136
137
  }

  void MergeFrom(const Booster* other) {
    std::lock_guard<std::mutex> lock(mutex_);
    boosting_->MergeFrom(other->boosting_.get());
Guolin Ke's avatar
Guolin Ke committed
138
139
140
141
  }

  ~Booster() {
  }
142

143
  void CreateObjectiveAndMetrics() {
Guolin Ke's avatar
Guolin Ke committed
144
    // create objective function
Guolin Ke's avatar
Guolin Ke committed
145
146
    objective_fun_.reset(ObjectiveFunction::CreateObjectiveFunction(config_.objective,
                                                                    config_));
Guolin Ke's avatar
Guolin Ke committed
147
148
149
150
151
152
153
154
155
156
    if (objective_fun_ == nullptr) {
      Log::Warning("Using self-defined objective function");
    }
    // initialize the objective function
    if (objective_fun_ != nullptr) {
      objective_fun_->Init(train_data_->metadata(), train_data_->num_data());
    }

    // create training metric
    train_metric_.clear();
Guolin Ke's avatar
Guolin Ke committed
157
    for (auto metric_type : config_.metric) {
Guolin Ke's avatar
Guolin Ke committed
158
      auto metric = std::unique_ptr<Metric>(
Guolin Ke's avatar
Guolin Ke committed
159
        Metric::CreateMetric(metric_type, config_));
Guolin Ke's avatar
Guolin Ke committed
160
161
162
163
164
      if (metric == nullptr) { continue; }
      metric->Init(train_data_->metadata(), train_data_->num_data());
      train_metric_.push_back(std::move(metric));
    }
    train_metric_.shrink_to_fit();
165
166
167
168
169
170
171
172
173
174
175
  }

  void ResetTrainingData(const Dataset* train_data) {
    if (train_data != train_data_) {
      std::lock_guard<std::mutex> lock(mutex_);
      train_data_ = train_data;
      CreateObjectiveAndMetrics();
      // reset the boosting
      boosting_->ResetTrainingData(train_data_,
                                   objective_fun_.get(), Common::ConstPtrInVectorWrapper<Metric>(train_metric_));
    }
wxchan's avatar
wxchan committed
176
177
  }

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  static void CheckDatasetResetConfig(
      const Config& old_config,
      const std::unordered_map<std::string, std::string>& new_param) {
    Config new_config;
    new_config.Set(new_param);
    if (new_param.count("data_random_seed") &&
        new_config.data_random_seed != old_config.data_random_seed) {
      Log::Fatal("Cannot change data_random_seed after constructed Dataset handle.");
    }
    if (new_param.count("max_bin") &&
        new_config.max_bin != old_config.max_bin) {
      Log::Fatal("Cannot change max_bin after constructed Dataset handle.");
    }
    if (new_param.count("max_bin_by_feature") &&
        new_config.max_bin_by_feature != old_config.max_bin_by_feature) {
      Log::Fatal(
          "Cannot change max_bin_by_feature after constructed Dataset handle.");
    }
    if (new_param.count("bin_construct_sample_cnt") &&
        new_config.bin_construct_sample_cnt !=
            old_config.bin_construct_sample_cnt) {
      Log::Fatal(
          "Cannot change bin_construct_sample_cnt after constructed Dataset "
          "handle.");
    }
    if (new_param.count("min_data_in_bin") &&
        new_config.min_data_in_bin != old_config.min_data_in_bin) {
      Log::Fatal(
          "Cannot change min_data_in_bin after constructed Dataset handle.");
    }
    if (new_param.count("use_missing") &&
        new_config.use_missing != old_config.use_missing) {
      Log::Fatal("Cannot change use_missing after constructed Dataset handle.");
    }
    if (new_param.count("zero_as_missing") &&
        new_config.zero_as_missing != old_config.zero_as_missing) {
      Log::Fatal(
          "Cannot change zero_as_missing after constructed Dataset handle.");
    }
    if (new_param.count("categorical_feature") &&
        new_config.categorical_feature != old_config.categorical_feature) {
      Log::Fatal(
          "Cannot change categorical_feature after constructed Dataset "
          "handle.");
    }
    if (new_param.count("feature_pre_filter") &&
        new_config.feature_pre_filter != old_config.feature_pre_filter) {
      Log::Fatal(
          "Cannot change feature_pre_filter after constructed Dataset handle.");
    }
    if (new_param.count("is_enable_sparse") &&
        new_config.is_enable_sparse != old_config.is_enable_sparse) {
      Log::Fatal(
          "Cannot change is_enable_sparse after constructed Dataset handle.");
    }
    if (new_param.count("pre_partition") &&
        new_config.pre_partition != old_config.pre_partition) {
      Log::Fatal(
          "Cannot change pre_partition after constructed Dataset handle.");
    }
    if (new_param.count("enable_bundle") &&
        new_config.enable_bundle != old_config.enable_bundle) {
      Log::Fatal(
          "Cannot change enable_bundle after constructed Dataset handle.");
    }
    if (new_param.count("header") && new_config.header != old_config.header) {
      Log::Fatal("Cannot change header after constructed Dataset handle.");
    }
    if (new_param.count("two_round") &&
        new_config.two_round != old_config.two_round) {
      Log::Fatal("Cannot change two_round after constructed Dataset handle.");
    }
    if (new_param.count("label_column") &&
        new_config.label_column != old_config.label_column) {
      Log::Fatal(
          "Cannot change label_column after constructed Dataset handle.");
    }
    if (new_param.count("weight_column") &&
        new_config.weight_column != old_config.weight_column) {
      Log::Fatal(
          "Cannot change weight_column after constructed Dataset handle.");
    }
    if (new_param.count("group_column") &&
        new_config.group_column != old_config.group_column) {
      Log::Fatal(
          "Cannot change group_column after constructed Dataset handle.");
    }
    if (new_param.count("ignore_column") &&
        new_config.ignore_column != old_config.ignore_column) {
      Log::Fatal(
          "Cannot change ignore_column after constructed Dataset handle.");
    }
    if (new_param.count("forcedbins_filename")) {
      Log::Fatal("Cannot change forced bins after constructed Dataset handle.");
    }
    if (new_param.count("min_data_in_leaf") &&
        new_config.min_data_in_leaf < old_config.min_data_in_leaf &&
        old_config.feature_pre_filter) {
      Log::Fatal(
          "Reducing `min_data_in_leaf` with `feature_pre_filter=true` may "
          "cause unexpected behaviour "
          "for features that were pre-filtered by the larger "
          "`min_data_in_leaf`.\n"
          "You need to set `feature_pre_filter=false` to dynamically change "
          "the `min_data_in_leaf`.");
    }
  }

wxchan's avatar
wxchan committed
286
  void ResetConfig(const char* parameters) {
Guolin Ke's avatar
Guolin Ke committed
287
    std::lock_guard<std::mutex> lock(mutex_);
Guolin Ke's avatar
Guolin Ke committed
288
    auto param = Config::Str2Map(parameters);
wxchan's avatar
wxchan committed
289
    if (param.count("num_class")) {
290
      Log::Fatal("Cannot change num_class during training");
wxchan's avatar
wxchan committed
291
    }
Guolin Ke's avatar
Guolin Ke committed
292
293
    if (param.count("boosting")) {
      Log::Fatal("Cannot change boosting during training");
wxchan's avatar
wxchan committed
294
    }
Guolin Ke's avatar
Guolin Ke committed
295
    if (param.count("metric")) {
296
      Log::Fatal("Cannot change metric during training");
Guolin Ke's avatar
Guolin Ke committed
297
    }
298
299
    CheckDatasetResetConfig(config_, param);

Guolin Ke's avatar
Guolin Ke committed
300
    config_.Set(param);
301

302
303
304
    if (config_.num_threads > 0) {
      omp_set_num_threads(config_.num_threads);
    }
Guolin Ke's avatar
Guolin Ke committed
305
306
307

    if (param.count("objective")) {
      // create objective function
Guolin Ke's avatar
Guolin Ke committed
308
309
      objective_fun_.reset(ObjectiveFunction::CreateObjectiveFunction(config_.objective,
                                                                      config_));
Guolin Ke's avatar
Guolin Ke committed
310
311
312
313
314
315
316
      if (objective_fun_ == nullptr) {
        Log::Warning("Using self-defined objective function");
      }
      // initialize the objective function
      if (objective_fun_ != nullptr) {
        objective_fun_->Init(train_data_->metadata(), train_data_->num_data());
      }
317
318
      boosting_->ResetTrainingData(train_data_,
                                   objective_fun_.get(), Common::ConstPtrInVectorWrapper<Metric>(train_metric_));
wxchan's avatar
wxchan committed
319
    }
Guolin Ke's avatar
Guolin Ke committed
320

Guolin Ke's avatar
Guolin Ke committed
321
    boosting_->ResetConfig(&config_);
wxchan's avatar
wxchan committed
322
323
324
325
326
  }

  void AddValidData(const Dataset* valid_data) {
    std::lock_guard<std::mutex> lock(mutex_);
    valid_metrics_.emplace_back();
Guolin Ke's avatar
Guolin Ke committed
327
328
    for (auto metric_type : config_.metric) {
      auto metric = std::unique_ptr<Metric>(Metric::CreateMetric(metric_type, config_));
wxchan's avatar
wxchan committed
329
330
331
332
333
334
      if (metric == nullptr) { continue; }
      metric->Init(valid_data->metadata(), valid_data->num_data());
      valid_metrics_.back().push_back(std::move(metric));
    }
    valid_metrics_.back().shrink_to_fit();
    boosting_->AddValidDataset(valid_data,
335
                               Common::ConstPtrInVectorWrapper<Metric>(valid_metrics_.back()));
wxchan's avatar
wxchan committed
336
  }
Guolin Ke's avatar
Guolin Ke committed
337

338
  bool TrainOneIter() {
wxchan's avatar
wxchan committed
339
    std::lock_guard<std::mutex> lock(mutex_);
Guolin Ke's avatar
Guolin Ke committed
340
    return boosting_->TrainOneIter(nullptr, nullptr);
341
342
  }

Guolin Ke's avatar
Guolin Ke committed
343
344
345
346
347
348
349
350
351
352
353
  void Refit(const int32_t* leaf_preds, int32_t nrow, int32_t ncol) {
    std::lock_guard<std::mutex> lock(mutex_);
    std::vector<std::vector<int32_t>> v_leaf_preds(nrow, std::vector<int32_t>(ncol, 0));
    for (int i = 0; i < nrow; ++i) {
      for (int j = 0; j < ncol; ++j) {
        v_leaf_preds[i][j] = leaf_preds[i * ncol + j];
      }
    }
    boosting_->RefitTree(v_leaf_preds);
  }

354
  bool TrainOneIter(const score_t* gradients, const score_t* hessians) {
wxchan's avatar
wxchan committed
355
    std::lock_guard<std::mutex> lock(mutex_);
Guolin Ke's avatar
Guolin Ke committed
356
    return boosting_->TrainOneIter(gradients, hessians);
357
358
  }

wxchan's avatar
wxchan committed
359
360
361
362
363
  void RollbackOneIter() {
    std::lock_guard<std::mutex> lock(mutex_);
    boosting_->RollbackOneIter();
  }

364
  void PredictSingleRow(int num_iteration, int predict_type, int ncol,
365
366
367
               std::function<std::vector<std::pair<int, double>>(int row_idx)> get_row_fun,
               const Config& config,
               double* out_result, int64_t* out_len) {
368
369
370
    if (!config.predict_disable_shape_check && ncol != boosting_->MaxFeatureIdx() + 1) {
      Log::Fatal("The number of features in data (%d) is not the same as it was in training data (%d).\n"\
                 "You can set ``predict_disable_shape_check=true`` to discard this error, but please be aware what you are doing.", ncol, boosting_->MaxFeatureIdx() + 1);
371
    }
372
    std::lock_guard<std::mutex> lock(mutex_);
373
    if (single_row_predictor_[predict_type].get() == nullptr ||
Guolin Ke's avatar
Guolin Ke committed
374
375
        !single_row_predictor_[predict_type]->IsPredictorEqual(config, num_iteration, boosting_.get())) {
      single_row_predictor_[predict_type].reset(new SingleRowPredictor(predict_type, boosting_.get(),
376
                                                                       config, num_iteration));
377
378
379
    }
    auto one_row = get_row_fun(0);
    auto pred_wrt_ptr = out_result;
380
    single_row_predictor_[predict_type]->predict_function(one_row, pred_wrt_ptr);
381

382
    *out_len = single_row_predictor_[predict_type]->num_pred_in_one_row;
383
384
385
  }


386
  void Predict(int num_iteration, int predict_type, int nrow, int ncol,
Guolin Ke's avatar
Guolin Ke committed
387
               std::function<std::vector<std::pair<int, double>>(int row_idx)> get_row_fun,
Guolin Ke's avatar
Guolin Ke committed
388
               const Config& config,
Guolin Ke's avatar
Guolin Ke committed
389
               double* out_result, int64_t* out_len) {
390
391
392
    if (!config.predict_disable_shape_check && ncol != boosting_->MaxFeatureIdx() + 1) {
      Log::Fatal("The number of features in data (%d) is not the same as it was in training data (%d).\n" \
                 "You can set ``predict_disable_shape_check=true`` to discard this error, but please be aware what you are doing.", ncol, boosting_->MaxFeatureIdx() + 1);
393
    }
wxchan's avatar
wxchan committed
394
    std::lock_guard<std::mutex> lock(mutex_);
Guolin Ke's avatar
Guolin Ke committed
395
396
    bool is_predict_leaf = false;
    bool is_raw_score = false;
Guolin Ke's avatar
Guolin Ke committed
397
    bool predict_contrib = false;
Guolin Ke's avatar
Guolin Ke committed
398
    if (predict_type == C_API_PREDICT_LEAF_INDEX) {
Guolin Ke's avatar
Guolin Ke committed
399
      is_predict_leaf = true;
Guolin Ke's avatar
Guolin Ke committed
400
    } else if (predict_type == C_API_PREDICT_RAW_SCORE) {
Guolin Ke's avatar
Guolin Ke committed
401
      is_raw_score = true;
402
    } else if (predict_type == C_API_PREDICT_CONTRIB) {
Guolin Ke's avatar
Guolin Ke committed
403
      predict_contrib = true;
Guolin Ke's avatar
Guolin Ke committed
404
405
    } else {
      is_raw_score = false;
Guolin Ke's avatar
Guolin Ke committed
406
    }
Guolin Ke's avatar
Guolin Ke committed
407

Guolin Ke's avatar
Guolin Ke committed
408
    Predictor predictor(boosting_.get(), num_iteration, is_raw_score, is_predict_leaf, predict_contrib,
409
                        config.pred_early_stop, config.pred_early_stop_freq, config.pred_early_stop_margin);
Guolin Ke's avatar
Guolin Ke committed
410
    int64_t num_pred_in_one_row = boosting_->NumPredictOneRow(num_iteration, is_predict_leaf, predict_contrib);
Guolin Ke's avatar
Guolin Ke committed
411
    auto pred_fun = predictor.GetPredictFunction();
412
413
    OMP_INIT_EX();
    #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
414
    for (int i = 0; i < nrow; ++i) {
415
      OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
416
      auto one_row = get_row_fun(i);
Tony-Y's avatar
Tony-Y committed
417
      auto pred_wrt_ptr = out_result + static_cast<size_t>(num_pred_in_one_row) * i;
Guolin Ke's avatar
Guolin Ke committed
418
      pred_fun(one_row, pred_wrt_ptr);
419
      OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
420
    }
421
    OMP_THROW_EX();
422
    *out_len = num_pred_in_one_row * nrow;
Guolin Ke's avatar
Guolin Ke committed
423
424
425
  }

  void Predict(int num_iteration, int predict_type, const char* data_filename,
Guolin Ke's avatar
Guolin Ke committed
426
               int data_has_header, const Config& config,
cbecker's avatar
cbecker committed
427
               const char* result_filename) {
Guolin Ke's avatar
Guolin Ke committed
428
429
430
    std::lock_guard<std::mutex> lock(mutex_);
    bool is_predict_leaf = false;
    bool is_raw_score = false;
Guolin Ke's avatar
Guolin Ke committed
431
    bool predict_contrib = false;
Guolin Ke's avatar
Guolin Ke committed
432
433
434
435
    if (predict_type == C_API_PREDICT_LEAF_INDEX) {
      is_predict_leaf = true;
    } else if (predict_type == C_API_PREDICT_RAW_SCORE) {
      is_raw_score = true;
436
    } else if (predict_type == C_API_PREDICT_CONTRIB) {
Guolin Ke's avatar
Guolin Ke committed
437
      predict_contrib = true;
Guolin Ke's avatar
Guolin Ke committed
438
439
440
    } else {
      is_raw_score = false;
    }
Guolin Ke's avatar
Guolin Ke committed
441
    Predictor predictor(boosting_.get(), num_iteration, is_raw_score, is_predict_leaf, predict_contrib,
442
                        config.pred_early_stop, config.pred_early_stop_freq, config.pred_early_stop_margin);
Guolin Ke's avatar
Guolin Ke committed
443
    bool bool_data_has_header = data_has_header > 0 ? true : false;
444
    predictor.Predict(data_filename, result_filename, bool_data_has_header, config.predict_disable_shape_check);
Guolin Ke's avatar
Guolin Ke committed
445
446
  }

Guolin Ke's avatar
Guolin Ke committed
447
  void GetPredictAt(int data_idx, double* out_result, int64_t* out_len) {
wxchan's avatar
wxchan committed
448
449
450
    boosting_->GetPredictAt(data_idx, out_result, out_len);
  }

451
452
  void SaveModelToFile(int start_iteration, int num_iteration, const char* filename) {
    boosting_->SaveModelToFile(start_iteration, num_iteration, filename);
Guolin Ke's avatar
Guolin Ke committed
453
  }
454

455
  void LoadModelFromString(const char* model_str) {
456
457
    size_t len = std::strlen(model_str);
    boosting_->LoadModelFromString(model_str, len);
458
459
  }

460
461
  std::string SaveModelToString(int start_iteration, int num_iteration) {
    return boosting_->SaveModelToString(start_iteration, num_iteration);
462
463
  }

464
  std::string DumpModel(int start_iteration, int num_iteration) {
465
    return boosting_->DumpModel(start_iteration, num_iteration);
wxchan's avatar
wxchan committed
466
  }
467

468
469
470
471
  std::vector<double> FeatureImportance(int num_iteration, int importance_type) {
    return boosting_->FeatureImportance(num_iteration, importance_type);
  }

472
473
474
475
476
477
478
479
480
481
  double UpperBoundValue() const {
    std::lock_guard<std::mutex> lock(mutex_);
    return boosting_->GetUpperBoundValue();
  }

  double LowerBoundValue() const {
    std::lock_guard<std::mutex> lock(mutex_);
    return boosting_->GetLowerBoundValue();
  }

Guolin Ke's avatar
Guolin Ke committed
482
  double GetLeafValue(int tree_idx, int leaf_idx) const {
Guolin Ke's avatar
Guolin Ke committed
483
    return dynamic_cast<GBDTBase*>(boosting_.get())->GetLeafValue(tree_idx, leaf_idx);
Guolin Ke's avatar
Guolin Ke committed
484
485
486
487
  }

  void SetLeafValue(int tree_idx, int leaf_idx, double val) {
    std::lock_guard<std::mutex> lock(mutex_);
Guolin Ke's avatar
Guolin Ke committed
488
    dynamic_cast<GBDTBase*>(boosting_.get())->SetLeafValue(tree_idx, leaf_idx, val);
Guolin Ke's avatar
Guolin Ke committed
489
490
  }

491
  void ShuffleModels(int start_iter, int end_iter) {
492
    std::lock_guard<std::mutex> lock(mutex_);
493
    boosting_->ShuffleModels(start_iter, end_iter);
494
495
  }

wxchan's avatar
wxchan committed
496
497
498
499
500
501
502
  int GetEvalCounts() const {
    int ret = 0;
    for (const auto& metric : train_metric_) {
      ret += static_cast<int>(metric->GetName().size());
    }
    return ret;
  }
503

504
505
  int GetEvalNames(char** out_strs, const int len, const size_t buffer_len, size_t *out_buffer_len) const {
    *out_buffer_len = 0;
wxchan's avatar
wxchan committed
506
507
508
    int idx = 0;
    for (const auto& metric : train_metric_) {
      for (const auto& name : metric->GetName()) {
509
510
511
512
513
        if (idx < len) {
          std::memcpy(out_strs[idx], name.c_str(), std::min(name.size() + 1, buffer_len));
          out_strs[idx][buffer_len - 1] = '\0';
        }
        *out_buffer_len = std::max(name.size() + 1, *out_buffer_len);
wxchan's avatar
wxchan committed
514
515
516
517
518
519
        ++idx;
      }
    }
    return idx;
  }

520
521
  int GetFeatureNames(char** out_strs, const int len, const size_t buffer_len, size_t *out_buffer_len) const {
    *out_buffer_len = 0;
wxchan's avatar
wxchan committed
522
523
    int idx = 0;
    for (const auto& name : boosting_->FeatureNames()) {
524
525
526
527
528
      if (idx < len) {
        std::memcpy(out_strs[idx], name.c_str(), std::min(name.size() + 1, buffer_len));
        out_strs[idx][buffer_len - 1] = '\0';
      }
      *out_buffer_len = std::max(name.size() + 1, *out_buffer_len);
wxchan's avatar
wxchan committed
529
530
531
532
533
      ++idx;
    }
    return idx;
  }

wxchan's avatar
wxchan committed
534
  const Boosting* GetBoosting() const { return boosting_.get(); }
Guolin Ke's avatar
Guolin Ke committed
535

Nikita Titov's avatar
Nikita Titov committed
536
 private:
wxchan's avatar
wxchan committed
537
  const Dataset* train_data_;
Guolin Ke's avatar
Guolin Ke committed
538
  std::unique_ptr<Boosting> boosting_;
539
  std::unique_ptr<SingleRowPredictor> single_row_predictor_[PREDICTOR_TYPES];
540

Guolin Ke's avatar
Guolin Ke committed
541
  /*! \brief All configs */
Guolin Ke's avatar
Guolin Ke committed
542
  Config config_;
Guolin Ke's avatar
Guolin Ke committed
543
  /*! \brief Metric for training data */
Guolin Ke's avatar
Guolin Ke committed
544
  std::vector<std::unique_ptr<Metric>> train_metric_;
Guolin Ke's avatar
Guolin Ke committed
545
  /*! \brief Metrics for validation data */
Guolin Ke's avatar
Guolin Ke committed
546
  std::vector<std::vector<std::unique_ptr<Metric>>> valid_metrics_;
Guolin Ke's avatar
Guolin Ke committed
547
  /*! \brief Training objective function */
Guolin Ke's avatar
Guolin Ke committed
548
  std::unique_ptr<ObjectiveFunction> objective_fun_;
wxchan's avatar
wxchan committed
549
  /*! \brief mutex for threading safe call */
550
  mutable std::mutex mutex_;
Guolin Ke's avatar
Guolin Ke committed
551
552
};

553
}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
554

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// explicitly declare symbols from LightGBM namespace
using LightGBM::AllgatherFunction;
using LightGBM::Booster;
using LightGBM::Common::CheckElementsIntervalClosed;
using LightGBM::Common::RemoveQuotationSymbol;
using LightGBM::Common::Vector2Ptr;
using LightGBM::Common::VectorSize;
using LightGBM::Config;
using LightGBM::data_size_t;
using LightGBM::Dataset;
using LightGBM::DatasetLoader;
using LightGBM::kZeroThreshold;
using LightGBM::LGBM_APIHandleException;
using LightGBM::Log;
using LightGBM::Network;
using LightGBM::Random;
using LightGBM::ReduceScatterFunction;
Guolin Ke's avatar
Guolin Ke committed
572

Guolin Ke's avatar
Guolin Ke committed
573
574
575
576
577
578
579
580
// some help functions used to convert data

std::function<std::vector<double>(int row_idx)>
RowFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major);

std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major);

581
582
583
std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseRows(const void** data, int num_col, int data_type);

Guolin Ke's avatar
Guolin Ke committed
584
585
std::function<std::vector<std::pair<int, double>>(int idx)>
RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
586
                   const void* data, int data_type, int64_t nindptr, int64_t nelem);
Guolin Ke's avatar
Guolin Ke committed
587
588
589

// Row iterator of on column for CSC matrix
class CSC_RowIterator {
Nikita Titov's avatar
Nikita Titov committed
590
 public:
Guolin Ke's avatar
Guolin Ke committed
591
  CSC_RowIterator(const void* col_ptr, int col_ptr_type, const int32_t* indices,
592
                  const void* data, int data_type, int64_t ncol_ptr, int64_t nelem, int col_idx);
Guolin Ke's avatar
Guolin Ke committed
593
594
595
596
597
  ~CSC_RowIterator() {}
  // return value at idx, only can access by ascent order
  double Get(int idx);
  // return next non-zero pair, if index < 0, means no more data
  std::pair<int, double> NextNonZero();
Nikita Titov's avatar
Nikita Titov committed
598
599

 private:
Guolin Ke's avatar
Guolin Ke committed
600
601
602
603
604
605
606
607
608
  int nonzero_idx_ = 0;
  int cur_idx_ = -1;
  double cur_val_ = 0.0f;
  bool is_end_ = false;
  std::function<std::pair<int, double>(int idx)> iter_fun_;
};

// start of c_api functions

Guolin Ke's avatar
Guolin Ke committed
609
const char* LGBM_GetLastError() {
wxchan's avatar
wxchan committed
610
  return LastErrorMsg();
Guolin Ke's avatar
Guolin Ke committed
611
612
}

Guolin Ke's avatar
Guolin Ke committed
613
int LGBM_DatasetCreateFromFile(const char* filename,
614
615
616
                               const char* parameters,
                               const DatasetHandle reference,
                               DatasetHandle* out) {
617
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
618
619
  auto param = Config::Str2Map(parameters);
  Config config;
620
621
622
623
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
624
  DatasetLoader loader(config, nullptr, 1, filename);
Guolin Ke's avatar
Guolin Ke committed
625
  if (reference == nullptr) {
626
    if (Network::num_machines() == 1) {
627
      *out = loader.LoadFromFile(filename);
628
    } else {
629
      *out = loader.LoadFromFile(filename, Network::rank(), Network::num_machines());
630
    }
Guolin Ke's avatar
Guolin Ke committed
631
  } else {
632
    *out = loader.LoadFromFileAlignWithOtherDataset(filename,
633
                                                    reinterpret_cast<const Dataset*>(reference));
Guolin Ke's avatar
Guolin Ke committed
634
  }
635
  API_END();
Guolin Ke's avatar
Guolin Ke committed
636
637
}

638

Guolin Ke's avatar
Guolin Ke committed
639
int LGBM_DatasetCreateFromSampledColumn(double** sample_data,
640
641
642
643
644
645
646
                                        int** sample_indices,
                                        int32_t ncol,
                                        const int* num_per_col,
                                        int32_t num_sample_row,
                                        int32_t num_total_row,
                                        const char* parameters,
                                        DatasetHandle* out) {
647
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
648
649
  auto param = Config::Str2Map(parameters);
  Config config;
650
651
652
653
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
Guolin Ke's avatar
Guolin Ke committed
654
  DatasetLoader loader(config, nullptr, 1, nullptr);
655
656
657
658
  *out = loader.CostructFromSampleData(sample_data, sample_indices, ncol, num_per_col,
                                       num_sample_row,
                                       static_cast<data_size_t>(num_total_row));
  API_END();
Guolin Ke's avatar
Guolin Ke committed
659
660
}

661

Guolin Ke's avatar
Guolin Ke committed
662
int LGBM_DatasetCreateByReference(const DatasetHandle reference,
663
664
                                  int64_t num_total_row,
                                  DatasetHandle* out) {
Guolin Ke's avatar
Guolin Ke committed
665
666
667
668
669
670
671
672
  API_BEGIN();
  std::unique_ptr<Dataset> ret;
  ret.reset(new Dataset(static_cast<data_size_t>(num_total_row)));
  ret->CreateValid(reinterpret_cast<const Dataset*>(reference));
  *out = ret.release();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
673
int LGBM_DatasetPushRows(DatasetHandle dataset,
674
675
676
677
678
                         const void* data,
                         int data_type,
                         int32_t nrow,
                         int32_t ncol,
                         int32_t start_row) {
Guolin Ke's avatar
Guolin Ke committed
679
680
681
  API_BEGIN();
  auto p_dataset = reinterpret_cast<Dataset*>(dataset);
  auto get_row_fun = RowFunctionFromDenseMatric(data, nrow, ncol, data_type, 1);
682
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
683
  #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
684
  for (int i = 0; i < nrow; ++i) {
685
    OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
686
687
688
    const int tid = omp_get_thread_num();
    auto one_row = get_row_fun(i);
    p_dataset->PushOneRow(tid, start_row + i, one_row);
689
    OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
690
  }
691
  OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
692
693
694
695
696
697
  if (start_row + nrow == p_dataset->num_data()) {
    p_dataset->FinishLoad();
  }
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
698
int LGBM_DatasetPushRowsByCSR(DatasetHandle dataset,
699
700
701
702
703
704
705
706
707
                              const void* indptr,
                              int indptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t nindptr,
                              int64_t nelem,
                              int64_t,
                              int64_t start_row) {
Guolin Ke's avatar
Guolin Ke committed
708
709
710
711
  API_BEGIN();
  auto p_dataset = reinterpret_cast<Dataset*>(dataset);
  auto get_row_fun = RowFunctionFromCSR(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
  int32_t nrow = static_cast<int32_t>(nindptr - 1);
712
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
713
  #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
714
  for (int i = 0; i < nrow; ++i) {
715
    OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
716
717
718
    const int tid = omp_get_thread_num();
    auto one_row = get_row_fun(i);
    p_dataset->PushOneRow(tid,
719
                          static_cast<data_size_t>(start_row + i), one_row);
720
    OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
721
  }
722
  OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
723
724
725
726
727
728
  if (start_row + nrow == static_cast<int64_t>(p_dataset->num_data())) {
    p_dataset->FinishLoad();
  }
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
729
int LGBM_DatasetCreateFromMat(const void* data,
730
731
732
733
734
735
736
                              int data_type,
                              int32_t nrow,
                              int32_t ncol,
                              int is_row_major,
                              const char* parameters,
                              const DatasetHandle reference,
                              DatasetHandle* out) {
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
  return LGBM_DatasetCreateFromMats(1,
                                    &data,
                                    data_type,
                                    &nrow,
                                    ncol,
                                    is_row_major,
                                    parameters,
                                    reference,
                                    out);
}


int LGBM_DatasetCreateFromMats(int32_t nmat,
                               const void** data,
                               int data_type,
                               int32_t* nrow,
                               int32_t ncol,
                               int is_row_major,
                               const char* parameters,
                               const DatasetHandle reference,
                               DatasetHandle* out) {
758
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
759
760
  auto param = Config::Str2Map(parameters);
  Config config;
761
762
763
764
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
Guolin Ke's avatar
Guolin Ke committed
765
  std::unique_ptr<Dataset> ret;
766
767
768
769
770
771
772
773
774
  int32_t total_nrow = 0;
  for (int j = 0; j < nmat; ++j) {
    total_nrow += nrow[j];
  }

  std::vector<std::function<std::vector<double>(int row_idx)>> get_row_fun;
  for (int j = 0; j < nmat; ++j) {
    get_row_fun.push_back(RowFunctionFromDenseMatric(data[j], nrow[j], ncol, data_type, is_row_major));
  }
775

Guolin Ke's avatar
Guolin Ke committed
776
777
  if (reference == nullptr) {
    // sample data first
Guolin Ke's avatar
Guolin Ke committed
778
    Random rand(config.data_random_seed);
779
780
    int sample_cnt = static_cast<int>(total_nrow < config.bin_construct_sample_cnt ? total_nrow : config.bin_construct_sample_cnt);
    auto sample_indices = rand.Sample(total_nrow, sample_cnt);
781
    sample_cnt = static_cast<int>(sample_indices.size());
782
    std::vector<std::vector<double>> sample_values(ncol);
Guolin Ke's avatar
Guolin Ke committed
783
    std::vector<std::vector<int>> sample_idx(ncol);
784
785
786

    int offset = 0;
    int j = 0;
Guolin Ke's avatar
Guolin Ke committed
787
    for (size_t i = 0; i < sample_indices.size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
788
      auto idx = sample_indices[i];
789
790
791
792
      while ((idx - offset) >= nrow[j]) {
        offset += nrow[j];
        ++j;
      }
793

794
795
796
797
798
      auto row = get_row_fun[j](static_cast<int>(idx - offset));
      for (size_t k = 0; k < row.size(); ++k) {
        if (std::fabs(row[k]) > kZeroThreshold || std::isnan(row[k])) {
          sample_values[k].emplace_back(row[k]);
          sample_idx[k].emplace_back(static_cast<int>(i));
Guolin Ke's avatar
Guolin Ke committed
799
        }
Guolin Ke's avatar
Guolin Ke committed
800
801
      }
    }
Guolin Ke's avatar
Guolin Ke committed
802
    DatasetLoader loader(config, nullptr, 1, nullptr);
803
804
    ret.reset(loader.CostructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                            Vector2Ptr<int>(&sample_idx).data(),
805
                                            ncol,
806
                                            VectorSize<double>(sample_values).data(),
807
                                            sample_cnt, total_nrow));
Guolin Ke's avatar
Guolin Ke committed
808
  } else {
809
    ret.reset(new Dataset(total_nrow));
Guolin Ke's avatar
Guolin Ke committed
810
    ret->CreateValid(
811
      reinterpret_cast<const Dataset*>(reference));
Guolin Ke's avatar
Guolin Ke committed
812
  }
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  int32_t start_row = 0;
  for (int j = 0; j < nmat; ++j) {
    OMP_INIT_EX();
    #pragma omp parallel for schedule(static)
    for (int i = 0; i < nrow[j]; ++i) {
      OMP_LOOP_EX_BEGIN();
      const int tid = omp_get_thread_num();
      auto one_row = get_row_fun[j](i);
      ret->PushOneRow(tid, start_row + i, one_row);
      OMP_LOOP_EX_END();
    }
    OMP_THROW_EX();

    start_row += nrow[j];
Guolin Ke's avatar
Guolin Ke committed
827
828
  }
  ret->FinishLoad();
Guolin Ke's avatar
Guolin Ke committed
829
  *out = ret.release();
830
  API_END();
831
832
}

Guolin Ke's avatar
Guolin Ke committed
833
int LGBM_DatasetCreateFromCSR(const void* indptr,
834
835
836
837
838
839
840
841
842
843
                              int indptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t nindptr,
                              int64_t nelem,
                              int64_t num_col,
                              const char* parameters,
                              const DatasetHandle reference,
                              DatasetHandle* out) {
844
  API_BEGIN();
845
846
847
848
849
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
Guolin Ke's avatar
Guolin Ke committed
850
851
  auto param = Config::Str2Map(parameters);
  Config config;
852
853
854
855
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
Guolin Ke's avatar
Guolin Ke committed
856
  std::unique_ptr<Dataset> ret;
857
  auto get_row_fun = RowFunctionFromCSR(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
858
859
860
  int32_t nrow = static_cast<int32_t>(nindptr - 1);
  if (reference == nullptr) {
    // sample data first
Guolin Ke's avatar
Guolin Ke committed
861
862
    Random rand(config.data_random_seed);
    int sample_cnt = static_cast<int>(nrow < config.bin_construct_sample_cnt ? nrow : config.bin_construct_sample_cnt);
863
    auto sample_indices = rand.Sample(nrow, sample_cnt);
864
    sample_cnt = static_cast<int>(sample_indices.size());
Guolin Ke's avatar
Guolin Ke committed
865
866
    std::vector<std::vector<double>> sample_values(num_col);
    std::vector<std::vector<int>> sample_idx(num_col);
867
868
869
870
    for (size_t i = 0; i < sample_indices.size(); ++i) {
      auto idx = sample_indices[i];
      auto row = get_row_fun(static_cast<int>(idx));
      for (std::pair<int, double>& inner_data : row) {
Nikita Titov's avatar
Nikita Titov committed
871
        CHECK_LT(inner_data.first, num_col);
Guolin Ke's avatar
Guolin Ke committed
872
        if (std::fabs(inner_data.second) > kZeroThreshold || std::isnan(inner_data.second)) {
Guolin Ke's avatar
Guolin Ke committed
873
874
          sample_values[inner_data.first].emplace_back(inner_data.second);
          sample_idx[inner_data.first].emplace_back(static_cast<int>(i));
875
876
877
        }
      }
    }
Guolin Ke's avatar
Guolin Ke committed
878
    DatasetLoader loader(config, nullptr, 1, nullptr);
879
880
    ret.reset(loader.CostructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                            Vector2Ptr<int>(&sample_idx).data(),
881
                                            static_cast<int>(num_col),
882
                                            VectorSize<double>(sample_values).data(),
883
                                            sample_cnt, nrow));
884
  } else {
885
    ret.reset(new Dataset(nrow));
Guolin Ke's avatar
Guolin Ke committed
886
    ret->CreateValid(
887
      reinterpret_cast<const Dataset*>(reference));
888
  }
889
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
890
  #pragma omp parallel for schedule(static)
891
  for (int i = 0; i < nindptr - 1; ++i) {
892
    OMP_LOOP_EX_BEGIN();
893
894
895
    const int tid = omp_get_thread_num();
    auto one_row = get_row_fun(i);
    ret->PushOneRow(tid, i, one_row);
896
    OMP_LOOP_EX_END();
897
  }
898
  OMP_THROW_EX();
899
  ret->FinishLoad();
Guolin Ke's avatar
Guolin Ke committed
900
  *out = ret.release();
901
  API_END();
902
903
}

904
int LGBM_DatasetCreateFromCSRFunc(void* get_row_funptr,
905
906
907
908
909
                                  int num_rows,
                                  int64_t num_col,
                                  const char* parameters,
                                  const DatasetHandle reference,
                                  DatasetHandle* out) {
910
  API_BEGIN();
911
912
913
914
915
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
  auto get_row_fun = *static_cast<std::function<void(int idx, std::vector<std::pair<int, double>>&)>*>(get_row_funptr);
  auto param = Config::Str2Map(parameters);
  Config config;
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
  std::unique_ptr<Dataset> ret;
  int32_t nrow = num_rows;
  if (reference == nullptr) {
    // sample data first
    Random rand(config.data_random_seed);
    int sample_cnt = static_cast<int>(nrow < config.bin_construct_sample_cnt ? nrow : config.bin_construct_sample_cnt);
    auto sample_indices = rand.Sample(nrow, sample_cnt);
    sample_cnt = static_cast<int>(sample_indices.size());
    std::vector<std::vector<double>> sample_values(num_col);
    std::vector<std::vector<int>> sample_idx(num_col);
    // local buffer to re-use memory
    std::vector<std::pair<int, double>> buffer;
    for (size_t i = 0; i < sample_indices.size(); ++i) {
      auto idx = sample_indices[i];
      get_row_fun(static_cast<int>(idx), buffer);
      for (std::pair<int, double>& inner_data : buffer) {
Nikita Titov's avatar
Nikita Titov committed
939
        CHECK_LT(inner_data.first, num_col);
940
941
942
943
944
945
946
        if (std::fabs(inner_data.second) > kZeroThreshold || std::isnan(inner_data.second)) {
          sample_values[inner_data.first].emplace_back(inner_data.second);
          sample_idx[inner_data.first].emplace_back(static_cast<int>(i));
        }
      }
    }
    DatasetLoader loader(config, nullptr, 1, nullptr);
947
948
    ret.reset(loader.CostructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                            Vector2Ptr<int>(&sample_idx).data(),
949
                                            static_cast<int>(num_col),
950
                                            VectorSize<double>(sample_values).data(),
951
952
953
954
955
956
                                            sample_cnt, nrow));
  } else {
    ret.reset(new Dataset(nrow));
    ret->CreateValid(
      reinterpret_cast<const Dataset*>(reference));
  }
957

958
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
959
960
  std::vector<std::pair<int, double>> thread_buffer;
  #pragma omp parallel for schedule(static) private(thread_buffer)
961
962
963
  for (int i = 0; i < num_rows; ++i) {
    OMP_LOOP_EX_BEGIN();
    {
964
      const int tid = omp_get_thread_num();
Guolin Ke's avatar
Guolin Ke committed
965
966
      get_row_fun(i, thread_buffer);
      ret->PushOneRow(tid, i, thread_buffer);
967
968
969
970
971
972
973
974
975
    }
    OMP_LOOP_EX_END();
  }
  OMP_THROW_EX();
  ret->FinishLoad();
  *out = ret.release();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
976
int LGBM_DatasetCreateFromCSC(const void* col_ptr,
977
978
979
980
981
982
983
984
985
986
                              int col_ptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t ncol_ptr,
                              int64_t nelem,
                              int64_t num_row,
                              const char* parameters,
                              const DatasetHandle reference,
                              DatasetHandle* out) {
987
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
988
989
  auto param = Config::Str2Map(parameters);
  Config config;
990
991
992
993
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
Guolin Ke's avatar
Guolin Ke committed
994
  std::unique_ptr<Dataset> ret;
Guolin Ke's avatar
Guolin Ke committed
995
996
997
  int32_t nrow = static_cast<int32_t>(num_row);
  if (reference == nullptr) {
    // sample data first
Guolin Ke's avatar
Guolin Ke committed
998
999
    Random rand(config.data_random_seed);
    int sample_cnt = static_cast<int>(nrow < config.bin_construct_sample_cnt ? nrow : config.bin_construct_sample_cnt);
Guolin Ke's avatar
Guolin Ke committed
1000
    auto sample_indices = rand.Sample(nrow, sample_cnt);
1001
    sample_cnt = static_cast<int>(sample_indices.size());
Guolin Ke's avatar
Guolin Ke committed
1002
    std::vector<std::vector<double>> sample_values(ncol_ptr - 1);
Guolin Ke's avatar
Guolin Ke committed
1003
    std::vector<std::vector<int>> sample_idx(ncol_ptr - 1);
1004
    OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1005
    #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
1006
    for (int i = 0; i < static_cast<int>(sample_values.size()); ++i) {
1007
      OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1008
1009
1010
      CSC_RowIterator col_it(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, i);
      for (int j = 0; j < sample_cnt; j++) {
        auto val = col_it.Get(sample_indices[j]);
Guolin Ke's avatar
Guolin Ke committed
1011
        if (std::fabs(val) > kZeroThreshold || std::isnan(val)) {
Guolin Ke's avatar
Guolin Ke committed
1012
1013
          sample_values[i].emplace_back(val);
          sample_idx[i].emplace_back(j);
Guolin Ke's avatar
Guolin Ke committed
1014
1015
        }
      }
1016
      OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
1017
    }
1018
    OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
1019
    DatasetLoader loader(config, nullptr, 1, nullptr);
1020
1021
    ret.reset(loader.CostructFromSampleData(Vector2Ptr<double>(&sample_values).data(),
                                            Vector2Ptr<int>(&sample_idx).data(),
1022
                                            static_cast<int>(sample_values.size()),
1023
                                            VectorSize<double>(sample_values).data(),
1024
                                            sample_cnt, nrow));
Guolin Ke's avatar
Guolin Ke committed
1025
  } else {
1026
    ret.reset(new Dataset(nrow));
Guolin Ke's avatar
Guolin Ke committed
1027
    ret->CreateValid(
1028
      reinterpret_cast<const Dataset*>(reference));
Guolin Ke's avatar
Guolin Ke committed
1029
  }
1030
  OMP_INIT_EX();
Guolin Ke's avatar
Guolin Ke committed
1031
  #pragma omp parallel for schedule(static)
Guolin Ke's avatar
Guolin Ke committed
1032
  for (int i = 0; i < ncol_ptr - 1; ++i) {
1033
    OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1034
    const int tid = omp_get_thread_num();
Guolin Ke's avatar
Guolin Ke committed
1035
    int feature_idx = ret->InnerFeatureIndex(i);
Guolin Ke's avatar
Guolin Ke committed
1036
    if (feature_idx < 0) { continue; }
Guolin Ke's avatar
Guolin Ke committed
1037
1038
    int group = ret->Feature2Group(feature_idx);
    int sub_feature = ret->Feture2SubFeature(feature_idx);
Guolin Ke's avatar
Guolin Ke committed
1039
    CSC_RowIterator col_it(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, i);
Guolin Ke's avatar
Guolin Ke committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
    auto bin_mapper = ret->FeatureBinMapper(feature_idx);
    if (bin_mapper->GetDefaultBin() == bin_mapper->GetMostFreqBin()) {
      int row_idx = 0;
      while (row_idx < nrow) {
        auto pair = col_it.NextNonZero();
        row_idx = pair.first;
        // no more data
        if (row_idx < 0) { break; }
        ret->PushOneData(tid, row_idx, group, sub_feature, pair.second);
      }
    } else {
      for (int row_idx = 0; row_idx < nrow; ++row_idx) {
        auto val = col_it.Get(row_idx);
        ret->PushOneData(tid, row_idx, group, sub_feature, val);
      }
Guolin Ke's avatar
Guolin Ke committed
1055
    }
1056
    OMP_LOOP_EX_END();
Guolin Ke's avatar
Guolin Ke committed
1057
  }
1058
  OMP_THROW_EX();
Guolin Ke's avatar
Guolin Ke committed
1059
  ret->FinishLoad();
Guolin Ke's avatar
Guolin Ke committed
1060
  *out = ret.release();
1061
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1062
1063
}

Guolin Ke's avatar
Guolin Ke committed
1064
int LGBM_DatasetGetSubset(
1065
  const DatasetHandle handle,
wxchan's avatar
wxchan committed
1066
1067
1068
  const int32_t* used_row_indices,
  int32_t num_used_row_indices,
  const char* parameters,
Guolin Ke's avatar
typo  
Guolin Ke committed
1069
  DatasetHandle* out) {
wxchan's avatar
wxchan committed
1070
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1071
1072
  auto param = Config::Str2Map(parameters);
  Config config;
1073
1074
1075
1076
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
1077
  auto full_dataset = reinterpret_cast<const Dataset*>(handle);
1078
  CHECK_GT(num_used_row_indices, 0);
1079
1080
  const int32_t lower = 0;
  const int32_t upper = full_dataset->num_data() - 1;
1081
  CheckElementsIntervalClosed(used_row_indices, lower, upper, num_used_row_indices, "Used indices of subset");
1082
1083
1084
  if (!std::is_sorted(used_row_indices, used_row_indices + num_used_row_indices)) {
    Log::Fatal("used_row_indices should be sorted in Subset");
  }
Guolin Ke's avatar
Guolin Ke committed
1085
  auto ret = std::unique_ptr<Dataset>(new Dataset(num_used_row_indices));
1086
  ret->CopyFeatureMapperFrom(full_dataset);
1087
  ret->CopySubrow(full_dataset, used_row_indices, num_used_row_indices, true);
wxchan's avatar
wxchan committed
1088
1089
1090
1091
  *out = ret.release();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1092
int LGBM_DatasetSetFeatureNames(
Guolin Ke's avatar
typo  
Guolin Ke committed
1093
  DatasetHandle handle,
Guolin Ke's avatar
Guolin Ke committed
1094
  const char** feature_names,
Guolin Ke's avatar
Guolin Ke committed
1095
  int num_feature_names) {
Guolin Ke's avatar
Guolin Ke committed
1096
1097
1098
  API_BEGIN();
  auto dataset = reinterpret_cast<Dataset*>(handle);
  std::vector<std::string> feature_names_str;
Guolin Ke's avatar
Guolin Ke committed
1099
  for (int i = 0; i < num_feature_names; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1100
1101
1102
1103
1104
1105
    feature_names_str.emplace_back(feature_names[i]);
  }
  dataset->set_feature_names(feature_names_str);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1106
int LGBM_DatasetGetFeatureNames(
1107
1108
  DatasetHandle handle,
  char** feature_names,
Guolin Ke's avatar
Guolin Ke committed
1109
  int* num_feature_names) {
1110
1111
1112
  API_BEGIN();
  auto dataset = reinterpret_cast<Dataset*>(handle);
  auto inside_feature_name = dataset->feature_names();
Guolin Ke's avatar
Guolin Ke committed
1113
1114
  *num_feature_names = static_cast<int>(inside_feature_name.size());
  for (int i = 0; i < *num_feature_names; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1115
    std::memcpy(feature_names[i], inside_feature_name[i].c_str(), inside_feature_name[i].size() + 1);
1116
1117
1118
1119
  }
  API_END();
}

1120
1121
1122
#ifdef _MSC_VER
  #pragma warning(disable : 4702)
#endif
Guolin Ke's avatar
Guolin Ke committed
1123
int LGBM_DatasetFree(DatasetHandle handle) {
1124
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1125
  delete reinterpret_cast<Dataset*>(handle);
1126
  API_END();
1127
1128
}

Guolin Ke's avatar
Guolin Ke committed
1129
int LGBM_DatasetSaveBinary(DatasetHandle handle,
1130
                           const char* filename) {
1131
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1132
1133
  auto dataset = reinterpret_cast<Dataset*>(handle);
  dataset->SaveBinaryFile(filename);
1134
  API_END();
1135
1136
}

1137
1138
1139
1140
1141
1142
1143
1144
int LGBM_DatasetDumpText(DatasetHandle handle,
                         const char* filename) {
  API_BEGIN();
  auto dataset = reinterpret_cast<Dataset*>(handle);
  dataset->DumpTextFile(filename);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1145
int LGBM_DatasetSetField(DatasetHandle handle,
1146
1147
1148
1149
                         const char* field_name,
                         const void* field_data,
                         int num_element,
                         int type) {
1150
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1151
  auto dataset = reinterpret_cast<Dataset*>(handle);
1152
  bool is_success = false;
Guolin Ke's avatar
Guolin Ke committed
1153
  if (type == C_API_DTYPE_FLOAT32) {
Guolin Ke's avatar
Guolin Ke committed
1154
    is_success = dataset->SetFloatField(field_name, reinterpret_cast<const float*>(field_data), static_cast<int32_t>(num_element));
Guolin Ke's avatar
Guolin Ke committed
1155
  } else if (type == C_API_DTYPE_INT32) {
Guolin Ke's avatar
Guolin Ke committed
1156
    is_success = dataset->SetIntField(field_name, reinterpret_cast<const int*>(field_data), static_cast<int32_t>(num_element));
Guolin Ke's avatar
Guolin Ke committed
1157
1158
  } else if (type == C_API_DTYPE_FLOAT64) {
    is_success = dataset->SetDoubleField(field_name, reinterpret_cast<const double*>(field_data), static_cast<int32_t>(num_element));
1159
  }
1160
  if (!is_success) { Log::Fatal("Input data type error or field not found"); }
1161
  API_END();
1162
1163
}

Guolin Ke's avatar
Guolin Ke committed
1164
int LGBM_DatasetGetField(DatasetHandle handle,
1165
1166
1167
1168
                         const char* field_name,
                         int* out_len,
                         const void** out_ptr,
                         int* out_type) {
1169
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1170
  auto dataset = reinterpret_cast<Dataset*>(handle);
1171
  bool is_success = false;
Guolin Ke's avatar
Guolin Ke committed
1172
  if (dataset->GetFloatField(field_name, out_len, reinterpret_cast<const float**>(out_ptr))) {
Guolin Ke's avatar
Guolin Ke committed
1173
    *out_type = C_API_DTYPE_FLOAT32;
1174
    is_success = true;
Guolin Ke's avatar
Guolin Ke committed
1175
  } else if (dataset->GetIntField(field_name, out_len, reinterpret_cast<const int**>(out_ptr))) {
Guolin Ke's avatar
Guolin Ke committed
1176
    *out_type = C_API_DTYPE_INT32;
1177
    is_success = true;
Guolin Ke's avatar
Guolin Ke committed
1178
1179
1180
  } else if (dataset->GetDoubleField(field_name, out_len, reinterpret_cast<const double**>(out_ptr))) {
    *out_type = C_API_DTYPE_FLOAT64;
    is_success = true;
Nikita Titov's avatar
Nikita Titov committed
1181
  }
1182
  if (!is_success) { Log::Fatal("Field not found"); }
wxchan's avatar
wxchan committed
1183
  if (*out_ptr == nullptr) { *out_len = 0; }
1184
  API_END();
1185
1186
}

1187
int LGBM_DatasetUpdateParamChecking(const char* old_parameters, const char* new_parameters) {
1188
  API_BEGIN();
1189
1190
1191
1192
1193
  auto old_param = Config::Str2Map(old_parameters);
  Config old_config;
  old_config.Set(old_param);
  auto new_param = Config::Str2Map(new_parameters);
  Booster::CheckDatasetResetConfig(old_config, new_param);
1194
1195
1196
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1197
int LGBM_DatasetGetNumData(DatasetHandle handle,
1198
                           int* out) {
1199
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1200
1201
  auto dataset = reinterpret_cast<Dataset*>(handle);
  *out = dataset->num_data();
1202
  API_END();
1203
1204
}

Guolin Ke's avatar
Guolin Ke committed
1205
int LGBM_DatasetGetNumFeature(DatasetHandle handle,
1206
                              int* out) {
1207
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1208
1209
  auto dataset = reinterpret_cast<Dataset*>(handle);
  *out = dataset->num_total_features();
1210
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1211
}
1212

1213
1214
1215
1216
1217
int LGBM_DatasetAddFeaturesFrom(DatasetHandle target,
                                DatasetHandle source) {
  API_BEGIN();
  auto target_d = reinterpret_cast<Dataset*>(target);
  auto source_d = reinterpret_cast<Dataset*>(source);
1218
  target_d->AddFeaturesFrom(source_d);
1219
1220
1221
  API_END();
}

1222
1223
// ---- start of booster

Guolin Ke's avatar
Guolin Ke committed
1224
int LGBM_BoosterCreate(const DatasetHandle train_data,
1225
1226
                       const char* parameters,
                       BoosterHandle* out) {
1227
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1228
  const Dataset* p_train_data = reinterpret_cast<const Dataset*>(train_data);
wxchan's avatar
wxchan committed
1229
1230
  auto ret = std::unique_ptr<Booster>(new Booster(p_train_data, parameters));
  *out = ret.release();
1231
  API_END();
1232
1233
}

Guolin Ke's avatar
Guolin Ke committed
1234
int LGBM_BoosterCreateFromModelfile(
1235
  const char* filename,
Guolin Ke's avatar
Guolin Ke committed
1236
  int* out_num_iterations,
1237
  BoosterHandle* out) {
1238
  API_BEGIN();
wxchan's avatar
wxchan committed
1239
  auto ret = std::unique_ptr<Booster>(new Booster(filename));
Guolin Ke's avatar
Guolin Ke committed
1240
  *out_num_iterations = ret->GetBoosting()->GetCurrentIteration();
wxchan's avatar
wxchan committed
1241
  *out = ret.release();
1242
  API_END();
1243
1244
}

Guolin Ke's avatar
Guolin Ke committed
1245
int LGBM_BoosterLoadModelFromString(
1246
1247
1248
1249
  const char* model_str,
  int* out_num_iterations,
  BoosterHandle* out) {
  API_BEGIN();
wxchan's avatar
wxchan committed
1250
  auto ret = std::unique_ptr<Booster>(new Booster(nullptr));
1251
1252
1253
1254
1255
1256
  ret->LoadModelFromString(model_str);
  *out_num_iterations = ret->GetBoosting()->GetCurrentIteration();
  *out = ret.release();
  API_END();
}

1257
1258
1259
#ifdef _MSC_VER
  #pragma warning(disable : 4702)
#endif
Guolin Ke's avatar
Guolin Ke committed
1260
int LGBM_BoosterFree(BoosterHandle handle) {
1261
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1262
  delete reinterpret_cast<Booster*>(handle);
1263
  API_END();
1264
1265
}

1266
int LGBM_BoosterShuffleModels(BoosterHandle handle, int start_iter, int end_iter) {
1267
1268
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1269
  ref_booster->ShuffleModels(start_iter, end_iter);
1270
1271
1272
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1273
int LGBM_BoosterMerge(BoosterHandle handle,
1274
                      BoosterHandle other_handle) {
wxchan's avatar
wxchan committed
1275
1276
1277
1278
1279
1280
1281
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  Booster* ref_other_booster = reinterpret_cast<Booster*>(other_handle);
  ref_booster->MergeFrom(ref_other_booster);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1282
int LGBM_BoosterAddValidData(BoosterHandle handle,
1283
                             const DatasetHandle valid_data) {
wxchan's avatar
wxchan committed
1284
1285
1286
1287
1288
1289
1290
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  const Dataset* p_dataset = reinterpret_cast<const Dataset*>(valid_data);
  ref_booster->AddValidData(p_dataset);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1291
int LGBM_BoosterResetTrainingData(BoosterHandle handle,
1292
                                  const DatasetHandle train_data) {
wxchan's avatar
wxchan committed
1293
1294
1295
1296
1297
1298
1299
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  const Dataset* p_dataset = reinterpret_cast<const Dataset*>(train_data);
  ref_booster->ResetTrainingData(p_dataset);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1300
int LGBM_BoosterResetParameter(BoosterHandle handle, const char* parameters) {
wxchan's avatar
wxchan committed
1301
1302
1303
1304
1305
1306
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  ref_booster->ResetConfig(parameters);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1307
int LGBM_BoosterGetNumClasses(BoosterHandle handle, int* out_len) {
wxchan's avatar
wxchan committed
1308
1309
1310
1311
1312
1313
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_len = ref_booster->GetBoosting()->NumberOfClasses();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1314
1315
1316
1317
1318
1319
1320
int LGBM_BoosterRefit(BoosterHandle handle, const int32_t* leaf_preds, int32_t nrow, int32_t ncol) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  ref_booster->Refit(leaf_preds, nrow, ncol);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1321
int LGBM_BoosterUpdateOneIter(BoosterHandle handle, int* is_finished) {
1322
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1323
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1324
1325
1326
1327
1328
  if (ref_booster->TrainOneIter()) {
    *is_finished = 1;
  } else {
    *is_finished = 0;
  }
1329
  API_END();
1330
1331
}

Guolin Ke's avatar
Guolin Ke committed
1332
int LGBM_BoosterUpdateOneIterCustom(BoosterHandle handle,
1333
1334
1335
                                    const float* grad,
                                    const float* hess,
                                    int* is_finished) {
1336
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1337
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1338
  #ifdef SCORE_T_USE_DOUBLE
1339
  Log::Fatal("Don't support custom loss function when SCORE_T_USE_DOUBLE is enabled");
1340
  #else
1341
1342
1343
1344
1345
  if (ref_booster->TrainOneIter(grad, hess)) {
    *is_finished = 1;
  } else {
    *is_finished = 0;
  }
1346
  #endif
1347
  API_END();
1348
1349
}

Guolin Ke's avatar
Guolin Ke committed
1350
int LGBM_BoosterRollbackOneIter(BoosterHandle handle) {
wxchan's avatar
wxchan committed
1351
1352
1353
1354
1355
1356
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  ref_booster->RollbackOneIter();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1357
int LGBM_BoosterGetCurrentIteration(BoosterHandle handle, int* out_iteration) {
wxchan's avatar
wxchan committed
1358
1359
1360
1361
1362
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_iteration = ref_booster->GetBoosting()->GetCurrentIteration();
  API_END();
}
Guolin Ke's avatar
Guolin Ke committed
1363

1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
int LGBM_BoosterNumModelPerIteration(BoosterHandle handle, int* out_tree_per_iteration) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_tree_per_iteration = ref_booster->GetBoosting()->NumModelPerIteration();
  API_END();
}

int LGBM_BoosterNumberOfTotalModel(BoosterHandle handle, int* out_models) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_models = ref_booster->GetBoosting()->NumberOfTotalModel();
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1378
int LGBM_BoosterGetEvalCounts(BoosterHandle handle, int* out_len) {
wxchan's avatar
wxchan committed
1379
1380
1381
1382
1383
1384
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_len = ref_booster->GetEvalCounts();
  API_END();
}

1385
1386
1387
1388
1389
1390
int LGBM_BoosterGetEvalNames(BoosterHandle handle,
                             const int len,
                             int* out_len,
                             const size_t buffer_len,
                             size_t* out_buffer_len,
                             char** out_strs) {
wxchan's avatar
wxchan committed
1391
1392
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1393
  *out_len = ref_booster->GetEvalNames(out_strs, len, buffer_len, out_buffer_len);
wxchan's avatar
wxchan committed
1394
1395
1396
  API_END();
}

1397
1398
1399
1400
1401
1402
int LGBM_BoosterGetFeatureNames(BoosterHandle handle,
                                const int len,
                                int* out_len,
                                const size_t buffer_len,
                                size_t* out_buffer_len,
                                char** out_strs) {
wxchan's avatar
wxchan committed
1403
1404
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1405
  *out_len = ref_booster->GetFeatureNames(out_strs, len, buffer_len, out_buffer_len);
wxchan's avatar
wxchan committed
1406
1407
1408
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1409
int LGBM_BoosterGetNumFeature(BoosterHandle handle, int* out_len) {
wxchan's avatar
wxchan committed
1410
1411
1412
1413
1414
1415
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  *out_len = ref_booster->GetBoosting()->MaxFeatureIdx() + 1;
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1416
int LGBM_BoosterGetEval(BoosterHandle handle,
1417
1418
1419
                        int data_idx,
                        int* out_len,
                        double* out_results) {
1420
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1421
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1422
  auto boosting = ref_booster->GetBoosting();
wxchan's avatar
wxchan committed
1423
  auto result_buf = boosting->GetEvalAt(data_idx);
Guolin Ke's avatar
Guolin Ke committed
1424
  *out_len = static_cast<int>(result_buf.size());
1425
  for (size_t i = 0; i < result_buf.size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
1426
    (out_results)[i] = static_cast<double>(result_buf[i]);
1427
  }
1428
  API_END();
1429
1430
}

Guolin Ke's avatar
Guolin Ke committed
1431
int LGBM_BoosterGetNumPredict(BoosterHandle handle,
1432
1433
                              int data_idx,
                              int64_t* out_len) {
Guolin Ke's avatar
Guolin Ke committed
1434
1435
1436
1437
1438
1439
  API_BEGIN();
  auto boosting = reinterpret_cast<Booster*>(handle)->GetBoosting();
  *out_len = boosting->GetNumPredictAt(data_idx);
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1440
int LGBM_BoosterGetPredict(BoosterHandle handle,
1441
1442
1443
                           int data_idx,
                           int64_t* out_len,
                           double* out_result) {
1444
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1445
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1446
  ref_booster->GetPredictAt(data_idx, out_result, out_len);
1447
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1448
1449
}

Guolin Ke's avatar
Guolin Ke committed
1450
int LGBM_BoosterPredictForFile(BoosterHandle handle,
1451
1452
1453
1454
                               const char* data_filename,
                               int data_has_header,
                               int predict_type,
                               int num_iteration,
1455
                               const char* parameter,
1456
                               const char* result_filename) {
1457
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1458
1459
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
1460
1461
1462
1463
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
Guolin Ke's avatar
Guolin Ke committed
1464
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
cbecker's avatar
cbecker committed
1465
  ref_booster->Predict(num_iteration, predict_type, data_filename, data_has_header,
Guolin Ke's avatar
Guolin Ke committed
1466
                       config, result_filename);
1467
  API_END();
1468
1469
}

Guolin Ke's avatar
Guolin Ke committed
1470
int LGBM_BoosterCalcNumPredict(BoosterHandle handle,
1471
1472
1473
1474
                               int num_row,
                               int predict_type,
                               int num_iteration,
                               int64_t* out_len) {
Guolin Ke's avatar
Guolin Ke committed
1475
1476
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1477
1478
  *out_len = static_cast<int64_t>(num_row) * ref_booster->GetBoosting()->NumPredictOneRow(
    num_iteration, predict_type == C_API_PREDICT_LEAF_INDEX, predict_type == C_API_PREDICT_CONTRIB);
Guolin Ke's avatar
Guolin Ke committed
1479
1480
1481
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1482
int LGBM_BoosterPredictForCSR(BoosterHandle handle,
1483
1484
1485
1486
1487
1488
1489
                              const void* indptr,
                              int indptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t nindptr,
                              int64_t nelem,
1490
                              int64_t num_col,
1491
1492
                              int predict_type,
                              int num_iteration,
1493
                              const char* parameter,
1494
1495
                              int64_t* out_len,
                              double* out_result) {
1496
  API_BEGIN();
1497
1498
1499
1500
1501
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
Guolin Ke's avatar
Guolin Ke committed
1502
1503
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
1504
1505
1506
1507
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
Guolin Ke's avatar
Guolin Ke committed
1508
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1509
  auto get_row_fun = RowFunctionFromCSR(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
Guolin Ke's avatar
Guolin Ke committed
1510
  int nrow = static_cast<int>(nindptr - 1);
1511
  ref_booster->Predict(num_iteration, predict_type, nrow, static_cast<int>(num_col), get_row_fun,
Guolin Ke's avatar
Guolin Ke committed
1512
                       config, out_result, out_len);
1513
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1514
}
1515

1516
int LGBM_BoosterPredictForCSRSingleRow(BoosterHandle handle,
1517
1518
1519
1520
1521
1522
1523
                                       const void* indptr,
                                       int indptr_type,
                                       const int32_t* indices,
                                       const void* data,
                                       int data_type,
                                       int64_t nindptr,
                                       int64_t nelem,
1524
                                       int64_t num_col,
1525
1526
1527
1528
1529
                                       int predict_type,
                                       int num_iteration,
                                       const char* parameter,
                                       int64_t* out_len,
                                       double* out_result) {
1530
  API_BEGIN();
1531
1532
1533
1534
1535
  if (num_col <= 0) {
    Log::Fatal("The number of columns should be greater than zero.");
  } else if (num_col >= INT32_MAX) {
    Log::Fatal("The number of columns should be smaller than INT32_MAX.");
  }
1536
1537
1538
1539
1540
1541
1542
1543
  auto param = Config::Str2Map(parameter);
  Config config;
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  auto get_row_fun = RowFunctionFromCSR(indptr, indptr_type, indices, data, data_type, nindptr, nelem);
1544
  ref_booster->PredictSingleRow(num_iteration, predict_type, static_cast<int32_t>(num_col), get_row_fun, config, out_result, out_len);
1545
1546
1547
1548
  API_END();
}


Guolin Ke's avatar
Guolin Ke committed
1549
int LGBM_BoosterPredictForCSC(BoosterHandle handle,
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
                              const void* col_ptr,
                              int col_ptr_type,
                              const int32_t* indices,
                              const void* data,
                              int data_type,
                              int64_t ncol_ptr,
                              int64_t nelem,
                              int64_t num_row,
                              int predict_type,
                              int num_iteration,
1560
                              const char* parameter,
1561
1562
                              int64_t* out_len,
                              double* out_result) {
Guolin Ke's avatar
Guolin Ke committed
1563
1564
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
Guolin Ke's avatar
Guolin Ke committed
1565
1566
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
1567
1568
1569
1570
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
1571
  int num_threads = OMP_NUM_THREADS();
Guolin Ke's avatar
Guolin Ke committed
1572
  int ncol = static_cast<int>(ncol_ptr - 1);
Guolin Ke's avatar
Guolin Ke committed
1573
1574
1575
1576
1577
  std::vector<std::vector<CSC_RowIterator>> iterators(num_threads, std::vector<CSC_RowIterator>());
  for (int i = 0; i < num_threads; ++i) {
    for (int j = 0; j < ncol; ++j) {
      iterators[i].emplace_back(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, j);
    }
Guolin Ke's avatar
Guolin Ke committed
1578
1579
  }
  std::function<std::vector<std::pair<int, double>>(int row_idx)> get_row_fun =
Guolin Ke's avatar
Guolin Ke committed
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
      [&iterators, ncol](int i) {
        std::vector<std::pair<int, double>> one_row;
        one_row.reserve(ncol);
        const int tid = omp_get_thread_num();
        for (int j = 0; j < ncol; ++j) {
          auto val = iterators[tid][j].Get(i);
          if (std::fabs(val) > kZeroThreshold || std::isnan(val)) {
            one_row.emplace_back(j, val);
          }
        }
        return one_row;
      };
1592
  ref_booster->Predict(num_iteration, predict_type, static_cast<int>(num_row), ncol, get_row_fun, config,
cbecker's avatar
cbecker committed
1593
                       out_result, out_len);
Guolin Ke's avatar
Guolin Ke committed
1594
1595
1596
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1597
int LGBM_BoosterPredictForMat(BoosterHandle handle,
1598
1599
1600
1601
1602
1603
1604
                              const void* data,
                              int data_type,
                              int32_t nrow,
                              int32_t ncol,
                              int is_row_major,
                              int predict_type,
                              int num_iteration,
1605
                              const char* parameter,
1606
1607
                              int64_t* out_len,
                              double* out_result) {
1608
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1609
1610
  auto param = Config::Str2Map(parameter);
  Config config;
Guolin Ke's avatar
Guolin Ke committed
1611
1612
1613
1614
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
Guolin Ke's avatar
Guolin Ke committed
1615
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1616
  auto get_row_fun = RowPairFunctionFromDenseMatric(data, nrow, ncol, data_type, is_row_major);
1617
  ref_booster->Predict(num_iteration, predict_type, nrow, ncol, get_row_fun,
Guolin Ke's avatar
Guolin Ke committed
1618
                       config, out_result, out_len);
1619
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1620
}
1621

1622
int LGBM_BoosterPredictForMatSingleRow(BoosterHandle handle,
1623
1624
1625
1626
1627
1628
1629
1630
1631
                                       const void* data,
                                       int data_type,
                                       int32_t ncol,
                                       int is_row_major,
                                       int predict_type,
                                       int num_iteration,
                                       const char* parameter,
                                       int64_t* out_len,
                                       double* out_result) {
1632
1633
1634
1635
1636
1637
1638
1639
1640
  API_BEGIN();
  auto param = Config::Str2Map(parameter);
  Config config;
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  auto get_row_fun = RowPairFunctionFromDenseMatric(data, 1, ncol, data_type, is_row_major);
1641
  ref_booster->PredictSingleRow(num_iteration, predict_type, ncol, get_row_fun, config, out_result, out_len);
1642
1643
1644
1645
  API_END();
}


1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
int LGBM_BoosterPredictForMats(BoosterHandle handle,
                               const void** data,
                               int data_type,
                               int32_t nrow,
                               int32_t ncol,
                               int predict_type,
                               int num_iteration,
                               const char* parameter,
                               int64_t* out_len,
                               double* out_result) {
  API_BEGIN();
  auto param = Config::Str2Map(parameter);
  Config config;
  config.Set(param);
  if (config.num_threads > 0) {
    omp_set_num_threads(config.num_threads);
  }
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  auto get_row_fun = RowPairFunctionFromDenseRows(data, ncol, data_type);
1665
  ref_booster->Predict(num_iteration, predict_type, nrow, ncol, get_row_fun, config, out_result, out_len);
1666
1667
1668
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1669
int LGBM_BoosterSaveModel(BoosterHandle handle,
1670
                          int start_iteration,
1671
1672
                          int num_iteration,
                          const char* filename) {
1673
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1674
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1675
  ref_booster->SaveModelToFile(start_iteration, num_iteration, filename);
wxchan's avatar
wxchan committed
1676
1677
1678
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1679
int LGBM_BoosterSaveModelToString(BoosterHandle handle,
1680
                                  int start_iteration,
1681
                                  int num_iteration,
1682
                                  int64_t buffer_len,
1683
                                  int64_t* out_len,
1684
                                  char* out_str) {
1685
1686
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1687
  std::string model = ref_booster->SaveModelToString(start_iteration, num_iteration);
1688
  *out_len = static_cast<int64_t>(model.size()) + 1;
1689
  if (*out_len <= buffer_len) {
Guolin Ke's avatar
Guolin Ke committed
1690
    std::memcpy(out_str, model.c_str(), *out_len);
1691
1692
1693
1694
  }
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1695
int LGBM_BoosterDumpModel(BoosterHandle handle,
1696
                          int start_iteration,
1697
                          int num_iteration,
1698
1699
                          int64_t buffer_len,
                          int64_t* out_len,
1700
                          char* out_str) {
wxchan's avatar
wxchan committed
1701
1702
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
1703
  std::string model = ref_booster->DumpModel(start_iteration, num_iteration);
1704
  *out_len = static_cast<int64_t>(model.size()) + 1;
wxchan's avatar
wxchan committed
1705
  if (*out_len <= buffer_len) {
Guolin Ke's avatar
Guolin Ke committed
1706
    std::memcpy(out_str, model.c_str(), *out_len);
wxchan's avatar
wxchan committed
1707
  }
1708
  API_END();
Guolin Ke's avatar
Guolin Ke committed
1709
}
1710

Guolin Ke's avatar
Guolin Ke committed
1711
int LGBM_BoosterGetLeafValue(BoosterHandle handle,
1712
1713
1714
                             int tree_idx,
                             int leaf_idx,
                             double* out_val) {
Guolin Ke's avatar
Guolin Ke committed
1715
1716
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
Guolin Ke's avatar
Guolin Ke committed
1717
  *out_val = static_cast<double>(ref_booster->GetLeafValue(tree_idx, leaf_idx));
Guolin Ke's avatar
Guolin Ke committed
1718
1719
1720
  API_END();
}

Guolin Ke's avatar
Guolin Ke committed
1721
int LGBM_BoosterSetLeafValue(BoosterHandle handle,
1722
1723
1724
                             int tree_idx,
                             int leaf_idx,
                             double val) {
Guolin Ke's avatar
Guolin Ke committed
1725
1726
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
Guolin Ke's avatar
Guolin Ke committed
1727
  ref_booster->SetLeafValue(tree_idx, leaf_idx, val);
Guolin Ke's avatar
Guolin Ke committed
1728
1729
1730
  API_END();
}

1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
int LGBM_BoosterFeatureImportance(BoosterHandle handle,
                                  int num_iteration,
                                  int importance_type,
                                  double* out_results) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  std::vector<double> feature_importances = ref_booster->FeatureImportance(num_iteration, importance_type);
  for (size_t i = 0; i < feature_importances.size(); ++i) {
    (out_results)[i] = feature_importances[i];
  }
  API_END();
}

1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
int LGBM_BoosterGetUpperBoundValue(BoosterHandle handle,
                                   double* out_results) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  double max_value = ref_booster->UpperBoundValue();
  *out_results = max_value;
  API_END();
}

int LGBM_BoosterGetLowerBoundValue(BoosterHandle handle,
                                   double* out_results) {
  API_BEGIN();
  Booster* ref_booster = reinterpret_cast<Booster*>(handle);
  double min_value = ref_booster->LowerBoundValue();
  *out_results = min_value;
  API_END();
}

1762
1763
1764
1765
1766
int LGBM_NetworkInit(const char* machines,
                     int local_listen_port,
                     int listen_time_out,
                     int num_machines) {
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1767
  Config config;
1768
  config.machines = RemoveQuotationSymbol(std::string(machines));
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
  config.local_listen_port = local_listen_port;
  config.num_machines = num_machines;
  config.time_out = listen_time_out;
  if (num_machines > 1) {
    Network::Init(config);
  }
  API_END();
}

int LGBM_NetworkFree() {
  API_BEGIN();
  Network::Dispose();
  API_END();
}

1784
1785
1786
int LGBM_NetworkInitWithFunctions(int num_machines, int rank,
                                  void* reduce_scatter_ext_fun,
                                  void* allgather_ext_fun) {
ww's avatar
ww committed
1787
  API_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
1788
  if (num_machines > 1) {
1789
    Network::Init(num_machines, rank, (ReduceScatterFunction)reduce_scatter_ext_fun, (AllgatherFunction)allgather_ext_fun);
ww's avatar
ww committed
1790
1791
1792
  }
  API_END();
}
Guolin Ke's avatar
Guolin Ke committed
1793

Guolin Ke's avatar
Guolin Ke committed
1794
// ---- start of some help functions
1795
1796
1797

std::function<std::vector<double>(int row_idx)>
RowFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major) {
Guolin Ke's avatar
Guolin Ke committed
1798
  if (data_type == C_API_DTYPE_FLOAT32) {
1799
1800
    const float* data_ptr = reinterpret_cast<const float*>(data);
    if (is_row_major) {
1801
      return [=] (int row_idx) {
Guolin Ke's avatar
Guolin Ke committed
1802
        std::vector<double> ret(num_col);
1803
        auto tmp_ptr = data_ptr + static_cast<size_t>(num_col) * row_idx;
1804
        for (int i = 0; i < num_col; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1805
          ret[i] = static_cast<double>(*(tmp_ptr + i));
1806
1807
1808
1809
        }
        return ret;
      };
    } else {
1810
      return [=] (int row_idx) {
Guolin Ke's avatar
Guolin Ke committed
1811
        std::vector<double> ret(num_col);
1812
        for (int i = 0; i < num_col; ++i) {
1813
          ret[i] = static_cast<double>(*(data_ptr + static_cast<size_t>(num_row) * i + row_idx));
1814
1815
1816
1817
        }
        return ret;
      };
    }
Guolin Ke's avatar
Guolin Ke committed
1818
  } else if (data_type == C_API_DTYPE_FLOAT64) {
1819
1820
    const double* data_ptr = reinterpret_cast<const double*>(data);
    if (is_row_major) {
1821
      return [=] (int row_idx) {
Guolin Ke's avatar
Guolin Ke committed
1822
        std::vector<double> ret(num_col);
1823
        auto tmp_ptr = data_ptr + static_cast<size_t>(num_col) * row_idx;
1824
        for (int i = 0; i < num_col; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1825
          ret[i] = static_cast<double>(*(tmp_ptr + i));
1826
1827
1828
1829
        }
        return ret;
      };
    } else {
1830
      return [=] (int row_idx) {
Guolin Ke's avatar
Guolin Ke committed
1831
        std::vector<double> ret(num_col);
1832
        for (int i = 0; i < num_col; ++i) {
1833
          ret[i] = static_cast<double>(*(data_ptr + static_cast<size_t>(num_row) * i + row_idx));
1834
1835
1836
1837
1838
        }
        return ret;
      };
    }
  }
1839
  Log::Fatal("Unknown data type in RowFunctionFromDenseMatric");
1840
  return nullptr;
1841
1842
1843
1844
}

std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseMatric(const void* data, int num_row, int num_col, int data_type, int is_row_major) {
Guolin Ke's avatar
Guolin Ke committed
1845
1846
  auto inner_function = RowFunctionFromDenseMatric(data, num_row, num_col, data_type, is_row_major);
  if (inner_function != nullptr) {
1847
    return [inner_function] (int row_idx) {
Guolin Ke's avatar
Guolin Ke committed
1848
1849
      auto raw_values = inner_function(row_idx);
      std::vector<std::pair<int, double>> ret;
Guolin Ke's avatar
Guolin Ke committed
1850
      ret.reserve(raw_values.size());
Guolin Ke's avatar
Guolin Ke committed
1851
      for (int i = 0; i < static_cast<int>(raw_values.size()); ++i) {
Guolin Ke's avatar
Guolin Ke committed
1852
        if (std::fabs(raw_values[i]) > kZeroThreshold || std::isnan(raw_values[i])) {
Guolin Ke's avatar
Guolin Ke committed
1853
          ret.emplace_back(i, raw_values[i]);
1854
        }
Guolin Ke's avatar
Guolin Ke committed
1855
1856
1857
      }
      return ret;
    };
1858
  }
Guolin Ke's avatar
Guolin Ke committed
1859
  return nullptr;
1860
1861
}

1862
1863
1864
1865
1866
1867
1868
// data is array of pointers to individual rows
std::function<std::vector<std::pair<int, double>>(int row_idx)>
RowPairFunctionFromDenseRows(const void** data, int num_col, int data_type) {
  return [=](int row_idx) {
    auto inner_function = RowFunctionFromDenseMatric(data[row_idx], 1, num_col, data_type, /* is_row_major */ true);
    auto raw_values = inner_function(0);
    std::vector<std::pair<int, double>> ret;
Guolin Ke's avatar
Guolin Ke committed
1869
    ret.reserve(raw_values.size());
1870
1871
1872
1873
1874
1875
1876
1877
1878
    for (int i = 0; i < static_cast<int>(raw_values.size()); ++i) {
      if (std::fabs(raw_values[i]) > kZeroThreshold || std::isnan(raw_values[i])) {
        ret.emplace_back(i, raw_values[i]);
      }
    }
    return ret;
  };
}

1879
std::function<std::vector<std::pair<int, double>>(int idx)>
1880
RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, const void* data, int data_type, int64_t , int64_t ) {
Guolin Ke's avatar
Guolin Ke committed
1881
  if (data_type == C_API_DTYPE_FLOAT32) {
1882
    const float* data_ptr = reinterpret_cast<const float*>(data);
Guolin Ke's avatar
Guolin Ke committed
1883
    if (indptr_type == C_API_DTYPE_INT32) {
1884
      const int32_t* ptr_indptr = reinterpret_cast<const int32_t*>(indptr);
1885
      return [=] (int idx) {
1886
1887
1888
        std::vector<std::pair<int, double>> ret;
        int64_t start = ptr_indptr[idx];
        int64_t end = ptr_indptr[idx + 1];
1889
1890
1891
        if (end - start > 0)  {
          ret.reserve(end - start);
        }
Guolin Ke's avatar
Guolin Ke committed
1892
        for (int64_t i = start; i < end; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1893
          ret.emplace_back(indices[i], data_ptr[i]);
1894
1895
1896
        }
        return ret;
      };
Guolin Ke's avatar
Guolin Ke committed
1897
    } else if (indptr_type == C_API_DTYPE_INT64) {
1898
      const int64_t* ptr_indptr = reinterpret_cast<const int64_t*>(indptr);
1899
      return [=] (int idx) {
1900
1901
1902
        std::vector<std::pair<int, double>> ret;
        int64_t start = ptr_indptr[idx];
        int64_t end = ptr_indptr[idx + 1];
1903
1904
1905
        if (end - start > 0)  {
          ret.reserve(end - start);
        }
Guolin Ke's avatar
Guolin Ke committed
1906
        for (int64_t i = start; i < end; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1907
          ret.emplace_back(indices[i], data_ptr[i]);
1908
1909
1910
1911
        }
        return ret;
      };
    }
Guolin Ke's avatar
Guolin Ke committed
1912
  } else if (data_type == C_API_DTYPE_FLOAT64) {
1913
    const double* data_ptr = reinterpret_cast<const double*>(data);
Guolin Ke's avatar
Guolin Ke committed
1914
    if (indptr_type == C_API_DTYPE_INT32) {
1915
      const int32_t* ptr_indptr = reinterpret_cast<const int32_t*>(indptr);
1916
      return [=] (int idx) {
1917
1918
1919
        std::vector<std::pair<int, double>> ret;
        int64_t start = ptr_indptr[idx];
        int64_t end = ptr_indptr[idx + 1];
1920
1921
1922
        if (end - start > 0)  {
          ret.reserve(end - start);
        }
Guolin Ke's avatar
Guolin Ke committed
1923
        for (int64_t i = start; i < end; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1924
          ret.emplace_back(indices[i], data_ptr[i]);
1925
1926
1927
        }
        return ret;
      };
Guolin Ke's avatar
Guolin Ke committed
1928
    } else if (indptr_type == C_API_DTYPE_INT64) {
1929
      const int64_t* ptr_indptr = reinterpret_cast<const int64_t*>(indptr);
1930
      return [=] (int idx) {
1931
1932
1933
        std::vector<std::pair<int, double>> ret;
        int64_t start = ptr_indptr[idx];
        int64_t end = ptr_indptr[idx + 1];
1934
1935
1936
        if (end - start > 0)  {
          ret.reserve(end - start);
        }
Guolin Ke's avatar
Guolin Ke committed
1937
        for (int64_t i = start; i < end; ++i) {
Guolin Ke's avatar
Guolin Ke committed
1938
          ret.emplace_back(indices[i], data_ptr[i]);
1939
1940
1941
        }
        return ret;
      };
Guolin Ke's avatar
Guolin Ke committed
1942
1943
    }
  }
1944
  Log::Fatal("Unknown data type in RowFunctionFromCSR");
1945
  return nullptr;
1946
1947
}

Guolin Ke's avatar
Guolin Ke committed
1948
std::function<std::pair<int, double>(int idx)>
1949
IterateFunctionFromCSC(const void* col_ptr, int col_ptr_type, const int32_t* indices, const void* data, int data_type, int64_t ncol_ptr, int64_t , int col_idx) {
Guolin Ke's avatar
Guolin Ke committed
1950
  CHECK(col_idx < ncol_ptr && col_idx >= 0);
Guolin Ke's avatar
Guolin Ke committed
1951
  if (data_type == C_API_DTYPE_FLOAT32) {
1952
    const float* data_ptr = reinterpret_cast<const float*>(data);
Guolin Ke's avatar
Guolin Ke committed
1953
    if (col_ptr_type == C_API_DTYPE_INT32) {
1954
      const int32_t* ptr_col_ptr = reinterpret_cast<const int32_t*>(col_ptr);
Guolin Ke's avatar
Guolin Ke committed
1955
1956
      int64_t start = ptr_col_ptr[col_idx];
      int64_t end = ptr_col_ptr[col_idx + 1];
1957
1958
      return [=] (int offset) {
        int64_t i = static_cast<int64_t>(start + offset);
Guolin Ke's avatar
Guolin Ke committed
1959
1960
        if (i >= end) {
          return std::make_pair(-1, 0.0);
1961
        }
Guolin Ke's avatar
Guolin Ke committed
1962
1963
1964
        int idx = static_cast<int>(indices[i]);
        double val = static_cast<double>(data_ptr[i]);
        return std::make_pair(idx, val);
1965
      };
Guolin Ke's avatar
Guolin Ke committed
1966
    } else if (col_ptr_type == C_API_DTYPE_INT64) {
1967
      const int64_t* ptr_col_ptr = reinterpret_cast<const int64_t*>(col_ptr);
Guolin Ke's avatar
Guolin Ke committed
1968
1969
      int64_t start = ptr_col_ptr[col_idx];
      int64_t end = ptr_col_ptr[col_idx + 1];
1970
1971
      return [=] (int offset) {
        int64_t i = static_cast<int64_t>(start + offset);
Guolin Ke's avatar
Guolin Ke committed
1972
1973
        if (i >= end) {
          return std::make_pair(-1, 0.0);
1974
        }
Guolin Ke's avatar
Guolin Ke committed
1975
1976
1977
        int idx = static_cast<int>(indices[i]);
        double val = static_cast<double>(data_ptr[i]);
        return std::make_pair(idx, val);
1978
      };
Guolin Ke's avatar
Guolin Ke committed
1979
    }
Guolin Ke's avatar
Guolin Ke committed
1980
  } else if (data_type == C_API_DTYPE_FLOAT64) {
1981
    const double* data_ptr = reinterpret_cast<const double*>(data);
Guolin Ke's avatar
Guolin Ke committed
1982
    if (col_ptr_type == C_API_DTYPE_INT32) {
1983
      const int32_t* ptr_col_ptr = reinterpret_cast<const int32_t*>(col_ptr);
Guolin Ke's avatar
Guolin Ke committed
1984
1985
      int64_t start = ptr_col_ptr[col_idx];
      int64_t end = ptr_col_ptr[col_idx + 1];
1986
1987
      return [=] (int offset) {
        int64_t i = static_cast<int64_t>(start + offset);
Guolin Ke's avatar
Guolin Ke committed
1988
1989
        if (i >= end) {
          return std::make_pair(-1, 0.0);
1990
        }
Guolin Ke's avatar
Guolin Ke committed
1991
1992
1993
        int idx = static_cast<int>(indices[i]);
        double val = static_cast<double>(data_ptr[i]);
        return std::make_pair(idx, val);
1994
      };
Guolin Ke's avatar
Guolin Ke committed
1995
    } else if (col_ptr_type == C_API_DTYPE_INT64) {
1996
      const int64_t* ptr_col_ptr = reinterpret_cast<const int64_t*>(col_ptr);
Guolin Ke's avatar
Guolin Ke committed
1997
1998
      int64_t start = ptr_col_ptr[col_idx];
      int64_t end = ptr_col_ptr[col_idx + 1];
1999
2000
      return [=] (int offset) {
        int64_t i = static_cast<int64_t>(start + offset);
Guolin Ke's avatar
Guolin Ke committed
2001
2002
        if (i >= end) {
          return std::make_pair(-1, 0.0);
2003
        }
Guolin Ke's avatar
Guolin Ke committed
2004
2005
2006
        int idx = static_cast<int>(indices[i]);
        double val = static_cast<double>(data_ptr[i]);
        return std::make_pair(idx, val);
2007
      };
Guolin Ke's avatar
Guolin Ke committed
2008
2009
    }
  }
2010
  Log::Fatal("Unknown data type in CSC matrix");
2011
  return nullptr;
2012
2013
}

Guolin Ke's avatar
Guolin Ke committed
2014
CSC_RowIterator::CSC_RowIterator(const void* col_ptr, int col_ptr_type, const int32_t* indices,
2015
                                 const void* data, int data_type, int64_t ncol_ptr, int64_t nelem, int col_idx) {
Guolin Ke's avatar
Guolin Ke committed
2016
2017
2018
2019
2020
2021
2022
2023
2024
  iter_fun_ = IterateFunctionFromCSC(col_ptr, col_ptr_type, indices, data, data_type, ncol_ptr, nelem, col_idx);
}

double CSC_RowIterator::Get(int idx) {
  while (idx > cur_idx_ && !is_end_) {
    auto ret = iter_fun_(nonzero_idx_);
    if (ret.first < 0) {
      is_end_ = true;
      break;
2025
    }
Guolin Ke's avatar
Guolin Ke committed
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
    cur_idx_ = ret.first;
    cur_val_ = ret.second;
    ++nonzero_idx_;
  }
  if (idx == cur_idx_) {
    return cur_val_;
  } else {
    return 0.0f;
  }
}

std::pair<int, double> CSC_RowIterator::NextNonZero() {
  if (!is_end_) {
    auto ret = iter_fun_(nonzero_idx_);
    ++nonzero_idx_;
    if (ret.first < 0) {
      is_end_ = true;
2043
    }
Guolin Ke's avatar
Guolin Ke committed
2044
2045
2046
    return ret;
  } else {
    return std::make_pair(-1, 0.0);
2047
  }
Guolin Ke's avatar
Guolin Ke committed
2048
}