blob: fa15c95ce8bc187636b857e2fc130d2a0ded7fcb [file] [log] [blame]
Jason Henlineac232dd2016-10-25 20:18:56 +00001//===--- acxxel.cpp - Implementation details for the Acxxel API -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "acxxel.h"
11#include "config.h"
12
13#include <algorithm>
14#include <iostream>
15#include <string>
16
17namespace acxxel {
18
19namespace cuda {
20Expected<Platform *> getPlatform();
21} // namespace cuda
22
23namespace opencl {
24Expected<Platform *> getPlatform();
25} // namespace opencl
26
27void logWarning(const std::string &Message) {
28 std::cerr << "WARNING: " << Message << "\n";
29}
30
31Expected<Platform *> getCUDAPlatform() {
32#ifdef ACXXEL_ENABLE_CUDA
33 return cuda::getPlatform();
34#else
35 return Status("library was build without CUDA support");
36#endif
37}
38
39Expected<Platform *> getOpenCLPlatform() {
40#ifdef ACXXEL_ENABLE_OPENCL
41 return opencl::getPlatform();
42#else
43 return Status("library was build without OpenCL support");
44#endif
45}
46
47Stream::Stream(Stream &&) noexcept = default;
48Stream &Stream::operator=(Stream &&) noexcept = default;
49
50Status Stream::sync() {
51 return takeStatusOr(ThePlatform->streamSync(TheHandle.get()));
52}
53
54Status Stream::waitOnEvent(Event &Event) {
55 return takeStatusOr(ThePlatform->streamWaitOnEvent(
56 TheHandle.get(), ThePlatform->getEventHandle(Event)));
57}
58
59Stream &
60Stream::addCallback(std::function<void(Stream &, const Status &)> Callback) {
61 setStatus(ThePlatform->addStreamCallback(*this, std::move(Callback)));
62 return *this;
63}
64
65Stream &Stream::asyncKernelLaunch(const Kernel &TheKernel,
66 KernelLaunchDimensions LaunchDimensions,
67 Span<void *> Arguments,
68 Span<size_t> ArgumentSizes,
69 size_t SharedMemoryBytes) {
70 setStatus(ThePlatform->rawEnqueueKernelLaunch(
71 TheHandle.get(), TheKernel.TheHandle.get(), LaunchDimensions, Arguments,
72 ArgumentSizes, SharedMemoryBytes));
73 return *this;
74}
75
76Stream &Stream::enqueueEvent(Event &E) {
77 setStatus(ThePlatform->enqueueEvent(ThePlatform->getEventHandle(E),
78 TheHandle.get()));
79 return *this;
80}
81
82Event::Event(Event &&) noexcept = default;
83Event &Event::operator=(Event &&) noexcept = default;
84
85bool Event::isDone() { return ThePlatform->eventIsDone(TheHandle.get()); }
86
87Status Event::sync() { return ThePlatform->eventSync(TheHandle.get()); }
88
89Expected<float> Event::getSecondsSince(const Event &Previous) {
90 Expected<float> MaybeSeconds = ThePlatform->getSecondsBetweenEvents(
91 Previous.TheHandle.get(), TheHandle.get());
92 if (MaybeSeconds.isError())
93 MaybeSeconds.getError();
94 return MaybeSeconds;
95}
96
97Expected<Kernel> Program::createKernel(const std::string &Name) {
98 Expected<void *> MaybeKernelHandle =
99 ThePlatform->rawCreateKernel(TheHandle.get(), Name);
100 if (MaybeKernelHandle.isError())
101 return MaybeKernelHandle.getError();
102 return Kernel(ThePlatform, MaybeKernelHandle.getValue(),
103 ThePlatform->getKernelHandleDestructor());
104}
105
106Program::Program(Program &&) noexcept = default;
107Program &Program::operator=(Program &&That) noexcept = default;
108
109Kernel::Kernel(Kernel &&) noexcept = default;
110Kernel &Kernel::operator=(Kernel &&That) noexcept = default;
111
112} // namespace acxxel