Skip to content

Commit

Permalink
Merge pull request #88 from bwvdnbro/task-refactoring
Browse files Browse the repository at this point in the history
Task-based algorithm refactoring
  • Loading branch information
bwvdnbro authored Jul 27, 2020
2 parents a77e87a + feb9056 commit 1792388
Show file tree
Hide file tree
Showing 20 changed files with 1,821 additions and 1,441 deletions.
1 change: 1 addition & 0 deletions benchmarks/bondi.param
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ BondiProfile:
# Set up a mask for the central region of the box where the Bondi profile is
# divergent
HydroMask:
type: BlockSyntax
filename: bondi.yml

# RHD simulation parameters
Expand Down
1 change: 0 additions & 1 deletion src/CMacIonize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ int main(int argc, char **argv) {
parser.get_value< int_fast32_t >("threads"),
parser.get_value< std::string >("params"),
parser.get_value< bool >("task-plot"),
parser.get_value< bool >("verbose"),
!parser.get_value< bool >("no-initial-output"), log);

if (parser.get_value< bool >("dry-run")) {
Expand Down
136 changes: 136 additions & 0 deletions src/FlushContinuousPhotonBuffersTaskContext.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*******************************************************************************
* This file is part of CMacIonize
* Copyright (C) 2020 Bert Vandenbroucke ([email protected])
*
* CMacIonize is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CMacIonize is distributed in the hope that it will be useful,
* but WITOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with CMacIonize. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

/**
* @file FlushContinuousPhotonBuffersTaskContext.hpp
*
* @brief Task context responsible for flushing the continuous source photon
* buffers.
*
* @author Bert Vandenbroucke ([email protected])
*/
#ifndef FLUSHCONTINUOUSPHOTONBUFFERSTASKCONTEXT_HPP
#define FLUSHCONTINUOUSPHOTONBUFFERSTASKCONTEXT_HPP

#include "DensitySubGridCreator.hpp"
#include "MemorySpace.hpp"
#include "Task.hpp"
#include "TaskContext.hpp"
#include "TaskQueue.hpp"

/**
* @brief Task context responsible for flushing the continuous source photon
* buffers.
*/
class FlushContinuousPhotonBuffersTaskContext : public TaskContext {
private:
/*! @brief Photon buffer array. */
MemorySpace &_buffers;

/*! @brief Grid creator. */
DensitySubGridCreator< DensitySubGrid > &_grid_creator;

/*! @brief Task space. */
ThreadSafeVector< Task > &_tasks;

/*! @brief Buffers used to store sourced photon packets. */
std::vector< std::vector< PhotonBuffer > > &_continuous_buffers;

/*! @brief Queues per thread. */
std::vector< TaskQueue * > &_queues;

public:
/**
* @brief Constructor.
*
* @param buffers Photon buffer array.
* @param grid_creator Grid creator.
* @param tasks Task space.
* @param continuous_buffers Buffers used to store continuous photon packets.
* @param queues Queues per thread.
*/
inline FlushContinuousPhotonBuffersTaskContext(
MemorySpace &buffers,
DensitySubGridCreator< DensitySubGrid > &grid_creator,
ThreadSafeVector< Task > &tasks,
std::vector< std::vector< PhotonBuffer > > &continuous_buffers,
std::vector< TaskQueue * > &queues)
: _buffers(buffers), _grid_creator(grid_creator), _tasks(tasks),
_continuous_buffers(continuous_buffers), _queues(queues) {}

/**
* @brief Execute a continuous photon buffer flush task.
*
* @param thread_id ID of the thread that executes the task.
* @param thread_context Thread specific context for the task.
* @param tasks_to_add Array with indices of newly created tasks.
* @param queues_to_add Array with target queue indices for the newly created
* tasks.
* @param task Task to execute.
* @return Number of new tasks created by the task.
*/
inline uint_fast32_t execute(const int_fast32_t thread_id,
ThreadContext *thread_context,
uint_fast32_t *tasks_to_add,
int_fast32_t *queues_to_add, Task &task) {

const uint_fast32_t source_copy = task.get_subgrid();
for (uint_fast32_t i = 0; i < _continuous_buffers[source_copy].size();
++i) {
if (_continuous_buffers[source_copy][i].size() > 0) {
// yes: send the buffer off!
uint_fast32_t buffer_index = _buffers.get_free_buffer();
PhotonBuffer &input_buffer = _buffers[buffer_index];

// set general buffer information
input_buffer.grow(_continuous_buffers[source_copy][i].size());
input_buffer.set_subgrid_index(i);
input_buffer.set_direction(TRAVELDIRECTION_INSIDE);

// copy over the photons
for (uint_fast32_t iphoton = 0;
iphoton < _continuous_buffers[source_copy][i].size(); ++iphoton) {
input_buffer[iphoton] = _continuous_buffers[source_copy][i][iphoton];
}

// reset the active buffer
_continuous_buffers[source_copy][i].reset();

// add to the queue of the corresponding thread
DensitySubGrid &subgrid = *_grid_creator.get_subgrid(i);
const size_t task_index = _tasks.get_free_element();
Task &new_task = _tasks[task_index];
new_task.set_type(TASKTYPE_PHOTON_TRAVERSAL);
new_task.set_subgrid(i);
new_task.set_buffer(buffer_index);

// add dependency for task:
// - subgrid
// (the output buffers belong to the subgrid and do not count
// as a dependency)
new_task.set_dependency(subgrid.get_dependency());

_queues[subgrid.get_owning_thread()]->add_task(task_index);
}
}

return 0;
}
};

#endif // FLUSHCONTINUOUSPHOTONBUFFERSTASKCONTEXT_HPP
30 changes: 15 additions & 15 deletions src/HydroBoundary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class InflowHydroBoundary : public HydroBoundary {

HydroVariables right_state;

for (uint_fast8_t i = 0; i < 5; ++i) {
right_state.primitives(i) = left_state.primitives(i);
for (uint_fast8_t j = 0; j < 5; ++j) {
right_state.primitives(j) = left_state.primitives(j);
}

return right_state;
Expand All @@ -116,9 +116,9 @@ class InflowHydroBoundary : public HydroBoundary {

HydroVariables right_state;

for (uint_fast8_t i = 0; i < 5; ++i) {
right_state.primitives(i) = left_state.primitives(i);
right_state.primitive_gradients(i) = left_state.primitive_gradients(i);
for (uint_fast8_t j = 0; j < 5; ++j) {
right_state.primitives(j) = left_state.primitives(j);
right_state.primitive_gradients(j) = left_state.primitive_gradients(j);
}

return right_state;
Expand Down Expand Up @@ -146,8 +146,8 @@ class OutflowHydroBoundary : public HydroBoundary {

HydroVariables right_state;

for (uint_fast8_t i = 0; i < 5; ++i) {
right_state.primitives(i) = left_state.primitives(i);
for (uint_fast8_t j = 0; j < 5; ++j) {
right_state.primitives(j) = left_state.primitives(j);
}
// we need to reverse the velocity component aligned with the surface
// normal, but only if it is entering the box
Expand Down Expand Up @@ -175,9 +175,9 @@ class OutflowHydroBoundary : public HydroBoundary {

HydroVariables right_state;

for (uint_fast8_t i = 0; i < 5; ++i) {
right_state.primitives(i) = left_state.primitives(i);
right_state.primitive_gradients(i) = left_state.primitive_gradients(i);
for (uint_fast8_t j = 0; j < 5; ++j) {
right_state.primitives(j) = left_state.primitives(j);
right_state.primitive_gradients(j) = left_state.primitive_gradients(j);
}
// we need to reverse the velocity component aligned with the surface
// normal, but only if it is entering the box
Expand Down Expand Up @@ -211,8 +211,8 @@ class ReflectiveHydroBoundary : public HydroBoundary {

HydroVariables right_state;

for (uint_fast8_t i = 0; i < 5; ++i) {
right_state.primitives(i) = left_state.primitives(i);
for (uint_fast8_t j = 0; j < 5; ++j) {
right_state.primitives(j) = left_state.primitives(j);
}
// we need to reverse the velocity component aligned with the surface normal
right_state.primitives(1 + i) = -right_state.primitives(1 + i);
Expand All @@ -237,9 +237,9 @@ class ReflectiveHydroBoundary : public HydroBoundary {

HydroVariables right_state;

for (uint_fast8_t i = 0; i < 5; ++i) {
right_state.primitives(i) = left_state.primitives(i);
right_state.primitive_gradients(i) = left_state.primitive_gradients(i);
for (uint_fast8_t j = 0; j < 5; ++j) {
right_state.primitives(j) = left_state.primitives(j);
right_state.primitive_gradients(j) = left_state.primitive_gradients(j);
}
// we need to reverse the velocity component aligned with the surface normal
right_state.primitives(1 + i) = -right_state.primitives(1 + i);
Expand Down
2 changes: 1 addition & 1 deletion src/MemorySpace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MemorySpace {
* @return The index of the last buffer we copied into. If this index is not
* the same as the original index, the original index should be queued.
*/
inline size_t add_photons(const size_t index, PhotonBuffer &buffer) {
inline size_t add_photons(const size_t index, const PhotonBuffer &buffer) {

PhotonBuffer &buffer_target = _memory_space[index];
const uint_fast32_t size_in = buffer.size();
Expand Down
4 changes: 4 additions & 0 deletions src/PhotonPacketStatistics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*
* @author Bert Vandenbroucke ([email protected])
*/
#ifndef PHOTONPACKETSTATISTICS_HPP
#define PHOTONPACKETSTATISTICS_HPP

#include "AtomicValue.hpp"
#include "ParameterFile.hpp"
Expand Down Expand Up @@ -93,3 +95,5 @@ class PhotonPacketStatistics {
}
}
};

#endif // PHOTONPACKETSTATISTICS_HPP
Loading

0 comments on commit 1792388

Please sign in to comment.