Commit 9f19133f authored by Daniel Campora's avatar Daniel Campora
Browse files

Updated contributing.md.

parent 58b9dc71
......@@ -72,6 +72,7 @@ The newly created `test/CMakeLists.txt` file should reflect the project we are c
```cmake=
file(GLOB test_saxpy "saxpy/src/*cu")
include_directories(saxpy/include)
include_directories(../../stream/handlers/include)
cuda_add_library(Test STATIC
${test_saxpy}
......@@ -81,7 +82,11 @@ cuda_add_library(Test STATIC
Our CUDA algorithm `Saxpy.cuh` and `Saxpy.cu` will be as follows:
```clike=
#include "Handler.cuh"
__global__ void saxpy(float *x, float *y, int n, float a);
ALGORITHM(saxpy, saxpy_t)
```
```clike=
......@@ -93,6 +98,8 @@ __global__ void saxpy(float *x, float *y, int n, float a) {
}
```
The line with `ALGORITHM` encapsulates our algorithm `saxpy` into a class with name `saxpy_t`. We will use this class from now on to be able to refer to our algorithm.
Lastly, edit `stream/CMakeLists.txt` and modify `target_link_libraries`:
```cmake
......@@ -107,23 +114,7 @@ Ready to move on.
Some events from the input will be discarded throughout the execution, and only a fraction of them will be kept for further processing. That is conceptually the idea behind the _High Level Trigger 1_ stage of LHCb, and is what is intended to achieve with this project.
Therefore, we need to add our algorithm to the sequence of algorithms. In order to do that, go to `stream/sequence_setup/include/SequenceArgumentEnum.cuh` and add the algorithm to the `enum seq_enum_t` type as follows:
```clike
/**
* seq_enum_t contains all steps of the sequence in the expected
* order of execution.
*/
enum seq_enum_t {
...
prefix_sum_single_block_velo_track_hit_number,
prefix_sum_scan_velo_track_hit_number,
consolidate_tracks,
saxpy
};
```
Keep in mind the order matters, and will define when your algorithm is scheduled. In this case, we have chosen to add it after the algorithm identified by `consolidate_tracks`. Next, we need to add the __function identifier__ to the algorithms tuple. Our function identifier (the name of the function) is __saxpy__. Go to `stream/sequence_setup/include/SequenceSetup.cuh`:
Therefore, we need to add our algorithm to the sequence of algorithms. In order to do that, go to `stream/sequence_setup/include/ConfiguredSequence.cuh` and add the algorithm to the `SEQUENCE` line as follows:
__Note: Don't forget the `#include` line__
......@@ -132,21 +123,22 @@ __Note: Don't forget the `#include` line__
...
/**
* @brief Algorithm tuple definition. All algorithms in the sequence
* should be added here in the same order as seq_enum_t
* (this condition is checked at compile time).
* Especify here the algorithms to be executed in the sequence,
* in the expected order of execution.
*/
constexpr auto sequence_algorithms() {
return std::make_tuple(
...
prefix_sum_single_block,
prefix_sum_scan,
consolidate_tracks,
saxpy
);
}
SEQUENCE(
...
prefix_sum_reduce_velo_track_hit_number_t,
prefix_sum_single_block_velo_track_hit_number_t,
prefix_sum_scan_velo_track_hit_number_t,
consolidate_tracks_t,
saxpy_t,
...
)
```
Keep in mind the order matters, and will define when your algorithm is scheduled. In this case, we have chosen to add it after the algorithm identified by `consolidate_tracks_t`.
Next, we need to define the arguments to be passed to our function. We need to define them in order for the dynamic scheduling machinery to properly work - that is, allocate what is needed only when it's needed, and manage the memory for us.
We will distinguish arguments just passed by value from pointers to device memory. We don't need to schedule those simply passed by value like `n` and `a`. We care however about `x` and `y`, since they require some reserving and freeing in memory.
......@@ -192,7 +184,7 @@ Finally, we populate the _dependency tree_, ie. where are these arguments needed
```clike
std::vector<std::vector<int>> get_sequence_dependencies() {
...
sequence_dependencies[seq::saxpy] = {
sequence_dependencies[tuple_contains<saxpy_t, sequence_t>::index] = {
arg::dev_x,
arg::dev_y
};
......@@ -201,15 +193,9 @@ std::vector<std::vector<int>> get_sequence_dependencies() {
}
```
Optionally, we can give names to our algorithm and arguments. This will help when debugging ie. the memory manager. `stream/sequence_setup/src/SequenceSetup.cu`:
Optionally, we can give names to our arguments. This will help when debugging ie. the memory manager. `stream/sequence_setup/src/SequenceSetup.cu`:
```clike
std::array<std::string, std::tuple_size<algorithm_tuple_t>::value> get_sequence_names() {
...
a[seq::saxpy] = "Saxpy test";
return a;
}
std::array<std::string, std::tuple_size<argument_tuple_t>::value> get_argument_names() {
...
a[arg::dev_x] = "dev_x";
......@@ -234,19 +220,20 @@ std::vector<int> get_sequence_output_arguments() {
Now all the pieces are in place, we are ready to prepare the algorithm and do the actual invocation.
First go to `stream/sequence/include/Stream.cuh` and add the saxpy host memory pointer:
First go to `stream/sequence/include/HostBuffers.cuh` and add the saxpy host memory pointer:
```clike
...
// Pinned host datatypes
int* host_number_of_tracks;
int* host_accumulated_tracks;
uint* host_velo_tracks_atomics;
uint* host_velo_track_hit_number;
Hit* host_velo_track_hits;
char* host_velo_track_hits;
uint* host_total_number_of_velo_clusters;
uint* host_number_of_reconstructed_velo_tracks;
uint* host_accumulated_number_of_hits_in_velo_tracks;
char* host_velo_states;
uint* host_accumulated_number_of_ut_hits;
// Saxpy
int saxpy_N = 1<<20;
......@@ -255,19 +242,22 @@ First go to `stream/sequence/include/Stream.cuh` and add the saxpy host memory p
...
```
Reserve that host memory in `stream/sequence/src/Stream.cu`:
Reserve that host memory in `stream/sequence/src/HostBuffers.cu`:
```clike
...
// Memory allocations for host memory (copy back)
cudaCheck(cudaMallocHost((void**)&host_number_of_tracks, max_number_of_events * sizeof(int)));
cudaCheck(cudaMallocHost((void**)&host_accumulated_tracks, max_number_of_events * sizeof(int)));
cudaCheck(cudaMallocHost((void**)&host_velo_tracks_atomics, (2 * max_number_of_events + 1) * sizeof(int)));
cudaCheck(cudaMallocHost((void**)&host_velo_track_hit_number, max_number_of_events * VeloTracking::max_tracks * sizeof(uint)));
cudaCheck(cudaMallocHost((void**)&host_velo_track_hits, max_number_of_events * VeloTracking::max_tracks * 20 * sizeof(Hit)));
cudaCheck(cudaMallocHost((void**)&host_velo_track_hits, max_number_of_events * VeloTracking::max_tracks * VeloTracking::max_track_size * sizeof(Velo::Hit)));
cudaCheck(cudaMallocHost((void**)&host_total_number_of_velo_clusters, sizeof(uint)));
cudaCheck(cudaMallocHost((void**)&host_number_of_reconstructed_velo_tracks, sizeof(uint)));
cudaCheck(cudaMallocHost((void**)&host_accumulated_number_of_hits_in_velo_tracks, sizeof(uint)));
cudaCheck(cudaMallocHost((void**)&host_velo_states, max_number_of_events * VeloTracking::max_tracks * sizeof(Velo::State)));
cudaCheck(cudaMallocHost((void**)&host_veloUT_tracks, max_number_of_events * VeloUTTracking::max_num_tracks * sizeof(VeloUTTracking::TrackUT)));
cudaCheck(cudaMallocHost((void**)&host_atomics_veloUT, VeloUTTracking::num_atomics * max_number_of_events * sizeof(int)));
cudaCheck(cudaMallocHost((void**)&host_accumulated_number_of_ut_hits, sizeof(uint)));
cudaCheck(cudaMallocHost((void**)&host_accumulated_number_of_scifi_hits, sizeof(uint)));
// Saxpy memory allocations
cudaCheck(cudaMallocHost((void**)&host_x, saxpy_N * sizeof(float)));
......@@ -276,33 +266,29 @@ Reserve that host memory in `stream/sequence/src/Stream.cu`:
...
```
Finally, go to `stream/sequence/src/StreamSequence.cu` and insert the following code after _Consolidate tracks_:
Finally, create a visitor for your newly created algorithm. Create a containing folder structure for it in `stream/sequence_visitors/test/src/`, and a new file inside named `SaxpyVisitor.cu`. Insert the following code inside:
```clike
...
// Consolidate tracks
arguments.set_size<arg::dev_velo_track_hits>(host_accumulated_number_of_hits_in_velo_tracks[0]);
arguments.set_size<arg::dev_velo_states>(host_number_of_reconstructed_velo_tracks[0]);
scheduler.setup_next(argument_sizes, argument_offsets, sequence_step++);
sequence.set_opts<seq::consolidate_tracks>(dim3(number_of_events), dim3(32), stream);
sequence.set_arguments<seq::consolidate_tracks>(
arguments.offset<arg::dev_atomics_storage>(),
arguments.offset<arg::dev_tracks>(),
arguments.offset<arg::dev_velo_track_hit_number>(),
arguments.offset<arg::dev_velo_cluster_container>(),
arguments.offset<arg::dev_estimated_input_size>(),
arguments.offset<arg::dev_module_cluster_num>(),
arguments.offset<arg::dev_velo_track_hits>(),
arguments.offset<arg::dev_velo_states>()
);
sequence.invoke<seq::consolidate_tracks>();
#include "StreamVisitor.cuh"
#include "Saxpy.cuh"
template<>
void StreamVisitor::visit<saxpy_t>(
saxpy_t& state,
const int sequence_step,
const RuntimeOptions& runtime_options,
const Constants& constants,
ArgumentManager<argument_tuple_t>& arguments,
DynamicScheduler<sequence_t, argument_tuple_t>& scheduler,
HostBuffers& host_buffers,
cudaStream_t& cuda_stream,
cudaEvent_t& cuda_generic_event)
{
// Saxpy test
int saxpy_N = 1<<20;
for (int i = 0; i < saxpy_N; i++) {
host_x[i] = 1.0f;
host_y[i] = 2.0f;
host_buffers.host_x[i] = 1.0f;
host_buffers.host_y[i] = 2.0f;
}
// Set arguments size
......@@ -310,29 +296,30 @@ Finally, go to `stream/sequence/src/StreamSequence.cu` and insert the following
arguments.set_size<arg::dev_y>(saxpy_N);
// Reserve required arguments for this algorithm in the sequence
scheduler.setup_next(argument_sizes, argument_offsets, sequence_step++);
scheduler.setup_next(arguments, sequence_step);
// Copy memory from host to device
cudaCheck(cudaMemcpyAsync(
arguments.offset<arg::dev_x>(),
host_x,
host_buffers.host_x,
saxpy_N * sizeof(float),
cudaMemcpyHostToDevice,
stream
cuda_stream
));
cudaCheck(cudaMemcpyAsync(
arguments.offset<arg::dev_y>(),
host_y,
host_buffers.host_y,
saxpy_N * sizeof(float),
cudaMemcpyHostToDevice,
stream
cuda_stream
));
// Setup opts for kernel call
sequence.set_opts<seq::saxpy>(dim3((saxpy_N+255)/256), dim3(256), stream);
state.set_opts(dim3((saxpy_N+255)/256), dim3(256), cuda_stream);
// Setup arguments for kernel call
sequence.set_arguments<seq::saxpy>(
state.set_arguments(
arguments.offset<arg::dev_x>(),
arguments.offset<arg::dev_y>(),
saxpy_N,
......@@ -340,40 +327,41 @@ Finally, go to `stream/sequence/src/StreamSequence.cu` and insert the following
);
// Kernel call
sequence.invoke<seq::saxpy>();
state.invoke();
// Retrieve result
cudaCheck(cudaMemcpyAsync(host_y,
cudaCheck(cudaMemcpyAsync(
host_buffers.host_y,
arguments.offset<arg::dev_y>(),
arguments.size<arg::dev_y>(),
cudaMemcpyDeviceToHost,
stream
cuda_stream
));
// Wait to receive the result
cudaEventRecord(cuda_generic_event, stream);
cudaEventRecord(cuda_generic_event, cuda_stream);
cudaEventSynchronize(cuda_generic_event);
// Check the output
float maxError = 0.0f;
for (int i = 0; i < saxpy_N; i++) {
maxError = std::max(maxError, abs(host_y[i]-4.0f));
for (int i=0; i<saxpy_N; i++) {
maxError = std::max(maxError, abs(host_buffers.host_y[i]-4.0f));
}
info_cout << "Saxpy max error: " << maxError << std::endl << std::endl;
...
```
We can compile the code and run the program with simple settings, something like `./cu_hlt -f ../input/minbias/velopix_raw -e ../input/minbias/ut_hits -g ../input/geometry/`. If everything went well, the following text should appear:
We can compile the code and run the program `./cu_hlt`. If everything went well, the following text should appear:
```
Saxpy max error: 0.00
```
The cool thing is your algorithm is now part of the sequence. You can see how memory is managed, taking into account your algorithm, and how it changes on every step by appending the `-p` option: `./cu_hlt -f ../input/minbias/velopix_raw -e ../input/minbias/ut_hits -g ../input/geometry/ -p`
The cool thing is your algorithm is now part of the sequence. You can see how memory is managed, taking into account your algorithm, and how it changes on every step by appending the `-p` option: `./cu_hlt -p`
```
Sequence step 13 "Saxpy test" memory segments (MiB):
Sequence step 13 "saxpy_t" memory segments (MiB):
dev_velo_track_hit_number (0.01), unused (0.05), dev_atomics_storage (0.00), unused (1.30), dev_velo_track_hits (0.26), dev_x (4.00), dev_y (4.00), unused (1014.39),
Max memory required: 9.61 MiB
```
......@@ -381,122 +369,3 @@ Max memory required: 9.61 MiB
Now you are ready to take over.
Good luck!
### Bonus: Extending a Handler
Handlers are used internally to deal with each algorithm in the sequence. A Handler deduces the argument types from the kernel function identifier, and exposes the `set_opts` and `set_arguments` methods.
Handlers can be specialized and extended for a particular algorithm. This can be useful under certain situations, ie. if one wants to develop a checker method, or a printout method.
Coming back to Saxpy, we may not want to have the checking code laying around in the `run_sequence` body. Particularly, this code could live somewhere else:
```clike
// Retrieve result
cudaCheck(cudaMemcpyAsync(host_y,
arguments.offset<arg::dev_y>(),
arguments.size<arg::dev_y>(),
cudaMemcpyDeviceToHost,
stream
));
// Wait to receive the result
cudaEventRecord(cuda_generic_event, stream);
cudaEventSynchronize(cuda_generic_event);
// Check the output
float maxError = 0.0f;
for (int i = 0; i < saxpy_N; i++) {
maxError = std::max(maxError, abs(host_y[i]-4.0f));
}
info_cout << "Saxpy max error: " << maxError << std::endl << std::endl;
```
Let's start by specializing a Handler and registering that specialization. Create a new file in `stream/handlers/include/` and name it `HandlerSaxpy.cuh`. These are the contents:
```clike=
#pragma once
#include "../../../main/include/CudaCommon.h"
#include "../../../main/include/Logger.h"
#include "../../sequence_setup/include/SequenceArgumentEnum.cuh"
#include "HandlerDispatcher.cuh"
#include <iostream>
template<typename R, typename... T>
struct HandlerSaxpy : public Handler<seq::saxpy, R, T...> {
HandlerSaxpy() = default;
HandlerSaxpy(R(*param_function)(T...))
: Handler<seq::saxpy, R, T...>(param_function) {}
// Add your own methods
};
// Register partial specialization
template<>
struct HandlerDispatcher<seq::saxpy> {
template<typename R, typename... T>
using H = HandlerSaxpy<R, T...>;
};
```
Next, register that Handler. Modify `HandlerMaker.cuh` and add the Handler we just created:
```clike
// Note: Add here additional custom handlers
#include "HandlerSaxpy.cuh"
```
Now we are ready to extend HandlerSaxpy. The way this works is by using a partial specialization of `HandlerDispatcher` and defining `H` as our specific Handler. `HandlerMaker` takes care of the rest.
We can now add a `check` method to `HandlerSaxpy`:
```clike
// Add your own methods
void check(
float* host_y,
int saxpy_N,
float* dev_y,
size_t dev_y_size,
cudaStream_t& stream,
cudaEvent_t& cuda_generic_event
) {
// Retrieve result
cudaCheck(cudaMemcpyAsync(host_y,
dev_y,
dev_y_size,
cudaMemcpyDeviceToHost,
stream
));
// Wait to receive the result
cudaEventRecord(cuda_generic_event, stream);
cudaEventSynchronize(cuda_generic_event);
// Check the output
float maxError = 0.0f;
for (int i = 0; i < saxpy_N; i++) {
maxError = std::max(maxError, abs(host_y[i]-4.0f));
}
info_cout << "Saxpy max error: " << maxError << std::endl << std::endl;
}
```
And refactor `StreamSequence.cu` to reflect this change:
```clike
// Kernel call
sequence.invoke<seq::saxpy>();
// Check result
sequence.item<seq::saxpy>().check(
host_y,
saxpy_N,
arguments.offset<arg::dev_y>(),
arguments.size<arg::dev_y>(),
stream,
cuda_generic_event
);
```
Now you are a `cuda_hlt` hacker.
......@@ -11,7 +11,7 @@
*/
#define ALGORITHM(FUNCTION_NAME, EXPOSED_TYPE_NAME) \
struct EXPOSED_TYPE_NAME {\
constexpr static auto name {#FUNCTION_NAME};\
constexpr static auto name {#EXPOSED_TYPE_NAME};\
decltype(HandlerMaker::make_handler(FUNCTION_NAME)) handler {FUNCTION_NAME};\
void set_opts(\
const dim3& param_num_blocks,\
......
#pragma once
#include "RuntimeOptions.h"
#include "Logger.h"
#include "Constants.cuh"
#include "Argument.cuh"
#include "HostBuffers.cuh"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment