-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_double.cu
1150 lines (1068 loc) · 42.7 KB
/
main_double.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 "main.h"
// g++ -D BIRCH1 -g mainOMP.cpp -std=c++11 -O3 -msse4.2 -fopenmp -o birch1 -lm
// random_device rd;
// mt19937 gen(rd());
// unsigned int max_val = gen.max();
int numThreads = 1;
__constant__ double dev_centers_global[NUM_CLUSTER*DIMENSION]; // For using constant memory
int main(int argc, char const *argv[])
{
// Currently no argument processing logic, will always run birch1 for 2 times with N=10k
srand(time(NULL));
int numRuns,method;
int N = 0;
// for k-means parallel
int rounds = 5;
double oversampling = NUM_CLUSTER;
oversampling = 2*oversampling;
char dataFileName[100];
char mode[100];
char baseLogFile[100];
char resultFile[100];
sprintf(dataFileName,"%s%s","../data/",DATA);
sprintf(mode,"%s",argv[1]);
sprintf(baseLogFile,"../logs/%s/%s_",DATA,mode);
numRuns = atoi(argv[2]);
method = -1;
numThreads = 1;
if(getenv("OMP_NUM_THREADS") != NULL)
{
numThreads = atoi(getenv("OMP_NUM_THREADS"));
printf("numThreads as gotten from env::%d\n",numThreads);
if(numThreads == 0)
{
numThreads = 1;
}
}
else
{
printf("numThreads as gotten by default::%d\n",numThreads);
}
if(strcmp(mode,"random")==0)
{
method = 0;
}
if(strcmp(mode,"kmeans++")==0)
{
method = 1;
}
if(strcmp(mode,"d2-seeding")==0)
{
method = 2;
N = floor(NUM_CLUSTER * atof(argv[3]));
sprintf(baseLogFile,"%sN=%sk_",baseLogFile,argv[3]);
}
if(strcmp(mode,"kmeans-par")==0)
{
method = 3;
oversampling = NUM_CLUSTER * atof(argv[3]);
rounds = atoi(argv[4]);
sprintf(baseLogFile,"%sl=%sk_r=%d_",baseLogFile,argv[3],rounds);
}
// base log file name for individual runs
sprintf(baseLogFile,"%sthreads=%d_",baseLogFile,numThreads);
// log file for combined results. Mean and standard deviations
sprintf(resultFile,"%sresult.txt",baseLogFile);
sprintf(baseLogFile,"%srunNo=",baseLogFile);
struct timeval start,end;
// collect stats about all relevant parameters
double initTime[numRuns];
double iterTime[numRuns];
double totalTime[numRuns];
double initCost[numRuns];
double finalCost[numRuns];
double numIter[numRuns];
// read the data into a vector of "vector"
double* data;
FILE* reader;
int i = 0,j = 0;
data = (double*)malloc(NUM_POINTS*DIMENSION*sizeof(double));
reader = fopen(dataFileName,"r");
while(i < NUM_POINTS)
{
j = 0;
while(j < DIMENSION)
{
fscanf(reader,"\t%lf",&(data[i*DIMENSION + j]));
j++;
}
i++;
}
// Copy data onto device memory
double* dev_data;
cudaMalloc((void**)&dev_data,DIMENSION*NUM_POINTS*sizeof(double));
cudaMemcpy(dev_data,data,DIMENSION*NUM_POINTS*sizeof(double),cudaMemcpyHostToDevice);
FILE* logger;
int runNum;
for(runNum = 0; runNum < numRuns ; runNum++)
{
double samplingTime_1[NUM_CLUSTER];
double samplingTime_2[NUM_CLUSTER];
printf("Running runNum::%d\n",runNum );
gettimeofday(&start,NULL);
int numBlocks = 8;
int numThreadsPerBlock = 1024;
int numSampleBlocks = 128;
int numSampleTperB = 32;
int numGPUThreads = numBlocks*numThreadsPerBlock;
// double* distances_debug = (double*)malloc(NUM_POINTS*sizeof(double));
double* distances; // Using page-locked memory for distances
cudaHostAlloc((void**)&distances,NUM_POINTS*sizeof(double),cudaHostAllocDefault);
double* centers = (double*)malloc(NUM_CLUSTER*DIMENSION*sizeof(double));
double* rnd = (double*)malloc(2*N*sizeof(double));
double* multiset = (double*)malloc(N*DIMENSION*sizeof(double));
double* partition_sums = (double*)malloc(numGPUThreads*sizeof(double));
// double* partition_sums_debug = (double*)malloc(numGPUThreads*sizeof(double));
int* sampled_indices = (int*)malloc(N*sizeof(int));
double* dev_distances;
double* dev_partition_sums;
double* dev_rnd;
int* dev_sampled_indices;
// double* dev_centers; // When not using constant memory for centers
checkCudaErrors(cudaMalloc((void**)&dev_distances,NUM_POINTS*sizeof(double)));
checkCudaErrors(cudaMalloc((void**)&dev_partition_sums,numGPUThreads*sizeof(double)));
checkCudaErrors(cudaMalloc((void**)&dev_sampled_indices,N*sizeof(int)));
checkCudaErrors(cudaMalloc((void**)&dev_rnd,2*N*sizeof(double)));
// checkCudaErrors(cudaMalloc((void**)&dev_centers,NUM_CLUSTER*DIMENSION*sizeof(double))); // No need when using constant memory
// initialize the initial centers
if(method == 2) // d2-seeding
{
// ---------------------- GPU-Based Implementation Start ------------------------------------
cudaProfilerStart();
// First choosing the first point uniformly at random, no need to sample N points and all here
int tempPointIndex = (((double) rand())/RAND_MAX)*NUM_POINTS;
memcpy(centers, data+tempPointIndex*DIMENSION, DIMENSION*sizeof(double));
checkCudaErrors(cudaMemcpyToSymbol(dev_centers_global, data+tempPointIndex*DIMENSION, DIMENSION*sizeof(double),0,cudaMemcpyHostToDevice));
// checkCudaErrors(cudaMemcpy(dev_centers, data+tempPointIndex*DIMENSION, DIMENSION*sizeof(double),cudaMemcpyHostToDevice));
double compDistTime = 0, makeCumulativeTime = 0, samplingTime = 0, meanHeuristicTime = 0;
for(i = 1; i < NUM_CLUSTER; i++)
{
struct timeval sample_start,sample_end;
gettimeofday(&sample_start,NULL);
for(j = 0; j < N; ++j)
{
rnd[2*j] = ((double) rand())/RAND_MAX;
rnd[2*j+1] = ((double) rand())/RAND_MAX;
}
cudaMemcpy(dev_rnd,rnd,2*N*sizeof(double),cudaMemcpyHostToDevice);// Can be overlapped with computation
// comp_dist<<<numBlocks,numThreadsPerBlock>>>(dev_data, dev_distances, dev_partition_sums, dev_centers, i, NUM_POINTS, DIMENSION, numGPUThreads);
// For blocked access pattern
// comp_dist_glbl<<<numBlocks,numThreadsPerBlock>>>(dev_data, dev_distances, dev_partition_sums, i, NUM_POINTS, DIMENSION, numGPUThreads);
// cudaMemcpy(partition_sums,dev_partition_sums,numGPUThreads*sizeof(double),cudaMemcpyDeviceToHost);
// for (j = 1; j < numGPUThreads; ++j) // Need to do this scan operation on GPU only, but testing things first
// {
// partition_sums[j] += partition_sums[j-1];
// }
// cudaMemcpy(dev_partition_sums,partition_sums,numGPUThreads*sizeof(double),cudaMemcpyHostToDevice);
// int per_thread = (NUM_POINTS + numGPUThreads-1)/numGPUThreads;
// sample_from_distribution_gpu<<<numSampleBlocks,numSampleTperB>>>(dev_partition_sums, dev_distances, dev_sampled_indices, dev_rnd, per_thread, NUM_POINTS, N);
// For strided memory access pattern
comp_dist_glbl_strided<<<numBlocks,numThreadsPerBlock>>>(dev_data, dev_distances, dev_partition_sums, i, NUM_POINTS, DIMENSION, numGPUThreads);
cudaMemcpy(distances,dev_distances,NUM_POINTS*sizeof(double),cudaMemcpyDeviceToHost);
for (j = 1; j < NUM_POINTS; ++j)
{
distances[j] += distances[j-1];
}
cudaMemcpy(dev_distances,distances,NUM_POINTS*sizeof(double),cudaMemcpyHostToDevice);
sample_from_distribution_gpu_strided<<<numSampleBlocks,numSampleTperB>>>(dev_distances, dev_sampled_indices, dev_rnd, NUM_POINTS, N);
// // Division of distance array into blocks so that sampling is similar to blocked cost calculation approach
// int per_thread = (NUM_POINTS + numGPUThreads-1)/numGPUThreads;
// cudaMemcpy(distances,dev_distances,NUM_POINTS*sizeof(double),cudaMemcpyDeviceToHost);
// double prev_val = distances[0],prev_part_val=0;
// int p_ctr = 0;
// for (j = 1; j < NUM_POINTS; ++j)
// {
// distances[j] += prev_val;
// prev_val = distances[j];
// if ((j+1)%per_thread == 0)
// {
// partition_sums[p_ctr] = distances[j] + prev_part_val;
// prev_part_val = partition_sums[p_ctr];
// p_ctr += 1;
// prev_val = 0;
// }
// else if (j == NUM_POINTS -1)
// {
// partition_sums[p_ctr] = distances[j] + prev_part_val;
// prev_part_val = partition_sums[p_ctr];
// p_ctr += 1;
// prev_val = 0;
// }
// }
// cudaMemcpy(dev_distances,distances,NUM_POINTS*sizeof(double),cudaMemcpyHostToDevice);
// cudaMemcpy(dev_partition_sums,partition_sums,numGPUThreads*sizeof(double),cudaMemcpyHostToDevice);
// sample_from_distribution_gpu<<<numSampleBlocks,numSampleTperB>>>(dev_partition_sums, dev_distances, dev_sampled_indices, dev_rnd, per_thread, NUM_POINTS, N);
// Copy back indices of sampled points, no need to copy those points as we have the data here as well
cudaMemcpy(sampled_indices,dev_sampled_indices,N*sizeof(int),cudaMemcpyDeviceToHost);
for (int copy_i = 0; copy_i < N; ++copy_i)
{
int index = sampled_indices[copy_i];
for (int copy_j = 0; copy_j < DIMENSION; ++copy_j)
{
multiset[copy_i*DIMENSION + copy_j] = data[index*DIMENSION + copy_j];
}
}
gettimeofday(&sample_end,NULL);
compDistTime += get_time_diff(sample_start,sample_end);
// Code for sampling on CPU (first GPU implementation)
// // copy back to host memory for sampling purpose,
// cudaMemcpy(distances,dev_distances,NUM_POINTS*sizeof(double),cudaMemcpyDeviceToHost);
// cudaMemcpy(partition_sums,dev_partition_sums,numGPUThreads*sizeof(double),cudaMemcpyDeviceToHost);
// // Make it cumulative for sampling purpose, can be done on GPU as well
// // Already made cumulative above
// gettimeofday(&sample_start,NULL);
// for (j = 1; j < numGPUThreads; ++j)
// {
// partition_sums[j] += partition_sums[j-1];
// }
// gettimeofday(&sample_end,NULL);
// makeCumulativeTime += get_time_diff(sample_start,sample_end);
// int per_thread = (NUM_POINTS + numGPUThreads-1)/numGPUThreads;
// gettimeofday(&sample_start,NULL);
// for(j = 0 ; j < N ; j++)
// {
// rnd[2*j] = ((double) rand())/RAND_MAX;
// rnd[2*j+1] = ((double) rand())/RAND_MAX;
// int numValidPartitions = NUM_POINTS/per_thread + 1;
// // first pick a block from the local_sums distribution
// int groupNo = sample_from_distribution(partition_sums, 0, numValidPartitions, rnd[2*j]*partition_sums[numValidPartitions-1]);
// // the start and end index of this block
// int startIndex = groupNo * per_thread;
// int endIndex = (groupNo + 1) * per_thread;
// if(groupNo == numGPUThreads - 1) endIndex = NUM_POINTS;
// // now sample from the cumulative distribution of the block
// int pointIndex = sample_from_distribution(distances, startIndex, endIndex, rnd[2*j+1]*distances[endIndex-1]);
// for (int k = 0; k < DIMENSION; ++k)
// {
// multiset[j*DIMENSION + k] = data[pointIndex*DIMENSION + k];
// }
// }
// gettimeofday(&sample_end,NULL);
// samplingTime += get_time_diff(sample_start,sample_end);
gettimeofday(&sample_start,NULL);
double* nextCenter = mean_heuristic(multiset,N);
memcpy(centers + i*DIMENSION,nextCenter,DIMENSION*sizeof(double));
checkCudaErrors(cudaMemcpyToSymbol(dev_centers_global , nextCenter, DIMENSION*sizeof(double), i*DIMENSION*sizeof(double), cudaMemcpyHostToDevice));
// checkCudaErrors(cudaMemcpy(dev_centers + i*DIMENSION , nextCenter, DIMENSION*sizeof(double), cudaMemcpyHostToDevice));
gettimeofday(&sample_end,NULL);
meanHeuristicTime += get_time_diff(sample_start,sample_end);
}
printf("compDistTime\t\t%2.5f\t%2.5f\n",compDistTime,compDistTime/(NUM_CLUSTER-1) );
printf("makeCumulativeTime\t%2.5f\t%2.5f\n",makeCumulativeTime,makeCumulativeTime/(NUM_CLUSTER-1) );
printf("samplingTime\t\t%2.5f\t%2.5f\n",samplingTime,samplingTime/(NUM_CLUSTER-1) );
printf("meanHeuristicTime\t%2.5f\t%2.5f\n",meanHeuristicTime,meanHeuristicTime/(NUM_CLUSTER-1) );
cudaProfilerStop();
// ---------------------- GPU-Based Implementation End --------------------------------------
// ---------------------- CPU-Based Implementation Start ------------------------------------
// for(i = 0; i < NUM_CLUSTER; i++)
// {
// struct timeval sample_start,sample_end;
// gettimeofday(&sample_start,NULL);
// multiset = d2_sample(data,centers,NUM_POINTS,N,i);
// // multiset = d2_sample_2(data,centers,NUM_POINTS,N,i,distances);
// gettimeofday(&sample_end,NULL);
// printf("Time taken for d2_sample::%d-->%f\n",i,get_time_diff(sample_start,sample_end));
// samplingTime_1[i] = get_time_diff(sample_start,sample_end);
// gettimeofday(&sample_start,NULL);
// double* nextCenter = mean_heuristic(multiset,N);
// for (int j = 0; j < DIMENSION; ++j)
// {
// centers[i*DIMENSION + j] = nextCenter[j];
// }
// gettimeofday(&sample_end,NULL);
// printf("Time taken for mean_heuristic::%d-->%f\n",i,get_time_diff(sample_start,sample_end));
// samplingTime_2[i] = get_time_diff(sample_start,sample_end);
// }
// ---------------------- CPU-Based Implementation End --------------------------------------
}
else
{
printf("Only d2-seeding support for now::%d\n",method);
printf("Mode::%s\n",mode );
exit(0);
}
gettimeofday(&end,NULL);
initTime[runNum] = get_time_diff(start,end);
// now the Lloyd's iterations
// first we need to figure out the assignments
gettimeofday(&start,NULL);
double prev_cost = DBL_MAX;
int iteration = 0;
char tempFileName[100];
sprintf(tempFileName,"%s%d.txt",baseLogFile,runNum);
logger = fopen(tempFileName,"w");
// Can make first two static arrays
int* cluster_counts = (int*)malloc(NUM_CLUSTER*sizeof(int)); // number of points assigned to each cluster
double* cluster_sums = (double*)malloc(DIMENSION*NUM_CLUSTER*sizeof(double)); // sum of points assigned to each cluster
int** cluster_counts_pointers = (int**)malloc(numThreads*sizeof(int*)); // pointers to local "number of points assigned to each cluster"
double** cluster_sums_pointers = (double**)malloc(numThreads*sizeof(double*)); // pointers to local "sum of points assigned to each cluster"
while(true)
{
iteration++;
// initially, set everything to zero
for(int i = 0; i < NUM_CLUSTER; i++)
{
cluster_counts[i] = 0;
for(int j = 0; j < DIMENSION; j++)
{
cluster_sums[i*DIMENSION + j] = 0;
}
}
// cost according to the current solution
double current_cost = 0.0;
#pragma omp parallel reduction(+: current_cost)
{
int tid = omp_get_thread_num();
int local_cluster_counts[NUM_CLUSTER]; // local "number of points assigned to each cluster"
double local_cluster_sums[DIMENSION*NUM_CLUSTER]; // local "sum of points assigned to each cluster"
for(int i = 0; i < NUM_CLUSTER; i++)
{
local_cluster_counts[i] = 0;
for(int j = 0; j < DIMENSION; j++)
{
local_cluster_sums[i*DIMENSION + j] = 0;
}
}
cluster_counts_pointers[tid] = local_cluster_counts; // set the pointer
cluster_sums_pointers[tid] = local_cluster_sums; // set the pointer
int index;
double min_dist;
double current_dist;
// assign each point to their cluster center in parallel.
// update the cost of current solution and keep updating local counts and sums
#pragma omp for schedule(static)
for (int i = 0; i < NUM_POINTS; i++)
{
index = 0;
min_dist = DBL_MAX;
current_dist = 0;
for(int j = 0; j < NUM_CLUSTER; j++)
{
current_dist = distance(data + i*DIMENSION, centers + j*DIMENSION);
if(current_dist < min_dist)
{
index = j;
min_dist = current_dist;
}
}
current_cost += min_dist;
local_cluster_counts[index] += 1;
for(int j = 0; j < DIMENSION; j++)
{
local_cluster_sums[index*DIMENSION + j] = local_cluster_sums[index*DIMENSION + j] + data[i*DIMENSION + j];
}
}
// aggregate counts and sums across all threads
#pragma omp for schedule(static)
for(int i = 0; i < NUM_CLUSTER; i++)
{
for(int j = 0; j < numThreads; j++)
{
cluster_counts[i] = cluster_counts[i] + cluster_counts_pointers[j][i];
for(int k = 0; k < DIMENSION; k++)
{
cluster_sums[i*DIMENSION + k] = cluster_sums[i*DIMENSION + k] + cluster_sums_pointers[j][i*DIMENSION + k];
}
}
}
}
if(iteration == 1)
{
initCost[runNum] = current_cost;
}
// now scale all the sums by the number of points at each cluster
for(int i = 0; i < NUM_CLUSTER; i++)
{
int scaler = cluster_counts[i];
for(int j = 0; j < DIMENSION; j++)
{
centers[i*DIMENSION + j] = cluster_sums[i*DIMENSION + j]/scaler;
}
}
// log entry
fprintf(logger,"Iteration: %d Cost:%f\n",iteration,current_cost);
// termination criteria
if(1 - current_cost/prev_cost < 0.0001)
{
prev_cost = current_cost;
break;
}
prev_cost = current_cost;
}
gettimeofday(&end,NULL);
finalCost[runNum] = prev_cost;
numIter[runNum] = iteration;
iterTime[runNum] = get_time_diff(start,end)/numIter[runNum];
totalTime[runNum] = iterTime[runNum]*numIter[runNum] + initTime[runNum];
fprintf(logger, "Number of iterations:%f\n",numIter[runNum]);
fprintf(logger, "Initialization time:%f\n",initTime[runNum]);
fprintf(logger, "Initialization cost:%f\n",initCost[runNum]);
fprintf(logger, "Final cost:%f\n",finalCost[runNum]);
fprintf(logger, "Total time:%f\n",totalTime[runNum]);
fprintf(logger, "Per iteration time:%f\n",iterTime[runNum]);
fprintf(logger, "Total iteration time:%f\n",iterTime[runNum]*numIter[runNum]);
if(method == 2) // d2-seeding
{
fprintf(logger,"samplingTime_1:%f\n",mean(samplingTime_1,NUM_CLUSTER));
fprintf(logger,"samplingTime_2:%f\n",mean(samplingTime_2,NUM_CLUSTER));
}
fclose(logger);
free(cluster_counts);
free(cluster_sums);
free(cluster_counts_pointers);
free(cluster_sums_pointers);
free(centers);
cudaFreeHost(distances); // free this way when using page-locked memory for distances
// free(distances);
free(rnd);
free(multiset);
free(partition_sums);
}
logger = fopen(resultFile,"w");
fprintf(logger, "Initial cost: %f %f\n",mean(initCost,numRuns),sd(initCost,numRuns));
fprintf(logger, "Final cost: %f %f\n",mean(finalCost,numRuns),sd(finalCost,numRuns));
fprintf(logger, "Number of iterations: %f %f\n",mean(numIter,numRuns),sd(numIter,numRuns));
fprintf(logger, "Initialization time: %f %f\n",mean(initTime,numRuns),sd(initTime,numRuns));
fprintf(logger, "Per iteration time: %f %f\n",mean(iterTime,numRuns),sd(iterTime,numRuns));
fclose(logger);
return 0;
}
int sample_from_distribution(double* probabilities, int startIndex, int endIndex, double prob)
{
int start = startIndex,end = endIndex - 1;
int mid;
while(start <= end)
{
mid = (start+end)/2;
if(prob < probabilities[mid-1])
{
end = mid-1;
}
else if(prob > probabilities[mid])
{
start = mid+1;
}
else
{
break;
}
}
return mid;
}
// GPU version of sampling code
__global__ void sample_from_distribution_gpu(double* dev_partition_sums, double* dev_distances, int* dev_sampled_indices, double* dev_rnd,int per_thread, int dev_NUM_POINTS, int dev_N)
{
int numValidPartitions = dev_NUM_POINTS/per_thread + 1;
int start,mid,end,groupNo,pointIndex;
double prob;
int i = blockIdx.x*blockDim.x + threadIdx.x;
if( i < dev_N)
{
// first pick a block from the local_sums distribution
// int groupNo =sample_from_distribution(partition_sums,0, numValidPartitions, rnd[2*i]*partition_sums[numValidPartitions-1]);
start = 0;
end = numValidPartitions - 1;
prob = dev_rnd[2*i]*dev_partition_sums[end];
while(start <= end)
{
mid = (start+end)/2;
if(prob < dev_partition_sums[mid-1])
{
end = mid-1;
}
else if(prob > dev_partition_sums[mid])
{
start = mid+1;
}
else
{
break;
}
}
groupNo = mid;
// the start and end index of this block
// int startIndex = groupNo*per_thread;
// int endIndex = min((groupNo + 1)*per_thread, NUM_POINTS);
// now sample from the cumulative distribution of the block
// int pointIndex = sample_from_distribution(distances, startIndex, endIndex, rnd[2*i+1]*distances[endIndex-1]);
start = groupNo*per_thread;
end = min((groupNo + 1)*per_thread, NUM_POINTS) - 1;
prob = dev_rnd[2*i+1]*dev_distances[end];
while(start <= end)
{
mid = (start+end)/2;
if(prob < dev_distances[mid-1])
{
end = mid-1;
}
else if(prob > dev_distances[mid])
{
start = mid+1;
}
else
{
break;
}
}
pointIndex = mid;
dev_sampled_indices[i] = pointIndex;
}
}
// Sampling for case of strided memory access pattern, no dev_partition_sums here
__global__ void sample_from_distribution_gpu_strided(double* dev_distances, int* dev_sampled_indices, double* dev_rnd, int dev_NUM_POINTS, int dev_N)
{
int i = blockIdx.x*blockDim.x + threadIdx.x;
if( i < dev_N)
{
int start,mid,end,pointIndex;
double prob;
// the start and end index of this block
// int startIndex = groupNo*per_thread;
// int endIndex = min((groupNo + 1)*per_thread, NUM_POINTS);
// now sample from the cumulative distribution of the block
// int pointIndex = sample_from_distribution(distances, startIndex, endIndex, rnd[2*i+1]*distances[endIndex-1]);
start = 0;
end = dev_NUM_POINTS - 1;
prob = dev_rnd[i]*dev_distances[end];
mid = (start+end)/2;
while(start <= end)
{
mid = (start+end)/2;
if(prob < dev_distances[mid-1])
{
end = mid-1;
}
else if(prob > dev_distances[mid])
{
start = mid+1;
}
else
{
break;
}
}
pointIndex = mid;
dev_sampled_indices[i] = pointIndex;
}
}
// This function calcuates required distance for all points and partitions
// Need to do an all-prefix sum after this to make this thing cumulative
// Can be optimized by using distances calculated in previous iteration, i.e. when the previous center was sampled
// This does not do any sampling business
// Need not call this function when centerIter = 0,
// Not optimized to use distance calculted in previous iteration to calculate distance/cost for points
__global__ void comp_dist_2(double* dev_data,double* dev_distances,double* dev_partition_sums, double* dev_centers,int centerIter,int numPoints,int dev_dimension,int numGPUThreads)
{
// Starting off with very simplistic 1-D threads blocks and 1-D grids
int tid = threadIdx.x + blockIdx.x*blockDim.x;
// int jump = blockDim.x*gridDim.x;
int per_thread = (numPoints + numGPUThreads - 1)/numGPUThreads;// Term in the numerator is added to that we can get ceiling of numPoints/numGPUThreads
int startIndex = tid*per_thread;
int endIndex = min((tid + 1)*per_thread,numPoints);
double min_dist = DBL_MAX, local_dist,temp,prev_val = 0;
for (int dataIndex = startIndex; dataIndex < endIndex; ++dataIndex)
{
min_dist = DBL_MAX;
for (int i = 0; i < centerIter; ++i)
{
local_dist = 0;
for (int j = 0; j < dev_dimension; ++j)
{
temp = dev_data[dataIndex*dev_dimension + j] - dev_centers[i*dev_dimension + j];
local_dist += temp*temp;
}
min_dist = min(min_dist,local_dist);
}
dev_distances[dataIndex] = min_dist*min_dist + prev_val;
// dev_distances[dataIndex] = min_dist*min_dist;
prev_val = dev_distances[dataIndex];
}
dev_partition_sums[tid] = prev_val;
}
// Optimised to use previous distance values to calculate min_dist for points in next iteration
__global__ void comp_dist(double* dev_data,double* dev_distances,double* dev_partition_sums, double* dev_centers,int centerIter,int numPoints,int dev_dimension,int numGPUThreads)
{
// Starting off with very simplistic 1-D threads blocks and 1-D grids
int tid = threadIdx.x + blockIdx.x*blockDim.x;
int per_thread = (numPoints + numGPUThreads - 1)/numGPUThreads;// Term in the numerator is added to that we can get ceiling of numPoints/numGPUThreads
int startIndex = tid*per_thread;
int endIndex = min((tid + 1)*per_thread,numPoints);
double min_dist = DBL_MAX, local_dist,temp,prev_val = 0,old_prev_val=0;
for (int dataIndex = startIndex; dataIndex < endIndex; ++dataIndex)
{
if (centerIter == 1) // This is the first time dev_distances will get its values
{
min_dist = 0;
int i = 0;
for (int j = 0; j < dev_dimension; ++j)
{
temp = dev_data[dataIndex*dev_dimension + j] - dev_centers[i*dev_dimension + j];
min_dist += temp*temp;
}
dev_distances[dataIndex] = min_dist*min_dist + prev_val; // make it cumulative as you calculate it
prev_val = dev_distances[dataIndex];
}
else
{
int i = centerIter - 1; // i denotes the last center that was added to the list of centers
min_dist = dev_distances[dataIndex] - old_prev_val;
old_prev_val= dev_distances[dataIndex];
local_dist = 0;
for (int j = 0; j < dev_dimension; ++j)
{
temp = dev_data[dataIndex*dev_dimension + j] - dev_centers[i*dev_dimension + j];
local_dist += temp*temp;
}
min_dist = min(min_dist,local_dist*local_dist);
dev_distances[dataIndex] = min_dist + prev_val; // No need to square min_dist here, it is already squared value
prev_val = dev_distances[dataIndex];
}
}
dev_partition_sums[tid] = prev_val;
}
// Optimised to use previous distance values to calculate min_dist for points in next iteration
// Also makes use of constant memory for storing centers
__global__ void comp_dist_glbl(double* dev_data,double* dev_distances,double* dev_partition_sums,int centerIter,int numPoints,int dev_dimension,int numGPUThreads)
{
// Starting off with very simplistic 1-D threads blocks and 1-D grids
int tid = threadIdx.x + blockIdx.x*blockDim.x;
int per_thread = (numPoints + numGPUThreads - 1)/numGPUThreads;// Term in the numerator is added to that we can get ceiling of numPoints/numGPUThreads
int startIndex = tid*per_thread;
int endIndex = min((tid + 1)*per_thread,numPoints);
double min_dist = DBL_MAX, local_dist,temp,prev_val = 0,old_prev_val=0;
for (int dataIndex = startIndex; dataIndex < endIndex; ++dataIndex)
{
if (centerIter == 1) // This is the first time dev_distances will get its values
{
min_dist = 0;
int i = 0;
for (int j = 0; j < dev_dimension; ++j)
{
temp = dev_data[dataIndex*dev_dimension + j] - dev_centers_global[i*dev_dimension + j];
min_dist += temp*temp;
}
dev_distances[dataIndex] = min_dist*min_dist + prev_val; // make it cumulative as you calculate it
prev_val = dev_distances[dataIndex];
}
else
{
int i = centerIter - 1; // i denotes the last center that was added to the list of centers
min_dist = dev_distances[dataIndex] - old_prev_val;
old_prev_val= dev_distances[dataIndex];
local_dist = 0;
for (int j = 0; j < dev_dimension; ++j)
{
temp = dev_data[dataIndex*dev_dimension + j] - dev_centers_global[i*dev_dimension + j];
local_dist += temp*temp;
}
min_dist = min(min_dist,local_dist*local_dist);
dev_distances[dataIndex] = min_dist + prev_val; // No need to square min_dist here, it is already squared value
prev_val = dev_distances[dataIndex];
}
}
dev_partition_sums[tid] = prev_val;
}
// This addtionally does things in strided fashion as opposed to assigning each thread some fixed block
__global__ void comp_dist_glbl_strided(double* dev_data,double* dev_distances,double* dev_partition_sums,int centerIter,int numPoints,int dev_dimension,int numGPUThreads)
{
int dataIndex = threadIdx.x + blockIdx.x*blockDim.x;
int stride = blockDim.x*gridDim.x;
double min_dist, local_dist, temp;
while(dataIndex < numPoints)
{
if (centerIter == 1) // This is the first time dev_distances will get its values
{
min_dist = 0;
for (int j = 0; j < dev_dimension; ++j)
{
temp = dev_data[dataIndex*dev_dimension + j] - dev_centers_global[j]; // Accessing 0th center of dev_center_global
min_dist += temp*temp;
}
dev_distances[dataIndex] = min_dist*min_dist;
}
else
{
// Assuming that dev_distances has been made cumulative, after this function call
// if (dataIndex == 0)
// {
// min_dist = dev_distances[dataIndex];
// }
// else
// {
// min_dist = dev_distances[dataIndex] - dev_distances[dataIndex - 1];
// }
min_dist = DBL_MAX;
for (int i = 0; i < centerIter; ++i)
{
local_dist = 0;
for (int j = 0; j < dev_dimension; ++j)
{
temp = dev_data[dataIndex*dev_dimension + j] - dev_centers_global[i*dev_dimension + j];
local_dist += temp*temp;
}
min_dist = min(min_dist,local_dist*local_dist);
}
dev_distances[dataIndex] = min_dist; // --No-- Need to square min_dist here, it is *not* already squared value
}
dataIndex += stride;
}
}
// generate numSamples sized multiset from weighted data with weights wrt. centers where the current size of centers is size
// numPts : number of points in data
// numSamples: number of points to sample
// size : size of centers i.e. number of centers chosen already
double* d2_sample(double* data,double* centers,int numPts, int numSamples, int size)
{
// cumulative probability for each group of points
// the distances are cumulative only for a group. So, [0,...,numPts/numThreads], [numPts/numThreads+1,...,numPts*2/numThreads],... and so on. These groups contain cumulative distances.
double* distances = (double*)malloc(numPts*sizeof(double));
double* local_sums = (double*)malloc(numThreads*sizeof(double)); // local sums. first is sum for [0...numPts/numThreads-1], and so on. This is also a cumulative distribution.
double* result = (double*)malloc(numSamples*DIMENSION*sizeof(double));
for (int i = 0; i < numSamples; ++i)
{
for (int j = 0; j < DIMENSION; ++j)
{
result[i*DIMENSION + j] = 0;
}
}
// we're gonna need 2*numSamples random numbers.
double* rnd = (double*)malloc(2*numSamples*sizeof(double));
int i;
for(i = 0; i < 2*numSamples; i++){
rnd[i] = ((double) rand())/RAND_MAX;
}
#pragma omp parallel
{
struct timeval start,end;
gettimeofday(&start,NULL);
// create blocks of data
int tid = omp_get_thread_num();
int per_thread = (numPts + numThreads - 1) / numThreads;
int lower = tid * per_thread;
int higher = (tid + 1) * per_thread;
if(tid == numThreads - 1) higher = numPts;
int block_size = higher - lower;
double min_dist, local_dist;
double* p;
double prev_val = 0;
// cost of each block
double local_sum = 0;
int center_size = size;
int i,j;
for(i = 0;i < block_size;i++)
{
if(center_size == 0){
local_sum += 1;
distances[lower+i] = 1 + prev_val;
} else{
p = data + (lower+i)*DIMENSION;
min_dist = distance(p,centers);
for (j = 1; j < center_size; j++) {
local_dist = distance(p, centers + j*DIMENSION);
min_dist = min(min_dist, local_dist); // calculating minimum distances
}
local_sum += min_dist * min_dist;
distances[lower+i] = min_dist * min_dist + prev_val; // make cumulative
}
prev_val = distances[lower+i];
}
local_sums[tid] = local_sum;
#pragma omp barrier // everyone is here now
#pragma omp master
{
for(int i=1;i<numThreads;i++){
local_sums[i] = local_sums[i] + local_sums[i-1]; // make cumulative
}
// printf("Number of threads::%d\n",omp_get_num_threads());
}
gettimeofday(&end,NULL);
float cost_time = get_time_diff(start,end);
#pragma omp barrier
gettimeofday(&start,NULL);
#pragma omp for
for(int i = 0;i < numSamples;i++){
// first pick a block from the local_sums distribution
int groupNo = sample_from_distribution(local_sums, 0, numThreads, rnd[i*2]*local_sums[numThreads-1]);
// the start and end index of this block
int startIndex = groupNo * per_thread;
int endIndex = (groupNo + 1) * per_thread;
if(groupNo == numThreads - 1) endIndex = numPts;
// now sample from the cumulative distribution of the block
int pointIndex = sample_from_distribution(distances, startIndex, endIndex, rnd[2*i+1]*distances[endIndex-1]);
for (int j = 0; j < DIMENSION; ++j)
{
result[i*DIMENSION + j] = data[pointIndex*DIMENSION + j];
}
// memcpy(result + i*DIMENSION, data + pointIndex*DIMENSION, DIMENSION*sizeof(double));
}
gettimeofday(&end,NULL);
float sample_time = get_time_diff(start,end);
// if (center_size >= 99)
// {
// printf("Cost computation time ::%f\n",cost_time);
// printf("Sampling time ::%f\n",sample_time);
// }
}
free(distances);
free(local_sums);
return result;
}
// This version of d2_sample has optimized cost calculation by using cost computed in last iteration
double* d2_sample_2(double* data,double* centers,int numPts, int numSamples, int size, double* distances)
{
// cumulative probability for each group of points
// the distances are cumulative only for a group. So, [0,...,numPts/numThreads], [numPts/numThreads+1,...,numPts*2/numThreads],... and so on. These groups contain cumulative distances.
double* local_sums = (double*)malloc(numThreads*sizeof(double)); // local sums. first is sum for [0...numPts/numThreads-1], and so on. This is also a cumulative distribution.
double* result = (double*)malloc(numSamples*DIMENSION*sizeof(double));
for (int i = 0; i < numSamples; ++i)
{
for (int j = 0; j < DIMENSION; ++j)
{
result[i*DIMENSION + j] = 0;
}
}
// we're gonna need 2*numSamples random numbers.
double* rnd = (double*)malloc(2*numSamples*sizeof(double));
int i;
for(i = 0; i < 2*numSamples; i++){
rnd[i] = ((double) rand())/RAND_MAX;
}
#pragma omp parallel
{
struct timeval start,end;
gettimeofday(&start,NULL);
// create blocks of data
int tid = omp_get_thread_num();
int per_thread = (numPts + numThreads - 1) / numThreads;
int lower = tid * per_thread;
int higher = (tid + 1) * per_thread;
if(tid == numThreads - 1) higher = numPts;
int block_size = higher - lower;
double min_dist, local_dist;
double* p;
double prev_val = 0, old_prev_val = 0;
// cost of each block
double local_sum = 0;
int center_size = size;
int i;
for(i = 0;i < block_size;i++)
{
if(center_size == 0)
{
local_sum += 1;
distances[lower+i] = 1 + prev_val;
}
else if (center_size == 1)
{
p = data + (lower+i)*DIMENSION;
min_dist = distance(p,centers);
local_sum += min_dist * min_dist;
distances[lower+i] = min_dist * min_dist + prev_val; // make cumulative
}
else
{
p = data + (lower+i)*DIMENSION;
// min_dist = distance(p,centers[0]);
min_dist = distances[lower+i] - old_prev_val;
old_prev_val = distances[lower+i]; // Important for it to have old value of distance[lower+i] so that min_dist is correct in next iteration
local_dist = distance(p,centers + (center_size-1)*DIMENSION); // Find distance wrt last added new center;
local_dist = local_dist*local_dist;
min_dist = min(min_dist,local_dist);
local_sum += min_dist; // min_dist is already squared here because it is calculated usign cumulative distance
distances[lower+i] = min_dist + prev_val; // make cumulative
prev_val = distances[lower+i];
}
prev_val = distances[lower+i];
}
local_sums[tid] = local_sum;
#pragma omp barrier // everyone is here now
#pragma omp master
{
for(int i=1;i<numThreads;i++){
local_sums[i] = local_sums[i] + local_sums[i-1]; // make cumulative
}
}
gettimeofday(&end,NULL);
float cost_time = get_time_diff(start,end);
#pragma omp barrier
gettimeofday(&start,NULL);
#pragma omp for
for(int i = 0;i < numSamples;i++){
// first pick a block from the local_sums distribution
int groupNo = sample_from_distribution(local_sums, 0, numThreads, rnd[i*2]*local_sums[numThreads-1]);
// the start and end index of this block
int startIndex = groupNo * per_thread;
int endIndex = (groupNo + 1) * per_thread;
if(groupNo == numThreads - 1) endIndex = numPts;
// now sample from the cumulative distribution of the block
int pointIndex = sample_from_distribution(distances, startIndex, endIndex, rnd[2*i+1]*distances[endIndex-1]);
for (int j = 0; j < DIMENSION; ++j)
{
result[i*DIMENSION + j] = data[pointIndex*DIMENSION + j];
}
// memcpy(result + i*DIMENSION, data + pointIndex*DIMENSION, DIMENSION*sizeof(double));
}
gettimeofday(&end,NULL);
float sample_time = get_time_diff(start,end);
// if (center_size >= 99)
// {
// printf("Cost computation time ::%f\n",cost_time);
// printf("Sampling time ::%f\n",sample_time);
// }
}
free(local_sums);
return result;
}
double* mean_heuristic(double* multiset,int multisetSize)
{
// first do a kmeans++ initialiation on the multiset
int i,j;
// gettimeofday(&start,NULL);
double* level_2_sample = (double*)malloc(NUM_CLUSTER*DIMENSION*sizeof(double));
for(i = 0; i < NUM_CLUSTER; i++)
{
double* point = d2_sample(multiset,level_2_sample,multisetSize,1,i);
for (j = 0; j < DIMENSION; ++j)