Skip to content

Commit

Permalink
Merge branch 'codegen' into 'master'
Browse files Browse the repository at this point in the history
Fix memory alignment bug in PackInfo codegen

See merge request walberla/walberla!698
  • Loading branch information
Markus Holzer committed Nov 13, 2024
2 parents e564829 + bd5adb9 commit b54b65c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
12 changes: 10 additions & 2 deletions python/pystencils_walberla/templates/CpuPackInfo.tmpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "domain_decomposition/IBlock.h"
#include "communication/UniformPackInfo.h"

#include <memory>

#define FUNC_PREFIX

#ifdef __GNUC__
Expand Down Expand Up @@ -52,7 +54,10 @@ class {{class_name}} : public ::walberla::communication::UniformPackInfo

void unpackData(IBlock * receiver, stencil::Direction dir, mpi::RecvBuffer & buffer) {
const auto dataSize = size(dir, receiver);
unpack(dir, buffer.skip(dataSize), receiver);
auto bufferSize = dataSize + sizeof({{dtype}});
auto bufferPtr = reinterpret_cast<void*>(buffer.skip(bufferSize));
std::align(alignof({{dtype}}), dataSize, bufferPtr, bufferSize);
unpack(dir, reinterpret_cast<unsigned char*>(bufferPtr), receiver);
}

void communicateLocal(const IBlock * sender, IBlock * receiver, stencil::Direction dir) {
Expand All @@ -65,7 +70,10 @@ class {{class_name}} : public ::walberla::communication::UniformPackInfo

void packDataImpl(const IBlock * sender, stencil::Direction dir, mpi::SendBuffer & outBuffer) const {
const auto dataSize = size(dir, sender);
pack(dir, outBuffer.forward(dataSize), const_cast<IBlock*>(sender));
auto bufferSize = dataSize + sizeof({{dtype}});
auto bufferPtr = reinterpret_cast<void*>(outBuffer.forward(bufferSize));
std::align(alignof({{dtype}}), dataSize, bufferPtr, bufferSize);
pack(dir, reinterpret_cast<unsigned char*>(bufferPtr), const_cast<IBlock *>(sender));
}

void pack (stencil::Direction dir, unsigned char * buffer, IBlock * block) const;
Expand Down
2 changes: 1 addition & 1 deletion python/pystencils_walberla/templates/GpuPackInfo.tmpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class {{class_name}} : public ::walberla::gpu::GeneratedGPUPackInfo
void pack (stencil::Direction dir, unsigned char * buffer, IBlock * block, gpuStream_t stream) override;
void communicateLocal ( stencil::Direction /*dir*/, const IBlock* /* sender */, IBlock* /* receiver */, gpuStream_t /* stream */ ) override
{
WALBERLA_ABORT("Local Communication not implemented yet for standard PackInfos. To run your application turn of local communication in the Communication class")
WALBERLA_ABORT("Local Communication not implemented yet for standard PackInfos. To run your application, turn off local communication in the communication class, e.g. with useLocalCommunication=false")
}
void unpack(stencil::Direction dir, unsigned char * buffer, IBlock * block, gpuStream_t stream) override;
uint_t size (stencil::Direction dir, IBlock * block) override;
Expand Down
8 changes: 8 additions & 0 deletions src/communication/UniformPackInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class UniformPackInfo
*
* If NOT thread-safe, \ref threadsafeReceiving must return false!
*
* Implementations must reserve extra space and advance the send buffer
* pointer according to the alignment of the ghost layer data type!
* The buffer is char-aligned.
*
* @param receiver the block where the unpacked data should be stored into
* @param dir receive data from neighbor in this direction
* @param buffer buffer for reading the data from
Expand Down Expand Up @@ -145,6 +149,10 @@ class UniformPackInfo
*
* Must be thread-safe!
*
* Implementations must reserve extra space and advance the send buffer
* pointer according to the alignment of the ghost layer data type!
* The buffer is char-aligned.
*
* @param sender the block whose data should be packed into a buffer
* @param dir pack data for neighbor in this direction
* @param buffer buffer for writing the data into
Expand Down

0 comments on commit b54b65c

Please sign in to comment.