-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcuMetis.cu
3921 lines (3014 loc) · 98.5 KB
/
cuMetis.cu
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"include/cuMetis.h"
int test_time;
/*Time function params*/
double part_all = 0;
struct timeval begin_part_all;
struct timeval end_part_all;
double part_coarsen = 0;
struct timeval begin_part_coarsen;
struct timeval end_part_coarsen;
double part_init = 0;
struct timeval begin_part_init;
struct timeval end_part_init;
double part_uncoarsen = 0;
struct timeval begin_part_uncoarsen;
struct timeval end_part_uncoarsen;
//four calculation pattern
double part_match = 0;
struct timeval begin_part_match;
struct timeval end_part_match;
double part_contract = 0;
struct timeval begin_part_contract;
struct timeval end_part_contract;
double part_cmatch = 0;
struct timeval begin_part_cmatch;
struct timeval end_part_cmatch;
double part_ccontract = 0;
struct timeval begin_part_ccontract;
struct timeval end_part_ccontract;
double part_bfs = 0;
struct timeval begin_part_bfs;
struct timeval end_part_bfs;
double part_2refine = 0;
struct timeval begin_part_2refine;
struct timeval end_part_2refine;
double part_2map = 0;
struct timeval begin_part_2map;
struct timeval end_part_2map;
double part_slipt = 0;
struct timeval begin_part_slipt;
struct timeval end_part_slipt;
double part_krefine = 0;
struct timeval begin_part_krefine;
struct timeval end_part_krefine;
double part_map = 0;
struct timeval begin_part_map;
struct timeval end_part_map;
//match
double scuda_match = 0;
struct timeval begin_cuda_match;
struct timeval end_cuda_match;
double scuda_cleanv = 0;
struct timeval begin_cuda_cleanv;
struct timeval end_cuda_cleanv;
double sfindc1 = 0;
struct timeval begin_findc1;
struct timeval end_findc1;
double sfindc2 = 0;
struct timeval begin_findc2;
struct timeval end_findc2;
double sinclusive_scan = 0;
struct timeval begin_inclusive_scan;
struct timeval end_inclusive_scan;
double sfindc2_5 = 0;
struct timeval begin_findc2_5;
struct timeval end_findc2_5;
double sfindc3 = 0;
struct timeval begin_findc3;
struct timeval end_findc3;
double sfindc4 = 0;
struct timeval begin_findc4;
struct timeval end_findc4;
double sexclusive_scan = 0;
struct timeval begin_exclusive_scan;
struct timeval end_exclusive_scan;
double sfind_cnvtxsedge_original = 0;
struct timeval begin_find_cnvtxsedge_original;
struct timeval end_find_cnvtxsedge_original;
double sbb_segsort = 0;
struct timeval begin_bb_segsort;
struct timeval end_bb_segsort;
double sSort_cnedges_part2 = 0;
struct timeval begin_Sort_cnedges_part2;
struct timeval end_Sort_cnedges_part2;
double sinclusive_scan2 = 0;
struct timeval begin_inclusive_scan2;
struct timeval end_inclusive_scan2;
double sinitcuda_match = 0;
struct timeval begin_initcuda_match;
struct timeval end_initcuda_match;
double sinitcudajs = 0;
struct timeval begin_initcudajs;
struct timeval end_initcudajs;
double sSort_cnedges_part1 = 0;
struct timeval begin_Sort_cnedges_part1;
struct timeval end_Sort_cnedges_part1;
double sSort_cnedges_part2_5 = 0;
struct timeval begin_Sort_cnedges_part2_5;
struct timeval end_Sort_cnedges_part2_5;
double sSort_cnedges_part3 = 0;
struct timeval begin_Sort_cnedges_part3;
struct timeval end_Sort_cnedges_part3;
double sCoarsen = 0;
struct timeval begin_sCoarsen;
struct timeval end_sCoarsen;
/*Define functions*/
#define cuMetis_max(m,n) ((m)>=(n)?(m):(n))
#define cuMetis_min(m,n) ((m)>=(n)?(n):(m))
#define cuMetis_swap(m,n,temp) do{(temp)=(m);(m)=(n);(n)=(temp);} while(0)
#define cuMetis_tocsr(i,n,c) do{for(i=1;i<n;i++)c[i]+= c[i-1];for(i=n;i>0;i--)c[i]=c[i-1];c[0]=0;} while(0)
#define cuMetis_add_sub(m,n,temp) do{(m)+=(temp);(n)-=(temp);} while(0)
#define cuMetis_listinsert(n,list,lptr,i) do{list[n]=i;lptr[i]=(n)++;} while(0)
#define cuMetis_listdelete(n,list,lptr,i) do{list[lptr[i]]=list[--(n)];lptr[list[n]]=lptr[i];lptr[i]=-1;} while(0)
#define M_GT_N(m,n) ((m)>(n))
/*Graph data structure*/
typedef struct cuMetis_graph_t {
/*graph cpu params*/
int nvtxs; //Graph vertex
int nedges; //Graph edge
int *xadj; //Graph vertex csr array (xadj[nvtxs+1])
int *adjncy; //Graph adjacency list (adjncy[nedges])
int *adjwgt; //Graph edge weight array (adjwgt[nedges])
int *vwgt; //Graph vertex weight array(vwgr[nvtxs])
int *tvwgt; //The sum of graph vertex weight
float *tvwgt_reverse; //The reciprocal of tvwgt
int *label; //Graph vertex label(label[nvtxs])
int *cmap; //The Label of graph vertex in cgraph(cmap[nvtxs])
int mincut; //The min edfe-cut of graph partition
int *where; //The label of graph vertex in which part(where[nvtxs])
int *pwgts; //The partition vertex weight(pwgts[nparts])
int nbnd; //Boundary vertex number
int *bndlist; //Boundary vertex list
int *bndptr; //Boundary vertex pointer
int *id; //The sum of edge weight in same part
int *ed; //The sum of edge weight in different part
struct cuMetis_graph_t *coarser; //The coarser graph
struct cuMetis_graph_t *finer; //The finer graph
/*graph gpu params*/
int *cuda_nvtxs;
int *cuda_xadj;
int *cuda_adjncy;
int *cuda_adjwgt;
int *cuda_vwgt;
int *cuda_match; //CUDA graph vertex match array(match[nvtxs])
int *cuda_cmap;
int *cuda_maxvwgt; //CUDA graph constraint vertex weight
int *cuda_real_edge; //CUDA graph vertex pairs csr edge array(cuda_real_edge[cnvtxs+1])
int *cuda_real_nvtxs; //CUDA graph params (i<match[cmap[i]])
int *cuda_cnvtxs; //CUDA coarsen graph vertex
int *cuda_s; //CUDA support array (cuda_s[nvtxs])
int *cuda_scan_adjwgt_original; //CUDA support scan array (cuda_scan_adjwgt_original[nedges])
int *cuda_scan_nedges_original; //CUDA support scan array (cuda_scan_nedges_original[nedges])
int *cuda_js; //CUDA support array (cuda_js[cnvtxs])
int *cuda_scan_cnedges_original; //CUDA support scan array (cuda_scan_cnedges_original[nedges])
int *cuda_maxwgt; //CUDA part weight array (cuda_maxwgt[npart])
int *cuda_minwgt; //CUDA part weight array (cuda_minwgt[npart])
int *cuda_where;
int *cuda_pwgts;
int *cuda_bnd;
int *cuda_bndnum;
int *cpu_bndnum;
int *cuda_info; //CUDA support array(cuda_info[bnd_num*nparts])
int *cuda_real_bnd_num;
int *cuda_real_bnd;
int *cuda_nparts;
int *cuda_tvwgt;
float *cuda_tpwgts;
} cuMetis_graph_t;
/*Refinement available generate array*/
int *cu_bn;
int *cu_bt;
int *cu_g;
int *cu_csr;
int *cu_que;
/*Memory allocation information*/
typedef struct cuMetis_mop_t {
int type;
ssize_t nbytes;
void *ptr;
} cuMetis_mop_t;
/*Algorithm information*/
typedef struct cuMetis_mcore_t {
void *core;
size_t coresize;
size_t corecpos;
size_t nmops;
size_t cmop;
cuMetis_mop_t *mops;
size_t num_callocs;
size_t num_hallocs;
size_t size_callocs;
size_t size_hallocs;
size_t cur_callocs;
size_t cur_hallocs;
size_t max_callocs;
size_t max_hallocs;
} cuMetis_mcore_t;
/*Control information*/
typedef struct cuMetis_admin_t {
int Coarsen_threshold;
int nIparts;
int iteration_num;
int *maxvwgt;
int nparts;
float *ubfactors;
float *tpwgts;
float *part_balance;
float cfactor;
cuMetis_mcore_t *mcore;
size_t nbrpoolsize;
size_t nbrpoolcpos;
} cuMetis_admin_t;
/*Heap information*/
typedef struct cuMetis_rkv_t{
float key;
int val;
} cuMetis_rkv_t;
/*Queue information*/
typedef struct {
ssize_t nnodes;
ssize_t maxnodes;
cuMetis_rkv_t *heap;
ssize_t *locator;
} cuMetis_queue_t;
/*Compute log2 algorithm*/
int cuMetis_compute_log2(int a)
{
int i;
for(i=1;a>1;i++,a=a>>1);
return i-1;
}
/*Get int rand number*/
int cuMetis_int_rand()
{
if(sizeof(int)<=sizeof(int32_t))
return (int)(uint32_t)rand();
else
return (int)(uint64_t)rand();
}
/*Get int rand number between (0,max)*/
int cuMetis_int_randinrange(int max)
{
return (int)((cuMetis_int_rand())%max);
}
/*Compute sum of int array*/
int cuMetis_int_sum(size_t n, int *a)
{
size_t i;
int sum=0;
for(i=0;i<n;i++,a+=1){
sum+=(*a);
}
return sum;
}
/*Copy int array a to b*/
int *cuMetis_int_copy(size_t n, int *a, int *b)
{
return (int *)memmove((void *)b, (void *)a, sizeof(int)*n);
}
/*Set int array value*/
int *cuMetis_int_set_value(size_t n, int val, int *a)
{
size_t i;
for(i=0;i<n;i++){
a[i]=val;
}
return a;
}
/*Compute sum of float array*/
float cuMetis_float_sum(size_t n, float *a)
{
size_t i;
float sum=0;
for(i=0;i<n;i++,a+=1){
sum+=(*a);
}
return sum;
}
/*Rescale tpwgts array*/
float *cuMetis_tpwgts_rescale(size_t n, float wsum, float *a)
{
size_t i;
for(i=0;i<n;i++,a+=1){
(*a)*=wsum;
}
return a;
}
/*Compute Partition result edge-cut*/
int cuMetis_computecut(cuMetis_graph_t *graph, int *where)
{
int i,j,cut=0;
for(i=0;i<graph->nvtxs;i++){
for(j=graph->xadj[i];j<graph->xadj[i+1];j++)
if(where[i]!=where[graph->adjncy[j]]){
cut+=graph->adjwgt[j];
}
}
return cut/2;
}
/*Set graph admin params*/
cuMetis_admin_t *cuMetis_set_graph_admin(int nparts, float *tpwgts, float *ubvec)
{
int i;
cuMetis_admin_t *cuMetis_admin;
cuMetis_admin=(cuMetis_admin_t *)malloc(sizeof(cuMetis_admin_t));
memset((void *)cuMetis_admin,0,sizeof(cuMetis_admin_t));
cuMetis_admin->iteration_num=10;
cuMetis_admin->Coarsen_threshold=200;
cuMetis_admin->nparts=nparts;
cuMetis_admin->maxvwgt=(int*)malloc(sizeof(int));
cuMetis_admin->maxvwgt[0]=0;
cuMetis_admin->tpwgts=(float*)malloc(sizeof(float)*nparts);
for(i=0;i<nparts;i++){
cuMetis_admin->tpwgts[i]=1.0/nparts;
}
cuMetis_admin->ubfactors=(float*)malloc(sizeof(float));
cuMetis_admin->ubfactors[0] =1.03;
cuMetis_admin->part_balance =(float*) malloc(sizeof(float)*nparts);
return cuMetis_admin;
}
/*Set graph params*/
void cuMetis_init_cpu_graph(cuMetis_graph_t *graph)
{
memset((void *)graph,0,sizeof(cuMetis_graph_t));
graph->nvtxs = -1;
graph->nedges = -1;
graph->xadj = NULL;
graph->vwgt = NULL;
graph->adjncy = NULL;
graph->adjwgt = NULL;
graph->label = NULL;
graph->cmap = NULL;
graph->tvwgt = NULL;
graph->tvwgt_reverse = NULL;
graph->where = NULL;
graph->pwgts = NULL;
graph->mincut = -1;
graph->nbnd = -1;
graph->id = NULL;
graph->ed = NULL;
graph->bndptr = NULL;
graph->bndlist = NULL;
graph->coarser = NULL;
graph->finer = NULL;
}
/*Malloc graph*/
cuMetis_graph_t *cuMetis_create_cpu_graph(void)
{
cuMetis_graph_t *graph;
graph=(cuMetis_graph_t *)malloc(sizeof(cuMetis_graph_t));
cuMetis_init_cpu_graph(graph);
return graph;
}
/*Set graph tvwgt value*/
void cuMetis_set_graph_tvwgt(cuMetis_graph_t *graph)
{
if(graph->tvwgt==NULL){
graph->tvwgt=(int*)malloc(sizeof(int));
}
if(graph->tvwgt_reverse==NULL){
graph->tvwgt_reverse=(float*)malloc(sizeof(float));
}
graph->tvwgt[0]=cuMetis_int_sum(graph->nvtxs,graph->vwgt);
graph->tvwgt_reverse[0]=1.0/(graph->tvwgt[0]>0?graph->tvwgt[0]:1);
}
/*Set graph vertex label*/
void cuMetis_set_graph_label(cuMetis_graph_t *graph)
{
int i;
if(graph->label==NULL){
graph->label=(int*)malloc(sizeof(int)*(graph->nvtxs));
}
for(i=0;i<graph->nvtxs;i++){
graph->label[i]=i;
}
}
/*Set graph information*/
cuMetis_graph_t *cuMetis_set_graph(cuMetis_admin_t *cuMetis_admin, int nvtxs, \
int *xadj, int *adjncy, int *vwgt , int *adjwgt)
{
int i;
cuMetis_graph_t *graph;
graph = cuMetis_create_cpu_graph();
graph->nvtxs=nvtxs;
graph->nedges=xadj[nvtxs];
graph->xadj=xadj;
graph->adjncy=adjncy;
if(vwgt){
graph->vwgt=vwgt;
}
else{
vwgt=graph->vwgt=(int*)malloc(sizeof(int)*nvtxs);
for(i=0;i<nvtxs;i++){
vwgt[i]=graph->vwgt[i]=1;
}
}
graph->tvwgt=(int*)malloc(sizeof(int));
graph->tvwgt_reverse=(float*)malloc(sizeof(float));
graph->tvwgt[0]=cuMetis_int_sum(nvtxs, vwgt);
graph->tvwgt_reverse[0]=1.0/(graph->tvwgt[0]>0?graph->tvwgt[0]:1);
if(adjwgt){
graph->adjwgt=adjwgt;
}
else{
adjwgt=graph->adjwgt=(int*)malloc(sizeof(int)*(graph->nedges));
for(i=0;i<graph->nedges;i++){
adjwgt[i]=graph->adjwgt[i]=1;
}
}
cuMetis_set_graph_tvwgt(graph);
cuMetis_set_graph_label(graph);
return graph;
}
/*Creates mcore*/
cuMetis_mcore_t *cuMetis_create_mcore(size_t coresize)
{
cuMetis_mcore_t *mcore;
mcore=(cuMetis_mcore_t *)malloc(sizeof(cuMetis_mcore_t));
memset(mcore,0,sizeof(cuMetis_mcore_t));
mcore->coresize=coresize;
mcore->corecpos=0;
mcore->core=(coresize==0?NULL:(size_t*)malloc(sizeof(size_t)*(mcore->coresize)));
mcore->nmops=2048;
mcore->cmop=0;
mcore->mops=(cuMetis_mop_t *)malloc((mcore->nmops)*sizeof(cuMetis_mop_t));
return mcore;
}
/*Allocate work space*/
void cuMetis_allocatespace(cuMetis_admin_t *cuMetis_admin, cuMetis_graph_t *graph)
{
size_t coresize;
coresize=3*(graph->nvtxs+1)*sizeof(int)+5*(cuMetis_admin->nparts+1)*sizeof(int)\
+5*(cuMetis_admin->nparts+1)*sizeof(float);
cuMetis_admin->mcore=cuMetis_create_mcore(coresize);
cuMetis_admin->nbrpoolsize=0;
cuMetis_admin->nbrpoolcpos=0;
}
/*Add memory allocation*/
void cuMetis_add_mcore(cuMetis_mcore_t *mcore, int type, size_t nbytes, void *ptr)
{
if(mcore->cmop==mcore->nmops){
mcore->nmops*=2;
mcore->mops=(cuMetis_mop_t*)realloc(mcore->mops, mcore->nmops*sizeof(cuMetis_mop_t));
if(mcore->mops==NULL){
exit(0);
}
}
mcore->mops[mcore->cmop].type=type;
mcore->mops[mcore->cmop].nbytes=nbytes;
mcore->mops[mcore->cmop].ptr=ptr;
mcore->cmop++;
switch(type){
case 1:
break;
case 2:
mcore->num_callocs++;
mcore->size_callocs+=nbytes;
mcore->cur_callocs+=nbytes;
if(mcore->max_callocs<mcore->cur_callocs){
mcore->max_callocs=mcore->cur_callocs;
}
break;
case 3:
mcore->num_hallocs++;
mcore->size_hallocs+=nbytes;
mcore->cur_hallocs+=nbytes;
if(mcore->max_hallocs<mcore->cur_hallocs){
mcore->max_hallocs=mcore->cur_hallocs;
}
break;
default:
exit(0);
}
}
/*Malloc mcore*/
void *cuMetis_malloc_mcore(cuMetis_mcore_t *mcore, size_t nbytes)
{
void *ptr;
nbytes+=(nbytes%8==0?0:8-nbytes%8);
if(mcore->corecpos+nbytes<mcore->coresize){
ptr=((char *)mcore->core)+mcore->corecpos;
mcore->corecpos+=nbytes;
cuMetis_add_mcore(mcore,2,nbytes,ptr);
}
else{
ptr=(size_t*)malloc(nbytes);
cuMetis_add_mcore(mcore,3,nbytes,ptr);
}
return ptr;
}
/*Malloc mcore space*/
void *cuMetis_malloc_space(cuMetis_admin_t *cuMetis_admin, size_t nbytes)
{
return cuMetis_malloc_mcore(cuMetis_admin->mcore,nbytes);
}
/*Malloc int mcore space*/
int *cuMetis_int_malloc_space(cuMetis_admin_t *cuMetis_admin, size_t n)
{
return (int *)cuMetis_malloc_space(cuMetis_admin, n*sizeof(int));
}
/*Malloc float mcore space*/
float *cuMetis_float_malloc_space(cuMetis_admin_t *cuMetis_admin)
{
return (float *)cuMetis_malloc_space(cuMetis_admin,2*sizeof(float));
}
/*Compute 2way balance params*/
void cuMetis_compute_2way_balance(cuMetis_admin_t *cuMetis_admin, cuMetis_graph_t *graph, float *tpwgts)
{
int i;
for(i=0;i<2;i++){
cuMetis_admin->part_balance[i]=graph->tvwgt_reverse[0]/tpwgts[i];
}
}
/*Get random permute of p*/
void cuMetis_int_randarrayofp(int n, int *p, int m, int flag)
{
int i,u,v;
int temp;
if(flag==1){
for(i=0;i<n;i++)
p[i] = (int)i;
}
if(n<10){
for(i=0;i<n;i++){
v=cuMetis_int_randinrange(n);
u=cuMetis_int_randinrange(n);
cuMetis_swap(p[v],p[u],temp);
}
}
else{
for(i=0;i<m;i++){
v=cuMetis_int_randinrange(n-3);
u=cuMetis_int_randinrange(n-3);
cuMetis_swap(p[v+0],p[u+2],temp);
cuMetis_swap(p[v+1],p[u+3],temp);
cuMetis_swap(p[v+2],p[u+0],temp);
cuMetis_swap(p[v+3],p[u+1],temp);
}
}
}
/*Get permutation array*/
void cuMetis_matching_sort(cuMetis_admin_t *cuMetis_admin, int n, \
int max, int *keys, int *tperm, int *perm)
{
int i,ii;
int *counts;
counts=cuMetis_int_set_value(max+2,0,cuMetis_int_malloc_space(cuMetis_admin,max+2));
for(i=0; i<n; i++){
counts[keys[i]]++;
}
cuMetis_tocsr(i,max+1,counts);
for(ii=0;ii<n;ii++){
i=tperm[ii];
perm[counts[keys[i]]++]=i;
}
}
/*Malloc cpu coarsen graph params*/
cuMetis_graph_t *cuMetis_set_cpu_cgraph(cuMetis_graph_t *graph, int cnvtxs)
{
cuMetis_graph_t *cgraph;
cgraph=cuMetis_create_cpu_graph();
cgraph->nvtxs=cnvtxs;
cgraph->xadj=(int*)malloc(sizeof(int)*(cnvtxs+1));
cgraph->adjncy=(int*)malloc(sizeof(int)*(graph->nedges));
cgraph->adjwgt=(int*)malloc(sizeof(int)*(graph->nedges));
cgraph->vwgt=(int*)malloc(sizeof(int)*cnvtxs);
cgraph->tvwgt=(int*)malloc(sizeof(int));
cgraph->tvwgt_reverse=(float*)malloc(sizeof(float));
cgraph->finer=graph;
graph->coarser=cgraph;
return cgraph;
}
/*Malloc gpu coarsen graph params*/
cuMetis_graph_t *cuMetis_set_gpu_cgraph(cuMetis_graph_t *graph, int cnvtxs)
{
cuMetis_graph_t *cgraph;
cgraph=cuMetis_create_cpu_graph();
cgraph->nvtxs=cnvtxs;
cgraph->xadj=(int*)malloc(sizeof(int)*(cnvtxs+1));
cgraph->tvwgt=(int*)malloc(sizeof(int));
cgraph->tvwgt_reverse=(float*)malloc(sizeof(float));
cgraph->finer=graph;
graph->coarser=cgraph;
return cgraph;
}
/*Create cpu coarsen graph by contract*/
void cuMetis_cpu_create_cgraph(cuMetis_admin_t *cuMetis_admin, \
cuMetis_graph_t *graph, int cnvtxs, int *match,int level)
{
int j,k,m,istart,iend,nvtxs,nedges,cnedges,v,u;
int *xadj,*vwgt,*adjncy,*adjwgt;
int *cmap,*htable;
int *cxadj,*cvwgt,*cadjncy,*cadjwgt;
cuMetis_graph_t *cgraph;
nvtxs=graph->nvtxs;
xadj=graph->xadj;
vwgt=graph->vwgt;
adjncy=graph->adjncy;
adjwgt=graph->adjwgt;
cmap=graph->cmap;
cgraph=cuMetis_set_cpu_cgraph(graph,cnvtxs);
cxadj=cgraph->xadj;
cvwgt=cgraph->vwgt;
cadjncy=cgraph->adjncy;
cadjwgt=cgraph->adjwgt;
htable=cuMetis_int_set_value(cnvtxs,-1,cuMetis_int_malloc_space(cuMetis_admin,cnvtxs));
cxadj[0] = cnvtxs = cnedges = 0;
nedges=graph->nedges;
for(v=0;v<nvtxs;v++){
if((u=match[v])<v)
continue;
cvwgt[cnvtxs]=vwgt[v];
nedges=0;
istart=xadj[v];
iend=xadj[v+1];
for(j=istart;j<iend;j++){
k=cmap[adjncy[j]];
if((m=htable[k])==-1){
cadjncy[nedges]=k;
cadjwgt[nedges] = adjwgt[j];
htable[k] = nedges++;
}
else{
cadjwgt[m] += adjwgt[j];
}
}
if(v!=u){
cvwgt[cnvtxs]+=vwgt[u];
istart=xadj[u];
iend=xadj[u+1];
for(j=istart;j<iend;j++){
k=cmap[adjncy[j]];
if((m=htable[k])==-1){
cadjncy[nedges]=k;
cadjwgt[nedges]=adjwgt[j];
htable[k]=nedges++;
}
else{
cadjwgt[m] += adjwgt[j];
}
}
if((j=htable[cnvtxs])!=-1){
cadjncy[j]=cadjncy[--nedges];
cadjwgt[j]=cadjwgt[nedges];
htable[cnvtxs] = -1;
}
}
for(j=0;j<nedges;j++){
htable[cadjncy[j]] = -1;
}
cnedges+=nedges;
cxadj[++cnvtxs]=cnedges;
cadjncy+=nedges;
cadjwgt+=nedges;
}
cgraph->nedges=cnedges;
cgraph->tvwgt[0]=cuMetis_int_sum(cgraph->nvtxs,cgraph->vwgt);
cgraph->tvwgt_reverse[0]=1.0/(cgraph->tvwgt[0]>0?cgraph->tvwgt[0]:1);
}
/*Get cpu graph matching params by hem*/
int cuMetis_cpu_match(cuMetis_admin_t *cuMetis_admin, \
cuMetis_graph_t *graph,int level)
{
cudaDeviceSynchronize();
gettimeofday(&begin_part_cmatch,NULL);
int i,j,pi,k,nvtxs,cnvtxs,maxidx,maxwgt,aved;
int *xadj,*vwgt,*adjncy,*adjwgt,*maxvwgt;
int *match,*cmap,*d,*perm,*tperm;
nvtxs=graph->nvtxs;
xadj=graph->xadj;
vwgt=graph->vwgt;
adjncy=graph->adjncy;
adjwgt=graph->adjwgt;
cmap=graph->cmap;
maxvwgt=cuMetis_admin->maxvwgt;
cnvtxs=0;
match=cuMetis_int_set_value(nvtxs,-1, cuMetis_int_malloc_space(cuMetis_admin,nvtxs));
perm=cuMetis_int_malloc_space(cuMetis_admin,nvtxs);
tperm=cuMetis_int_malloc_space(cuMetis_admin,nvtxs);
d=cuMetis_int_malloc_space(cuMetis_admin,nvtxs);
cuMetis_int_randarrayofp(nvtxs,tperm,nvtxs/8,1);
aved=0.7*(xadj[nvtxs]/nvtxs);
for(i=0;i<nvtxs;i++){
d[i]=(xadj[i+1]-xadj[i]>aved?aved:xadj[i+1]-xadj[i]);
}
cuMetis_matching_sort(cuMetis_admin,nvtxs,aved,d,tperm,perm);
for(pi=0;pi<nvtxs;pi++)
{
i=perm[pi];
if(match[i]==-1){
maxidx=i;
maxwgt=-1;
for(j=xadj[i];j<xadj[i+1];j++){
k=adjncy[j];
if(match[k]==-1&&maxwgt<adjwgt[j]&&vwgt[i]+vwgt[k]<=maxvwgt[0]){
maxidx=k;
maxwgt=adjwgt[j];
}
if(maxidx==i&&3*vwgt[i]<maxvwgt[0]){
maxidx=-1;
}
}
if(maxidx!=-1){
cmap[i]=cmap[maxidx]=cnvtxs++;
match[i]=maxidx;
match[maxidx]=i;
}
}
}
for(cnvtxs=0,i=0;i<nvtxs;i++){
if(match[i]==-1){
match[i]=i;
cmap[i]=cnvtxs++;
}
else{
if(i<=match[i]){
cmap[i]=cmap[match[i]]=cnvtxs++;
}
}
}
cudaDeviceSynchronize();
gettimeofday(&end_part_cmatch,NULL);
part_cmatch += (end_part_cmatch.tv_sec - begin_part_cmatch.tv_sec) * 1000 + (end_part_cmatch.tv_usec - begin_part_cmatch.tv_usec) / 1000.0;
cudaDeviceSynchronize();
gettimeofday(&begin_part_ccontract,NULL);
cuMetis_cpu_create_cgraph(cuMetis_admin, graph, cnvtxs, match,level);
cudaDeviceSynchronize();
gettimeofday(&end_part_ccontract,NULL);
part_ccontract += (end_part_ccontract.tv_sec - begin_part_ccontract.tv_sec) * 1000 + (end_part_ccontract.tv_usec - begin_part_ccontract.tv_usec) / 1000.0;
return cnvtxs;
}
/*Malloc and memcpy original graph from cpu to gpu*/
void cuMetis_malloc_original_coarseninfo(cuMetis_admin_t *cuMetis_admin,cuMetis_graph_t *graph)
{
int nvtxs=graph->nvtxs;
int nedges=graph->nedges;
cudaMalloc((void**)&graph->cuda_nvtxs,sizeof(int));
cudaMemcpy(graph->cuda_nvtxs,&graph->nvtxs,sizeof(int),cudaMemcpyHostToDevice);
cudaMalloc((void**)&graph->cuda_match,nvtxs*sizeof(int));
cudaMalloc((void**)&graph->cuda_xadj,(nvtxs+1)*sizeof(int));
cudaMemcpy(graph->cuda_xadj,graph->xadj,(nvtxs+1)*sizeof(int),cudaMemcpyHostToDevice);
cudaMalloc((void**)&graph->cuda_vwgt,(nvtxs+1)*sizeof(int));
cudaMemcpy(graph->cuda_vwgt,graph->vwgt,nvtxs*sizeof(int),cudaMemcpyHostToDevice);
cudaMalloc((void**)&graph->cuda_adjncy,nedges*sizeof(int));
cudaMemcpy(graph->cuda_adjncy,graph->adjncy,nedges*sizeof(int),cudaMemcpyHostToDevice);
cudaMalloc((void**)&graph->cuda_adjwgt,nedges*sizeof(int));
cudaMemcpy(graph->cuda_adjwgt,graph->adjwgt,nedges*sizeof(int),cudaMemcpyHostToDevice);
cudaMalloc((void**)&graph->cuda_cmap,nvtxs*sizeof(int));
cudaMalloc((void**)&graph->cuda_maxvwgt,sizeof(int));
cudaMalloc((void**)&graph->cuda_s,(nvtxs)*sizeof(int));
cudaMalloc((void**)&graph->cuda_cnvtxs,sizeof(int));
cudaMalloc((void**)&graph->cuda_scan_nedges_original,(graph->nedges)*sizeof(int));
cudaMalloc((void**)&graph->cuda_scan_cnedges_original,(graph->nedges)*sizeof(int));
cudaMalloc((void**)&graph->cuda_scan_adjwgt_original,(graph->nedges)*sizeof(int));
}
/*Malloc gpu coarsen graph params*/
void cuMetis_malloc_coarseninfo(cuMetis_admin_t *cuMetis_admin,cuMetis_graph_t *graph)
{
int nvtxs=graph->nvtxs;
int nedges=graph->nedges;
cudaMalloc((void**)&graph->cuda_nvtxs,sizeof(int));
cudaMemcpy(graph->cuda_nvtxs,&graph->nvtxs,sizeof(int),cudaMemcpyHostToDevice);
cudaMalloc((void**)&graph->cuda_match,nvtxs*sizeof(int));
cudaMalloc((void**)&graph->cuda_cmap,nvtxs*sizeof(int));
cudaMalloc((void**)&graph->cuda_maxvwgt,sizeof(int));
cudaMalloc((void**)&graph->cuda_s,(nvtxs)*sizeof(int));
cudaMalloc((void**)&graph->cuda_cnvtxs,sizeof(int));
cudaMalloc((void**)&graph->cuda_scan_nedges_original,nedges*sizeof(int));
cudaMalloc((void**)&graph->cuda_scan_cnedges_original,nedges*sizeof(int));
cudaMalloc((void**)&graph->cuda_scan_adjwgt_original,nedges*sizeof(int));
}
/*CUDA-initial cuda_js array*/
__global__ void initcudajs(int *cuda_js, int *cn)
{
int ii;
ii=blockIdx.x*blockDim.x+threadIdx.x;
if(ii<cn[0]){
cuda_js[ii]=0;
}