capi_private.h 1.9 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

Y
Yu Yang 已提交
15
#include "capi.h"
Y
Yu Yang 已提交
16
#include "paddle/gserver/gradientmachines/GradientMachine.h"
Y
Yu Yang 已提交
17 18 19 20 21 22 23 24
#include "paddle/math/Matrix.h"
#include "paddle/math/Vector.h"
#include "paddle/parameter/Argument.h"
#pragma once

namespace paddle {
namespace capi {

Y
Yu Yang 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
enum CType { kIVECTOR = 0, kMATRIX, kARGUMENTS, kGRADIENT_MACHINE };

#define STRUCT_HEADER CType type;

struct CHeader {
  STRUCT_HEADER
};

struct CIVector {
  STRUCT_HEADER
  IVectorPtr vec;

  CIVector() : type(kIVECTOR) {}
Y
Yu Yang 已提交
38 39 40
};

struct CMatrix {
Y
Yu Yang 已提交
41
  STRUCT_HEADER
Y
Yu Yang 已提交
42
  MatrixPtr mat;
Y
Yu Yang 已提交
43 44

  CMatrix() : type(kMATRIX) {}
Y
Yu Yang 已提交
45 46 47
};

struct CArguments {
Y
Yu Yang 已提交
48
  STRUCT_HEADER
Y
Yu Yang 已提交
49
  std::vector<paddle::Argument> args;
Y
Yu Yang 已提交
50 51

  CArguments() : type(kARGUMENTS) {}
Y
Yu Yang 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

  template <typename T>
  paddle_error accessSeqPos(uint64_t ID, uint32_t nestedLevel, T callback) {
    if (ID >= args.size()) return kPD_OUT_OF_RANGE;
    switch (nestedLevel) {
      case 0:
        callback(args[ID].sequenceStartPositions);
        break;
      case 1:
        callback(args[ID].subSequenceStartPositions);
        break;
      default:
        return kPD_OUT_OF_RANGE;
    }
    return kPD_NO_ERROR;
  }
Y
Yu Yang 已提交
68 69
};

Y
Yu Yang 已提交
70
struct CGradientMachine {
Y
Yu Yang 已提交
71
  STRUCT_HEADER
Y
Yu Yang 已提交
72
  paddle::GradientMachinePtr machine;
Y
Yu Yang 已提交
73 74

  CGradientMachine() : type(kGRADIENT_MACHINE) {}
Y
Yu Yang 已提交
75 76
};

Y
Yu Yang 已提交
77 78 79 80
template <typename T>
inline T* cast(void* ptr) {
  return reinterpret_cast<T*>(ptr);
}
Y
Yu Yang 已提交
81 82
}  // namespace capi
}  // namespace paddle