test_partition.cc 6.59 KB
Newer Older
1
2
#include <gtest/gtest.h>
#include "../../src/partition/ndarray_partition.h"
3
#include "./common.h"
4
5
6
7
8


using namespace dgl;
using namespace dgl::partition;

9
template<DGLDeviceType XPU, typename IdType>
10
void _TestRemainder_GeneratePermutation() {
11
12
13
14
15
16
  const int64_t size = 160000;
  const int num_parts = 7;
  NDArrayPartitionRef part = CreatePartitionRemainderBased(
      size,  num_parts);

  IdArray idxs = aten::Range(0, size/10, sizeof(IdType)*8,
17
      DGLContext{XPU, 0});
18

19
  std::pair<IdArray, IdArray> result = part->GeneratePermutation(idxs);
20
21

  // first part of result should be the permutation
22
  IdArray perm = result.first.CopyTo(DGLContext{kDGLCPU, 0});
23
24
25
26
27
  ASSERT_TRUE(perm.Ptr<IdType>() != nullptr);
  ASSERT_EQ(perm->shape[0], idxs->shape[0]);
  const IdType * const perm_cpu = static_cast<const IdType*>(perm->data);

  // second part of result should be the counts
28
  IdArray counts = result.second.CopyTo(DGLContext{kDGLCPU, 0});
29
30
31
32
33
34
35
36
37
38
39
  ASSERT_TRUE(counts.Ptr<int64_t>() != nullptr);
  ASSERT_EQ(counts->shape[0], num_parts);
  const int64_t * const counts_cpu = static_cast<const int64_t*>(counts->data);

  std::vector<int64_t> prefix(num_parts+1, 0);
  for (int p = 0; p < num_parts; ++p) {
    prefix[p+1] = prefix[p] + counts_cpu[p];
  }
  ASSERT_EQ(prefix.back(), idxs->shape[0]);

  // copy original indexes to cpu
40
  idxs = idxs.CopyTo(DGLContext{kDGLCPU, 0});
41
42
43
44
45
46
47
48
49
  const IdType * const idxs_cpu = static_cast<const IdType*>(idxs->data);

  for (int p = 0; p < num_parts; ++p) {
    for (int64_t i = prefix[p]; i < prefix[p+1]; ++i) {
      EXPECT_EQ(idxs_cpu[perm_cpu[i]] % num_parts, p);
    }
  }
}

50
template<DGLDeviceType XPU, typename IdType>
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
void _TestRemainder_MapToX() {
  const int64_t size = 160000;
  const int num_parts = 7;
  NDArrayPartitionRef part = CreatePartitionRemainderBased(
      size,  num_parts);

  for (int part_id = 0; part_id < num_parts; ++part_id) {
    IdArray local = aten::Range(0, part->PartSize(part_id), sizeof(IdType)*8,
        DGLContext{XPU, 0});
    IdArray global = part->MapToGlobal(local, part_id);
    IdArray act_local = part->MapToLocal(global).CopyTo(CPU);

    // every global index should have the same remainder as the part id
    ASSERT_EQ(global->shape[0], local->shape[0]);
    global = global.CopyTo(CPU);
    for (size_t i = 0; i < global->shape[0]; ++i) {
      EXPECT_EQ(Ptr<IdType>(global)[i] % num_parts, part_id) << "i=" << i <<
          ", num_parts=" << num_parts << ", part_id=" << part_id;
    }

    // the remapped local indices to should match the original
    local = local.CopyTo(CPU);
    ASSERT_EQ(local->shape[0], act_local->shape[0]);
    for (size_t i = 0; i < act_local->shape[0]; ++i) {
      EXPECT_EQ(Ptr<IdType>(local)[i], Ptr<IdType>(act_local)[i]);
    }
  }
}

80

81
82
TEST(PartitionTest, TestRemainderPartition) {
#ifdef DGL_USE_CUDA
83
84
  _TestRemainder_GeneratePermutation<kDGLCUDA, int32_t>();
  _TestRemainder_GeneratePermutation<kDGLCUDA, int64_t>();
85

86
87
  _TestRemainder_MapToX<kDGLCUDA, int32_t>();
  _TestRemainder_MapToX<kDGLCUDA, int64_t>();
88
89
90
91
92
#endif
  // CPU is not implemented
}


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
template<typename INDEX, typename RANGE>
int _FindPart(
    const INDEX idx,
    const RANGE * const range,
    const int num_parts)
{
  for (int i = 0; i < num_parts; ++i) {
    if (range[i+1] > idx) {
      return i;
    }
  }

  return -1;
}

108
template<DGLDeviceType XPU, typename IdType>
109
110
111
void _TestRange_GeneratePermutation() {
  const int64_t size = 160000;
  const int num_parts = 7;
112
  IdArray range = aten::NewIdArray(num_parts+1, DGLContext{kDGLCPU, 0},
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        sizeof(IdType)*8);
  for (int i = 0; i < num_parts; ++i) {
    range.Ptr<IdType>()[i] = (size/num_parts)*i;
  }
  range.Ptr<IdType>()[num_parts] = size;
  NDArrayPartitionRef part = CreatePartitionRangeBased(
      size,  num_parts, range.CopyTo(DGLContext{XPU, 0}));

  IdArray idxs = aten::Range(0, size/10, sizeof(IdType)*8,
      DGLContext{XPU, 0});

  std::pair<IdArray, IdArray> result = part->GeneratePermutation(idxs);

  // first part of result should be the permutation
127
  IdArray perm = result.first.CopyTo(DGLContext{kDGLCPU, 0});
128
129
130
131
132
  ASSERT_TRUE(perm.Ptr<IdType>() != nullptr);
  ASSERT_EQ(perm->shape[0], idxs->shape[0]);
  const IdType * const perm_cpu = static_cast<const IdType*>(perm->data);

  // second part of result should be the counts
133
  IdArray counts = result.second.CopyTo(DGLContext{kDGLCPU, 0});
134
135
136
  ASSERT_TRUE(counts.Ptr<int64_t>() != nullptr);
  ASSERT_EQ(counts->shape[0], num_parts);
  const int64_t * const counts_cpu = static_cast<const int64_t*>(counts->data);
137

138
139
140
141
142
143
144
  std::vector<int64_t> prefix(num_parts+1, 0);
  for (int p = 0; p < num_parts; ++p) {
    prefix[p+1] = prefix[p] + counts_cpu[p];
  }
  ASSERT_EQ(prefix.back(), idxs->shape[0]);

  // copy original indexes to cpu
145
  idxs = idxs.CopyTo(DGLContext{kDGLCPU, 0});
146
147
148
149
150
151
152
153
154
  const IdType * const idxs_cpu = static_cast<const IdType*>(idxs->data);

  for (int p = 0; p < num_parts; ++p) {
    for (int64_t i = prefix[p]; i < prefix[p+1]; ++i) {
      EXPECT_EQ(_FindPart(idxs_cpu[perm_cpu[i]], range.Ptr<IdType>(), num_parts), p);
    }
  }
}

155
template<DGLDeviceType XPU, typename IdType>
156
157
158
void _TestRange_MapToX() {
  const int64_t size = 160000;
  const int num_parts = 7;
159
  IdArray range = aten::NewIdArray(num_parts+1, DGLContext{kDGLCPU, 0},
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        sizeof(IdType)*8);
  for (int i = 0; i < num_parts; ++i) {
    Ptr<IdType>(range)[i] = (size/num_parts)*i;
  }
  range.Ptr<IdType>()[num_parts] = size;
  NDArrayPartitionRef part = CreatePartitionRangeBased(
      size,  num_parts, range.CopyTo(DGLContext{XPU, 0}));

  for (int part_id = 0; part_id < num_parts; ++part_id) {
    IdArray local = aten::Range(0, part->PartSize(part_id), sizeof(IdType)*8,
        DGLContext{XPU, 0});
    IdArray global = part->MapToGlobal(local, part_id);
    IdArray act_local = part->MapToLocal(global).CopyTo(CPU);

    ASSERT_EQ(global->shape[0], local->shape[0]);
    global = global.CopyTo(CPU);
    for (size_t i = 0; i < global->shape[0]; ++i) {
      EXPECT_EQ(_FindPart(Ptr<IdType>(global)[i], Ptr<IdType>(range), num_parts), part_id) << "i=" << i <<
          ", num_parts=" << num_parts << ", part_id=" << part_id << ", shape=" << global->shape[0];
    }

    // the remapped local indices to should match the original
    local = local.CopyTo(CPU);
    ASSERT_EQ(local->shape[0], act_local->shape[0]);
    for (size_t i = 0; i < act_local->shape[0]; ++i) {
      EXPECT_EQ(Ptr<IdType>(local)[i], Ptr<IdType>(act_local)[i]);
    }
  }
}

TEST(PartitionTest, TestRangePartition) {
#ifdef DGL_USE_CUDA
192
193
  _TestRange_GeneratePermutation<kDGLCUDA, int32_t>();
  _TestRange_GeneratePermutation<kDGLCUDA, int64_t>();
194

195
196
  _TestRange_MapToX<kDGLCUDA, int32_t>();
  _TestRange_MapToX<kDGLCUDA, int64_t>();
197
198
199
#endif
  // CPU is not implemented
}