Spaces:
Runtime error
Runtime error
File size: 55,340 Bytes
be11144 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 |
/******************************************************************************
* Copyright (c) 2011, Duane Merrill. All rights reserved.
* Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the NVIDIA CORPORATION nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#pragma once
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#undef small // Windows is terrible for polluting macro namespace
#else
#include <sys/resource.h>
#endif
#include <cuda_runtime.h>
#include <stdio.h>
#include <float.h>
#include <cmath>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <limits>
#include "mersenne.h"
#include "half.h"
#include "cub/util_debug.cuh"
#include "cub/util_device.cuh"
#include "cub/util_type.cuh"
#include "cub/util_macro.cuh"
#include "cub/iterator/discard_output_iterator.cuh"
/******************************************************************************
* Type conversion macros
******************************************************************************/
/**
* Return a value of type `T` with the same bitwise representation of `in`.
* Types `T` and `U` must be the same size.
*/
template <typename T, typename U>
T SafeBitCast(const U& in)
{
static_assert(sizeof(T) == sizeof(U), "Types must be same size.");
T out;
memcpy(&out, &in, sizeof(T));
return out;
}
/******************************************************************************
* Assertion macros
******************************************************************************/
/**
* Assert equals
*/
#define AssertEquals(a, b) if ((a) != (b)) { std::cerr << "\n(" << __FILE__ << ": " << __LINE__ << ")\n"; exit(1);}
/******************************************************************************
* Command-line parsing functionality
******************************************************************************/
/**
* Utility for parsing command line arguments
*/
struct CommandLineArgs
{
std::vector<std::string> keys;
std::vector<std::string> values;
std::vector<std::string> args;
cudaDeviceProp deviceProp;
float device_giga_bandwidth;
size_t device_free_physmem;
size_t device_total_physmem;
/**
* Constructor
*/
CommandLineArgs(int argc, char **argv) :
keys(10),
values(10)
{
using namespace std;
// Initialize mersenne generator
unsigned int mersenne_init[4]= {0x123, 0x234, 0x345, 0x456};
mersenne::init_by_array(mersenne_init, 4);
for (int i = 1; i < argc; i++)
{
string arg = argv[i];
if ((arg[0] != '-') || (arg[1] != '-'))
{
args.push_back(arg);
continue;
}
string::size_type pos;
string key, val;
if ((pos = arg.find('=')) == string::npos) {
key = string(arg, 2, arg.length() - 2);
val = "";
} else {
key = string(arg, 2, pos - 2);
val = string(arg, pos + 1, arg.length() - 1);
}
keys.push_back(key);
values.push_back(val);
}
}
/**
* Checks whether a flag "--<flag>" is present in the commandline
*/
bool CheckCmdLineFlag(const char* arg_name)
{
using namespace std;
for (int i = 0; i < int(keys.size()); ++i)
{
if (keys[i] == string(arg_name))
return true;
}
return false;
}
/**
* Returns number of naked (non-flag and non-key-value) commandline parameters
*/
template <typename T>
int NumNakedArgs()
{
return args.size();
}
/**
* Returns the commandline parameter for a given index (not including flags)
*/
template <typename T>
void GetCmdLineArgument(int index, T &val)
{
using namespace std;
if (index < args.size()) {
istringstream str_stream(args[index]);
str_stream >> val;
}
}
/**
* Returns the value specified for a given commandline parameter --<flag>=<value>
*/
template <typename T>
void GetCmdLineArgument(const char *arg_name, T &val)
{
using namespace std;
for (int i = 0; i < int(keys.size()); ++i)
{
if (keys[i] == string(arg_name))
{
istringstream str_stream(values[i]);
str_stream >> val;
}
}
}
/**
* Returns the values specified for a given commandline parameter --<flag>=<value>,<value>*
*/
template <typename T>
void GetCmdLineArguments(const char *arg_name, std::vector<T> &vals)
{
using namespace std;
if (CheckCmdLineFlag(arg_name))
{
// Clear any default values
vals.clear();
// Recover from multi-value string
for (int i = 0; i < keys.size(); ++i)
{
if (keys[i] == string(arg_name))
{
string val_string(values[i]);
istringstream str_stream(val_string);
string::size_type old_pos = 0;
string::size_type new_pos = 0;
// Iterate comma-separated values
T val;
while ((new_pos = val_string.find(',', old_pos)) != string::npos)
{
if (new_pos != old_pos)
{
str_stream.width(new_pos - old_pos);
str_stream >> val;
vals.push_back(val);
}
// skip over comma
str_stream.ignore(1);
old_pos = new_pos + 1;
}
// Read last value
str_stream >> val;
vals.push_back(val);
}
}
}
}
/**
* The number of pairs parsed
*/
int ParsedArgc()
{
return (int) keys.size();
}
/**
* Initialize device
*/
cudaError_t DeviceInit(int dev = -1)
{
cudaError_t error = cudaSuccess;
do
{
int deviceCount;
error = CubDebug(cudaGetDeviceCount(&deviceCount));
if (error) break;
if (deviceCount == 0) {
fprintf(stderr, "No devices supporting CUDA.\n");
exit(1);
}
if (dev < 0)
{
GetCmdLineArgument("device", dev);
}
if ((dev > deviceCount - 1) || (dev < 0))
{
dev = 0;
}
error = CubDebug(cudaSetDevice(dev));
if (error) break;
CubDebugExit(cudaMemGetInfo(&device_free_physmem, &device_total_physmem));
int ptx_version = 0;
error = CubDebug(cub::PtxVersion(ptx_version));
if (error) break;
error = CubDebug(cudaGetDeviceProperties(&deviceProp, dev));
if (error) break;
if (deviceProp.major < 1) {
fprintf(stderr, "Device does not support CUDA.\n");
exit(1);
}
device_giga_bandwidth = float(deviceProp.memoryBusWidth) * deviceProp.memoryClockRate * 2 / 8 / 1000 / 1000;
if (!CheckCmdLineFlag("quiet"))
{
printf(
"Using device %d: %s (PTX version %d, SM%d, %d SMs, "
"%lld free / %lld total MB physmem, "
"%.3f GB/s @ %d kHz mem clock, ECC %s)\n",
dev,
deviceProp.name,
ptx_version,
deviceProp.major * 100 + deviceProp.minor * 10,
deviceProp.multiProcessorCount,
(unsigned long long) device_free_physmem / 1024 / 1024,
(unsigned long long) device_total_physmem / 1024 / 1024,
device_giga_bandwidth,
deviceProp.memoryClockRate,
(deviceProp.ECCEnabled) ? "on" : "off");
fflush(stdout);
}
} while (0);
return error;
}
};
/******************************************************************************
* Random bits generator
******************************************************************************/
int g_num_rand_samples = 0;
template <typename T>
bool IsNaN(T /* val */) { return false; }
template<>
__noinline__ bool IsNaN<float>(float val)
{
return std::isnan(val);
}
template<>
__noinline__ bool IsNaN<float1>(float1 val)
{
return (IsNaN(val.x));
}
template<>
__noinline__ bool IsNaN<float2>(float2 val)
{
return (IsNaN(val.y) || IsNaN(val.x));
}
template<>
__noinline__ bool IsNaN<float3>(float3 val)
{
return (IsNaN(val.z) || IsNaN(val.y) || IsNaN(val.x));
}
template<>
__noinline__ bool IsNaN<float4>(float4 val)
{
return (IsNaN(val.y) || IsNaN(val.x) || IsNaN(val.w) || IsNaN(val.z));
}
template<>
__noinline__ bool IsNaN<double>(double val)
{
return std::isnan(val);
}
template<>
__noinline__ bool IsNaN<double1>(double1 val)
{
return (IsNaN(val.x));
}
template<>
__noinline__ bool IsNaN<double2>(double2 val)
{
return (IsNaN(val.y) || IsNaN(val.x));
}
template<>
__noinline__ bool IsNaN<double3>(double3 val)
{
return (IsNaN(val.z) || IsNaN(val.y) || IsNaN(val.x));
}
template<>
__noinline__ bool IsNaN<double4>(double4 val)
{
return (IsNaN(val.y) || IsNaN(val.x) || IsNaN(val.w) || IsNaN(val.z));
}
template<>
__noinline__ bool IsNaN<half_t>(half_t val)
{
const auto bits = SafeBitCast<unsigned short>(val);
// commented bit is always true, leaving for documentation:
return (((bits >= 0x7C01) && (bits <= 0x7FFF)) ||
((bits >= 0xFC01) /*&& (bits <= 0xFFFFFFFF)*/));
}
/**
* Generates random keys.
*
* We always take the second-order byte from rand() because the higher-order
* bits returned by rand() are commonly considered more uniformly distributed
* than the lower-order bits.
*
* We can decrease the entropy level of keys by adopting the technique
* of Thearling and Smith in which keys are computed from the bitwise AND of
* multiple random samples:
*
* entropy_reduction | Effectively-unique bits per key
* -----------------------------------------------------
* -1 | 0
* 0 | 32
* 1 | 25.95 (81%)
* 2 | 17.41 (54%)
* 3 | 10.78 (34%)
* 4 | 6.42 (20%)
* ... | ...
*
*/
template <typename K>
void RandomBits(
K &key,
int entropy_reduction = 0,
int begin_bit = 0,
int end_bit = sizeof(K) * 8)
{
const int NUM_BYTES = sizeof(K);
const int WORD_BYTES = sizeof(unsigned int);
const int NUM_WORDS = (NUM_BYTES + WORD_BYTES - 1) / WORD_BYTES;
unsigned int word_buff[NUM_WORDS];
if (entropy_reduction == -1)
{
memset((void *) &key, 0, sizeof(key));
return;
}
if (end_bit < 0)
end_bit = sizeof(K) * 8;
while (true)
{
// Generate random word_buff
for (int j = 0; j < NUM_WORDS; j++)
{
int current_bit = j * WORD_BYTES * 8;
unsigned int word = 0xffffffff;
word &= 0xffffffff << CUB_MAX(0, begin_bit - current_bit);
word &= 0xffffffff >> CUB_MAX(0, (current_bit + (WORD_BYTES * 8)) - end_bit);
for (int i = 0; i <= entropy_reduction; i++)
{
// Grab some of the higher bits from rand (better entropy, supposedly)
word &= mersenne::genrand_int32();
g_num_rand_samples++;
}
word_buff[j] = word;
}
memcpy(&key, word_buff, sizeof(K));
K copy = key;
if (!IsNaN(copy))
break; // avoids NaNs when generating random floating point numbers
}
}
/// Randomly select number between [0:max)
template <typename T>
T RandomValue(T max)
{
unsigned int bits;
unsigned int max_int = (unsigned int) -1;
do {
RandomBits(bits);
} while (bits == max_int);
return (T) ((double(bits) / double(max_int)) * double(max));
}
/******************************************************************************
* Console printing utilities
******************************************************************************/
/**
* Helper for casting character types to integers for cout printing
*/
template <typename T>
T CoutCast(T val) { return val; }
int CoutCast(char val) { return val; }
int CoutCast(unsigned char val) { return val; }
int CoutCast(signed char val) { return val; }
/******************************************************************************
* Test value initialization utilities
******************************************************************************/
/**
* Test problem generation options
*/
enum GenMode
{
UNIFORM, // Assign to '2', regardless of integer seed
INTEGER_SEED, // Assign to integer seed
RANDOM, // Assign to random, regardless of integer seed
RANDOM_BIT, // Assign to randomly chosen 0 or 1, regardless of integer seed
};
/**
* Initialize value
*/
template <typename T>
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, T &value, int index = 0)
{
switch (gen_mode)
{
#if (CUB_PTX_ARCH == 0)
case RANDOM:
RandomBits(value);
break;
case RANDOM_BIT:
char c;
RandomBits(c, 0, 0, 1);
value = (c > 0) ? (T) 1 : (T) -1;
break;
#endif
case UNIFORM:
value = 2;
break;
case INTEGER_SEED:
default:
value = (T) index;
break;
}
}
/**
* Initialize value (bool)
*/
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, bool &value, int index = 0)
{
switch (gen_mode)
{
#if (CUB_PTX_ARCH == 0)
case RANDOM:
case RANDOM_BIT:
char c;
RandomBits(c, 0, 0, 1);
value = (c > 0);
break;
#endif
case UNIFORM:
value = true;
break;
case INTEGER_SEED:
default:
value = (index > 0);
break;
}
}
/**
* cub::NullType test initialization
*/
__host__ __device__ __forceinline__ void InitValue(GenMode /* gen_mode */,
cub::NullType &/* value */,
int /* index */ = 0)
{}
/**
* cub::KeyValuePair<OffsetT, ValueT>test initialization
*/
template <typename KeyT, typename ValueT>
__host__ __device__ __forceinline__ void InitValue(
GenMode gen_mode,
cub::KeyValuePair<KeyT, ValueT>& value,
int index = 0)
{
InitValue(gen_mode, value.value, index);
// Assign corresponding flag with a likelihood of the last bit being set with entropy-reduction level 3
RandomBits(value.key, 3);
value.key = (value.key & 0x1);
}
/******************************************************************************
* Comparison and ostream operators
******************************************************************************/
/**
* KeyValuePair ostream operator
*/
template <typename Key, typename Value>
std::ostream& operator<<(std::ostream& os, const cub::KeyValuePair<Key, Value> &val)
{
os << '(' << CoutCast(val.key) << ',' << CoutCast(val.value) << ')';
return os;
}
/******************************************************************************
* Comparison and ostream operators for CUDA vector types
******************************************************************************/
/**
* Vector1 overloads
*/
#define CUB_VEC_OVERLOAD_1(T, BaseT) \
/* Ostream output */ \
std::ostream& operator<<( \
std::ostream& os, \
const T& val) \
{ \
os << '(' << CoutCast(val.x) << ')'; \
return os; \
} \
/* Inequality */ \
__host__ __device__ __forceinline__ bool operator!=( \
const T &a, \
const T &b) \
{ \
return (a.x != b.x); \
} \
/* Equality */ \
__host__ __device__ __forceinline__ bool operator==( \
const T &a, \
const T &b) \
{ \
return (a.x == b.x); \
} \
/* Test initialization */ \
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, T &value, int index = 0) \
{ \
InitValue(gen_mode, value.x, index); \
} \
/* Max */ \
__host__ __device__ __forceinline__ bool operator>( \
const T &a, \
const T &b) \
{ \
return (a.x > b.x); \
} \
/* Min */ \
__host__ __device__ __forceinline__ bool operator<( \
const T &a, \
const T &b) \
{ \
return (a.x < b.x); \
} \
/* Summation (non-reference addends for VS2003 -O3 warpscan workaround */ \
__host__ __device__ __forceinline__ T operator+( \
T a, \
T b) \
{ \
T retval = make_##T(a.x + b.x); \
return retval; \
} \
namespace cub { \
template<> \
struct NumericTraits<T> \
{ \
static const Category CATEGORY = NOT_A_NUMBER; \
enum { \
PRIMITIVE = false, \
NULL_TYPE = false, \
}; \
static T Max() \
{ \
T retval = { \
NumericTraits<BaseT>::Max()}; \
return retval; \
} \
static T Lowest() \
{ \
T retval = { \
NumericTraits<BaseT>::Lowest()}; \
return retval; \
} \
}; \
} /* namespace std */
/**
* Vector2 overloads
*/
#define CUB_VEC_OVERLOAD_2(T, BaseT) \
/* Ostream output */ \
std::ostream& operator<<( \
std::ostream& os, \
const T& val) \
{ \
os << '(' \
<< CoutCast(val.x) << ',' \
<< CoutCast(val.y) << ')'; \
return os; \
} \
/* Inequality */ \
__host__ __device__ __forceinline__ bool operator!=( \
const T &a, \
const T &b) \
{ \
return (a.x != b.x) || \
(a.y != b.y); \
} \
/* Equality */ \
__host__ __device__ __forceinline__ bool operator==( \
const T &a, \
const T &b) \
{ \
return (a.x == b.x) && \
(a.y == b.y); \
} \
/* Test initialization */ \
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, T &value, int index = 0) \
{ \
InitValue(gen_mode, value.x, index); \
InitValue(gen_mode, value.y, index); \
} \
/* Max */ \
__host__ __device__ __forceinline__ bool operator>( \
const T &a, \
const T &b) \
{ \
if (a.x > b.x) return true; else if (b.x > a.x) return false; \
return a.y > b.y; \
} \
/* Min */ \
__host__ __device__ __forceinline__ bool operator<( \
const T &a, \
const T &b) \
{ \
if (a.x < b.x) return true; else if (b.x < a.x) return false; \
return a.y < b.y; \
} \
/* Summation (non-reference addends for VS2003 -O3 warpscan workaround */ \
__host__ __device__ __forceinline__ T operator+( \
T a, \
T b) \
{ \
T retval = make_##T( \
a.x + b.x, \
a.y + b.y); \
return retval; \
} \
namespace cub { \
template<> \
struct NumericTraits<T> \
{ \
static const Category CATEGORY = NOT_A_NUMBER; \
enum { \
PRIMITIVE = false, \
NULL_TYPE = false, \
}; \
static T Max() \
{ \
T retval = { \
NumericTraits<BaseT>::Max(), \
NumericTraits<BaseT>::Max()}; \
return retval; \
} \
static T Lowest() \
{ \
T retval = { \
NumericTraits<BaseT>::Lowest(), \
NumericTraits<BaseT>::Lowest()}; \
return retval; \
} \
}; \
} /* namespace cub */
/**
* Vector3 overloads
*/
#define CUB_VEC_OVERLOAD_3(T, BaseT) \
/* Ostream output */ \
std::ostream& operator<<( \
std::ostream& os, \
const T& val) \
{ \
os << '(' \
<< CoutCast(val.x) << ',' \
<< CoutCast(val.y) << ',' \
<< CoutCast(val.z) << ')'; \
return os; \
} \
/* Inequality */ \
__host__ __device__ __forceinline__ bool operator!=( \
const T &a, \
const T &b) \
{ \
return (a.x != b.x) || \
(a.y != b.y) || \
(a.z != b.z); \
} \
/* Equality */ \
__host__ __device__ __forceinline__ bool operator==( \
const T &a, \
const T &b) \
{ \
return (a.x == b.x) && \
(a.y == b.y) && \
(a.z == b.z); \
} \
/* Test initialization */ \
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, T &value, int index = 0) \
{ \
InitValue(gen_mode, value.x, index); \
InitValue(gen_mode, value.y, index); \
InitValue(gen_mode, value.z, index); \
} \
/* Max */ \
__host__ __device__ __forceinline__ bool operator>( \
const T &a, \
const T &b) \
{ \
if (a.x > b.x) return true; else if (b.x > a.x) return false; \
if (a.y > b.y) return true; else if (b.y > a.y) return false; \
return a.z > b.z; \
} \
/* Min */ \
__host__ __device__ __forceinline__ bool operator<( \
const T &a, \
const T &b) \
{ \
if (a.x < b.x) return true; else if (b.x < a.x) return false; \
if (a.y < b.y) return true; else if (b.y < a.y) return false; \
return a.z < b.z; \
} \
/* Summation (non-reference addends for VS2003 -O3 warpscan workaround */ \
__host__ __device__ __forceinline__ T operator+( \
T a, \
T b) \
{ \
T retval = make_##T( \
a.x + b.x, \
a.y + b.y, \
a.z + b.z); \
return retval; \
} \
namespace cub { \
template<> \
struct NumericTraits<T> \
{ \
static const Category CATEGORY = NOT_A_NUMBER; \
enum { \
PRIMITIVE = false, \
NULL_TYPE = false, \
}; \
static T Max() \
{ \
T retval = { \
NumericTraits<BaseT>::Max(), \
NumericTraits<BaseT>::Max(), \
NumericTraits<BaseT>::Max()}; \
return retval; \
} \
static T Lowest() \
{ \
T retval = { \
NumericTraits<BaseT>::Lowest(), \
NumericTraits<BaseT>::Lowest(), \
NumericTraits<BaseT>::Lowest()}; \
return retval; \
} \
}; \
} /* namespace cub */
/**
* Vector4 overloads
*/
#define CUB_VEC_OVERLOAD_4(T, BaseT) \
/* Ostream output */ \
std::ostream& operator<<( \
std::ostream& os, \
const T& val) \
{ \
os << '(' \
<< CoutCast(val.x) << ',' \
<< CoutCast(val.y) << ',' \
<< CoutCast(val.z) << ',' \
<< CoutCast(val.w) << ')'; \
return os; \
} \
/* Inequality */ \
__host__ __device__ __forceinline__ bool operator!=( \
const T &a, \
const T &b) \
{ \
return (a.x != b.x) || \
(a.y != b.y) || \
(a.z != b.z) || \
(a.w != b.w); \
} \
/* Equality */ \
__host__ __device__ __forceinline__ bool operator==( \
const T &a, \
const T &b) \
{ \
return (a.x == b.x) && \
(a.y == b.y) && \
(a.z == b.z) && \
(a.w == b.w); \
} \
/* Test initialization */ \
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, T &value, int index = 0) \
{ \
InitValue(gen_mode, value.x, index); \
InitValue(gen_mode, value.y, index); \
InitValue(gen_mode, value.z, index); \
InitValue(gen_mode, value.w, index); \
} \
/* Max */ \
__host__ __device__ __forceinline__ bool operator>( \
const T &a, \
const T &b) \
{ \
if (a.x > b.x) return true; else if (b.x > a.x) return false; \
if (a.y > b.y) return true; else if (b.y > a.y) return false; \
if (a.z > b.z) return true; else if (b.z > a.z) return false; \
return a.w > b.w; \
} \
/* Min */ \
__host__ __device__ __forceinline__ bool operator<( \
const T &a, \
const T &b) \
{ \
if (a.x < b.x) return true; else if (b.x < a.x) return false; \
if (a.y < b.y) return true; else if (b.y < a.y) return false; \
if (a.z < b.z) return true; else if (b.z < a.z) return false; \
return a.w < b.w; \
} \
/* Summation (non-reference addends for VS2003 -O3 warpscan workaround */ \
__host__ __device__ __forceinline__ T operator+( \
T a, \
T b) \
{ \
T retval = make_##T( \
a.x + b.x, \
a.y + b.y, \
a.z + b.z, \
a.w + b.w); \
return retval; \
} \
namespace cub { \
template<> \
struct NumericTraits<T> \
{ \
static const Category CATEGORY = NOT_A_NUMBER; \
enum { \
PRIMITIVE = false, \
NULL_TYPE = false, \
}; \
static T Max() \
{ \
T retval = { \
NumericTraits<BaseT>::Max(), \
NumericTraits<BaseT>::Max(), \
NumericTraits<BaseT>::Max(), \
NumericTraits<BaseT>::Max()}; \
return retval; \
} \
static T Lowest() \
{ \
T retval = { \
NumericTraits<BaseT>::Lowest(), \
NumericTraits<BaseT>::Lowest(), \
NumericTraits<BaseT>::Lowest(), \
NumericTraits<BaseT>::Lowest()}; \
return retval; \
} \
}; \
} /* namespace cub */
/**
* All vector overloads
*/
#define CUB_VEC_OVERLOAD(COMPONENT_T, BaseT) \
CUB_VEC_OVERLOAD_1(COMPONENT_T##1, BaseT) \
CUB_VEC_OVERLOAD_2(COMPONENT_T##2, BaseT) \
CUB_VEC_OVERLOAD_3(COMPONENT_T##3, BaseT) \
CUB_VEC_OVERLOAD_4(COMPONENT_T##4, BaseT)
/**
* Define for types
*/
CUB_VEC_OVERLOAD(char, char)
CUB_VEC_OVERLOAD(short, short)
CUB_VEC_OVERLOAD(int, int)
CUB_VEC_OVERLOAD(long, long)
CUB_VEC_OVERLOAD(longlong, long long)
CUB_VEC_OVERLOAD(uchar, unsigned char)
CUB_VEC_OVERLOAD(ushort, unsigned short)
CUB_VEC_OVERLOAD(uint, unsigned int)
CUB_VEC_OVERLOAD(ulong, unsigned long)
CUB_VEC_OVERLOAD(ulonglong, unsigned long long)
CUB_VEC_OVERLOAD(float, float)
CUB_VEC_OVERLOAD(double, double)
//---------------------------------------------------------------------
// Complex data type TestFoo
//---------------------------------------------------------------------
/**
* TestFoo complex data type
*/
struct TestFoo
{
long long x;
int y;
short z;
char w;
// Factory
static __host__ __device__ __forceinline__ TestFoo MakeTestFoo(long long x, int y, short z, char w)
{
TestFoo retval = {x, y, z, w};
return retval;
}
// Assignment from int operator
__host__ __device__ __forceinline__ TestFoo& operator =(int b)
{
x = b;
y = b;
z = b;
w = b;
return *this;
}
// Summation operator
__host__ __device__ __forceinline__ TestFoo operator+(const TestFoo &b) const
{
return MakeTestFoo(x + b.x, y + b.y, z + b.z, w + b.w);
}
// Inequality operator
__host__ __device__ __forceinline__ bool operator !=(const TestFoo &b) const
{
return (x != b.x) || (y != b.y) || (z != b.z) || (w != b.w);
}
// Equality operator
__host__ __device__ __forceinline__ bool operator ==(const TestFoo &b) const
{
return (x == b.x) && (y == b.y) && (z == b.z) && (w == b.w);
}
// Less than operator
__host__ __device__ __forceinline__ bool operator <(const TestFoo &b) const
{
if (x < b.x) return true; else if (b.x < x) return false;
if (y < b.y) return true; else if (b.y < y) return false;
if (z < b.z) return true; else if (b.z < z) return false;
return w < b.w;
}
// Greater than operator
__host__ __device__ __forceinline__ bool operator >(const TestFoo &b) const
{
if (x > b.x) return true; else if (b.x > x) return false;
if (y > b.y) return true; else if (b.y > y) return false;
if (z > b.z) return true; else if (b.z > z) return false;
return w > b.w;
}
};
/**
* TestFoo ostream operator
*/
std::ostream& operator<<(std::ostream& os, const TestFoo& val)
{
os << '(' << val.x << ',' << val.y << ',' << val.z << ',' << CoutCast(val.w) << ')';
return os;
}
/**
* TestFoo test initialization
*/
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, TestFoo &value, int index = 0)
{
InitValue(gen_mode, value.x, index);
InitValue(gen_mode, value.y, index);
InitValue(gen_mode, value.z, index);
InitValue(gen_mode, value.w, index);
}
/// numeric_limits<TestFoo> specialization
namespace cub {
template<>
struct NumericTraits<TestFoo>
{
static const Category CATEGORY = NOT_A_NUMBER;
enum {
PRIMITIVE = false,
NULL_TYPE = false,
};
static TestFoo Max()
{
return TestFoo::MakeTestFoo(
NumericTraits<long long>::Max(),
NumericTraits<int>::Max(),
NumericTraits<short>::Max(),
NumericTraits<char>::Max());
}
static TestFoo Lowest()
{
return TestFoo::MakeTestFoo(
NumericTraits<long long>::Lowest(),
NumericTraits<int>::Lowest(),
NumericTraits<short>::Lowest(),
NumericTraits<char>::Lowest());
}
};
} // namespace cub
//---------------------------------------------------------------------
// Complex data type TestBar (with optimizations for fence-free warp-synchrony)
//---------------------------------------------------------------------
/**
* TestBar complex data type
*/
struct TestBar
{
long long x;
int y;
// Constructor
__host__ __device__ __forceinline__ TestBar() : x(0), y(0)
{}
// Constructor
__host__ __device__ __forceinline__ TestBar(int b) : x(b), y(b)
{}
// Constructor
__host__ __device__ __forceinline__ TestBar(long long x, int y) : x(x), y(y)
{}
// Assignment from int operator
__host__ __device__ __forceinline__ TestBar& operator =(int b)
{
x = b;
y = b;
return *this;
}
// Summation operator
__host__ __device__ __forceinline__ TestBar operator+(const TestBar &b) const
{
return TestBar(x + b.x, y + b.y);
}
// Inequality operator
__host__ __device__ __forceinline__ bool operator !=(const TestBar &b) const
{
return (x != b.x) || (y != b.y);
}
// Equality operator
__host__ __device__ __forceinline__ bool operator ==(const TestBar &b) const
{
return (x == b.x) && (y == b.y);
}
// Less than operator
__host__ __device__ __forceinline__ bool operator <(const TestBar &b) const
{
if (x < b.x) return true; else if (b.x < x) return false;
return y < b.y;
}
// Greater than operator
__host__ __device__ __forceinline__ bool operator >(const TestBar &b) const
{
if (x > b.x) return true; else if (b.x > x) return false;
return y > b.y;
}
};
/**
* TestBar ostream operator
*/
std::ostream& operator<<(std::ostream& os, const TestBar& val)
{
os << '(' << val.x << ',' << val.y << ')';
return os;
}
/**
* TestBar test initialization
*/
__host__ __device__ __forceinline__ void InitValue(GenMode gen_mode, TestBar &value, int index = 0)
{
InitValue(gen_mode, value.x, index);
InitValue(gen_mode, value.y, index);
}
/// numeric_limits<TestBar> specialization
namespace cub {
template<>
struct NumericTraits<TestBar>
{
static const Category CATEGORY = NOT_A_NUMBER;
enum {
PRIMITIVE = false,
NULL_TYPE = false,
};
static TestBar Max()
{
return TestBar(
NumericTraits<long long>::Max(),
NumericTraits<int>::Max());
}
static TestBar Lowest()
{
return TestBar(
NumericTraits<long long>::Lowest(),
NumericTraits<int>::Lowest());
}
};
} // namespace cub
/******************************************************************************
* Helper routines for list comparison and display
******************************************************************************/
/**
* Compares the equivalence of two arrays
*/
template <typename S, typename T, typename OffsetT>
int CompareResults(T* computed, S* reference, OffsetT len, bool verbose = true)
{
for (OffsetT i = 0; i < len; i++)
{
if (computed[i] != reference[i])
{
if (verbose) std::cout << "INCORRECT: [" << i << "]: "
<< CoutCast(computed[i]) << " != "
<< CoutCast(reference[i]);
return 1;
}
}
return 0;
}
/**
* Compares the equivalence of two arrays
*/
template <typename OffsetT>
int CompareResults(float* computed, float* reference, OffsetT len, bool verbose = true)
{
for (OffsetT i = 0; i < len; i++)
{
if (computed[i] != reference[i])
{
float difference = std::abs(computed[i]-reference[i]);
float fraction = difference / std::abs(reference[i]);
if (fraction > 0.0001)
{
if (verbose) std::cout << "INCORRECT: [" << i << "]: "
<< "(computed) " << CoutCast(computed[i]) << " != "
<< CoutCast(reference[i]) << " (difference:" << difference << ", fraction: " << fraction << ")";
return 1;
}
}
}
return 0;
}
/**
* Compares the equivalence of two arrays
*/
template <typename OffsetT>
int CompareResults(cub::NullType* computed, cub::NullType* reference, OffsetT len, bool verbose = true)
{
return 0;
}
/**
* Compares the equivalence of two arrays
*/
template <typename OffsetT>
int CompareResults(double* computed, double* reference, OffsetT len, bool verbose = true)
{
for (OffsetT i = 0; i < len; i++)
{
if (computed[i] != reference[i])
{
double difference = std::abs(computed[i]-reference[i]);
double fraction = difference / std::abs(reference[i]);
if (fraction > 0.0001)
{
if (verbose) std::cout << "INCORRECT: [" << i << "]: "
<< CoutCast(computed[i]) << " != "
<< CoutCast(reference[i]) << " (difference:" << difference << ", fraction: " << fraction << ")";
return 1;
}
}
}
return 0;
}
/**
* Verify the contents of a device array match those
* of a host array
*/
int CompareDeviceResults(
cub::NullType */* h_reference */,
cub::NullType */* d_data */,
size_t /* num_items */,
bool /* verbose */ = true,
bool /* display_data */ = false)
{
return 0;
}
/**
* Verify the contents of a device array match those
* of a host array
*/
template <typename S, typename OffsetT>
int CompareDeviceResults(
S *h_reference,
cub::DiscardOutputIterator<OffsetT> d_data,
size_t num_items,
bool verbose = true,
bool display_data = false)
{
return 0;
}
/**
* Verify the contents of a device array match those
* of a host array
*/
template <typename S, typename T>
int CompareDeviceResults(
S *h_reference,
T *d_data,
size_t num_items,
bool verbose = true,
bool display_data = false)
{
// Allocate array on host
T *h_data = (T*) malloc(num_items * sizeof(T));
// Copy data back
cudaMemcpy(h_data, d_data, sizeof(T) * num_items, cudaMemcpyDeviceToHost);
// Display data
if (display_data)
{
printf("Reference:\n");
for (int i = 0; i < int(num_items); i++)
{
std::cout << CoutCast(h_reference[i]) << ", ";
}
printf("\n\nComputed:\n");
for (int i = 0; i < int(num_items); i++)
{
std::cout << CoutCast(h_data[i]) << ", ";
}
printf("\n\n");
}
// Check
int retval = CompareResults(h_data, h_reference, num_items, verbose);
// Cleanup
if (h_data) free(h_data);
return retval;
}
/**
* Verify the contents of a device array match those
* of a device array
*/
template <typename T>
int CompareDeviceDeviceResults(
T *d_reference,
T *d_data,
size_t num_items,
bool verbose = true,
bool display_data = false)
{
// Allocate array on host
T *h_reference = (T*) malloc(num_items * sizeof(T));
T *h_data = (T*) malloc(num_items * sizeof(T));
// Copy data back
cudaMemcpy(h_reference, d_reference, sizeof(T) * num_items, cudaMemcpyDeviceToHost);
cudaMemcpy(h_data, d_data, sizeof(T) * num_items, cudaMemcpyDeviceToHost);
// Display data
if (display_data) {
printf("Reference:\n");
for (int i = 0; i < num_items; i++)
{
std::cout << CoutCast(h_reference[i]) << ", ";
}
printf("\n\nComputed:\n");
for (int i = 0; i < num_items; i++)
{
std::cout << CoutCast(h_data[i]) << ", ";
}
printf("\n\n");
}
// Check
int retval = CompareResults(h_data, h_reference, num_items, verbose);
// Cleanup
if (h_reference) free(h_reference);
if (h_data) free(h_data);
return retval;
}
/**
* Print the contents of a host array
*/
void DisplayResults(
cub::NullType */* h_data */,
size_t /* num_items */)
{}
/**
* Print the contents of a host array
*/
template <typename InputIteratorT>
void DisplayResults(
InputIteratorT h_data,
size_t num_items)
{
// Display data
for (int i = 0; i < int(num_items); i++)
{
std::cout << CoutCast(h_data[i]) << ", ";
}
printf("\n");
}
/**
* Print the contents of a device array
*/
template <typename T>
void DisplayDeviceResults(
T *d_data,
size_t num_items)
{
// Allocate array on host
T *h_data = (T*) malloc(num_items * sizeof(T));
// Copy data back
cudaMemcpy(h_data, d_data, sizeof(T) * num_items, cudaMemcpyDeviceToHost);
DisplayResults(h_data, num_items);
// Cleanup
if (h_data) free(h_data);
}
/******************************************************************************
* Segment descriptor generation
******************************************************************************/
/**
* Initialize segments
*/
void InitializeSegments(
int num_items,
int num_segments,
int *h_segment_offsets,
bool verbose = false)
{
if (num_segments <= 0)
return;
unsigned int expected_segment_length = (num_items + num_segments - 1) / num_segments;
int offset = 0;
for (int i = 0; i < num_segments; ++i)
{
h_segment_offsets[i] = offset;
unsigned int segment_length = RandomValue((expected_segment_length * 2) + 1);
offset += segment_length;
offset = CUB_MIN(offset, num_items);
}
h_segment_offsets[num_segments] = num_items;
if (verbose)
{
printf("Segment offsets: ");
DisplayResults(h_segment_offsets, num_segments + 1);
}
}
/******************************************************************************
* Timing
******************************************************************************/
struct CpuTimer
{
#if defined(_WIN32) || defined(_WIN64)
LARGE_INTEGER ll_freq;
LARGE_INTEGER ll_start;
LARGE_INTEGER ll_stop;
CpuTimer()
{
QueryPerformanceFrequency(&ll_freq);
}
void Start()
{
QueryPerformanceCounter(&ll_start);
}
void Stop()
{
QueryPerformanceCounter(&ll_stop);
}
float ElapsedMillis()
{
double start = double(ll_start.QuadPart) / double(ll_freq.QuadPart);
double stop = double(ll_stop.QuadPart) / double(ll_freq.QuadPart);
return float((stop - start) * 1000);
}
#else
rusage start;
rusage stop;
void Start()
{
getrusage(RUSAGE_SELF, &start);
}
void Stop()
{
getrusage(RUSAGE_SELF, &stop);
}
float ElapsedMillis()
{
float sec = stop.ru_utime.tv_sec - start.ru_utime.tv_sec;
float usec = stop.ru_utime.tv_usec - start.ru_utime.tv_usec;
return (sec * 1000) + (usec / 1000);
}
#endif
};
struct GpuTimer
{
cudaEvent_t start;
cudaEvent_t stop;
GpuTimer()
{
cudaEventCreate(&start);
cudaEventCreate(&stop);
}
~GpuTimer()
{
cudaEventDestroy(start);
cudaEventDestroy(stop);
}
void Start()
{
cudaEventRecord(start, 0);
}
void Stop()
{
cudaEventRecord(stop, 0);
}
float ElapsedMillis()
{
float elapsed;
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsed, start, stop);
return elapsed;
}
};
|