Skip to content

Commit e776e50

Browse files
committed
[oneAPI samples][Fourier correlation] Suggested changes to Fourier correlation samples
1 parent 7e7073f commit e776e50

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

Libraries/oneMKL/fourier_correlation/fcorr_1d_buffers.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ naive_cross_correlation(sycl::queue& Q,
4545
}
4646

4747
int main(int argc, char **argv) {
48-
unsigned int N = (argc == 1) ? 32 : std::stoi(argv[1]);
48+
const unsigned int N = (argc > 1) ? std::stoi(argv[1]) : 32;
4949
// N >= 8 required for the arbitrary signals as defined herein
50-
if (N < 8)
51-
throw std::invalid_argument("The input value N must be 8 or greater.");
50+
if (N < 8 || N > INT_MAX)
51+
throw std::invalid_argument("The period of the signal, chosen as input of "
52+
"the program, must be 8 or greater.");
5253

5354
// Let s be an integer s.t. 0 <= s < N and let
5455
// corr[s] = \sum_{j = 0}^{N-1} sig1[j] sig2[(j - s + N) mod N]
@@ -67,12 +68,15 @@ int main(int argc, char **argv) {
6768
// Initialize signal and correlation buffers. The buffers must be large enough
6869
// to store the forward and backward domains' data, consisting of N real
6970
// values and (N/2 + 1) complex values, respectively (for the DFT-based
70-
// calculations).
71+
// calculations). Note: 2 * (N / 2 + 1) > N for all N > 0, since
72+
// 2 * (N / 2 + 1) = N + 1 if N is odd
73+
// 2 * (N / 2 + 1) = N + 2 if N is even
74+
// so max(N, 2 * (N / 2 + 1)) == 2 * (N / 2 + 1)
7175
sycl::buffer<float> sig1{2 * (N / 2 + 1)};
7276
sycl::buffer<float> sig2{2 * (N / 2 + 1)};
7377
sycl::buffer<float> corr{2 * (N / 2 + 1)};
7478
// Buffer used for calculating corr without Discrete Fourier Transforms
75-
// (for comparison purposes):
79+
// for comparison purposes (calculations entirely done in forward domain):
7680
sycl::buffer<float> naive_corr{N};
7781

7882
// Initialize input signals with artificial "noise" data (random values of
@@ -116,6 +120,8 @@ int main(int argc, char **argv) {
116120
// Initialize DFT descriptor
117121
oneapi::mkl::dft::descriptor<oneapi::mkl::dft::precision::SINGLE,
118122
oneapi::mkl::dft::domain::REAL> desc(N);
123+
// oneMKL DFT descriptors use unit scaling factors by default. Explicitly set
124+
// the non-default scaling factor for the backward ("inverse") DFT:
119125
desc.set_value(oneapi::mkl::dft::config_param::BACKWARD_SCALE, 1.0f / N);
120126
desc.commit(Q);
121127
// Compute in-place forward transforms of both signals:

Libraries/oneMKL/fourier_correlation/fcorr_1d_usm.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ naive_cross_correlation(sycl::queue& Q,
5050
}
5151

5252
int main(int argc, char** argv) {
53-
unsigned int N = (argc == 1) ? 32 : std::stoi(argv[1]);
53+
const unsigned int N = (argc > 1) ? std::stoi(argv[1]) : 32;
5454
// N >= 8 required for the arbitrary signals as defined herein
55-
if (N < 8)
56-
throw std::invalid_argument("The input value N must be 8 or greater.");
55+
if (N < 8 || N > INT_MAX)
56+
throw std::invalid_argument("The period of the signal, chosen as input of "
57+
"the program, must be 8 or greater.");
5758

5859
// Let s be an integer s.t. 0 <= s < N and let
5960
// corr[s] = \sum_{j = 0}^{N-1} sig1[j] sig2[(j - s + N) mod N]
@@ -72,12 +73,15 @@ int main(int argc, char** argv) {
7273
// Initialize signal and correlation arrays. The arrays must be large enough
7374
// to store the forward and backward domains' data, consisting of N real
7475
// values and (N/2 + 1) complex values, respectively (for the DFT-based
75-
// calculations).
76+
// calculations). Note: 2 * (N / 2 + 1) > N for all N > 0, since
77+
// 2 * (N / 2 + 1) = N + 1 if N is odd
78+
// 2 * (N / 2 + 1) = N + 2 if N is even
79+
// so max(N, 2 * (N / 2 + 1)) == 2 * (N / 2 + 1)
7680
auto sig1 = sycl::malloc_shared<float>(2 * (N / 2 + 1), Q);
7781
auto sig2 = sycl::malloc_shared<float>(2 * (N / 2 + 1), Q);
7882
auto corr = sycl::malloc_shared<float>(2 * (N / 2 + 1), Q);
7983
// Array used for calculating corr without Discrete Fourier Transforms
80-
// (for comparison purposes):
84+
// for comparison purposes (calculations entirely done in forward domain):
8185
auto naive_corr = sycl::malloc_shared<float>(N, Q);
8286

8387
// Initialize input signals with artificial "noise" data (random values of
@@ -113,6 +117,8 @@ int main(int argc, char** argv) {
113117
// Initialize DFT descriptor
114118
oneapi::mkl::dft::descriptor<oneapi::mkl::dft::precision::SINGLE,
115119
oneapi::mkl::dft::domain::REAL> desc(N);
120+
// oneMKL DFT descriptors use unit scaling factors by default. Explicitly set
121+
// the non-default scaling factor for the backward ("inverse") DFT:
116122
desc.set_value(oneapi::mkl::dft::config_param::BACKWARD_SCALE, 1.0f / N);
117123
desc.commit(Q);
118124
// Compute in-place forward transforms of both signals:

Libraries/oneMKL/fourier_correlation/fcorr_2d_usm.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ int main(int argc, char **argv) {
109109
int temp = (argc <= 1) ? 8 : std::stoi(argv[1]);
110110
// n_rows >= 6 required for the arbitrary signals as defined herein
111111
if (temp < 6)
112-
throw std::invalid_argument("The first argument (n_rows) must be 6 or greater.");
112+
throw std::invalid_argument("The number of rows of the images, chosen as "
113+
"first input of the program, must be 6 or greater.");
113114
const unsigned n_rows = temp;
114115
temp = (argc <= 2) ? 8 : std::stoi(argv[2]);
115116
// n_cols >= 7 required for the arbitrary signals as defined herein
116117
if (temp < 7)
117-
throw std::invalid_argument("The second argument (n_cols) must be 7 or greater.");
118+
throw std::invalid_argument("The number of columns of the images, chosen as "
119+
"second input of the program, must be 7 or greater.");
118120
const unsigned n_cols = temp;
119121
const unsigned num_elem = n_rows * n_cols;
120122

@@ -145,6 +147,10 @@ int main(int argc, char **argv) {
145147
// to store the forward and backward domains' data, consisting of
146148
// n_rows * n_cols real values and n_rows * (n_cols / 2 + 1) complex values,
147149
// respectively (for the DFT-based calculations).
150+
// Note: 2 * (n_cols / 2 + 1) > n_cols for all n_cols > 0, so
151+
// max(n_rows * n_cols real,
152+
// n_rows * (n_cols / 2 + 1) * 2) == n_rows * (n_cols / 2 + 1) * 2
153+
// since n_rows > 0
148154
auto img1 = sycl::malloc_shared<float>(n_rows * (n_cols / 2 + 1) * 2, Q);
149155
auto img2 = sycl::malloc_shared<float>(n_rows * (n_cols / 2 + 1) * 2, Q);
150156
auto corr = sycl::malloc_shared<float>(n_rows * (n_cols / 2 + 1) * 2, Q);
@@ -200,6 +206,8 @@ int main(int argc, char **argv) {
200206
oneapi::mkl::dft::descriptor<oneapi::mkl::dft::precision::SINGLE,
201207
oneapi::mkl::dft::domain::REAL>
202208
desc({n_rows, n_cols});
209+
// oneMKL DFT descriptors use unit scaling factors by default. Explicitly set
210+
// the non-default scaling factor for the backward ("inverse") DFT:
203211
desc.set_value(oneapi::mkl::dft::config_param::BACKWARD_SCALE,
204212
1.0f / num_elem);
205213
desc.commit(Q);

0 commit comments

Comments
 (0)