test_graph.cu 4.8 KB
Newer Older
S
seemingwang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <gtest/gtest.h>
16

S
seemingwang 已提交
17
#include <vector>
18

S
seemingwang 已提交
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 46 47 48 49 50 51 52
#include "paddle/fluid/framework/fleet/heter_ps/feature_value.h"
#include "paddle/fluid/framework/fleet/heter_ps/graph_gpu_ps_table.h"
#include "paddle/fluid/framework/fleet/heter_ps/heter_comm.h"
#include "paddle/fluid/framework/fleet/heter_ps/heter_resource.h"
#include "paddle/fluid/framework/fleet/heter_ps/optimizer.cuh.h"
#include "paddle/fluid/platform/cuda_device_guard.h"

using namespace paddle::framework;
TEST(TEST_FLEET, graph_comm) {
  int gpu_count = 3;
  std::vector<int> dev_ids;
  dev_ids.push_back(0);
  dev_ids.push_back(1);
  dev_ids.push_back(2);
  std::shared_ptr<HeterPsResource> resource =
      std::make_shared<HeterPsResource>(dev_ids);
  resource->enable_p2p();
  GpuPsGraphTable g(resource);
  int node_count = 10;
  std::vector<std::vector<int64_t>> neighbors(node_count);
  int ind = 0;
  int64_t node_id = 0;
  std::vector<GpuPsCommGraph> graph_list(gpu_count);
  while (ind < node_count) {
    int neighbor_size = ind + 1;
    graph_list[ind % gpu_count].node_size++;
    graph_list[ind % gpu_count].neighbor_size += neighbor_size;
    while (neighbor_size--) {
      neighbors[ind].push_back(node_id++);
    }
    ind++;
  }
  std::vector<int> neighbor_offset(gpu_count, 0), node_index(gpu_count, 0);
  for (int i = 0; i < graph_list.size(); i++) {
D
danleifeng 已提交
53 54
    graph_list[i].node_list = new uint64_t[graph_list[i].node_size];
    graph_list[i].node_info_list = new GpuPsNodeInfo[graph_list[i].node_size];
S
seemingwang 已提交
55 56 57 58
    graph_list[i].neighbor_list = new int64_t[graph_list[i].neighbor_size];
  }
  for (int i = 0; i < node_count; i++) {
    ind = i % gpu_count;
D
danleifeng 已提交
59 60
    graph_list[ind].node_list[node_index[ind]] = i;
    graph_list[ind].node_info_list[node_index[ind]].neighbor_offset =
S
seemingwang 已提交
61
        neighbor_offset[ind];
D
danleifeng 已提交
62
    graph_list[ind].node_info_list[node_index[ind]].neighbor_size =
S
seemingwang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        neighbors[i].size();
    for (auto x : neighbors[i]) {
      graph_list[ind].neighbor_list[neighbor_offset[ind]++] = x;
    }
    node_index[ind]++;
  }
  g.build_graph_from_cpu(graph_list);
  /*
  gpu 0:
  0,3,6,9
  gpu 1:
  1,4,7
  gpu 2:
  2,5,8

  query(2,6) returns nodes [6,9,1,4,7,2]
  */
  int64_t answer[6] = {6, 9, 1, 4, 7, 2};
  int64_t *res = new int64_t[6];
  auto query_res = g.query_node_list(0, 2, 6);
  cudaMemcpy(res, query_res->val, 48, cudaMemcpyDeviceToHost);
  ASSERT_EQ(query_res->actual_sample_size, 6);
  for (int i = 0; i < 6; i++) {
    ASSERT_EQ(res[i], answer[i]);
  }
  delete[] res;
  delete query_res;
  /*
   node x's neighbor list = [(1+x)*x/2,(1+x)*x/2 + 1,.....,(1+x)*x/2 + x]
   so node 6's neighbors are [21,22...,27]
   node 7's neighbors are [28,29,..35]
    node 0's neighbors are [0]
   query([7,0,6],sample_size=3) should return [28,29,30,0,x,x,21,22,23]
   6 --index-->2
   0 --index--->0
   7 --index-->2
  */
100

S
seemingwang 已提交
101 102 103 104 105
  int64_t cpu_key[3] = {7, 0, 6};
  void *key;
  cudaMalloc((void **)&key, 3 * sizeof(int64_t));
  cudaMemcpy(key, cpu_key, 3 * sizeof(int64_t), cudaMemcpyHostToDevice);
  auto neighbor_sample_res = g.graph_neighbor_sample(0, (int64_t *)key, 3, 3);
106 107 108
  res = new int64_t[7];
  cudaMemcpy(res, neighbor_sample_res->val, 56, cudaMemcpyDeviceToHost);
  int *actual_sample_size = new int[3];
109 110 111
  cudaMemcpy(actual_sample_size,
             neighbor_sample_res->actual_sample_size,
             12,
112 113
             cudaMemcpyDeviceToHost);  // 3, 1, 3
  int *cumsum_sample_size = new int[3];
114 115 116
  cudaMemcpy(cumsum_sample_size,
             neighbor_sample_res->offset,
             12,
117 118 119 120 121 122 123 124 125 126 127
             cudaMemcpyDeviceToHost);  // 0, 3, 4

  std::vector<std::vector<int64_t>> neighbors_;
  std::vector<int64_t> neighbors_7 = {28, 29, 30, 31, 32, 33, 34, 35};
  std::vector<int64_t> neighbors_0 = {0};
  std::vector<int64_t> neighbors_6 = {21, 22, 23, 24, 25, 26, 27};
  neighbors_.push_back(neighbors_7);
  neighbors_.push_back(neighbors_0);
  neighbors_.push_back(neighbors_6);
  for (int i = 0; i < 3; i++) {
    for (int j = cumsum_sample_size[i];
128 129
         j < cumsum_sample_size[i] + actual_sample_size[i];
         j++) {
130 131 132 133 134 135 136 137
      bool flag = false;
      for (int k = 0; k < neighbors_[i].size(); k++) {
        if (res[j] == neighbors_[i][k]) {
          flag = true;
          break;
        }
      }
      ASSERT_EQ(flag, true);
S
seemingwang 已提交
138 139
    }
  }
140

S
seemingwang 已提交
141
  delete[] res;
142 143
  delete[] actual_sample_size;
  delete[] cumsum_sample_size;
S
seemingwang 已提交
144 145
  delete neighbor_sample_res;
}