Skip to content

Commit 277114d

Browse files
authored
Merge pull request #13 from ORNL-Fusion/underflow
When piecewise and index node evaluated to negative indicies, this wo…
2 parents 368ab48 + caac9b4 commit 277114d

4 files changed

Lines changed: 200 additions & 39 deletions

File tree

‎graph_framework.xcodeproj/project.pbxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,7 @@
21262126
"-lLLVMSandboxIR",
21272127
"-lLLVMObjectYAML",
21282128
"-lLLVMPlugins",
2129+
"-lLLVMABI",
21292130
"-lLLVMFrontendAtomic",
21302131
"-lclangFrontend",
21312132
"-lclangBasic",
@@ -2301,6 +2302,7 @@
23012302
"-lLLVMSandboxIR",
23022303
"-lLLVMObjectYAML",
23032304
"-lLLVMPlugins",
2305+
"-lLLVMABI",
23042306
"-lLLVMFrontendAtomic",
23052307
"-lclangFrontend",
23062308
"-lclangBasic",

‎graph_framework/piecewise.hpp‎

Lines changed: 89 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,40 @@ void compile_index(std::ostringstream &stream,
2828
const size_t length,
2929
const T scale,
3030
const T offset) {
31-
const std::string type = jit::smallest_int_type<T> (length);
32-
stream << "min(max(("
33-
<< type
34-
<< ")";
31+
const std::string type = jit::type_to_string<T> ();
32+
stream << "(" << jit::smallest_uint_type<T> (length) << ")min";
33+
if constexpr (!jit::use_metal<T> ()) {
34+
stream << "<" << type << ">";
35+
}
36+
stream << "(max";
37+
if constexpr (!jit::use_metal<T> ()) {
38+
stream << "<" << type << ">";
39+
}
40+
stream << "(";
3541
if constexpr (jit::complex_scalar<T>) {
3642
stream << "real(";
3743
}
38-
stream << "((" << register_name << " - ";
44+
stream << "(" << register_name << " - ";
3945
if constexpr (jit::complex_scalar<T>) {
4046
stream << jit::get_type_string<T> ();
4147
}
4248
stream << offset << ")/";
4349
if constexpr (jit::complex_scalar<T>) {
4450
stream << jit::get_type_string<T> ();
4551
}
46-
stream << scale << ")";
52+
stream << scale;
4753
if constexpr (jit::complex_scalar<T>) {
4854
stream << ")";
4955
}
50-
stream << ",(" << type << ")0),("
51-
<< type << ")" << length - 1 << ")";
56+
stream << ",";
57+
if constexpr (jit::use_metal<T> ()) {
58+
stream << "(" << type << ")";
59+
}
60+
stream << "0),";
61+
if constexpr (jit::use_metal<T> ()) {
62+
stream << "(" << type << ")";
63+
}
64+
stream << length - 1 << ")";
5265
}
5366

5467
//******************************************************************************
@@ -192,9 +205,17 @@ void compile_index(std::ostringstream &stream,
192205
virtual shared_leaf<T, SAFE_MATH> reduce() {
193206
if (constant_cast(this->arg).get()) {
194207
const T arg = (this->arg->evaluate().at(0) + offset)/scale;
195-
const size_t i = std::min(static_cast<size_t> (std::real(arg)),
196-
this->get_size() - 1);
197-
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i]);
208+
if constexpr (jit::float_base<T>) {
209+
const size_t i = std::max<float> (std::min<float> (std::real(arg),
210+
this->get_size() - 1),
211+
0);
212+
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i]);
213+
} else {
214+
const size_t i = std::max<double> (std::min<double> (std::real(arg),
215+
this->get_size() - 1),
216+
0);
217+
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i]);
218+
}
198219
}
199220

200221
if (evaluate().is_same()) {
@@ -333,7 +354,7 @@ void compile_index(std::ostringstream &stream,
333354
#ifdef USE_INDEX_CACHE
334355
indices[a.get()] = jit::to_string('i', a.get());
335356
stream << " const "
336-
<< jit::smallest_int_type<T> (length) << " "
357+
<< jit::smallest_uint_type<T> (length) << " "
337358
<< indices[a.get()] << " = ";
338359
compile_index<T> (stream, registers[a.get()], length,
339360
scale, offset);
@@ -347,7 +368,7 @@ void compile_index(std::ostringstream &stream,
347368
stream << " " << registers[this] << " = ";
348369
#ifdef USE_CUDA_TEXTURES
349370
if constexpr (jit::use_cuda()) {
350-
if constexpr (float_base<T>) {
371+
if constexpr (jit::float_base<T>) {
351372
if constexpr (complex_scalar<T>) {
352373
stream << "to_cmp_float(tex1D<float2> (";
353374
} else {
@@ -835,25 +856,56 @@ void compile_index(std::ostringstream &stream,
835856
constant_cast(this->right).get()) {
836857
const T l = (this->left->evaluate().at(0) + x_offset)/x_scale;
837858
const T r = (this->right->evaluate().at(0) + y_offset)/y_scale;
838-
const size_t i = std::min(static_cast<size_t> (std::real(l)),
839-
this->get_num_rows() - 1);
840-
const size_t j = std::min(static_cast<size_t> (std::real(r)),
841-
this->get_num_columns() - 1);
842-
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i*this->get_num_columns() + j]);
859+
860+
if constexpr (jit::float_base<T>) {
861+
const size_t i = std::max<float> (std::min<float> (std::real(l),
862+
this->get_num_rows() - 1),
863+
0);
864+
const size_t j = std::max<float> (std::min<float> (std::real(r),
865+
this->get_num_columns() - 1),
866+
0);
867+
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i*this->get_num_columns() + j]);
868+
} else {
869+
const size_t i = std::max<double> (std::min<double> (std::real(l),
870+
this->get_num_rows() - 1),
871+
0);
872+
const size_t j = std::max<double> (std::min<double> (std::real(r),
873+
this->get_num_columns() - 1),
874+
0);
875+
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i*this->get_num_columns() + j]);
876+
}
843877
} else if (constant_cast(this->left).get()) {
844878
const T l = (this->left->evaluate().at(0) + x_offset)/x_scale;
845-
const size_t i = std::min(static_cast<size_t> (std::real(l)),
846-
this->get_num_rows() - 1);
847879

848-
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_row(i, this->get_num_columns()),
849-
this->right, y_scale, y_offset);
880+
if constexpr (jit::float_base<T>) {
881+
const size_t i = std::max<float> (std::min<float> (std::real(l),
882+
this->get_num_rows() - 1),
883+
0);
884+
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_row(i, this->get_num_columns()),
885+
this->right, y_scale, y_offset);
886+
} else {
887+
const size_t i = std::max<double> (std::min<double> (std::real(l),
888+
this->get_num_rows() - 1),
889+
0);
890+
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_row(i, this->get_num_columns()),
891+
this->right, y_scale, y_offset);
892+
}
850893
} else if (constant_cast(this->right).get()) {
851894
const T r = (this->right->evaluate().at(0) + y_offset)/y_scale;
852-
const size_t j = std::min(static_cast<size_t> (std::real(r)),
853-
this->get_num_columns() - 1);
854-
855-
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_column(j, this->get_num_columns()),
856-
this->left, x_scale, x_offset);
895+
896+
if constexpr (jit::float_base<T>) {
897+
const size_t j = std::max<float> (std::min<float> (std::real(r),
898+
this->get_num_columns() - 1),
899+
0);
900+
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_column(j, this->get_num_columns()),
901+
this->left, x_scale, x_offset);
902+
} else {
903+
const size_t j = std::max<double> (std::min<double> (std::real(r),
904+
this->get_num_columns() - 1),
905+
0);
906+
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_column(j, this->get_num_columns()),
907+
this->left, x_scale, x_offset);
908+
}
857909
}
858910

859911
if (evaluate().is_same()) {
@@ -1015,7 +1067,7 @@ void compile_index(std::ostringstream &stream,
10151067
if (indices.find(x.get()) == indices.end()) {
10161068
indices[x.get()] = jit::to_string('i', x.get());
10171069
stream << " const "
1018-
<< jit::smallest_int_type<T> (num_rows) << " "
1070+
<< jit::smallest_uint_type<T> (num_rows) << " "
10191071
<< indices[x.get()] << " = ";
10201072
compile_index<T> (stream, registers[x.get()], num_rows,
10211073
x_scale, x_offset);
@@ -1024,7 +1076,7 @@ void compile_index(std::ostringstream &stream,
10241076
if (indices.find(y.get()) == indices.end()) {
10251077
indices[y.get()] = jit::to_string('i', y.get());
10261078
stream << " const "
1027-
<< jit::smallest_int_type<T> (num_columns) << " "
1079+
<< jit::smallest_uint_type<T> (num_columns) << " "
10281080
<< indices[y.get()] << " = ";
10291081
compile_index<T> (stream, registers[y.get()], num_columns,
10301082
y_scale, y_offset);
@@ -1040,7 +1092,7 @@ void compile_index(std::ostringstream &stream,
10401092
if (indices.find(temp.get()) == indices.end()) {
10411093
indices[temp.get()] = jit::to_string('i', temp.get());
10421094
stream << " const "
1043-
<< jit::smallest_int_type<T> (length) << " "
1095+
<< jit::smallest_uint_type<T> (length) << " "
10441096
<< indices[temp.get()] << " = "
10451097
<< indices[x.get()]
10461098
<< "*" << num_columns << " + "
@@ -1056,7 +1108,7 @@ void compile_index(std::ostringstream &stream,
10561108
stream << " " << registers[this] << " = ";
10571109
#ifdef USE_CUDA_TEXTURES
10581110
if constexpr (jit::use_cuda()) {
1059-
if constexpr (float_base<T>) {
1111+
if constexpr (jit::float_base<T>) {
10601112
if constexpr (complex_scalar<T>) {
10611113
stream << "to_cmp_float(tex1D<float2> (";
10621114
} else {
@@ -1075,8 +1127,8 @@ void compile_index(std::ostringstream &stream,
10751127
if constexpr (jit::use_metal<T> ()) {
10761128
#ifdef USE_INDEX_CACHE
10771129
stream << ".read("
1078-
<< jit::smallest_int_type<T> (std::max(num_rows,
1079-
num_columns))
1130+
<< jit::smallest_uint_type<T> (std::max(num_rows,
1131+
num_columns))
10801132
<< "2("
10811133
<< indices[y.get()]
10821134
<< ","
@@ -1461,7 +1513,7 @@ void compile_index(std::ostringstream &stream,
14611513
#ifdef USE_INDEX_CACHE
14621514
indices[a.get()] = jit::to_string('i', a.get());
14631515
stream << " const "
1464-
<< jit::smallest_int_type<T> (length) << " "
1516+
<< jit::smallest_uint_type<T> (length) << " "
14651517
<< indices[a.get()] << " = ";
14661518
compile_index<T> (stream, registers[a.get()], length,
14671519
scale, offset);
@@ -1797,7 +1849,7 @@ void compile_index(std::ostringstream &stream,
17971849
if (indices.find(x.get()) == indices.end()) {
17981850
indices[x.get()] = jit::to_string('i', x.get());
17991851
stream << " const "
1800-
<< jit::smallest_int_type<T> (num_rows) << " "
1852+
<< jit::smallest_uint_type<T> (num_rows) << " "
18011853
<< indices[x.get()] << " = ";
18021854
compile_index<T> (stream, registers[x.get()], num_rows,
18031855
x_scale, x_offset);
@@ -1806,7 +1858,7 @@ void compile_index(std::ostringstream &stream,
18061858
if (indices.find(y.get()) == indices.end()) {
18071859
indices[y.get()] = jit::to_string('i', y.get());
18081860
stream << " const "
1809-
<< jit::smallest_int_type<T> (num_columns) << " "
1861+
<< jit::smallest_uint_type<T> (num_columns) << " "
18101862
<< indices[y.get()] << " = ";
18111863
compile_index<T> (stream, registers[y.get()], num_columns,
18121864
y_scale, y_offset);
@@ -1819,7 +1871,7 @@ void compile_index(std::ostringstream &stream,
18191871
if (indices.find(temp.get()) == indices.end()) {
18201872
indices[temp.get()] = jit::to_string('i', temp.get());
18211873
stream << " const "
1822-
<< jit::smallest_int_type<T> (length) << " "
1874+
<< jit::smallest_uint_type<T> (length) << " "
18231875
<< indices[temp.get()] << " = "
18241876
<< indices[x.get()]
18251877
<< "*" << num_columns << " + "

‎graph_framework/register.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace jit {
105105
/// @returns The smallest integer type as a string.
106106
//------------------------------------------------------------------------------
107107
template<float_scalar T>
108-
std::string smallest_int_type(const size_t max_size) {
108+
std::string smallest_uint_type(const size_t max_size) {
109109
if (max_size <= std::numeric_limits<unsigned char>::max()) {
110110
if constexpr (jit::use_metal<T> ()) {
111111
return "ushort";

0 commit comments

Comments
 (0)