-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiedGS.c
1305 lines (1153 loc) · 33 KB
/
modifiedGS.c
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<omp.h>
#include<math.h>
#include<assert.h>
#include<pthread.h>
#include<limits.h>
#include<semaphore.h>
#include<float.h>
#include<sys/time.h>
#define OMP_PTHREADS 1
#define DEBUG 0
int should_die = 0;
sem_t thread_sema;
int num_threads = 8;
pthread_t match_thread[24];
int thread_ids[24];
int thread_data[24]; /* Int Array for thread data. USAGE: memset in your function and access with thread_data[tid].
* Create new array for a different data type
*/
pthread_attr_t attr;
pthread_mutex_t q_mutex;
int current_color = 0;
/*
* Structure to hold an edge.
* Single long long integer is used to hold two integers
* Given an edge (a , b), assuming both integers:
* Last 32 bits hold 'b' and the prev 32 bits hold 'a'
*/
typedef struct {
long long int x;
pthread_mutex_t e_mutex;
int weight; /* FIXME: should be float? */
int valid;
} edge;
typedef struct {
int id;/* index of the neighbor */
int weight;/* weight of the edge to the neighbor */
} neighbor;
/*
* Structure to hold vertex info.
* degree of vertex
* index of vertex is implicilty stored in the accessing array iterator
*/
typedef struct {
int id;/* integer index */
int deg;/* degree of vertex */
int inC;
int inL;
int inS;/* is in the current MIS */
int inW;/* to be considered for next MIS */
int color;
int match; /* if matched, its match; else -1 */
neighbor *neighbors; /* adjacency list */
int neighbor_count;
pthread_mutex_t v_mutex;
int weight;/* FIXME: should be float? */
int predecessor_id[2]; /* This vertex's association with atmost 2 vertices in previous graph */
int successor_id; /* This vertex's association with the vertex in next graph */
} vertex;
typedef struct graph {
pthread_mutex_t g_mutex;
int num_v; /* number of vertices */
int num_e; /* number of edges */
vertex *vl; /* vertex list */
edge *edl; /* edge list */
int uncolored; /* number of vertices not colored */
int unmatched; /* number of vertices not matched */
int num_colors;
int max_degree;
struct graph *next;/* pointer to next graph */
} graph;
typedef void*(*funct)(void*);
/* element of the work queue */
typedef struct work_elem {
funct func;
int bucket;
struct work_elem *next;
} work;
typedef struct work_queue {
int size;
work *head;
work *tail;
pthread_mutex_t wq_mutex; // for mutual exclusion while adding/removing element
sem_t wq_sema;
} work_queue;
typedef struct queue_elem {
int v; /* index of the vertex */
struct queue_elem *next;
} elem;
typedef struct queue {
int size;
elem *head;
} queue;
graph *gr_root;
graph *cur_gr;/* Points to the current graph being processed. Used by threads for sharing */
graph *next_level_gr;/* Points to the graph next to current graph being processed. Only used in build_adjacnecy_list_next_graph() */
queue *q;
work_queue* wqueue[24];
pthread_t thread[24];
static queue* queue_init() {
queue *q = malloc(sizeof(queue));
if (!q) {
printf("%s: malloc failed \n", __func__);
exit(-1);
}
q->size = 0;
q->head = NULL;
pthread_mutex_init(&q_mutex, NULL);
return q;
}
work_queue* wq_init() {
work_queue *wq = malloc(sizeof(work_queue));
if (!wq) {
printf("%s: malloc failed \n", __func__);
exit(-1);
}
wq->size = 0;
wq->head = NULL;
wq->tail = NULL;
pthread_mutex_init(&wq->wq_mutex, NULL);
if (sem_init(&wq->wq_sema, 0, 0)) {
printf("sem_init failed\n");
exit(0);
}
return wq;
}
static void queue_free(queue *q) {
int i;
assert(q);
elem *cur = q->head;
elem *temp;
while(cur) {
temp = cur->next;
free (cur);
cur = temp;
}
free(q);
pthread_mutex_destroy(&q_mutex);
}
static void queue_add(queue *q, int v) {
elem *e = malloc(sizeof(elem));
if (!e) {
printf("%s: malloc failed\n", __func__);
exit(-1);
}
e->v = v;
/* add element into the front of the queue */
pthread_mutex_lock(&q_mutex);
e->next = q->head;
q->head = e;
q->size++;
pthread_mutex_unlock(&q_mutex);
}
void wq_add(work_queue *wq, funct f) {
work *w = calloc(1, sizeof(work));
if (!w) {
printf("%s: malloc failed\n", __func__);
exit(-1);
}
w->func = f;
w->next = NULL;
pthread_mutex_lock(&wq->wq_mutex);
/* add element at the end of queue */
if (wq->head == NULL) {
wq->head = w;
wq->tail = w;
} else {
wq->tail->next = w;
wq->tail = w;
}
wq->size++;
pthread_mutex_unlock(&wq->wq_mutex);
#if DEBUG > 1
printf("adding an element to wq: size=%d\n", wq->size);
#endif
sem_post(&wq->wq_sema);
}
work* wq_remove(work_queue *wq) {
work *ret;
/* remove element from the front of the queue */
pthread_mutex_lock(&wq->wq_mutex);
#if DEBUG > 1
printf("%s: size=%d, head = %p, tail = %p\n", __func__, wq->size, (void *)wq->head, (void *)wq->tail);
#endif
if (wq->head == NULL || wq->size == 0)
return NULL;
ret = wq->head;
wq->head = ret->next;
wq->size--;
if (wq-> size == 1) {
assert(wq->head == wq->tail);
}
if(wq->head == NULL) {
wq->tail = NULL;
}
pthread_mutex_unlock(&wq->wq_mutex);
return ret;
}
void *thread_func(void *id) {
int tid = *(int *)(id);
work *w;
int value;
while (1) {
#if DEBUG > 1
printf("Thread[%d]: going to sleep...\n", tid);
#endif
//sem_getvalue(&wqueue[tid]->wq_sema, &value);
//printf("The value of the semaphore is %d\n", value);
sem_wait(&wqueue[tid]->wq_sema);
if (should_die) {
printf("Thread[%d] exiting...\n", tid);
pthread_exit(NULL);
}
/* 1. wait till an element is in work_queue
2. pick a work from work_queue
3. Execute it
*/
#if DEBUG > 1
printf("Thread[%d]: woke up\n", tid);
#endif
w = wq_remove(wqueue[tid]);
#if DEBUG > 1
printf("Thread[%d]: died in wq_remove\n", tid);
#endif
assert(w);
assert(w->func);
#if DEBUG > 1
printf("Thread[%d]: died in assert\n", tid);
#endif
w->func(id);
free(w);
}
}
void safe_decrement(int *x) {
graph *gr = cur_gr;
pthread_mutex_lock(&gr->g_mutex);
*x = *x - 1;
pthread_mutex_unlock(&gr->g_mutex);
}
void safe_increment(int *x) {
graph *gr = cur_gr;
pthread_mutex_lock(&gr->g_mutex);
*x = *x + 1;
pthread_mutex_unlock(&gr->g_mutex);
}
void setup_threads(int count)
{
int i;
int ret;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
/* initialize thread ids */
for (i = 0; i < count; i++) {
thread_ids[i] = i;
}
for (i = 0; i < count; i++) {
ret = pthread_create(&thread[i], &attr, thread_func, (void *) &thread_ids[i]);
if (ret) {
printf("Error creating thread %d : %d \n", i, ret);
exit(-1);
} else {
#if DEBUG > 1
printf("created thread %d\n", i);
#endif
}
}
/* initialize semaphores */
sem_init(&thread_sema, 0, 0);
}
/* create requested number of joinable threads */
static void create_threads(int count, void* (*work) (void *))
{
int i;
int ret;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i = 0; i < count; i++) {
ret = pthread_create(&match_thread[i], &attr, work, (void *) &thread_ids[i]);
if (ret) {
printf("Error creating thread %d : %d \n", i, ret);
exit(-1);
} else {
#if DEBUG > 1
printf("created thread %d\n", i);
#endif
}
}
}
/* wait for all the threads to complete */
static void join_threads(int count)
{
int i, ret;
void *status;
for (i = 0; i < count; i++) {
ret = pthread_join(match_thread[i], &status);
if (ret) {
printf("Error joining thread %d : %d\n", i, ret);
} else {
#if DEBUG > 1
printf("joining thread %d\n", i);
#endif
}
}
}
static void printinSVerts(vertex *vl, int numvertices)
{
int i = 0;
for ( i = 0; i < numvertices; i++) {
if (vl[i].inS) {
printf("Vertex = %d Degree = %d\n", i, vl[i].deg);
}
}
}
int isX_greatestNeighborOf_Y_inS(vertex *vl, int ix, int iy)
{
int i = 0, id = 0;
for (i = 0; i < vl[iy].neighbor_count; i++) {
id = vl[iy].neighbors[i].id;
if (vl[id].inS) {
if (vl[ix].deg < vl[id].deg) {
#if DEBUG > 1
printf("vertex = %d not greatest neighbor of vertex = %d in S\n", ix, iy);
#endif
return 0;
}
else if (vl[ix].deg == vl[id].deg) {
if (vl[ix].weight < vl[id].weight) {
#if DEBUG > 1
printf("vertex = %d not greatest neighbor of vertex = %d in S\n", ix, iy);
#endif
return 0;
}
}
}
}
#if DEBUG > 1
printf("vertex = %d is the greatest neighbor of vertex = %d in S\n", ix, iy);
#endif
return 1;
}
/* Checks first for greatest degree and if not then for greatest weight among neighbors */
int greatestAmongNeighbors(vertex *vl, int id)
{
int i = 0, greatestDeg = 1, greatestWeight = 1, idx = 0;
/*
* 1. If my degree greater than any of my neighbors's degree, return true.
* 2. If my degree less than or equal to my neighbors's degree, check if
* my weight greater than any of my neighbor's weight. If yes return true, else false.
*/
for (i = 0; i < vl[id].neighbor_count; i++) {
idx = vl[id].neighbors[i].id;
if (vl[idx].inL) {
if (vl[id].deg < vl[idx].deg) {
return 0;
}
else if (vl[id].deg == vl[idx].deg) {
if (vl[id].weight < vl[idx].weight) {
return 0;
}
}
}
}
return 1;
}
/*
* FIXME: Walk the graph linked list starting from graph_root and
* free all graphs
*/
static void free_graph(graph *gr) {
int i;
assert(gr);
for (i = 0; i < gr->num_v; i++) {
free(gr->vl[i].neighbors);
}
if (gr->vl) free(gr->vl);
if (gr->edl) free(gr->edl);
pthread_mutex_destroy(&gr->g_mutex);
free(gr);
}
static graph* create_graph() {
graph *gr = malloc(sizeof(graph));
if (!gr) {
printf("%s: malloc failed\n", __func__);
exit(-1);
}
gr->vl = NULL;
gr->edl = NULL;
gr->max_degree = 0;
gr->num_colors = 0;
gr->uncolored = 0;
gr->unmatched = 0;
pthread_mutex_init(&gr->g_mutex, NULL);
gr->next = NULL;
return gr;
}
static void print_full_graph(graph *gr, int match)
{
int i;
for (i = 0; i < gr->num_v; i++) {
int j;
if (match) {
/* print match in the parantheses */
printf("%d(match:%d)", i, gr->vl[i].match);
} else {
/* print color in the parantheses */
printf("%d(color:%d)", i, gr->vl[i].color);
}
printf(" (sid:%d) ", gr->vl[i].successor_id);
printf("(pid[0]:%d pid[1]:%d)\n", gr->vl[i].predecessor_id[0], gr->vl[i].predecessor_id[1]);
for(j = 0; j < gr->vl[i].neighbor_count; j++) {
printf("->");
printf("%d(%d)", gr->vl[i].neighbors[j].id, gr->vl[i].neighbors[j].weight);
}
printf("\n");
}
}
static void populate_graph(graph *gr, char *file)
{
FILE *fp;
int i = 0, x, y, w;
int j = 0;
long long e;
fp = fopen(file, "r");
fscanf(fp, "%d %d", &gr->num_v, &gr->num_e);
gr->uncolored = gr->num_v;
gr->unmatched = gr->num_v;
/* I assumed that there are more vertices than threads.
* If this assert hits, change the graph or num_threads.
* The issue will be with sharing across threads.
*/
assert(gr->num_v > num_threads);
gr->edl = (edge *)calloc(gr->num_e, sizeof(edge));
if (!gr->edl) {
printf("Failed to allocate memory for graph edges. Bailing out.\n");
exit(0);
}
gr->vl = (vertex *)calloc(gr->num_v, sizeof(vertex));
if (!gr->vl) {
printf("Failed to allocate memory for graph vertices. Bailing out.\n");
free_graph(gr);
exit(0);
}
for (i = 0; i < gr->num_e; i++) {
fscanf(fp, "%d %d %d", &x, &y, &w);
e = 0;
x -= 1;
y -= 1;
e = (long long int)x;
e = e << 32;
e = e | (long long int)y;
gr->edl[i].x = e;
gr->edl[i].valid = 1;
gr->edl[i].weight = w;
#if DEBUG > 1
printf("Added edge %d,%d\n", x, y);
#endif
/* for vertex 'x' */
gr->vl[x].id = x;
gr->vl[x].deg++;
gr->vl[x].color = 0; /* colors start from 1 */
gr->vl[x].match = -1; /* vertex is not yet matched */
gr->vl[x].inC = 1;
gr->vl[x].inS = 0;
gr->vl[x].inL = 0;
gr->vl[x].predecessor_id[0] = -1;
gr->vl[x].predecessor_id[1] = -1;
gr->vl[x].successor_id = -1;
if (gr->max_degree < gr->vl[x].deg) { gr->max_degree = gr->vl[x].deg; }
/* for vertex 'y' */
gr->vl[y].id = y;
gr->vl[y].deg++;
gr->vl[y].color = 0;
gr->vl[y].match = -1;
gr->vl[y].inC = 1;
gr->vl[y].inS = 0;
gr->vl[y].inL = 0;
gr->vl[y].predecessor_id[0] = -1;
gr->vl[y].predecessor_id[1] = -1;
gr->vl[y].successor_id = -1;
if (gr->max_degree < gr->vl[y].deg) { gr->max_degree = gr->vl[y].deg; }
}
/* Loop through the edges to create adjacency list for each vertex */
for (i = 0; i < gr->num_e; i++) {
x = gr->edl[i].x >> 32;
y = gr->edl[i].x & 0xFFFFFFFF;
if (gr->vl[x].neighbors == NULL) {
gr->vl[x].neighbors = (neighbor *)calloc(gr->max_degree, sizeof(neighbor));
}
if (gr->vl[y].neighbors == NULL) {
gr->vl[y].neighbors = (neighbor *)calloc(gr->max_degree, sizeof(neighbor));
}
gr->vl[x].neighbors[gr->vl[x].neighbor_count].id = y;
gr->vl[x].neighbors[gr->vl[x].neighbor_count].weight = gr->edl[i].weight;
gr->vl[x].neighbor_count++;
gr->vl[y].neighbors[gr->vl[y].neighbor_count].id = x;
gr->vl[y].neighbors[gr->vl[y].neighbor_count].weight = gr->edl[i].weight;
gr->vl[y].neighbor_count++;
}
}
/* get the smallest color not taken by any
* of the neighbors of the given vertex id
*/
static int get_best_color(int id)
{
graph *gr = cur_gr;
int i, j, idx;
int min = 1, max = gr->num_colors;
int ret = 1;
int is_seen = 0;
assert(gr->vl[id].color < 1); /* shouldn't be colored earlier */
for (i = min; i <= max; i++) {
is_seen = 0;
for (j = 0; j < gr->vl[id].neighbor_count; j++) {
idx = gr->vl[id].neighbors[j].id;
if (gr->vl[idx].color == i) {
is_seen = 1;
}
}
if (is_seen == 0)
return i;
}
return i;
}
/* find the neighbor for vertex 'src'
* that is not yet matched and has the
* highest weight.
* FIXME: for now weight is just the index
* New Policy: If my index is greatest of all my neighbors, go ahead else quit.
* Also find the greatest indexed one of all my neighbors. Then pick that neighbor and see if I am its greatest indexed neighbor.
* If true pair myself with that neighbor.
*/
int get_best_unmatched_neighbor(graph *gr, int src) {
int i = 0, ret = -1, max_w = -1, idx = 0;
for (i = 0; i < gr->vl[src].neighbor_count; i++) {
idx = gr->vl[src].neighbors[i].id;
#if 0
if (gr->vl[idx].match == -1 && gr->vl[idx].weight > max_w) {
max_w = gr->vl[idx].weight;
ret = idx;
}
#endif
if (gr->vl[idx].match == -1 && idx > max_w) {
max_w = idx;
}
}
return max_w;
#if 0
int greatest_neighbor = -1;
int idx, i;
for (i = 0; i < gr->vl[src].neighbor_count; i++) {
idx = gr->vl[src].neighbors[i].id;
if (idx > greatest_neighbor) greatest_neighbor = idx;
}
if (src < greatest_neighbor) {
return -1;/* let the greatest neighbor handle matching */
}
/* Check if I am the greatest neighbor of my greatest neighbor */
for (i = 0; i < gr->vl[greatest_neighbor].neighbor_count; i++) {
idx = gr->vl[greatest_neighbor].neighbors[i].id;
if (idx > src) return -1;
}
return greatest_neighbor;
#endif
}
void* do_matching(void *tid) {
graph *gr = cur_gr;
int share = (gr->num_v)/num_threads;
int start = *(int *)(tid) * share;
int end = start + share - 1;
int i;
int src, target; //(u, v) in algo
/* last threads gets all remaining */
if (*(int *)tid == num_threads - 1) {
end = gr->num_v - 1;
}
#if DEBUG > 1
printf("%s: I am thread[%d] working on [%d to %d]\n", __func__, *(int*)tid, start, end);
#endif
/* no locking needed because each thread
* get mutually exclusive share of the vertices
*/
for (i = start; i <= end; i++) {
src = i;
/* unmatched vertices of this color */
if (gr->vl[src].color == current_color &&
gr->vl[src].match == -1) {
/* FIXME: ensures ties are broken and only one thread changes the match */
target = get_best_unmatched_neighbor(gr, src);
if ( target > -1) {
gr->vl[src].match = target;
gr->vl[target].match = src;
safe_decrement(&gr->unmatched);
safe_decrement(&gr->unmatched);
queue_add(q, src);
}
}
}
pthread_attr_destroy(&attr);
pthread_exit(NULL);
}
void maximal_matching(graph *gr)
{
int colors = gr->num_colors;
int i;
elem *cur;
int src_match, dst_match, dst, j;
for (i = 1; i <= gr->num_colors; i++) {
current_color = i;
q = queue_init();
#if 0
#pragma omp for
for (j = 0; j < gr->num_v; j++) {
src = j;
/* unmatched vertices of this color */
if (gr->vl[src].color == i &&
gr->vl[src].match == -1) {
/* FIXME: ensures ties are broken and only one thread changes the match */
target = get_best_unmatched_neighbor(gr, src);
gr->vl[src].match = target;
gr->vl[target].match = src;
queue_add(q, src);
}
}
#endif
create_threads(num_threads, do_matching);
join_threads(num_threads);
cur = q->head;
while(cur) {
src_match = gr->vl[cur->v].match;
dst = src_match;
dst_match = gr->vl[dst].match;
if (src_match > -1 && dst_match != cur->v) {
gr->vl[cur->v].match = -1;
safe_increment(&gr->unmatched);
safe_increment(&gr->unmatched);
}
cur = cur->next;
}
if (((gr->num_v - gr->unmatched) % 2) != 0) {
printf("======= Error in matching Total vertices %d, unmatched %d====== \n", gr->num_v, gr->unmatched);
#if 0
print_full_graph(cur_gr, 1);
#endif
for (i = 0; i < gr->num_v; i++) {
if (gr->vl[i].match == -1) {
printf("Unmatched %d(-1)\n", i);
} else {
if (i != gr->vl[gr->vl[i].match].match) {
printf("Incorrect match %d matched to (%d) while %d matched to (%d)", i, gr->vl[i].match, gr->vl[i].match, gr->vl[gr->vl[i].match].match);
} else {
printf("Correct match %d(%d)\n", i, gr->vl[i].match);
}
}
}
}
assert(((gr->num_v - gr->unmatched) % 2) == 0);
queue_free(q);
}
}
void * mis_transition_from_L_to_S(void *thread_id)
{
graph *gr = cur_gr;
int share = (gr->num_v)/num_threads;
int tid = *(int *)(thread_id);
int start = tid * share;
int end = start + share - 1;
int i;
/* last threads gets all remaining */
if (tid == num_threads - 1) {
end = gr->num_v - 1;
}
#if DEBUG > 1
printf("%s: I am thread[%d] working on [%d to %d]\n", __func__, tid, start, end);
#endif
/* no locking needed because each thread
* get mutually exclusive share of the vertices
*/
for (i = start; i <= end; i++) {
if(gr->vl[i].inL) {
if(greatestAmongNeighbors(gr->vl, i)) {
/* Put yourself in S. Remove yourself from C */
if(gr->vl[i].inS) {
continue;
}
gr->vl[i].inS = 1;
gr->vl[i].inC = 0;
#if DEBUG > 1
printf("Putting vertex = %d weight = %d in S\n", i, gr->vl[i].weight);
#endif
thread_data[tid]++;
}
}
}
pthread_attr_destroy(&attr);
pthread_exit(NULL);
}
void * mis_transition_remove_neighbors_of_S_from_C(void *thread_id)
{
graph *gr = cur_gr;
int share = (gr->num_v)/num_threads;
int tid = *(int *)(thread_id);
int start = tid * share;
int end = start + share - 1;
int i, j;
/* last threads gets all remaining */
if (tid == num_threads - 1) {
end = gr->num_v - 1;
}
#if DEBUG > 1
printf("%s: I am thread[%d] working on [%d to %d]\n", __func__, tid, start, end);
#endif
/* no locking needed because each thread
* get mutually exclusive share of the vertices
*/
for (i = start; i <= end; i++) {
if(gr->vl[i].inS) {
gr->vl[i].inL = 0;
/* Walk through my neighbor list. Delete each neighbor if I am its greatest neighbor */
for (j = 0; j < gr->vl[i].neighbor_count; j++) {
int idx = gr->vl[i].neighbors[j].id;
#if DEBUG > 1
printf("vertex = %d neighbor count = %d Neighbor vertex = %d\n", i, gr->vl[i].neighbor_count, idx);
#endif
if (gr->vl[idx].inC) {
if(isX_greatestNeighborOf_Y_inS(gr->vl, i, idx)) {
gr->vl[idx].inC = 0;
gr->vl[idx].inL = 0;
thread_data[tid]++;
#if DEBUG > 1
printf("vertex = %d removed vertex = %d from C\n", i, idx);
#endif
}
}
}
}
}
pthread_attr_destroy(&attr);
pthread_exit(NULL);
}
static void omp_mis_luby(graph *gr)
{
int i = 0, x, y, nvertsinC = 0, nedges = 0;
int j = 0, itr = -1;
long long maxweight, e;
float sum_of_sel_probability = 0.0;
/* Run luby's only for uncolored vertices */
for (i = 0; i < gr->num_v; i++) {
if(gr->vl[i].color == 0) { /* not yet colored */
gr->vl[i].inC = 1; /* include them in the MIS */
nvertsinC++;
}
}
maxweight = pow(nvertsinC, 4);
while (nvertsinC) {
int sum_removed = 0, num_verts_to_choose = 0;
sum_of_sel_probability = 0.0;
itr++;
// printf("Iteration = %d vertices in C = %d\n", itr, nvertsinC);
/* Clear thread_data before usage */
memset(thread_data, 0, sizeof(thread_data));
for (i = 0; i < gr->num_v; i++) {
if(gr->vl[i].inC) {
gr->vl[i].weight = rand() % maxweight;
sum_of_sel_probability += 1.0/(2 * (gr->vl[i].deg));
}
}
/* Choose a random number k between 2 to numvertices/2
* Select 1 vertex based on 1/2d probability
* Select a random weight between 0 to sum_of_sel_probability
* Find the weight that causes current sum to cross the random number
* Repeat k times
*/
num_verts_to_choose = nvertsinC;
if(nvertsinC > num_threads) {
num_verts_to_choose = num_threads + rand()%(nvertsinC/2);
}
#if DEBUG > 1
printf("Num vertices to choose in L = %d\n", num_verts_to_choose);
#endif
while(num_verts_to_choose) {
float rnd, cursum = 0.0;
rnd = drand48()*sum_of_sel_probability;
for (i = 0; i < gr->num_v; i++) {
if(gr->vl[i].inC && !gr->vl[i].inL) {
cursum += 1.0/(2* (gr->vl[i].deg));
if (cursum > rnd) {
gr->vl[i].inL = 1;
sum_of_sel_probability -= 1.0/(2 * (gr->vl[i].deg));
#if DEBUG > 1
printf("Vertex chosen in L = %d\n", i);
#endif
break;
}
}
}
num_verts_to_choose--;
}
create_threads(num_threads, mis_transition_from_L_to_S);
join_threads(num_threads);
#if 0
#pragma omp parallel for
for (i = 0; i < gr->num_v; i++) {
if(gr->vl[i].inL) {
if(greatestAmongNeighbors(gr->vl, i)) {
/* Put yourself in S. Remove yourself from C */
if(gr->vl[i].inS) {
continue;
}
gr->vl[i].inS = 1;
gr->vl[i].inC = 0;
#if DEBUG > 1
printf("Putting vertex = %d weight = %d in S\n", i, gr->vl[i].weight);
#endif
remVertsPerThread[omp_get_thread_num()]++;
}
}
}
#endif
create_threads(num_threads, mis_transition_remove_neighbors_of_S_from_C);
join_threads(num_threads);
#if 0
/* Remove neighbors of nodes in S */
#pragma omp parallel for private(j)
for (i = 0; i < gr->num_v; i++) {
if(gr->vl[i].inS) {
gr->vl[i].inL = 0;
/* Walk through my neighbor list. Delete each neighbor if I am its greatest neighbor */
for (j = 0; j < gr->vl[i].neighbor_count; j++) {
int idx = gr->vl[i].neighbors[j].id;
#if DEBUG > 1
printf("vertex = %d neighbor count = %d Neighbor vertex = %d\n", i, gr->vl[i].neighbor_count, idx);
#endif
if (gr->vl[idx].inC) {
if(isX_greatestNeighborOf_Y_inS(gr->vl, i, idx)) {
gr->vl[idx].inC = 0;
gr->vl[idx].inL = 0;
//nvertsinC--;
remVertsPerThread[omp_get_thread_num()]++;
#if DEBUG > 1
printf("vertex = %d removed vertex = %d from C\n", i, idx);
#endif
}
}
}
}
}
#endif
for (i = 0; i < num_threads; i++) {
sum_removed += thread_data[i];
}
nvertsinC -= sum_removed;
}
//printf("Total iterations = %d\n", itr);
//printf("Final set of independent vertices.\n");
//printinSVerts(gr->vl, gr->num_v);
}
void *do_color_graph(void *tid)
{
graph *gr = cur_gr;
int share = (gr->num_v)/num_threads;
int start = *(int *)(tid) * share;
int end = start + share - 1;
int i;
/* last threads gets all remaining */
if (*(int *)tid == num_threads - 1) {
end = gr->num_v - 1;
}
#if DEBUG > 1
printf("%s: I am thread[%d] working on [%d to %d]\n", __func__, *(int*)tid, start, end);
#endif
/* no locking needed because each thread
* get mutually exclusive share of the vertices
*/
for (i = start; i <= end; i++) {
if(gr->vl[i].inS == 1) { /* in MIS */
assert(gr->vl[i].color == 0);
gr->vl[i].color = get_best_color(i);
if (gr->vl[i].color > gr->num_colors) {
gr->num_colors = gr->vl[i].color;
}
safe_decrement(&gr->uncolored);
gr->vl[i].inS = 0; /* remove from S */
gr->vl[i].inC = 0; /* FIXME: needed? remove from C */
}
}
pthread_attr_destroy(&attr);