-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend the documentation with more information about multidimensional ranges #1569
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f478845
Extend the documentation with more information about multidimensional…
akukanov 9f51006
Fix the sample test.
akukanov 9a0fc35
Fix a typo
akukanov 368859a
Address review feedback
akukanov 0534363
Apply suggestions from code review
akukanov 7017e59
Improve with the help of review feedback
akukanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
doc/main/tbb_userguide/snippets/blocked_nd_range_example.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "blocked_nd_range_example.h" | ||
#include <vector> | ||
#include <cassert> | ||
|
||
int main() { | ||
const int kernel_length = 9; | ||
const int kernel_width = 5; | ||
const int kernel_height = 5; | ||
|
||
const int feature_maps_length = 128; | ||
const int feature_maps_width = 16; | ||
const int feature_maps_heigth = 16; | ||
|
||
const int out_length = feature_maps_length - kernel_length + 1; | ||
const int out_width = feature_maps_width - kernel_width + 1; | ||
const int out_heigth = feature_maps_heigth - kernel_height + 1; | ||
|
||
// Initializes feature maps with 1 in each cell and out with zeros. | ||
std::vector<std::vector<std::vector<float>>> feature_maps(feature_maps_length, std::vector<std::vector<float>>(feature_maps_width, std::vector<float>(feature_maps_heigth, 1.0f))); | ||
std::vector<std::vector<std::vector<float>>> out(out_length, std::vector<std::vector<float>>(out_width, std::vector<float>(out_heigth, 0.f))); | ||
|
||
// 3D convolution calculates the sum of all elements in the kernel | ||
convolution3d(feature_maps, out, | ||
out_length, out_width, out_heigth, | ||
kernel_length, kernel_width, kernel_height); | ||
|
||
// Checks correctness of convolution by equality to the expected sum of elements | ||
float expected = float(kernel_length * kernel_height * kernel_width); | ||
for (auto i : out) { | ||
for (auto j : i) { | ||
for (auto k : j) { | ||
assert(k == expected && "convolution failed to calculate correctly"); | ||
} | ||
} | ||
} | ||
return 0; | ||
} |
37 changes: 37 additions & 0 deletions
37
doc/main/tbb_userguide/snippets/blocked_nd_range_example.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "oneapi/tbb/blocked_nd_range.h" | ||
#include "oneapi/tbb/parallel_for.h" | ||
|
||
template<typename Features> | ||
float kernel3d(const Features& feature_maps, int i, int j, int k, | ||
int kernel_length, int kernel_width, int kernel_height) { | ||
float result = 0.f; | ||
|
||
for (int feature_i = i; feature_i < i + kernel_length; ++feature_i) | ||
for (int feature_j = j; feature_j < j + kernel_width; ++feature_j) | ||
for (int feature_k = k; feature_k < k + kernel_width; ++feature_k) | ||
result += feature_maps[feature_i][feature_j][feature_k]; | ||
|
||
return result; | ||
} | ||
|
||
template<typename Features, typename Output> | ||
void convolution3d(const Features& feature_maps, Output& out, | ||
int out_length, int out_width, int out_heigth, | ||
int kernel_length, int kernel_width, int kernel_height) { | ||
using range_t = oneapi::tbb::blocked_nd_range<int, 3>; | ||
|
||
oneapi::tbb::parallel_for( | ||
range_t({0, out_length}, {0, out_width}, {0, out_heigth}), | ||
[&](const range_t& out_range) { | ||
auto out_x = out_range.dim(0); | ||
auto out_y = out_range.dim(1); | ||
auto out_z = out_range.dim(2); | ||
|
||
for (int i = out_x.begin(); i < out_x.end(); ++i) | ||
for (int j = out_y.begin(); j < out_y.end(); ++j) | ||
for (int k = out_z.begin(); k < out_z.end(); ++k) | ||
out[i][j][k] = kernel3d(feature_maps, i, j, k, | ||
kernel_length, kernel_width, kernel_height); | ||
} | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that I got it right, but maybe smth like:
The
blocked_nd_range<T,N>
class template represents a blocked iteration space of any dimensionality. However, unlike other classes, all dimensions inblocked_nd_range
must share the same value type. Instead of specifying boundary values directly, you passN
instances ofblocked_range<T>
to the constructor. This difference is also reflected in its different naming pattern.