Skip to content

Commit

Permalink
Adding BufferOrigin class
Browse files Browse the repository at this point in the history
  • Loading branch information
davschneller committed May 20, 2024
1 parent 3c2bb45 commit 40f554d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 12 deletions.
89 changes: 89 additions & 0 deletions async/BufferOrigin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @file
* This file is part of ASYNC
*
* @author David Schneller <[email protected]>
*
* @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 <cassert>
#include <cstdlib>
#include <cstring>
#include <vector>

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
10 changes: 9 additions & 1 deletion async/ExecInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#ifndef ASYNC_EXECINFO_H
#define ASYNC_EXECINFO_H

#include "async/BufferOrigin.h"
#include <cassert>
#include <vector>

Expand All @@ -50,6 +51,8 @@ class ExecInfo {
/** The size for all buffers */
std::vector<size_t> m_bufferSize;

std::vector<BufferOrigin*> m_bufferOrigin;

public:
virtual ~ExecInfo() = default;

Expand All @@ -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.)
*/
Expand All @@ -88,4 +96,4 @@ class ExecInfo {

} // namespace async

#endif // ASYNC_EXECINFO_H
#endif // ASYNC_EXECINFO_H
18 changes: 10 additions & 8 deletions async/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#ifndef ASYNC_MODULE_H
#define ASYNC_MODULE_H

#include "async/BufferOrigin.h"
#include <memory>
#ifdef USE_MPI
#include <mpi.h>
#endif // USE_MPI
Expand All @@ -59,32 +61,30 @@ namespace async {
template <class Executor, typename InitParameter, typename Parameter>
class Module : public ModuleBase {
private:
async::as::Base<Executor, InitParameter, Parameter>* m_async;
std::unique_ptr<async::as::Base<Executor, InitParameter, Parameter>> m_async;

public:
Module() {
switch (Config::mode()) {
case SYNC:
m_async = new async::as::Sync<Executor, InitParameter, Parameter>();
m_async = std::make_unique<async::as::Sync<Executor, InitParameter, Parameter>>();
break;
case THREAD:
m_async = new async::as::Thread<Executor, InitParameter, Parameter>();
m_async = std::make_unique<async::as::Thread<Executor, InitParameter, Parameter>>();
break;
case MPI:
#ifdef USE_MPI
if (Config::useAsyncCopy())
m_async = new async::as::MPIAsync<Executor, InitParameter, Parameter>();
m_async = std::make_unique<async::as::MPIAsync<Executor, InitParameter, Parameter>>();
else
m_async = new async::as::MPI<Executor, InitParameter, Parameter>();
m_async = std::make_unique<async::as::MPI<Executor, InitParameter, Parameter>>();
#else // USE_MPI
logError() << "Asynchronous MPI is not supported.";
#endif // USE_MPI
break;
}
}

~Module() override { delete m_async; }

void setExecutor(Executor& executor) { m_async->setExecutor(executor); }

bool isAffinityNecessary() { return m_async->isAffinityNecessary(); }
Expand Down Expand Up @@ -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 <typename T>
T managedBuffer(unsigned int id) {
return static_cast<T>(m_async->managedBuffer(id));
Expand Down Expand Up @@ -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
};

Expand Down
2 changes: 1 addition & 1 deletion async/NoParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ struct NoParam {};

} // namespace async

#endif // ASYNC_NOPARAM_H
#endif // ASYNC_NOPARAM_H
2 changes: 0 additions & 2 deletions async/as/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down

0 comments on commit 40f554d

Please sign in to comment.