aboutsummaryrefslogtreecommitdiff
path: root/offload/unittests/Conformance/lib/DeviceContext.cpp
blob: 6c3425f1e17c2564cda92e601d51fbfd8387011d (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the implementation of helpers and non-template member
/// functions for the DeviceContext class.
///
//===----------------------------------------------------------------------===//

#include "mathtest/DeviceContext.hpp"

#include "mathtest/ErrorHandling.hpp"

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"

#include <OffloadAPI.h>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <system_error>
#include <vector>

using namespace mathtest;

//===----------------------------------------------------------------------===//
// Helpers
//===----------------------------------------------------------------------===//

namespace {

// The static 'Wrapper' instance ensures olInit() is called once at program
// startup and olShutDown() is called once at program termination
struct OffloadInitWrapper {
  OffloadInitWrapper() { OL_CHECK(olInit()); }
  ~OffloadInitWrapper() { OL_CHECK(olShutDown()); }
};
static OffloadInitWrapper Wrapper{};

[[nodiscard]] std::string getDeviceName(ol_device_handle_t DeviceHandle) {
  std::size_t PropSize = 0;
  OL_CHECK(olGetDeviceInfoSize(DeviceHandle, OL_DEVICE_INFO_PRODUCT_NAME,
                               &PropSize));

  if (PropSize == 0)
    return "";

  std::string PropValue(PropSize, '\0');
  OL_CHECK(olGetDeviceInfo(DeviceHandle, OL_DEVICE_INFO_PRODUCT_NAME, PropSize,
                           PropValue.data()));
  PropValue.pop_back(); // Remove the null terminator

  return PropValue;
}

[[nodiscard]] ol_platform_handle_t
getDevicePlatform(ol_device_handle_t DeviceHandle) noexcept {
  ol_platform_handle_t PlatformHandle = nullptr;
  OL_CHECK(olGetDeviceInfo(DeviceHandle, OL_DEVICE_INFO_PLATFORM,
                           sizeof(PlatformHandle), &PlatformHandle));
  return PlatformHandle;
}

[[nodiscard]] std::string getPlatformName(ol_platform_handle_t PlatformHandle) {
  std::size_t PropSize = 0;
  OL_CHECK(
      olGetPlatformInfoSize(PlatformHandle, OL_PLATFORM_INFO_NAME, &PropSize));

  if (PropSize == 0)
    return "";

  std::string PropValue(PropSize, '\0');
  OL_CHECK(olGetPlatformInfo(PlatformHandle, OL_PLATFORM_INFO_NAME, PropSize,
                             PropValue.data()));
  PropValue.pop_back(); // Remove the null terminator

  return PropValue;
}

[[nodiscard]] ol_platform_backend_t
getPlatformBackend(ol_platform_handle_t PlatformHandle) noexcept {
  ol_platform_backend_t Backend = OL_PLATFORM_BACKEND_UNKNOWN;
  OL_CHECK(olGetPlatformInfo(PlatformHandle, OL_PLATFORM_INFO_BACKEND,
                             sizeof(Backend), &Backend));
  return Backend;
}

struct Device {
  ol_device_handle_t Handle;
  std::string Name;
  std::string Platform;
  ol_platform_backend_t Backend;
};

const std::vector<Device> &getDevices() {
  // Thread-safe initialization of a static local variable
  static auto Devices = []() {
    std::vector<Device> TmpDevices;

    // Discovers all devices that are not the host
    const auto *const ResultFromIterate = olIterateDevices(
        [](ol_device_handle_t DeviceHandle, void *Data) {
          ol_platform_handle_t PlatformHandle = getDevicePlatform(DeviceHandle);
          ol_platform_backend_t Backend = getPlatformBackend(PlatformHandle);

          if (Backend != OL_PLATFORM_BACKEND_HOST) {
            auto Name = getDeviceName(DeviceHandle);
            auto Platform = getPlatformName(PlatformHandle);

            static_cast<std::vector<Device> *>(Data)->push_back(
                {DeviceHandle, Name, Platform, Backend});
          }

          return true;
        },
        &TmpDevices);

    OL_CHECK(ResultFromIterate);

    return TmpDevices;
  }();

  return Devices;
}
} // namespace

const llvm::SetVector<llvm::StringRef> &mathtest::getPlatforms() {
  // Thread-safe initialization of a static local variable
  static auto Platforms = []() {
    llvm::SetVector<llvm::StringRef> TmpPlatforms;

    for (const auto &Device : getDevices())
      TmpPlatforms.insert(Device.Platform);

    return TmpPlatforms;
  }();

  return Platforms;
}

void detail::allocManagedMemory(ol_device_handle_t DeviceHandle,
                                std::size_t Size,
                                void **AllocationOut) noexcept {
  OL_CHECK(
      olMemAlloc(DeviceHandle, OL_ALLOC_TYPE_MANAGED, Size, AllocationOut));
}

//===----------------------------------------------------------------------===//
// DeviceContext
//===----------------------------------------------------------------------===//

DeviceContext::DeviceContext(std::size_t GlobalDeviceId)
    : GlobalDeviceId(GlobalDeviceId), DeviceHandle(nullptr) {
  const auto &Devices = getDevices();

  if (GlobalDeviceId >= Devices.size())
    FATAL_ERROR("Invalid GlobalDeviceId: " + llvm::Twine(GlobalDeviceId) +
                ", but the number of available devices is " +
                llvm::Twine(Devices.size()));

  DeviceHandle = Devices[GlobalDeviceId].Handle;
}

DeviceContext::DeviceContext(llvm::StringRef Platform, std::size_t DeviceId)
    : DeviceHandle(nullptr) {
  const auto &Platforms = getPlatforms();

  if (!llvm::any_of(Platforms, [&](llvm::StringRef CurrentPlatform) {
        return CurrentPlatform.equals_insensitive(Platform);
      }))
    FATAL_ERROR("There is no platform that matches with '" +
                llvm::Twine(Platform) +
                "'. Available platforms are: " + llvm::join(Platforms, ", "));

  const auto &Devices = getDevices();

  std::optional<std::size_t> FoundGlobalDeviceId;
  std::size_t MatchCount = 0;

  for (std::size_t Index = 0; Index < Devices.size(); ++Index) {
    if (Platform.equals_insensitive(Devices[Index].Platform)) {
      if (MatchCount == DeviceId) {
        FoundGlobalDeviceId = Index;
        break;
      }
      MatchCount++;
    }
  }

  if (!FoundGlobalDeviceId)
    FATAL_ERROR("Invalid DeviceId: " + llvm::Twine(DeviceId) +
                ", but the number of available devices on '" + Platform +
                "' is " + llvm::Twine(MatchCount));

  GlobalDeviceId = *FoundGlobalDeviceId;
  DeviceHandle = Devices[GlobalDeviceId].Handle;
}

[[nodiscard]] llvm::Expected<std::shared_ptr<DeviceImage>>
DeviceContext::loadBinary(llvm::StringRef Directory,
                          llvm::StringRef BinaryName) const {
  auto Backend = getDevices()[GlobalDeviceId].Backend;
  llvm::StringRef Extension;

  switch (Backend) {
  case OL_PLATFORM_BACKEND_AMDGPU:
    Extension = ".amdgpu.bin";
    break;
  case OL_PLATFORM_BACKEND_CUDA:
    Extension = ".nvptx64.bin";
    break;
  default:
    return llvm::createStringError(
        "Unsupported backend to infer binary extension");
  }

  llvm::SmallString<128> FullPath(Directory);
  llvm::sys::path::append(FullPath, llvm::Twine(BinaryName) + Extension);

  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
      llvm::MemoryBuffer::getFile(FullPath);

  if (std::error_code ErrorCode = FileOrErr.getError())
    return llvm::createStringError(
        llvm::Twine("Failed to read device binary file '") + FullPath +
        "': " + ErrorCode.message());

  std::unique_ptr<llvm::MemoryBuffer> &BinaryData = *FileOrErr;

  ol_program_handle_t ProgramHandle = nullptr;
  const ol_result_t OlResult =
      olCreateProgram(DeviceHandle, BinaryData->getBufferStart(),
                      BinaryData->getBufferSize(), &ProgramHandle);

  if (OlResult != OL_SUCCESS) {
    llvm::StringRef Details =
        OlResult->Details ? OlResult->Details : "No details provided";

    // clang-format off
    return llvm::createStringError(
      llvm::Twine(Details) +
      " (code " + llvm::Twine(OlResult->Code) + ")");
    // clang-format on
  }

  return std::shared_ptr<DeviceImage>(
      new DeviceImage(DeviceHandle, ProgramHandle));
}

[[nodiscard]] llvm::Expected<ol_symbol_handle_t>
DeviceContext::getKernelHandle(ol_program_handle_t ProgramHandle,
                               llvm::StringRef KernelName) const noexcept {
  ol_symbol_handle_t Handle = nullptr;
  llvm::SmallString<32> NameBuffer(KernelName);

  const ol_result_t OlResult = olGetSymbol(ProgramHandle, NameBuffer.c_str(),
                                           OL_SYMBOL_KIND_KERNEL, &Handle);

  if (OlResult != OL_SUCCESS) {
    llvm::StringRef Details =
        OlResult->Details ? OlResult->Details : "No details provided";

    // clang-format off
    return llvm::createStringError(
      llvm::Twine(Details) +
      " (code " + llvm::Twine(OlResult->Code) + ")");
    // clang-format on
  }

  return Handle;
}

void DeviceContext::launchKernelImpl(
    ol_symbol_handle_t KernelHandle, uint32_t NumGroups, uint32_t GroupSize,
    const void *KernelArgs, std::size_t KernelArgsSize) const noexcept {
  ol_kernel_launch_size_args_t LaunchSizeArgs;
  LaunchSizeArgs.Dimensions = 1;
  LaunchSizeArgs.NumGroups = {NumGroups, 1, 1};
  LaunchSizeArgs.GroupSize = {GroupSize, 1, 1};
  LaunchSizeArgs.DynSharedMemory = 0;

  OL_CHECK(olLaunchKernel(nullptr, DeviceHandle, KernelHandle, KernelArgs,
                          KernelArgsSize, &LaunchSizeArgs));
}

[[nodiscard]] llvm::StringRef DeviceContext::getName() const noexcept {
  return getDevices()[GlobalDeviceId].Name;
}

[[nodiscard]] llvm::StringRef DeviceContext::getPlatform() const noexcept {
  return getDevices()[GlobalDeviceId].Platform;
}