1203 {
1204 if (extent < 0 || depth < 0 || (extent * depth > 0 && !
x))
1205 throw std::invalid_argument("invalid Scheme II operand");
1206 const PhaseTimer timer(
profile, Phase::PrepareWall);
1207
1208
1209
1210
1211 const bool element_major = rows ? transpose : !transpose;
1212 const auto at = [&](std::int64_t e, std::int64_t l) {
1213 return element_major ?
x[l + e * ld] :
x[e + l * ld];
1214 };
1216 out.data_ = std::make_unique<Operand::Data>();
1217 auto& d = *
out.data_;
1218 d.rows = rows;
1219 d.extent = extent;
1220 d.depth = depth;
1221 d.moduli =
static_cast<int>(
plan.
moduli.size());
1224 throw std::invalid_argument("Scheme II plan from another context");
1225 d.exponents.resize(static_cast<std::size_t>(extent));
1226 const int count = d.moduli;
1228 d.unsigned_values = int8_engine && !rows &&
int8->unsignedColumns();
1230 !host_residues;
1231 if (d.deferred)
1232 d.l_contiguous = element_major;
1233 else if (int8_engine)
1234 d.i8.resize(static_cast<std::size_t>(count) * d.stride());
1235 else
1236 d.f64.resize(static_cast<std::size_t>(count) * d.stride());
1237 if (extent == 0 || depth == 0)
return out;
1239
1240
1241
1242
1243 if (exponents) {
1244 std::copy_n(exponents, extent, d.exponents.begin());
1245 } else {
1246 const std::int64_t e_span = 512;
1247 const long e_pieces =
1248 static_cast<long>((extent + e_span - 1) / e_span);
1249 const long wanted = 4L *
threads;
1250 const long l_pieces = static_cast<long>(std::clamp<std::int64_t>(
1251 (wanted + e_pieces - 1) / e_pieces, 1, (depth + 255) / 256));
1252 const std::int64_t l_span = (depth + l_pieces - 1) / l_pieces;
1253 std::vector<double> partial(
1254 static_cast<std::size_t>(l_pieces * extent), 0.0);
1255 std::atomic<bool> finite{true};
1257 threads, e_pieces * l_pieces, [&](
long piece) {
1258 const PhaseTimer piece_timer(
profile, Phase::Prepare);
1259 const long ep = piece % e_pieces, lp = piece / e_pieces;
1260 const std::int64_t first = ep * e_span;
1261 const std::int64_t count_e =
1262 std::min(extent, first + e_span) - first;
1263 const std::int64_t l0 = lp * l_span;
1264 const std::int64_t l1 = std::min(depth, l0 + l_span);
1265 double* largest = partial.data() + static_cast<std::size_t>(
1266 lp * extent + first);
1267 bool ok = true;
1268 if (element_major) {
1269 for (std::int64_t q = 0; q < count_e; ++q) {
1270 const double* v =
x + (first + q) * ld;
1271 double big = 0.0;
1272 for (std::int64_t l = l0; l < l1; ++l) {
1273 ok = ok & std::isfinite(v[l]);
1274 big = std::max(big, std::abs(v[l]));
1275 }
1276 largest[q] = big;
1277 }
1278 } else {
1279 for (std::int64_t l = l0; l < l1; ++l) {
1280 const double* v =
x + first + l * ld;
1281 for (std::int64_t q = 0; q < count_e; ++q) {
1282 ok = ok & std::isfinite(v[q]);
1283 largest[q] =
1284 std::max(largest[q], std::abs(v[q]));
1285 }
1286 }
1287 }
1288 if (!ok) finite = false;
1289 });
1290 if (!finite)
1291 throw std::invalid_argument("nonfinite Scheme II operand");
1292 for (std::int64_t e = 0; e < extent; ++e) {
1293 double big = 0.0;
1294 for (long lp = 0; lp < l_pieces; ++lp)
1295 big = std::max(
1296 big,
1297 partial[static_cast<std::size_t>(lp * extent + e)]);
1298 d.exponents[static_cast<std::size_t>(e)] =
1300 }
1301 }
1302
1303
1304
1305
1306 const std::int64_t length = rows ? extent : depth;
1307 const std::int64_t columns = rows ? depth : extent;
1308 const std::int64_t group = std::max<std::int64_t>(
1309 1, 16384 / std::max<std::int64_t>(1, length));
1310 const long pieces = static_cast<long>((columns + group - 1) / group);
1311 const double limit = std::ldexp(1.0,
plan.
bits - 1);
1312
1313 std::vector<double> half_scale(static_cast<std::size_t>(extent)),
1314 rest_scale(static_cast<std::size_t>(extent));
1315 for (std::int64_t e = 0; e < extent; ++e) {
1316 const int sh = d.exponents[static_cast<std::size_t>(e)];
1317 const int h = sh / 2;
1318 half_scale[static_cast<std::size_t>(e)] = std::ldexp(1.0, h);
1319 rest_scale[static_cast<std::size_t>(e)] = std::ldexp(1.0, sh - h);
1320 }
1321 if (d.deferred) {
1322
1323
1324
1325 const std::int64_t outer = element_major ? extent : depth;
1326 const std::int64_t inner = element_major ? depth : extent;
1327 if (borrow) {
1329 d.source_ld = ld;
1330 } else {
1331 d.values.resize(d.stride());
1332 d.source = d.values.data();
1333 d.source_ld = inner;
1334 }
1335 const std::int64_t span = std::max<std::int64_t>(
1336 1, 65536 / std::max<std::int64_t>(1, inner));
1337 const long parts = static_cast<long>((outer + span - 1) / span);
1338 std::atomic<bool> within_all{true};
1340 const PhaseTimer piece_timer(
profile, Phase::Prepare);
1341 const std::int64_t first = part * span;
1342 const std::int64_t last = std::min(outer, first + span);
1343 bool within = true;
1344 for (std::int64_t o = first; o < last; ++o) {
1345 const double* in =
x + o * ld;
1346 if (!borrow)
1347 std::copy_n(in, inner,
1348 d.values.data() +
1349 static_cast<std::size_t>(o * inner));
1350 if (!exponents) continue;
1351 for (std::int64_t q = 0; q < inner; ++q) {
1352 const std::size_t e =
1353 static_cast<std::size_t>(element_major ? o : q);
1354 within = within &
1355 (std::abs(std::rint(in[q] * half_scale[e] *
1356 rest_scale[e])) <= limit);
1357 }
1358 }
1359 if (!within) within_all = false;
1360 });
1361 if (!within_all)
1362 throw std::invalid_argument(
1363 "Scheme II operand exceeds its scale exponent");
1365 }
1367 const PhaseTimer piece_timer(
profile, Phase::Prepare);
1368 std::vector<double>&
y =
scratch();
1369 y.resize(
static_cast<std::size_t
>(length));
1370 const std::int64_t first = piece * group;
1371 const std::int64_t last = std::min(columns, first + group);
1372 for (std::int64_t col = first; col < last; ++col) {
1373 bool within = true;
1374 if (rows) {
1375
1376 const std::int64_t l = col;
1377 for (std::int64_t e = 0; e < length; ++e) {
1378 const double v =
at(e, l);
1379 const double scaled = std::rint(
1380 v * half_scale[static_cast<std::size_t>(e)] *
1381 rest_scale[static_cast<std::size_t>(e)]);
1382 within = within & (std::abs(scaled) <= limit);
1383 y[
static_cast<std::size_t
>(e)] = scaled;
1384 }
1385 } else {
1386
1387 const std::int64_t e = col;
1388 const double f1 = half_scale[static_cast<std::size_t>(e)];
1389 const double f2 = rest_scale[static_cast<std::size_t>(e)];
1390 for (std::int64_t l = 0; l < length; ++l) {
1391 const double scaled = std::rint(
at(e, l) * f1 * f2);
1392 within = within & (std::abs(scaled) <= limit);
1393 y[
static_cast<std::size_t
>(l)] = scaled;
1394 }
1395 }
1396 if (!within)
1397 throw std::invalid_argument(
1398 "Scheme II operand exceeds its scale exponent");
1399 for (int t = 0; t < count; ++t) {
1400 const double p =
plan.
moduli[
static_cast<std::size_t
>(t)];
1401 const double inverse = 1.0 / p;
1402 const double low = -std::floor(p / 2);
1403 const double high = p - 1 + low;
1404 const std::size_t base =
1405 static_cast<std::size_t>(t) * d.stride() +
1406 static_cast<std::size_t>(col * length);
1407
1408
1409
1410 if (int8_engine) {
1411 std::int8_t* out_i8 = d.i8.data() + base;
1412 const bool positive = d.unsigned_values;
1413 std::int64_t q = 0;
1414#if defined(__x86_64__) && defined(__GNUC__)
1415 if (!positive && length >= 8 &&
1416 signedResidueSimdAvailable())
1417 q = signedInt8ResiduesAvx2(
y.data(), out_i8, length,
1418 p, inverse, low, high);
1419#endif
1420 for (; q < length; ++q) {
1421 const double v =
y[
static_cast<std::size_t
>(q)];
1422 double r = std::fma(-std::rint(v * inverse), p, v);
1423 r = std::fma(-std::rint(r * inverse), p, r);
1424 if (positive)
1425 r = r < 0.0 ? r + p : r;
1426 else {
1427 r = r > high ? r - p : r;
1428 r = r < low ? r + p : r;
1429 }
1430 out_i8[q] = static_cast<std::int8_t>(
1431 static_cast<std::uint8_t>(static_cast<int>(r)));
1432 }
1433 } else {
1434 double* out_f64 = d.f64.data() + base;
1435 std::int64_t q = 0;
1436#if defined(__x86_64__) && defined(__GNUC__)
1437 if (length >= 4 && signedResidueSimdAvailable())
1438 q = signedFp64ResiduesAvx2(
y.data(), out_f64,
1439 length, p, inverse, low,
1440 high);
1441#endif
1442 for (; q < length; ++q) {
1443 const double v =
y[
static_cast<std::size_t
>(q)];
1444 double r = std::fma(-std::rint(v * inverse), p, v);
1445 r = std::fma(-std::rint(r * inverse), p, r);
1446 r = r > high ? r - p : r;
1447 r = r < low ? r + p : r;
1448 out_f64[q] = r;
1449 }
1450 }
1451 }
1452 }
1453 });
1455 }
void runWith(int threads, long count, F &&fn)
run() with an explicit thread count in place of the configured one.
static TilePool & instance()
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
int scaleExponent(double max_abs, int bits) noexcept
static std::vector< double > & scratch()