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
|
#include <iostream> #include <assert.h> #include <fstream> #include <unordered_map> #include <string> #include <vector> #include <sstream> #include <thread> #include <stdlib.h> #include <time.h>
#include "rocksdb/db.h" #include "rocksdb/slice.h" #include "rocksdb/options.h" #define MAX_MUT_NUM 80
using namespace std; using ROCKSDB_NAMESPACE::DB; using ROCKSDB_NAMESPACE::Options; using ROCKSDB_NAMESPACE::ReadOptions; using ROCKSDB_NAMESPACE::Status; using ROCKSDB_NAMESPACE::WriteBatch; using ROCKSDB_NAMESPACE::WriteOptions;
typedef int ReturnType; typedef string str_t; typedef vector<str_t> vec_str_t; typedef vector<int> vec_int_t;
const char kDBPath[] = "db/data2/"; str_t gTxtPath = "2.txt";
enum ReturValue { SUCCESS, FAILED, };
DB* db = nullptr; vec_int_t result; vec_str_t visited; mutex mutexs[MAX_MUT_NUM]; vec_str_t blacks; int threads = 0; int bkn = 0; double io_time = 0;
ReturnType Generation(str_t gTxtPath) { Status s; ReturnType r = FAILED; ifstream fin(gTxtPath); str_t k, v; unordered_map<str_t, str_t> g; while (fin >> k >> v) { g[k] += v + " "; g[v] += k + " "; } int n = g.size(); for (int u = 0; u < n; u++) { str_t v = to_string(u); s = db->Put(WriteOptions(), v, g[v]); if (!s.ok()) return r; } r = SUCCESS; return r; }
void MyGet(str_t k, str_t &v) { clock_t start_time = clock(); db->Get(ReadOptions(), k, &v); io_time += (double)(clock()-start_time); }
vec_str_t GetFriends1Vec(str_t k) { str_t v; vec_str_t vec; MyGet(k, v); stringstream ss(v); while (ss >> v) vec.push_back(v); return vec; }
void Computation(int start) { for (int u = start; u < bkn; u += threads) { str_t v = blacks[u]; vec_str_t friends1 = GetFriends1Vec(v); for (auto f1: friends1) { int f1_; stringstream ss1(f1); ss1 >> f1_; mutexs[f1_%MAX_MUT_NUM].lock(); if (visited[f1_] != v) { result[f1_]++; visited[f1_] = v; } mutexs[f1_%MAX_MUT_NUM].unlock(); vec_str_t friends2 = GetFriends1Vec(f1); for (auto f2: friends2) { int f2_; stringstream ss2(f2); ss2 >> f2_; mutexs[f2_%MAX_MUT_NUM].lock(); if (visited[f2_] != v) { result[f2_]++; visited[f2_] = v; } mutexs[f2_%MAX_MUT_NUM].unlock(); } } } }
int main(int argc, char* argv[]) { Options options; Status s; options.IncreaseParallelism(); options.OptimizeLevelStyleCompaction(); options.create_if_missing = true; int rm = 0;
assert(argc == 2); gTxtPath = argv[1];
assert(DB::Open(options, kDBPath, &db).ok());
generation: cout << "generation start!" << endl; assert(Generation(gTxtPath) == SUCCESS); cout << "generation finish!" << endl;
computation: uint64_t n; db->GetIntProperty("rocksdb.estimate-num-keys", &n); cout << "vertex numbers: " << n << endl; float p = 0; while (p<=0 || p>=1) { cout << "black ratio: "; cin >> p; cout << p << endl; } bkn = n*p; for (int i = 0; i < bkn; i++) { blacks.push_back(to_string(rand()%n)); } bkn = blacks.size(); cout << "black numbers: " << bkn << endl; result.resize(n); visited.resize(n);
while (threads <= 0 || threads > MAX_MUT_NUM) { cout << "threads: "; cin >> threads; cout << threads << endl; }
thread pool[threads]; double total_time;
clock_t start_time = clock(); for (int start = 0; start < threads; start++) { pool[start] = thread(Computation, start); } for (int start = 0; start < threads; start++) { pool[start].join(); } total_time = (double)(clock()-start_time);
printf("Coputation Time: %.0f CLKs\n", total_time); printf("I/O Time: %.0f CLKs\n", io_time); rm = system("make rm2"); cout << "Work Finished!\n\n\n\n" << endl;
delete db; return 0; }
|