acxxel.cpp
3.36 KB
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
//===--- acxxel.cpp - Implementation details for the Acxxel API -----------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "acxxel.h"
#include "config.h"
#include <algorithm>
#include <iostream>
#include <string>
namespace acxxel {
namespace cuda {
Expected<Platform *> getPlatform();
} // namespace cuda
namespace opencl {
Expected<Platform *> getPlatform();
} // namespace opencl
void logWarning(const std::string &Message) {
std::cerr << "WARNING: " << Message << "\n";
}
Expected<Platform *> getCUDAPlatform() {
#ifdef ACXXEL_ENABLE_CUDA
return cuda::getPlatform();
#else
return Status("library was build without CUDA support");
#endif
}
Expected<Platform *> getOpenCLPlatform() {
#ifdef ACXXEL_ENABLE_OPENCL
return opencl::getPlatform();
#else
return Status("library was build without OpenCL support");
#endif
}
Stream::Stream(Stream &&) noexcept = default;
Stream &Stream::operator=(Stream &&) noexcept = default;
Status Stream::sync() {
return takeStatusOr(ThePlatform->streamSync(TheHandle.get()));
}
Status Stream::waitOnEvent(Event &Event) {
return takeStatusOr(ThePlatform->streamWaitOnEvent(
TheHandle.get(), ThePlatform->getEventHandle(Event)));
}
Stream &
Stream::addCallback(std::function<void(Stream &, const Status &)> Callback) {
setStatus(ThePlatform->addStreamCallback(*this, std::move(Callback)));
return *this;
}
Stream &Stream::asyncKernelLaunch(const Kernel &TheKernel,
KernelLaunchDimensions LaunchDimensions,
Span<void *> Arguments,
Span<size_t> ArgumentSizes,
size_t SharedMemoryBytes) {
setStatus(ThePlatform->rawEnqueueKernelLaunch(
TheHandle.get(), TheKernel.TheHandle.get(), LaunchDimensions, Arguments,
ArgumentSizes, SharedMemoryBytes));
return *this;
}
Stream &Stream::enqueueEvent(Event &E) {
setStatus(ThePlatform->enqueueEvent(ThePlatform->getEventHandle(E),
TheHandle.get()));
return *this;
}
Event::Event(Event &&) noexcept = default;
Event &Event::operator=(Event &&) noexcept = default;
bool Event::isDone() { return ThePlatform->eventIsDone(TheHandle.get()); }
Status Event::sync() { return ThePlatform->eventSync(TheHandle.get()); }
Expected<float> Event::getSecondsSince(const Event &Previous) {
Expected<float> MaybeSeconds = ThePlatform->getSecondsBetweenEvents(
Previous.TheHandle.get(), TheHandle.get());
if (MaybeSeconds.isError())
MaybeSeconds.getError();
return MaybeSeconds;
}
Expected<Kernel> Program::createKernel(const std::string &Name) {
Expected<void *> MaybeKernelHandle =
ThePlatform->rawCreateKernel(TheHandle.get(), Name);
if (MaybeKernelHandle.isError())
return MaybeKernelHandle.getError();
return Kernel(ThePlatform, MaybeKernelHandle.getValue(),
ThePlatform->getKernelHandleDestructor());
}
Program::Program(Program &&) noexcept = default;
Program &Program::operator=(Program &&That) noexcept = default;
Kernel::Kernel(Kernel &&) noexcept = default;
Kernel &Kernel::operator=(Kernel &&That) noexcept = default;
} // namespace acxxel