#include <stdlib.h> #include <stdio.h> #include "message.h" #include "teatr.h" #define LL long long #define DBG(X) class RangeSum { LL** bucket_; int bucketsCount_; LL* bucketSum_; LL totalSum_; int maxValue_; public: RangeSum(int maxValue) { bucketsCount_ = (maxValue / 1024) + 2; bucket_ = new LL*[bucketsCount_]; for (int i=0; i < bucketsCount_; i++) { bucket_[i] = new LL[1024]; } bucketSum_ = new LL[bucketsCount_]; for (int i=0; i < bucketsCount_; i++) { bucketSum_[i] = 0LL; } totalSum_ = 0; DBG(printf("Allocation OK\n")); } void increment(unsigned int key) { unsigned int bucketId = (key >> 10); // / 1024 unsigned int bucketKey = (key & 0x3FF); // % 1024 ++bucket_[bucketId][bucketKey]; ++bucketSum_[bucketId]; ++totalSum_; } LL countGreaterThan(unsigned int key) { unsigned int bucketId = (key >> 10); // / 1024 unsigned int bucketKey = (key & 0x3FF); // % 1024 LL res = 0; if (bucketKey >= 512) { for (unsigned int i=bucketKey+1; i < 1024; i++) { res += bucket_[bucketId][i]; } } else { LL lessThanSum = 0; for (int i=bucketKey; i >=0; --i) { lessThanSum += bucket_[bucketId][i]; } res += (bucketSum_[bucketId] - lessThanSum); } if (bucketId >= 512) { for (unsigned int i=bucketId+1; i < bucketsCount_; i++) { res += bucketSum_[i]; } } else { LL bucketSumLessThanSum = 0; for (int i=bucketId; i >= 0; --i) { bucketSumLessThanSum += bucketSum_[i]; } res += (totalSum_ - bucketSumLessThanSum); } return res; } void verifyInput(unsigned int key) { if (key > maxValue_) { printf("Too large key value: %u (max is %d)", key, maxValue_); } } }; #if 0 void selftest_assert(LL x, LL y) { if (x != y) { printf("test: expected %lld, has: %lld\n", x, y); } else { printf("OK (%lld==%lld)\n", x, y); } } void test_rangeSum() { RangeSum rs(1000000); rs.increment(3); rs.increment(5); rs.increment(100); selftest_assert(3, rs.countGreaterThan(0)); selftest_assert(2, rs.countGreaterThan(4)); selftest_assert(1, rs.countGreaterThan(5)); selftest_assert(0, rs.countGreaterThan(100)); rs.increment(100000); rs.increment(200000); rs.increment(300000); selftest_assert(6, rs.countGreaterThan(0)); selftest_assert(0, rs.countGreaterThan(300000)); selftest_assert(2, rs.countGreaterThan(199999)); rs.increment(1024); rs.increment(2048); rs.increment(1024); selftest_assert(4, rs.countGreaterThan(1024)); selftest_assert(6, rs.countGreaterThan(1023)); } int main_local() { return 0; } //#define TEST_F1 out: 2499912950050000 #ifdef TEST_F1 int GetN() { return int(1e8); } int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; } #endif #define TEST_F2 Out: 2497220719002543 #ifdef TEST_F2 int GetN() { return 100 * 1000 * 1000; } int GetElement(int i) { return 1 + (((LL)i * 27) + i & (i %113) + ((i % 37) ^ (i % 20)) * 3571) % 1000000LL; } #endif //#define TEST_F3 out: 299999394 #ifdef TEST_F3 int GetN() { return 100 * 1000 * 1000; } int GetElement(int i) { if (i < 100 * 1000 * 1000 - 3) return 1 + i / 100; else return 100 * 1000 * 1000 - i; } #endif #endif // if 0 int main() { int length = GetN(); int nid = MyNodeId(); int numberOfNodes = NumberOfNodes(); int MAX_K = 1000010; RangeSum rs(MAX_K); LL totalLocal = 0; for (int i=0; i < length; i++) { int val = GetElement(i); rs.increment(val); if (i % numberOfNodes == nid) { totalLocal += rs.countGreaterThan(val); } } if (nid > 0) { // send to the master node PutLL(0, totalLocal); Send(0); } else { // master node LL totalAllInstances = totalLocal; // wait for all other instances... for (int node=1; node < numberOfNodes; node++) { Receive(node); LL totalFromNode = GetLL(node); totalAllInstances += totalFromNode; } printf("%lld\n", totalAllInstances); } DBG(printf("%lld\n", totalLocal)); return 0; }
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 | #include <stdlib.h> #include <stdio.h> #include "message.h" #include "teatr.h" #define LL long long #define DBG(X) class RangeSum { LL** bucket_; int bucketsCount_; LL* bucketSum_; LL totalSum_; int maxValue_; public: RangeSum(int maxValue) { bucketsCount_ = (maxValue / 1024) + 2; bucket_ = new LL*[bucketsCount_]; for (int i=0; i < bucketsCount_; i++) { bucket_[i] = new LL[1024]; } bucketSum_ = new LL[bucketsCount_]; for (int i=0; i < bucketsCount_; i++) { bucketSum_[i] = 0LL; } totalSum_ = 0; DBG(printf("Allocation OK\n")); } void increment(unsigned int key) { unsigned int bucketId = (key >> 10); // / 1024 unsigned int bucketKey = (key & 0x3FF); // % 1024 ++bucket_[bucketId][bucketKey]; ++bucketSum_[bucketId]; ++totalSum_; } LL countGreaterThan(unsigned int key) { unsigned int bucketId = (key >> 10); // / 1024 unsigned int bucketKey = (key & 0x3FF); // % 1024 LL res = 0; if (bucketKey >= 512) { for (unsigned int i=bucketKey+1; i < 1024; i++) { res += bucket_[bucketId][i]; } } else { LL lessThanSum = 0; for (int i=bucketKey; i >=0; --i) { lessThanSum += bucket_[bucketId][i]; } res += (bucketSum_[bucketId] - lessThanSum); } if (bucketId >= 512) { for (unsigned int i=bucketId+1; i < bucketsCount_; i++) { res += bucketSum_[i]; } } else { LL bucketSumLessThanSum = 0; for (int i=bucketId; i >= 0; --i) { bucketSumLessThanSum += bucketSum_[i]; } res += (totalSum_ - bucketSumLessThanSum); } return res; } void verifyInput(unsigned int key) { if (key > maxValue_) { printf("Too large key value: %u (max is %d)", key, maxValue_); } } }; #if 0 void selftest_assert(LL x, LL y) { if (x != y) { printf("test: expected %lld, has: %lld\n", x, y); } else { printf("OK (%lld==%lld)\n", x, y); } } void test_rangeSum() { RangeSum rs(1000000); rs.increment(3); rs.increment(5); rs.increment(100); selftest_assert(3, rs.countGreaterThan(0)); selftest_assert(2, rs.countGreaterThan(4)); selftest_assert(1, rs.countGreaterThan(5)); selftest_assert(0, rs.countGreaterThan(100)); rs.increment(100000); rs.increment(200000); rs.increment(300000); selftest_assert(6, rs.countGreaterThan(0)); selftest_assert(0, rs.countGreaterThan(300000)); selftest_assert(2, rs.countGreaterThan(199999)); rs.increment(1024); rs.increment(2048); rs.increment(1024); selftest_assert(4, rs.countGreaterThan(1024)); selftest_assert(6, rs.countGreaterThan(1023)); } int main_local() { return 0; } //#define TEST_F1 out: 2499912950050000 #ifdef TEST_F1 int GetN() { return int(1e8); } int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; } #endif #define TEST_F2 Out: 2497220719002543 #ifdef TEST_F2 int GetN() { return 100 * 1000 * 1000; } int GetElement(int i) { return 1 + (((LL)i * 27) + i & (i %113) + ((i % 37) ^ (i % 20)) * 3571) % 1000000LL; } #endif //#define TEST_F3 out: 299999394 #ifdef TEST_F3 int GetN() { return 100 * 1000 * 1000; } int GetElement(int i) { if (i < 100 * 1000 * 1000 - 3) return 1 + i / 100; else return 100 * 1000 * 1000 - i; } #endif #endif // if 0 int main() { int length = GetN(); int nid = MyNodeId(); int numberOfNodes = NumberOfNodes(); int MAX_K = 1000010; RangeSum rs(MAX_K); LL totalLocal = 0; for (int i=0; i < length; i++) { int val = GetElement(i); rs.increment(val); if (i % numberOfNodes == nid) { totalLocal += rs.countGreaterThan(val); } } if (nid > 0) { // send to the master node PutLL(0, totalLocal); Send(0); } else { // master node LL totalAllInstances = totalLocal; // wait for all other instances... for (int node=1; node < numberOfNodes; node++) { Receive(node); LL totalFromNode = GetLL(node); totalAllInstances += totalFromNode; } printf("%lld\n", totalAllInstances); } DBG(printf("%lld\n", totalLocal)); return 0; } |