“f09989a1b292ac6fdeceb46ca24f8f50a38d391a”上不存在“paddle/legacy/utils/CustomStackTrace.cpp”
hl_table_apply.cu 3.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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 "hl_base.h"
#include "hl_cuda.h"
L
liaogang 已提交
17
#include "hl_device_functions.cuh"
Z
zhangjinchao01 已提交
18 19
#include "paddle/utils/Logging.h"

L
liaogang 已提交
20 21 22 23 24
template <int blockDimX, int blockDimY, int gridDimX, bool AddRow>
__global__ void KeMatrixAddRows(real* output,
                                int ldo,
                                real* table,
                                int ldt,
Z
zhangjinchao01 已提交
25 26 27 28 29 30 31 32 33 34
                                int* ids,
                                int numSamples,
                                int tableSize,
                                int dim) {
  int idx = threadIdx.x;
  int idy = blockIdx.x + threadIdx.y * gridDimX;

  while (idy < numSamples) {
    int tableId = ids[idy];
    if ((0 <= tableId) && (tableId < tableSize)) {
L
liaogang 已提交
35 36
      real* out = output + idy * ldo;
      real* tab = table + tableId * ldt;
Z
zhangjinchao01 已提交
37 38
      for (int i = idx; i < dim; i += blockDimX) {
        if (AddRow) {
39
          paddle::paddleAtomicAdd(&tab[i], out[i]);
Z
zhangjinchao01 已提交
40 41 42 43 44 45 46 47 48
        } else {
          out[i] += tab[i];
        }
      }
    }
    idy += blockDimY * gridDimX;
  }
}

L
liaogang 已提交
49 50 51 52
void hl_matrix_select_rows(real* output,
                           int ldo,
                           real* table,
                           int ldt,
Z
zhangjinchao01 已提交
53 54 55 56 57 58 59 60 61 62
                           int* ids,
                           int numSamples,
                           int tableSize,
                           int dim) {
  CHECK_NOTNULL(output);
  CHECK_NOTNULL(table);
  CHECK_NOTNULL(ids);

  dim3 threads(128, 8);
  dim3 grid(8, 1);
L
liaogang 已提交
63 64
  KeMatrixAddRows<128, 8, 8, 0><<<grid, threads, 0, STREAM_DEFAULT>>>(
      output, ldo, table, ldt, ids, numSamples, tableSize, dim);
Z
zhangjinchao01 已提交
65 66 67 68

  CHECK_SYNC("hl_matrix_select_rows failed");
}

L
liaogang 已提交
69 70 71 72
void hl_matrix_add_to_rows(real* table,
                           int ldt,
                           real* input,
                           int ldi,
Z
zhangjinchao01 已提交
73 74 75 76 77 78 79 80 81 82
                           int* ids,
                           int numSamples,
                           int tableSize,
                           int dim) {
  CHECK_NOTNULL(input);
  CHECK_NOTNULL(table);
  CHECK_NOTNULL(ids);

  dim3 threads(128, 8);
  dim3 grid(8, 1);
L
liaogang 已提交
83 84
  KeMatrixAddRows<128, 8, 8, 1><<<grid, threads, 0, STREAM_DEFAULT>>>(
      input, ldi, table, ldt, ids, numSamples, tableSize, dim);
Z
zhangjinchao01 已提交
85 86 87 88

  CHECK_SYNC("hl_matrix_add_to_rows failed");
}

L
liaogang 已提交
89 90 91
template <class T, int blockDimX, int gridDimX>
__global__ void KeVectorSelect(
    T* dst, int sized, const T* src, int sizes, const int* ids, int sizei) {
Z
zhangjinchao01 已提交
92 93 94 95 96 97 98 99 100 101
  int idx = threadIdx.x + blockDimX * blockIdx.x;
  while (idx < sizei) {
    int index = ids[idx];
    // check(index < sizes);
    dst[idx] = src[index];
    idx += blockDimX * gridDimX;
  }
}

template <class T>
L
liaogang 已提交
102 103
void hl_vector_select_from(
    T* dst, int sized, const T* src, int sizes, const int* ids, int sizei) {
Z
zhangjinchao01 已提交
104 105 106 107 108 109 110
  CHECK_NOTNULL(dst);
  CHECK_NOTNULL(src);
  CHECK_NOTNULL(ids);
  CHECK_EQ(sized, sizei);

  dim3 threads(512, 1);
  dim3 grid(8, 1);
L
liaogang 已提交
111 112
  KeVectorSelect<T, 512, 8><<<grid, threads, 0, STREAM_DEFAULT>>>(
      dst, sized, src, sizes, ids, sizei);
Z
zhangjinchao01 已提交
113 114 115 116

  CHECK_SYNC("hl_vector_select_from failed");
}

L
liaogang 已提交
117 118 119 120 121 122 123 124
template void hl_vector_select_from(real* dst,
                                    int sized,
                                    const real* src,
                                    int sizes,
                                    const int* ids,
                                    int sizei);
template void hl_vector_select_from(
    int* dst, int sized, const int* src, int sizes, const int* ids, int sizei);