-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCIA_multi_thread.cpp
More file actions
480 lines (354 loc) · 11.4 KB
/
Copy pathCIA_multi_thread.cpp
File metadata and controls
480 lines (354 loc) · 11.4 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <list>
#include <vector>
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <pthread.h>
#include <cmath>
#include <time.h>
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION >= 40300
#include <tr1/unordered_map>
using namespace std::tr1;
//namespace std {
// std::string to_string(size_t n) {
// std::ostringstream s;
// s << n;
// return s.str();
// }
//}
#define hash_map unordered_map
#else
#include <unordered_map>
#endif
using namespace std;
// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
long randint(long min, long max){
return long(rand() / (RAND_MAX + 0.0) * (max - min) + min);
}
bool my_comparator ( const pair<double, int>& l, const pair<double, int>& r)
{
return l.first > r.first;
}
class Data{
public:
typedef unsigned long int vertex;
long N;
//adjacency list
//weighted graph
unordered_map<vertex,set<pair<vertex, double> > > network_map;
//1 : weighted, 0: unweighted
int weighted_graph;
//priors
//probability of being benign for each node
double *prior;
//network file
char *network_file;
char *post_file;
char *prior_file;
//char *post_file_iter;
//during computation, post is treated as the normalized multiplication of incoming messages of each node.
//
double *post;
double *post_pre;
//to support different input format
char *train_set_file;
double alpha;
//parameters of PMR model when input is a set of labeled nodes, instead of priors
double theta_pos;
double theta_neg;
double theta_unl;
//this weight is used when the input is an unweighted graph
double weight;
double max_iter;
int* ordering_array;
int num_threads;
set<vertex> neg_train_set;
// directed graph edges
//int** graph_array;
//unordered_map<pair<vertex, vertex>, pair<int, double>, pairhash > graph_array;
set<long> valid_user;
//edge types
//1 : ourgoing
//2 : incoming
//3 : bidirectional
class CIA_arg{
public:
Data * data_pointer;
int current_thread;
CIA_arg(){}
};
Data(){
}
void add_edge(vertex node1, vertex node2, double w){
// add edge (node1, node2)
// no self loops
if(node1==node2){
return;
}
//add node2 to the adjacency list of node1
network_map[node1].insert(make_pair(node2, w));
}
/* Read in the social graph */
//the format for the social graph is
//each line corresponds to an edge, e.g, 3 2 0.8
//each edge in the graph appears twice, e.g.,
//3 2 0.8
//2 3 0.9
void read_network(){
ifstream in(network_file,ifstream::in);
assert(in);
string line;
vertex node1,node2, max_node=0;
double w;
//read edges
while(getline(in,line)!=NULL){
node1=(vertex)atol(strtok((char *)line.c_str()," \n\t\r"));
node2=(vertex)atol(strtok(NULL," \n\t\r"));
if (weighted_graph == 1) {
w=(double)atof(strtok(NULL," \n\t\r"));
}
else{
w = weight;
}
//cout << node1 << " " << node2 << " " << w << endl;
add_edge(node1, node2,w);
if(node1 > max_node){
max_node = node1;
}
if(node2 > max_node){
max_node = node2;
}
}
in.close();
//number of nodes in the graph
N = network_map.size();
//N = max_node + 1;
//cout<<N<<endl;
//allocate space for final scores
post = (double*) malloc(sizeof(double)*(N));
post_pre = (double*) malloc(sizeof(double)*(N));
//allocate space for final scores
prior = (double*) malloc(sizeof(double)*(N));
cout<<"read network done"<<endl;
}
void read_prior(){
//initialize priors as theta_unl
vertex node;
for (node = 0; node < N; node++) {
prior[node] = 0;
}
if (prior_file != "") {
ifstream in(prior_file, ifstream::in);
assert(in);
string line;
double score;
while (getline(in, line) != NULL){
node = (vertex)atol(strtok((char *)line.c_str(), " \n\t\r"));
score = (double)atof(strtok(NULL, " \n\t\r"));
prior[node] = score;
}
in.close();
}
if (train_set_file != ""){
ifstream in(train_set_file, ifstream::in);
assert(in);
string line;
//reading labeled sybil nodes.
getline(in, line);
getline(in, line);
istringstream neg_train_str(line);
vertex sub;
while (neg_train_str){
neg_train_str >> sub;
neg_train_set.insert(sub);
}
set<vertex>::iterator iter;
for (iter = neg_train_set.begin(); iter != neg_train_set.end(); iter++) {
prior[*iter] = 1.0;
}
in.close();
}
}
void write_posterior(){
ofstream out(post_file, ofstream::out);
for (vertex i = 0; i < N; i++) {
out << i << " " << setprecision(10) << 1-post[i] << endl;
}
out.close();
}
static void * CIA_thread(void *arg_pointer){
Data * pointer = ((CIA_arg *)arg_pointer)->data_pointer;
int current_thread = ((CIA_arg *)arg_pointer)->current_thread;
int num_nodes = ceil(((float)pointer->N) / pointer->num_threads);
int start = current_thread * num_nodes;
int end = current_thread * num_nodes + num_nodes;
if (end > pointer->N) {
end = pointer->N;
}
//cout << "begins " << current_thread << " " << start << " " << end << endl;
vertex node;
double message;
//list<pair<vertex, double> >::iterator nei_iter;
//pair<long, long> s;
//stringstream convert1, convert2;
set<pair<vertex, double> >::iterator iter;
double nei_weight;
double p;
unordered_map<vertex,set<pair<vertex, double> > >::iterator iter_node, find_nei_wei;
for (vertex index = start; index < end; index++) {
node = pointer->ordering_array[index];
message = 0;
if (pointer->network_map.find(node) != pointer->network_map.end()){
for (iter = pointer->network_map[node].begin(); iter != pointer->network_map[node].end(); iter++){
double sum_wei = 0;
find_nei_wei = pointer->network_map.find((*iter).first);
if (find_nei_wei != pointer->network_map.end()){
sum_wei = find_nei_wei->second.size();
}else{
cout<<"network out error at node "<<(*iter).first<<endl;
}
if(sum_wei == 0){
message += 0;
}else{
message += pointer -> post_pre[(*iter).first] * (*iter).second / sum_wei;
}
}
}
pointer->post[node] = (1 - pointer->alpha) * message + pointer->alpha * pointer->prior[node];
}
}
void CIA(){
//use iterative method to propagate messages
//flooding method's performance is not as good as iterative method
ordering_array = (int*) malloc(sizeof(int) * (N) );
//initialize posts
memcpy(post, prior, sizeof(double) * (N));
//random ordering
vector<vertex> ordering;
for (vertex i = 0; i < N; i++) {
ordering.push_back(i);
}
int iter = 1;
vector<vertex>::iterator iter_order;
CIA_arg * arg_pointer;
int current_thread;
pthread_t thread;
vector<pthread_t> threads;
vertex i = 0;
max_iter = (int)log(N);
while (iter <= max_iter) {
threads.clear();
memcpy(post_pre, post, sizeof(double) * (N));
random_shuffle(ordering.begin(), ordering.end(), p_myrandom);
for (iter_order = ordering.begin(), i = 0; iter_order != ordering.end(); iter_order++, i++){
ordering_array[i] = *iter_order;
}
for (current_thread = 0; current_thread < num_threads; current_thread++) {
arg_pointer = new CIA_arg();
arg_pointer->data_pointer = this;
arg_pointer->current_thread = current_thread;
pthread_create(&thread, NULL, CIA_thread, (void*)arg_pointer);
threads.push_back(thread);
}
for (i = 0; i < threads.size(); i++) {
pthread_join(threads[i], NULL);
//cout << i << endl;
}
/* //output result of this iteration
stringstream post_file_iter;
post_file_iter << post_file << iter << ".txt";
const char *addr = post_file_iter.str().c_str();
ofstream out(addr, ofstream::out);
for (vertex i = 0; i < N; i++) {
out << i << " " << setprecision(10) << post[i] << endl;
}
out.close(); */
iter += 1;
}
}
void parse_par(int argc, char **argv){
//default setting
network_file = "";
alpha = 0.15;
max_iter = 10;
theta_pos = 0.9;
theta_neg = 0.1;
theta_unl = 0;
train_set_file = "";
post_file = "";
prior_file = "";
num_threads = 1;
//by default, weighted graph
weighted_graph = 1;
weight = 0.9;
int i = 1;
while (i < argc) {
if (strcmp(argv[i],"-graphfile") == 0){
network_file = argv[i + 1];
}
else if (strcmp(argv[i],"-trainfile") == 0){
train_set_file = argv[i + 1];
}
else if (strcmp(argv[i],"-priorfile") == 0){
prior_file = argv[i + 1];
}
else if (strcmp(argv[i], "-postfile") == 0){
post_file = argv[i + 1];
}
else if (strcmp(argv[i],"-mIter") == 0){
max_iter = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-alpha") ==0){
alpha = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-tp") == 0){
theta_pos = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-tn") == 0){
theta_neg = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-tu") == 0){
theta_unl = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-nt") == 0){
num_threads = atoi(argv[i + 1]);
}
else{
cout << "undefined inputs: " << argv[i] <<endl;
exit(0);
}
i += 2;
}
}
};
int main (int argc, char **argv)
{
srand ( time(NULL) );
Data data;
cout<<1<<endl;
data.parse_par(argc, argv);
cout<<2<<endl;
data.read_network();
cout<<3<<endl;
data.read_prior();
cout<<4<<endl;
data.CIA();
cout << 5 << endl;
data.write_posterior();
return 0;
}