place.h 2.8 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

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 <string>

19 20 21 22 23 24 25 26 27 28 29 30 31
namespace pten {

enum class AllocationType : int8_t {
  UNDEF = 0,
  CPU = 1,
  GPU = 2,
  GPUPINNED = 3,
  XPU = 4,
  NPU = 5,
  NPUPINNED = 6,
  IPU = 7,
  MLU = 8,
};
32

33
const char *AllocationTypeStr(AllocationType type);
34 35

/// \brief The place is used to specify where the data is stored.
36
class Place {
37
 public:
38
  Place() : device(0), alloc_type_(AllocationType::UNDEF) {}
39

40 41
  explicit Place(AllocationType type, int8_t id)
      : device(id), alloc_type_(type) {}
42

43
  explicit Place(AllocationType type) : device(0), alloc_type_(type) {}
44

45 46 47
  void Reset(AllocationType type, int8_t device_id = 0) noexcept {
    alloc_type_ = type;
    device = device_id;
48 49
  }

50
  AllocationType GetType() const { return alloc_type_; }
51

52
  int8_t GetDeviceId() const { return device; }
53 54 55

  std::string DebugString() const;

56 57 58 59
 public:
  // TODO(wilber): Just because of backward compatibility, it needs to be
  // changed to private in the future.
  int8_t device;
60 61

 private:
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
  AllocationType alloc_type_;
};

class CPUPlace : public Place {
 public:
  CPUPlace() : Place(AllocationType::CPU, 0) {}
};

class GPUPlace : public Place {
 public:
  GPUPlace() : Place(AllocationType::GPU, 0) {}
  explicit GPUPlace(int device_id) : Place(AllocationType::GPU, device_id) {}
};

class GPUPinnedPlace : public Place {
 public:
  GPUPinnedPlace() : Place(AllocationType::GPUPINNED) {}
};

class XPUPlace : public Place {
 public:
  XPUPlace() : Place(AllocationType::XPU, 0) {}
  explicit XPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {}
};

class NPUPlace : public Place {
 public:
  NPUPlace() : Place(AllocationType::NPU, 0) {}
  explicit NPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {}
};

class NPUPinnedPlace : public Place {
 public:
  NPUPinnedPlace() : Place(AllocationType::NPUPINNED) {}
};

class IPUPlace : public Place {
 public:
  IPUPlace() : Place(AllocationType::XPU, 0) {}
  explicit IPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {}
};

class MLUPlace : public Place {
 public:
  MLUPlace() : Place(AllocationType::MLU, 0) {}
  explicit MLUPlace(int device_id) : Place(AllocationType::MLU, device_id) {}
108 109
};

110
std::ostream &operator<<(std::ostream &, const Place &);
111

112
}  // namespace pten