diff --git a/async/BufferOrigin.h b/async/BufferOrigin.h new file mode 100644 index 0000000..c5da71a --- /dev/null +++ b/async/BufferOrigin.h @@ -0,0 +1,89 @@ +/** + * @file + * This file is part of ASYNC + * + * @author David Schneller + * + * @copyright Copyright (c) 2024, Technische Universitaet Muenchen. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ASYNC_BUFFERORIGIN_H +#define ASYNC_BUFFERORIGIN_H + +#include +#include +#include +#include + +namespace async { + +/** + * Buffer information send to the executor on each exec and execInit call + */ +class BufferOrigin { + public: + // allocates memory in the buffer allocation zone + virtual void* malloc(size_t size) = 0; + + // frees memory in the buffer allocation zone + virtual void free(void* ptr) = 0; + + // copies memory from the buffer allocation zone to the host + virtual void copyFrom(void* dest, void* source, size_t size) = 0; + + // copies memory from the host to the buffer allocation zone + virtual void copyTo(void* dest, void* source, size_t size) = 0; + + // copies memory from the buffer allocation zone to the buffer allocation zone + virtual void copyBetween(void* dest, void* source, size_t size) = 0; + + // memory can be accessed on host + virtual bool transparentHost() = 0; + + // memory can be directly passed to MPI + virtual bool transparentMPI() = 0; +}; + +class HostBufferOrigin : public BufferOrigin { + public: + void* malloc(size_t size) override { return std::malloc(size); } + void free(void* ptr) override { std::free(ptr); } + void copyTo(void* dest, void* source, size_t size) override { std::memcpy(dest, source, size); } + void copyFrom(void* dest, void* source, size_t size) override { std::memcpy(dest, source, size); } + void copyBetween(void* dest, void* source, size_t size) override { + std::memcpy(dest, source, size); + } + bool transparentHost() override { return true; } + bool transparentMPI() override { return true; } +}; + +} // namespace async + +#endif // ASYNC_BUFFERORIGIN_H diff --git a/async/ExecInfo.h b/async/ExecInfo.h index dda8c72..36404ab 100644 --- a/async/ExecInfo.h +++ b/async/ExecInfo.h @@ -37,6 +37,7 @@ #ifndef ASYNC_EXECINFO_H #define ASYNC_EXECINFO_H +#include "async/BufferOrigin.h" #include #include @@ -50,6 +51,8 @@ class ExecInfo { /** The size for all buffers */ std::vector m_bufferSize; + std::vector m_bufferOrigin; + public: virtual ~ExecInfo() = default; @@ -67,6 +70,11 @@ class ExecInfo { return m_bufferSize[id]; } + BufferOrigin& bufferOrigin(unsigned int id) const { + assert(id < numBuffers()); + return *m_bufferOrigin[id]; + } + /** * @return Read-only pointer to the buffer (Useful for executors.) */ @@ -88,4 +96,4 @@ class ExecInfo { } // namespace async -#endif // ASYNC_EXECINFO_H \ No newline at end of file +#endif // ASYNC_EXECINFO_H diff --git a/async/Module.h b/async/Module.h index 151551a..3c63870 100644 --- a/async/Module.h +++ b/async/Module.h @@ -37,6 +37,8 @@ #ifndef ASYNC_MODULE_H #define ASYNC_MODULE_H +#include "async/BufferOrigin.h" +#include #ifdef USE_MPI #include #endif // USE_MPI @@ -59,23 +61,23 @@ namespace async { template class Module : public ModuleBase { private: - async::as::Base* m_async; + std::unique_ptr> m_async; public: Module() { switch (Config::mode()) { case SYNC: - m_async = new async::as::Sync(); + m_async = std::make_unique>(); break; case THREAD: - m_async = new async::as::Thread(); + m_async = std::make_unique>(); break; case MPI: #ifdef USE_MPI if (Config::useAsyncCopy()) - m_async = new async::as::MPIAsync(); + m_async = std::make_unique>(); else - m_async = new async::as::MPI(); + m_async = std::make_unique>(); #else // USE_MPI logError() << "Asynchronous MPI is not supported."; #endif // USE_MPI @@ -83,8 +85,6 @@ class Module : public ModuleBase { } } - ~Module() override { delete m_async; } - void setExecutor(Executor& executor) { m_async->setExecutor(executor); } bool isAffinityNecessary() { return m_async->isAffinityNecessary(); } @@ -113,6 +113,8 @@ class Module : public ModuleBase { size_t bufferSize(unsigned int id) const { return m_async->bufferSize(id); } + BufferOrigin& bufferOrigin() const { return m_async->bufferOrigin(id); } + template T managedBuffer(unsigned int id) { return static_cast(m_async->managedBuffer(id)); @@ -141,7 +143,7 @@ class Module : public ModuleBase { private: #ifdef USE_MPI - void setScheduler(as::MPIScheduler& scheduler) { m_async->setScheduler(scheduler); } + void setScheduler(as::MPIScheduler& scheduler) override { m_async->setScheduler(scheduler); } #endif // USE_MPI }; diff --git a/async/NoParam.h b/async/NoParam.h index 38aa73a..a4ffaa7 100644 --- a/async/NoParam.h +++ b/async/NoParam.h @@ -47,4 +47,4 @@ struct NoParam {}; } // namespace async -#endif // ASYNC_NOPARAM_H \ No newline at end of file +#endif // ASYNC_NOPARAM_H diff --git a/async/as/Base.h b/async/as/Base.h index 833102a..5ab06ea 100644 --- a/async/as/Base.h +++ b/async/as/Base.h @@ -187,8 +187,6 @@ class Base : public async::ExecInfo { virtual void finalize() { _finalize(); } - virtual const void* buffer(unsigned int id) const = 0; - protected: Executor& executor() { return *m_executor; }