| Jason Henline | ac232dd | 2016-10-25 20:18:56 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 17 | namespace acxxel { |
| 18 | |
| 19 | namespace cuda { |
| 20 | Expected<Platform *> getPlatform(); |
| 21 | } // namespace cuda |
| 22 | |
| 23 | namespace opencl { |
| 24 | Expected<Platform *> getPlatform(); |
| 25 | } // namespace opencl |
| 26 | |
| 27 | void logWarning(const std::string &Message) { |
| 28 | std::cerr << "WARNING: " << Message << "\n"; |
| 29 | } |
| 30 | |
| 31 | Expected<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 | |
| 39 | Expected<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 | |
| 47 | Stream::Stream(Stream &&) noexcept = default; |
| 48 | Stream &Stream::operator=(Stream &&) noexcept = default; |
| 49 | |
| 50 | Status Stream::sync() { |
| 51 | return takeStatusOr(ThePlatform->streamSync(TheHandle.get())); |
| 52 | } |
| 53 | |
| 54 | Status Stream::waitOnEvent(Event &Event) { |
| 55 | return takeStatusOr(ThePlatform->streamWaitOnEvent( |
| 56 | TheHandle.get(), ThePlatform->getEventHandle(Event))); |
| 57 | } |
| 58 | |
| 59 | Stream & |
| 60 | Stream::addCallback(std::function<void(Stream &, const Status &)> Callback) { |
| 61 | setStatus(ThePlatform->addStreamCallback(*this, std::move(Callback))); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | Stream &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 | |
| 76 | Stream &Stream::enqueueEvent(Event &E) { |
| 77 | setStatus(ThePlatform->enqueueEvent(ThePlatform->getEventHandle(E), |
| 78 | TheHandle.get())); |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | Event::Event(Event &&) noexcept = default; |
| 83 | Event &Event::operator=(Event &&) noexcept = default; |
| 84 | |
| 85 | bool Event::isDone() { return ThePlatform->eventIsDone(TheHandle.get()); } |
| 86 | |
| 87 | Status Event::sync() { return ThePlatform->eventSync(TheHandle.get()); } |
| 88 | |
| 89 | Expected<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 | |
| 97 | Expected<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 | |
| 106 | Program::Program(Program &&) noexcept = default; |
| 107 | Program &Program::operator=(Program &&That) noexcept = default; |
| 108 | |
| 109 | Kernel::Kernel(Kernel &&) noexcept = default; |
| 110 | Kernel &Kernel::operator=(Kernel &&That) noexcept = default; |
| 111 | |
| 112 | } // namespace acxxel |