Skip to content

Commit bf29eba

Browse files
authored
Fix double to float (#1447)
1 parent 11d8285 commit bf29eba

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

DirectProgramming/C++SYCL/GraphTraversal/hidden-markov-models/src/hidden-markov-models.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,37 @@ constexpr int M = 20;
4747
constexpr int T = 20;
4848
// The parameter for generating the sequence.
4949
constexpr int seed = 0;
50-
// Minimal double to initialize logarithms for Viterbi values equal to 0.
51-
constexpr double MIN_DOUBLE = -1.0 * std::numeric_limits<double>::max();
50+
// Minimal float to initialize logarithms for Viterbi values equal to 0.
51+
constexpr float MIN_FLOAT = -1.0 * std::numeric_limits<float>::max();
5252

53-
bool ViterbiCondition(double x, double y, double z, double compare);
53+
bool ViterbiCondition(float x, float y, float z, float compare);
5454

5555
int main() {
5656
try {
5757
// Initializing and generating initial probabilities for the hidden states.
58-
double(*pi) = new double[N];
58+
float(*pi) = new float[N];
5959
for (int i = 0; i < N; ++i) {
6060
pi[i] = sycl::log10(1.0f / N);
6161
}
62-
buffer<double, 1> pi_buf(pi, N);
62+
buffer<float, 1> pi_buf(pi, N);
6363

6464
//Device initialization.
6565
queue q(default_selector_v);
6666
cout << "Device: " << q.get_device().get_info<info::device::name>() << " "
6767
<< q.get_device().get_platform().get_info<info::platform::name>() << "\n";
6868

6969
//Buffers initialization.
70-
buffer<double, 2> viterbi(range<2>(N, T));
70+
buffer<float, 2> viterbi(range<2>(N, T));
7171
buffer<int, 2> back_pointer(range<2>(N, T));
72-
buffer<double, 2> a(range<2>(N, N));
73-
buffer<double, 2> b(range<2>(N, M));
72+
buffer<float, 2> a(range<2>(N, N));
73+
buffer<float, 2> b(range<2>(N, M));
7474

7575
// Generating transition matrix A for the Markov process.
7676
q.submit([&](handler& h) {
7777
auto a_acc = a.get_access<access::mode::write>(h);
7878
h.parallel_for(range<2>(N, N), [=](id<2> index) {
7979
// The sum of the probabilities in each row of the matrix A has to be equal to 1.
80-
double prob = 1.0f / N;
80+
float prob = 1.0f / N;
8181
// The algorithm computes logarithms of the probability values to improve small numbers processing.
8282
a_acc[index] = sycl::log10(prob);
8383
});
@@ -88,9 +88,9 @@ int main() {
8888
auto b_acc = b.get_access<access::mode::write>(h);
8989
h.parallel_for(range<2>(N, M), [=](id<2> index) {
9090
// The sum of the probabilities in each row of the matrix B has to be equal to 1.
91-
double prob = ((index[0] + index[1]) % M) * 2.0f / M / (M - 1);
91+
float prob = ((index[0] + index[1]) % M) * 2.0f / M / (M - 1);
9292
// The algorithm computes logarithms of the probability values to improve small numbers processing.
93-
b_acc[index] = (prob == 0.0f) ? MIN_DOUBLE : sycl::log10(prob);
93+
b_acc[index] = (prob == 0.0f) ? MIN_FLOAT : sycl::log10(prob);
9494
});
9595
});
9696

@@ -113,7 +113,7 @@ int main() {
113113
int j = index[1];
114114
// At starting point only the first Viterbi values are defined and these Values are substituted
115115
// with logarithms due to the following equation: log(x*y) = log(x) + log(y).
116-
v_acc[index] = (j != 0) ? MIN_DOUBLE : pi_acc[i] + b_acc[i][seq_acc[0]];
116+
v_acc[index] = (j != 0) ? MIN_FLOAT : pi_acc[i] + b_acc[i][seq_acc[0]];
117117
// Default values of all the back pointers are (-1) to show that they are not determined yet.
118118
b_ptr_acc[index] = -1;
119119
});
@@ -147,7 +147,7 @@ int main() {
147147
auto v_acc = viterbi.get_access<access::mode::read>();
148148
auto b_ptr_acc = back_pointer.get_access<access::mode::read>();
149149
auto vit_path_acc = vit_path.get_access<access::mode::read_write>();
150-
double v_max = MIN_DOUBLE;
150+
float v_max = MIN_FLOAT;
151151
// Constructing the Viterbi path. The last state of this path is the one with
152152
// the biggest Viterbi value (the most likely state).
153153
for (int i = 0; i < N; ++i) {
@@ -180,8 +180,8 @@ int main() {
180180
}
181181

182182
// The method checks if all three components of the sum are not equivalent to logarithm of zero
183-
// (that is incorrect value and is substituted with minimal possible value of double) and that
183+
// (that is incorrect value and is substituted with minimal possible value of float) and that
184184
// the Viterbi value on the new step exceeds the current one.
185-
bool ViterbiCondition(double x, double y, double z, double compare) {
186-
return (x > MIN_DOUBLE) && (y > MIN_DOUBLE) && (z > MIN_DOUBLE) && (x + y + z > compare);
185+
bool ViterbiCondition(float x, float y, float z, float compare) {
186+
return (x > MIN_FLOAT) && (y > MIN_FLOAT) && (z > MIN_FLOAT) && (x + y + z > compare);
187187
}

0 commit comments

Comments
 (0)