File: test_issue_1548.cpp

package info (click to toggle)
pocl 7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 29,768 kB
  • sloc: lisp: 151,669; ansic: 135,425; cpp: 65,801; python: 1,846; sh: 1,084; ruby: 255; pascal: 231; tcl: 180; makefile: 174; asm: 81; java: 72; xml: 49
file content (75 lines) | stat: -rw-r--r-- 2,504 bytes parent folder | download
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
#define CL_HPP_ENABLE_EXCEPTIONS
#include "CL/opencl.hpp"

#include <cstdlib>
#include <iostream>
#include <string>

#define ASSERT_SUCCESS(_exp)                                                   \
  do {                                                                         \
    if ((_exp) != CL_SUCCESS) {                                                \
      std::cerr << "OpenCL error at " << __FILE__ << ":" << __LINE__ << "\n";  \
      exit(2);                                                                 \
    }                                                                          \
  } while (0)

template <typename FnTy>
FnTy LoadOclExtFn(cl::Platform &P, const std::string &FnName) {
  auto PId = P.Wrapper<cl_platform_id>::get();
  auto TheFn = reinterpret_cast<FnTy>(
      clGetExtensionFunctionAddressForPlatform(PId, FnName.c_str()));
  if (!TheFn) {
    std::cerr << "Could not find platform extension: " << FnName
              << ";\nTest SKIPPED\n";
    exit(77);
  }
  return TheFn;
}

int main() try {
  unsigned PlatformIdx = 0;

  std::vector<cl::Platform> Platforms;
  cl::Platform::get(&Platforms);
  cl::Platform Platform = Platforms.at(PlatformIdx);
  std::cout << "Selected platform: " << Platform.getInfo<CL_PLATFORM_NAME>()
            << std::endl;

  std::vector<cl::Device> Devices;
  Platform.getDevices(CL_DEVICE_TYPE_ALL, &Devices);
  unsigned DevIdx = 0;
  for (auto &Dev : Devices)
    std::cout << "Devices[" << DevIdx++
              << "]: " << Dev.getInfo<CL_DEVICE_NAME>() << std::endl;
  if (Devices.size() < 2) {
    std::cerr << "This test requires at least two devices with USM support\n";
    exit(77);
  }

  auto AllocDevMem =
      LoadOclExtFn<clDeviceMemAllocINTEL_fn>(Platform, "clDeviceMemAllocINTEL");
  auto FreeMem = LoadOclExtFn<clMemFreeINTEL_fn>(Platform, "clMemFreeINTEL");

  auto Ctx = cl::Context(Devices);

  cl_int Err;

  void *Dev0Mem = AllocDevMem(Ctx.get(), Devices[0].get(), nullptr, 8, 8, &Err);
  ASSERT_SUCCESS(Err);
  Err = FreeMem(Ctx.get(), Dev0Mem);
  ASSERT_SUCCESS(Err);

  void *Dev1Mem = AllocDevMem(Ctx.get(), Devices[1].get(), nullptr, 8, 8, &Err);
  ASSERT_SUCCESS(Err);
  Err = FreeMem(Ctx.get(), Dev1Mem);
  ASSERT_SUCCESS(Err);

  return 0;
} catch (cl::Error &Ex) {
  std::cerr << "Caught OpenCL exception: " << Ex.what() << "\n";
  std::cerr << "Error code: " << Ex.err() << "\n";
  return 2;
} catch (std::exception &Ex) {
  std::cerr << "Caught STL exception: " << Ex.what() << "\n";
  return 2;
}