SIMDFunctions.h 4.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 46 47 48 49 50 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

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. */

#pragma once
#include <stddef.h>
#include <stdint.h>

namespace paddle {

namespace simd {

namespace naive {
template <typename Type>
inline void addTo(Type* a, const Type* b, size_t len) {
  for (size_t i = 0; i < len; ++i) {
    a[i] += b[i];
  }
}

template <typename Type>
inline void batchAddTo(Type* a, const Type* b[], int batch, size_t len) {
  for (int i = 0; i < batch; ++i) {
    for (size_t j = 0; j < len; ++j) {
      a[j] += b[i][j];
    }
  }
}

/**
 * @note this method is unused in paddle.
 */
template <typename Type>
inline void colMax(Type* result, const Type* data, int dim, int numSamples) {
  for (int d = 0; d < dim; ++d) {
    Type sm = data[d];
    for (int i = 1; i < numSamples; ++i) {
      sm = sm > data[i * dim + d] ? sm : data[i * dim + d];
    }
    result[d] = sm;
  }
}

template <typename Type>
inline void decayL1(Type* dst, Type* src, Type* lr, Type lambda, size_t len) {
  for (size_t i = 0; i < len; ++i) {
    Type& src_val = src[i];
    float nlambda = lr[i] * lambda;
    if (src_val > 0) {
      dst[i] = ((src_val > nlambda) ? (src_val - nlambda) : 0);
    } else {
      dst[i] = ((-src_val > nlambda) ? (src_val + nlambda) : 0);
    }
  }
}

template <class Type>
inline void decayL1(Type* dst, Type* src, Type lambda, size_t len) {
  for (size_t i = 0; i < len; ++i) {
    Type& src_val = src[i];
    if (src_val > 0) {
      dst[i] = ((src_val > lambda) ? (src_val - lambda) : 0);
    } else {
      dst[i] = ((-src_val > lambda) ? (src_val + lambda) : 0);
    }
  }
}
}  // namespace naive

template <typename Type>
inline void addTo(Type* a, const Type* b, size_t len) {
  naive::addTo(a, b, len);
}

template <typename Type>
inline void batchAddTo(Type* a, const Type* b[], int batch, size_t len) {
  naive::batchAddTo(a, b, batch, len);
}

template <typename Type>
inline void colMax(Type* result, const Type* data, int dim, int numSamples) {
  naive::colMax(result, data, dim, numSamples);
}

template <typename Type>
inline void decayL1(Type* dst, Type* src, Type* lr, Type lambda, size_t len) {
  naive::decayL1(dst, src, lr, lambda, len);
}

template <typename Type>
inline void decayL1(Type* dst, Type* src, Type lambda, size_t len) {
  naive::decayL1(dst, src, lambda, len);
}

template <size_t AlignSize>
inline bool isPointerAlign(void* ptr) {
  return reinterpret_cast<uintptr_t>(ptr) % AlignSize == 0;
}

inline bool vec_check(size_t len) {
#ifdef __AVX__
  return len % 8 == 0;
#else
  return len % 4 == 0;
#endif
}

namespace internal {
119
#ifdef __SSE3__
Z
zhangjinchao01 已提交
120 121 122
void addToImpl(float* a, const float* b, size_t len);
void batchAddToImpl(float* a, const float* b[], int batch, size_t len);
void colMaxImpl(float* result, const float* data, int dim, int numSamples);
123
#endif
Z
zhangjinchao01 已提交
124 125
#ifdef __AVX__
void decayL1AvxImpl(float* dst, float* src, float lambda, size_t len);
126 127
void decayL1AvxImpl(
    float* dst, float* src, float* lr, float lambda, size_t len);
Z
zhangjinchao01 已提交
128 129 130 131 132
#endif
}  // namespace internal

template <>
inline void addTo(float* a, const float* b, size_t len) {
133
#ifdef __SSE3__
Z
zhangjinchao01 已提交
134
  internal::addToImpl(a, b, len);
135 136 137
#else
  naive::addTo(a, b, len);
#endif
Z
zhangjinchao01 已提交
138 139 140 141
}

template <>
inline void batchAddTo(float* a, const float* b[], int batch, size_t len) {
142
#ifdef __SSE3__
Z
zhangjinchao01 已提交
143
  internal::batchAddToImpl(a, b, batch, len);
144 145 146
#else
  naive::batchAddTo(a, b, batch, len);
#endif
Z
zhangjinchao01 已提交
147 148 149 150
}

template <>
inline void colMax(float* result, const float* data, int dim, int numSamples) {
151
#ifdef __SSE3__
Z
zhangjinchao01 已提交
152
  internal::colMaxImpl(result, data, dim, numSamples);
153 154 155
#else
  naive::colMax(result, data, dim, numSamples);
#endif
Z
zhangjinchao01 已提交
156 157 158 159 160 161 162 163 164 165 166 167
}

template <>
inline void decayL1(float* dst, float* src, float lambda, size_t len) {
#ifdef __AVX__
  internal::decayL1AvxImpl(dst, src, lambda, len);
#else
  naive::decayL1(dst, src, lambda, len);
#endif
}

template <>
168 169
inline void decayL1(
    float* dst, float* src, float* lr, float lambda, size_t len) {
Z
zhangjinchao01 已提交
170 171 172 173 174 175 176 177 178 179
#ifdef __AVX__
  internal::decayL1AvxImpl(dst, src, lr, lambda, len);
#else
  naive::decayL1(dst, src, lr, lambda, len);
#endif
}

}  // namespace simd

}  // namespace paddle