Skip to content

Commit 96039d6

Browse files
committed
add use_cuda option to Example_compute_gradient
1 parent 27559e3 commit 96039d6

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

examples/Example_compute_gradient.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,29 @@ int main(int argc, char **argv) {
5959
ParticleData<float> output;
6060
std::vector<float> deltas = {options.dy, options.dx, options.dz};
6161

62-
if(options.sobel) {
63-
APRNumerics::gradient_magnitude_sobel(apr, parts, output, deltas);
64-
} else {
65-
APRNumerics::gradient_magnitude_cfd(apr, parts, output, deltas);
62+
bool done = false;
63+
64+
if(options.use_cuda) {
65+
#ifdef APR_USE_CUDA
66+
auto access = apr.gpuAPRHelper();
67+
auto tree_access = apr.gpuTreeHelper();
68+
if(options.sobel) {
69+
APRNumericsGPU::gradient_magnitude_sobel(access, tree_access, parts.data, output.data, deltas);
70+
} else {
71+
APRNumericsGPU::gradient_magnitude_cfd(access, tree_access, parts.data, output.data, deltas);
72+
}
73+
done = true;
74+
#else
75+
std::cout << "Option -use_cuda was given, but LibAPR was not built with CUDA enabled. Using CPU implementation." << std::endl;
76+
#endif
77+
}
78+
79+
if(!done) {
80+
if (options.sobel) {
81+
APRNumerics::gradient_magnitude_sobel(apr, parts, output, deltas);
82+
} else {
83+
APRNumerics::gradient_magnitude_cfd(apr, parts, output, deltas);
84+
}
6685
}
6786
timer.stop_timer();
6887

@@ -144,6 +163,11 @@ cmdLineOptions read_command_line_options(int argc, char **argv){
144163
result.dz = std::stof(get_command_option(argv, argv + argc, "-dz"));
145164
}
146165

166+
if(command_option_exists(argv, argv + argc, "-use_cuda"))
167+
{
168+
result.use_cuda = true;
169+
}
170+
147171

148172
return result;
149173

examples/Example_compute_gradient.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "numerics/APRNumerics.hpp"
1818
#include "numerics/APRReconstruction.hpp"
1919

20+
#ifdef APR_USE_CUDA
21+
#include "numerics/APRNumericsGPU.hpp"
22+
#endif
23+
2024
struct cmdLineOptions{
2125
std::string output = "";
2226
std::string stats = "";
@@ -26,6 +30,7 @@ struct cmdLineOptions{
2630
float dx = 1.0f;
2731
float dy = 1.0f;
2832
float dz = 1.0f;
33+
bool use_cuda = false;
2934
};
3035

3136
cmdLineOptions read_command_line_options(int argc, char **argv);

0 commit comments

Comments
 (0)