Commit 7bf03d19 authored by Jaroslaw Zola's avatar Jaroslaw Zola

q overflow fix.

parent a6fe1bb1
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
PROJECT(sabnatk CXX)
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
......
......@@ -216,17 +216,18 @@ private:
const std::vector<std::pair<std::vector<vect>, double>>& data_;
};
// assume that q will not overflow, this will be checked by sabna calling code
// if q overflows it does not make sense in any scoring function anywayu
int m_q__(const set_type& pa) const {
int q = 1;
long long int q = 1;
for (int i = 0; i < set_max_size<set_type>(); ++i) {
if (in_set(pa, i)) {
q *= r(i);
if (q > std::numeric_limits<int>::max()) return std::numeric_limits<int>::max();
}
}
return q;
return static_cast<int>(q);
} // m_q__
std::vector<std::vector<int>> r_idx_;
......
/***
* $Id$
**
* File: MDLsimd.hpp
* Created: Apr 04, 2019
*
* Author: Jaroslaw Zola <[email protected]>
* Copyright (c) 2019 SCoRe Group http://www.score-group.org/
* Distributed under the MIT License.
* See accompanying file LICENSE.
*/
#ifndef MDL_SIMD_HPP
#define MDL_SIMD_HPP
#include <cmath>
#include <utility>
class MDL {
public:
typedef std::pair<double, double> score_type;
explicit MDL(int m = 0) : m_(m) { }
void init(int ri, int qi) { score_ = 0.0; nc_ = 0.0; ri_ = ri; qi_ = qi; count_ = 0; }
void finalize(int qi) {
// Uncomment to change how number of observed states in handled
// qi_ = qi;
for (int i = 0; i < count_; ++i) score_ += (nijk_[i] * log2(p_[i]));
nc_ = 0.5 * log2(m_) * (ri_ - 1) * qi_;
} // finalize
void operator()(int Nij) { }
void operator()(int Nijk, int Nij) {
p_[count_] = static_cast<double>(Nijk) / Nij;
nijk_[count_] = Nijk;
count_++;
if (count_ == SIMD_SIZE) {
for (int i = 0; i < SIMD_SIZE; ++i) score_ += (nijk_[i] * log2(p_[i]));
count_ = 0;
}
} // operator()
int r() const { return ri_; }
score_type score() const { return {-(score_ - nc_), -score_}; }
private:
static const int SIMD_SIZE = 32;
alignas(16) double p_[SIMD_SIZE];
alignas(16) double nijk_[SIMD_SIZE];
int count_ = 0;
double nc_ = 0.0;
double score_ = 0.0;
int m_ = 0;
int ri_ = 1;
int qi_ = 1;
}; // class MDL
#endif // MDL_SIMD_HPP
......@@ -213,17 +213,19 @@ private:
return count_unique_row;
} // m_radix_sort_core__
// if q overflows it does not make sense in any scoring function anyway
int m_compute_q_pa__(const set_type& pa, const std::vector<pair_data_type>& state_range_pa) const {
int q = 1;
long long int q = 1;
for (int xi = 0, idx_xi = 0; xi < n_; ++xi) {
if (in_set(pa, xi)) {
q *= state_range_pa[idx_xi].second - state_range_pa[idx_xi].first;
q *= (state_range_pa[idx_xi].second - state_range_pa[idx_xi].first);
if (q > std::numeric_limits<int>::max()) return std::numeric_limits<int>::max();
++idx_xi;
}
}
return q;
return static_cast<int>(q);
} // m_compute_q_pa__
int n_;
......
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