summaryrefslogtreecommitdiff
path: root/parallel-libs/streamexecutor/lib/unittests/SimpleHostPlatformDevice.h
blob: 184c2d7f273cf2ba685b4b9432c972ae3a28962d (plain)
1
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//===-- SimpleHostPlatformDevice.h - Host device for testing ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// The SimpleHostPlatformDevice class is a streamexecutor::PlatformDevice that
/// is really just the host processor and memory. It is useful for testing
/// because no extra device platform is required.
///
//===----------------------------------------------------------------------===//

#ifndef STREAMEXECUTOR_LIB_UNITTESTS_SIMPLEHOSTPLATFORMDEVICE_H
#define STREAMEXECUTOR_LIB_UNITTESTS_SIMPLEHOSTPLATFORMDEVICE_H

#include <cstdlib>
#include <cstring>

#include "streamexecutor/PlatformInterfaces.h"

namespace streamexecutor {
namespace test {

/// A streamexecutor::PlatformDevice that simply forwards all operations to the
/// host platform.
///
/// The allocate and copy methods are simple wrappers for std::malloc and
/// std::memcpy.
class SimpleHostPlatformDevice : public streamexecutor::PlatformDevice {
public:
  std::string getName() const override { return "SimpleHostPlatformDevice"; }

  streamexecutor::Expected<
      std::unique_ptr<streamexecutor::PlatformStreamHandle>>
  createStream() override {
    return nullptr;
  }

  streamexecutor::Expected<void *>
  allocateDeviceMemory(size_t ByteCount) override {
    return std::malloc(ByteCount);
  }

  streamexecutor::Error freeDeviceMemory(const void *Handle) override {
    std::free(const_cast<void *>(Handle));
    return streamexecutor::Error::success();
  }

  streamexecutor::Expected<void *>
  allocateHostMemory(size_t ByteCount) override {
    return std::malloc(ByteCount);
  }

  streamexecutor::Error freeHostMemory(void *Memory) override {
    std::free(const_cast<void *>(Memory));
    return streamexecutor::Error::success();
  }

  streamexecutor::Error registerHostMemory(void *Memory,
                                           size_t ByteCount) override {
    return streamexecutor::Error::success();
  }

  streamexecutor::Error unregisterHostMemory(void *Memory) override {
    return streamexecutor::Error::success();
  }

  streamexecutor::Error copyD2H(streamexecutor::PlatformStreamHandle *S,
                                const void *DeviceHandleSrc,
                                size_t SrcByteOffset, void *HostDst,
                                size_t DstByteOffset,
                                size_t ByteCount) override {
    std::memcpy(static_cast<char *>(HostDst) + DstByteOffset,
                static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
                ByteCount);
    return streamexecutor::Error::success();
  }

  streamexecutor::Error copyH2D(streamexecutor::PlatformStreamHandle *S,
                                const void *HostSrc, size_t SrcByteOffset,
                                const void *DeviceHandleDst,
                                size_t DstByteOffset,
                                size_t ByteCount) override {
    std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
                    DstByteOffset,
                static_cast<const char *>(HostSrc) + SrcByteOffset, ByteCount);
    return streamexecutor::Error::success();
  }

  streamexecutor::Error
  copyD2D(streamexecutor::PlatformStreamHandle *S, const void *DeviceHandleSrc,
          size_t SrcByteOffset, const void *DeviceHandleDst,
          size_t DstByteOffset, size_t ByteCount) override {
    std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
                    DstByteOffset,
                static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
                ByteCount);
    return streamexecutor::Error::success();
  }

  streamexecutor::Error synchronousCopyD2H(const void *DeviceHandleSrc,
                                           size_t SrcByteOffset, void *HostDst,
                                           size_t DstByteOffset,
                                           size_t ByteCount) override {
    std::memcpy(static_cast<char *>(HostDst) + DstByteOffset,
                static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
                ByteCount);
    return streamexecutor::Error::success();
  }

  streamexecutor::Error synchronousCopyH2D(const void *HostSrc,
                                           size_t SrcByteOffset,
                                           const void *DeviceHandleDst,
                                           size_t DstByteOffset,
                                           size_t ByteCount) override {
    std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
                    DstByteOffset,
                static_cast<const char *>(HostSrc) + SrcByteOffset, ByteCount);
    return streamexecutor::Error::success();
  }

  streamexecutor::Error synchronousCopyD2D(const void *DeviceHandleSrc,
                                           size_t SrcByteOffset,
                                           const void *DeviceHandleDst,
                                           size_t DstByteOffset,
                                           size_t ByteCount) override {
    std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
                    DstByteOffset,
                static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
                ByteCount);
    return streamexecutor::Error::success();
  }

  /// Gets the value at the given index from a GlobalDeviceMemory<T> instance
  /// created by this class.
  template <typename T>
  static T getDeviceValue(const streamexecutor::GlobalDeviceMemory<T> &Memory,
                          size_t Index) {
    return static_cast<const T *>(Memory.getHandle())[Index];
  }
};

} // namespace test
} // namespace streamexecutor

#endif // STREAMEXECUTOR_LIB_UNITTESTS_SIMPLEHOSTPLATFORMDEVICE_H