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