array_args.h 4.02 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
#ifndef LIGHTGBM_UTILS_ARRAY_AGRS_H_
#define LIGHTGBM_UTILS_ARRAY_AGRS_H_

#include <vector>
#include <algorithm>
Guolin Ke's avatar
Guolin Ke committed
6
#include <LightGBM/utils/openmp_wrapper.h>
Guolin Ke's avatar
Guolin Ke committed
7
8
9
10
11
12
13
14
15

namespace LightGBM {

/*!
* \brief Contains some operation for a array, e.g. ArgMax, TopK.
*/
template<typename VAL_T>
class ArrayArgs {
public:
Guolin Ke's avatar
Guolin Ke committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  inline static size_t ArgMaxMT(const std::vector<VAL_T>& array) {
    int num_threads = 1;
#pragma omp parallel
#pragma omp master
    {
      num_threads = omp_get_num_threads();
    }
    int step = std::max(1, (static_cast<int>(array.size()) + num_threads - 1) / num_threads);
    std::vector<size_t> arg_maxs(num_threads, 0);
    #pragma omp parallel for schedule(static,1)
    for (int i = 0; i < num_threads; ++i) {
      size_t start = step * i;
      if (start >= array.size()) { continue; }
      size_t end = std::min(array.size(), start + step);
      size_t arg_max = start;
      for (size_t j = start + 1; j < end; ++j) {
        if (array[j] > array[arg_max]) {
          arg_max = j;
        }
      }
      arg_maxs[i] = arg_max;
    }
    size_t ret = arg_maxs[0];
    for (int i = 1; i < num_threads; ++i) {
      if (array[arg_maxs[i]] > array[ret]) {
        ret = arg_maxs[i];
      }
    }
    return ret;
  }
Guolin Ke's avatar
Guolin Ke committed
46
  inline static size_t ArgMax(const std::vector<VAL_T>& array) {
Guolin Ke's avatar
Guolin Ke committed
47
    if (array.empty()) {
Guolin Ke's avatar
Guolin Ke committed
48
49
      return 0;
    }
Guolin Ke's avatar
Guolin Ke committed
50
    if (array.size() > 1024) {
Guolin Ke's avatar
Guolin Ke committed
51
52
53
54
55
56
57
      return ArgMaxMT(array);
    } else {
      size_t arg_max = 0;
      for (size_t i = 1; i < array.size(); ++i) {
        if (array[i] > array[arg_max]) {
          arg_max = i;
        }
Guolin Ke's avatar
Guolin Ke committed
58
      }
Guolin Ke's avatar
Guolin Ke committed
59
      return arg_max;
Guolin Ke's avatar
Guolin Ke committed
60
61
62
63
    }
  }

  inline static size_t ArgMin(const std::vector<VAL_T>& array) {
Guolin Ke's avatar
Guolin Ke committed
64
    if (array.empty()) {
Guolin Ke's avatar
Guolin Ke committed
65
66
      return 0;
    }
Guolin Ke's avatar
Guolin Ke committed
67
    size_t arg_min = 0;
Guolin Ke's avatar
Guolin Ke committed
68
    for (size_t i = 1; i < array.size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
69
70
      if (array[i] < array[arg_min]) {
        arg_min = i;
Guolin Ke's avatar
Guolin Ke committed
71
72
      }
    }
Guolin Ke's avatar
Guolin Ke committed
73
    return arg_min;
Guolin Ke's avatar
Guolin Ke committed
74
75
76
77
78
79
  }

  inline static size_t ArgMax(const VAL_T* array, size_t n) {
    if (n <= 0) {
      return 0;
    }
Guolin Ke's avatar
Guolin Ke committed
80
    size_t arg_max = 0;
Guolin Ke's avatar
Guolin Ke committed
81
    for (size_t i = 1; i < n; ++i) {
Guolin Ke's avatar
Guolin Ke committed
82
83
      if (array[i] > array[arg_max]) {
        arg_max = i;
Guolin Ke's avatar
Guolin Ke committed
84
85
      }
    }
Guolin Ke's avatar
Guolin Ke committed
86
    return arg_max;
Guolin Ke's avatar
Guolin Ke committed
87
88
89
90
91
92
  }

  inline static size_t ArgMin(const VAL_T* array, size_t n) {
    if (n <= 0) {
      return 0;
    }
Guolin Ke's avatar
Guolin Ke committed
93
    size_t arg_min = 0;
Guolin Ke's avatar
Guolin Ke committed
94
    for (size_t i = 1; i < n; ++i) {
Guolin Ke's avatar
Guolin Ke committed
95
96
      if (array[i] < array[arg_min]) {
        arg_min = i;
Guolin Ke's avatar
Guolin Ke committed
97
98
      }
    }
Guolin Ke's avatar
Guolin Ke committed
99
    return arg_min;
Guolin Ke's avatar
Guolin Ke committed
100
101
  }

Guolin Ke's avatar
Guolin Ke committed
102
103
104
105
106
107
108
  inline static void Partition(std::vector<VAL_T>* arr, int start, int end, int* l, int* r) {
    int i = start - 1;
    int j = end - 1;
    int p = i;
    int q = j;
    if (start >= end) {
      return;
Guolin Ke's avatar
Guolin Ke committed
109
    }
Guolin Ke's avatar
Guolin Ke committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    std::vector<VAL_T>& ref = *arr;
    VAL_T v = ref[end - 1];
    for (;;) {
      while (ref[++i] > v);
      while (v > ref[--j]) { if (j == start) { break; } }
      if (i >= j) { break; }
      std::swap(ref[i], ref[j]);
      if (ref[i] == v) { p++; std::swap(ref[p], ref[i]); }
      if (v == ref[j]) { q--; std::swap(ref[j], ref[q]); }
    }
    std::swap(ref[i], ref[end - 1]);
    j = i - 1;
    i = i + 1;
    for (int k = start; k <= p; k++, j--) { std::swap(ref[k], ref[j]); }
    for (int k = end - 2; k >= q; k--, i++) { std::swap(ref[i], ref[k]); }
    *l = j;
    *r = i;
Guolin Ke's avatar
Guolin Ke committed
127
128
  };

Guolin Ke's avatar
Guolin Ke committed
129
130
  inline static int ArgMaxAtK(std::vector<VAL_T>* arr, int start, int end, int k) {
    if (start >= end - 1) {
Guolin Ke's avatar
Guolin Ke committed
131
132
      return start;
    }
Guolin Ke's avatar
Guolin Ke committed
133
134
135
136
137
138
139
140
141
    int l = start;
    int r = end - 1;
    Partition(arr, start, end, &l, &r);
    if ((k > l && k < r) || l == 0 || r == end - 1) {
      return k;
    } else if (k <= l) {
      return ArgMaxAtK(arr, start, l, k);
    } else {
      return ArgMaxAtK(arr, r, end, k);
Guolin Ke's avatar
Guolin Ke committed
142
143
144
    }
  }

Guolin Ke's avatar
Guolin Ke committed
145
  inline static void MaxK(const std::vector<VAL_T>& array, int k, std::vector<VAL_T>* out) {
Guolin Ke's avatar
Guolin Ke committed
146
147
148
149
150
151
152
    out->clear();
    if (k <= 0) {
      return;
    }
    for (auto val : array) {
      out->push_back(val);
    }
Guolin Ke's avatar
Guolin Ke committed
153
    if (static_cast<size_t>(k) >= array.size()) {
Guolin Ke's avatar
Guolin Ke committed
154
155
      return;
    }
Guolin Ke's avatar
Guolin Ke committed
156
    ArgMaxAtK(out, 0, static_cast<int>(out->size()), k - 1);
Guolin Ke's avatar
Guolin Ke committed
157
158
159
160
161
162
163
    out->erase(out->begin() + k, out->end());
  }

};

}  // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
164
#endif   // LightGBM_UTILS_ARRAY_AGRS_H_
Guolin Ke's avatar
Guolin Ke committed
165