-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrixKernels.cu
740 lines (620 loc) · 23.7 KB
/
matrixKernels.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
/*
author: Krzysztof Sopyla (ksopyla@uwm.edu.pl)
*/
#define BLOCK_SIZE 64
#define WARP_SIZE 32
texture<float,1,cudaReadModeElementType> vectorTexRef;
//helper functions
//Use binary search algorith to find index and value in B matrix
__device__ float FindValForBIdx(const int* BIdx,
const float* BVals,
const int index,
int col_start, int col_end)
{
int low = col_start;
int high = col_end-1;
int mid=-1;
while (low < high) {
mid = low + ((high - low) / 2);
if (BIdx[mid] < index)
low = mid + 1;
else
//can't be high = mid-1: here A[mid] >= value,
//so high can't be < mid if A[mid] == value
high = mid;
}
// high == low, using high or low depends on taste
if ((low < col_end) && (BIdx[low] == index))
return BVals[low]; // found
else
return 0.0; // not found
// int low = col_start;
// int high=col_end-1;
//int mid=-1;
//int curIdx=-1;
// do
// mid = low + ((high-low)/ 2);//better bit shift
// curIdx =BIdx[mid];
// if( index > curIdx){
// low= mid + 1;
// }
// else
// high= mid - 1;
// while (curIdx = index) || (low > high);
}
//computes two sparse matrix product in CRS format
//AVals - values for first matrix
//AIdx - indexes for first matrix
//APtrs - pointers to next vector
//BVals - values for second matrix
//BIdx - indexes for second matrix
//BPtrs - pointers to next vectors
//result - result matrix
//ARows - number of rows in first matrix
//BCols - number of cols in second matrix
extern "C" __global__ void spmm_csr_naive(const float * AVals,
const int * AIdx,
const int * APtrs,
const float * BVals,
const int * BIdx,
const int * BPtrs,
float * result,
const int ARows,
const int BCols,
const int AElements,
const int BElements)
{
const int row = blockIdx.y*blockDim.y+threadIdx.y;
const int col = blockIdx.x*blockDim.x+threadIdx.x;
if( !(row<ARows && col<BCols) )
{
return;
}
//possible optimization, cache this in shared memory
//int AStart = APtrs[row];
int curPosA = APtrs[row];
int AEnd = APtrs[row+1];
//int BStart = BPtrs[col];
int curPosB = BPtrs[col];
int BEnd = BPtrs[col+1];
int AcurIdx=-1;
int BcurIdx=-1;
float sum=0;
while(curPosA<AEnd && curPosB<BEnd)
{
AcurIdx = AIdx[curPosA];
BcurIdx = BIdx[curPosB];
if(AcurIdx == BcurIdx)
{
sum+=AVals[curPosA]*BVals[curPosB];
curPosA++;
curPosB++;
}else if( AcurIdx< BcurIdx)
{
curPosA++;
}else
{
curPosB++;
}
}
result[row*BCols+col] = sum;
}
//computes two sparse matrix product in CRS format, use shared memory to cache
//one column vector in second matrix
//AVals - values for first matrix
//AIdx - indexes for first matrix
//APtrs - pointers to next vector
//BVals - values for second matrix
//BIdx - indexes for second matrix
//BPtrs - pointers to next vectors
//result - result matrix
//ARows - number of rows in first matrix
//BCols - number of cols in second matrix
extern "C" __global__ void spmm_csr_naive_shared_one(const float * AVals,
const int * AIdx,
const int * APtrs,
const float * BVals,
const int * BIdx,
const int * BPtrs,
float * result,
const int ARows,
const int BCols,
const int AElements,
const int BElements)
{
//max size = 4081
__shared__ int svIdx[121];
__shared__ float svVals[121];
//barier[0]=BStart
//barier[1]=BEnd
__shared__ int barier[2];
const int row = blockIdx.y*blockDim.y+threadIdx.y;
const int col = blockIdx.x*blockDim.x+threadIdx.x;
if( !(row<ARows && col<BCols) )
{
return;
}
//int BStart = BPtrs[col];
if(threadIdx.y<2){
barier[threadIdx.y]=BPtrs[col+threadIdx.y] ;
}
//????
__syncthreads();
int curPosB = barier[0];
int diff=barier[1]-barier[0];
//int curPosB = BPtrs[col];
//int diff = BPtrs[col+1] - curPosB;
int BcurIdx;
for(int th=threadIdx.y; th<diff;th+=blockDim.y)
{
svVals[th]= BVals[curPosB+th];
svIdx[th]=BIdx[curPosB+th];
}
__syncthreads();
int curPosA = APtrs[row];
int AEnd = APtrs[row+1];
int AcurIdx;
float sum=0;
//now B column is in shared mem, so it starts from 0
curPosB=0;
while(curPosA<AEnd && curPosB<diff)
{
AcurIdx = AIdx[curPosA];
BcurIdx = svIdx[curPosB];
if(AcurIdx == BcurIdx)
{
sum+=AVals[curPosA]*svVals[curPosB];
curPosA++;
curPosB++;
}else if( AcurIdx< BcurIdx)
{
curPosA++;
}else
{
curPosB++;
}
}
__syncthreads();
result[row*BCols+col] = sum;
//column major order
//result[row+ARows*col] = sum;
}
//computes two sparse matrix product in CRS format, try to align memory access
//in warps
//AVals - values for first matrix
//AIdx - indexes for first matrix
//APtrs - pointers to next vector
//BVals - values for second matrix
//BIdx - indexes for second matrix
//BPtrs - pointers to next vectors
//result - result matrix
//ARows - number of rows in first matrix
//BCols - number of cols in second matrix
extern "C" __global__ void spmm_csr_warp(const float * AVals,
const int * AIdx,
const int * APtrs,
const float * BVals,
const int * BIdx,
const int * BPtrs,
float * result,
const int ARows,
const int BCols,
const int AElements,
const int BElements)
{
__shared__ float sdata[BLOCK_SIZE + 16]; // padded to avoid reduction ifs
__shared__ int ptrs[BLOCK_SIZE/WARP_SIZE][2];
//stores "start" and "end" of column
//__shared__ int bShPtrs[BLOCK_SIZE/WARP_SIZE][2];
// global thread index
const int thread_id = BLOCK_SIZE * blockIdx.y + threadIdx.y;
// thread index within the warp (0,31)
const int thread_lane = threadIdx.y & (WARP_SIZE-1);
// global warp index
const int warp_id = thread_id / WARP_SIZE;
// warp index within the CTA
const int warp_lane = threadIdx.y / WARP_SIZE;
// total number of active warps
const int num_warps = (BLOCK_SIZE / WARP_SIZE) * gridDim.y;
//index of column in B matrix
const int col = blockDim.x*blockIdx.x+threadIdx.x;
/*
if(thread_lane<2){
bShPtrs[warp_lane][thread_lane]=BPtrs[col+thread_lane] ;
}
const int col_start = bShPtrs[warp_lane][0];
const int col_end = bShPtrs[warp_lane][1];
*/
const int col_start = BPtrs[col];
const int col_end = BPtrs[col+1];
for(int row = warp_id; row < ARows; row += num_warps){
// use two threads to fetch vecPointers[row] and vecPointers[row+1]
// this is considerably faster than the straightforward version
if(thread_lane < 2)
ptrs[warp_lane][thread_lane] = APtrs[row + thread_lane];
const int row_start = ptrs[warp_lane][0]; //same as: row_start = vecPointers[row];
const int row_end = ptrs[warp_lane][1]; //same as: row_end = vecPointers[row+1];
// compute local sum
float sum = 0;
float bVal=0;
for(int jj = row_start + thread_lane; jj < row_end; jj += WARP_SIZE)
{
bVal=FindValForBIdx(BIdx,BVals,AIdx[jj],col_start,col_end);
sum += AVals[jj] * bVal;
}
// reduce local sums to row sum (ASSUME: warpsize 32)
sdata[threadIdx.y] = sum;
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 16];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 8];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 4];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 2];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 1];
__syncthreads();
// first thread writes warp result
if (thread_lane == 0){
//results[row] += sdata[threadIdx.x];
//result[row] =sdata[threadIdx.x];
result[row*BCols+col] = sdata[threadIdx.y];
}
}
}
//computes two sparse matrix product in CRS format, try to align memory access
//in warps use shared memory to cache B column
//AVals - values for first matrix
//AIdx - indexes for first matrix
//APtrs - pointers to next vector
//BVals - values for second matrix
//BIdx - indexes for second matrix
//BPtrs - pointers to next vectors
//result - result matrix
//ARows - number of rows in first matrix
//BCols - number of cols in second matrix
extern "C" __global__ void spmm_csr_warp_shared(const float * AVals,
const int * AIdx,
const int * APtrs,
const float * BVals,
const int * BIdx,
const int * BPtrs,
float * result,
const int ARows,
const int BCols,
const int AElements,
const int BElements)
{
__shared__ float sdata[BLOCK_SIZE + 16]; // padded to avoid reduction ifs
__shared__ int ptrs[BLOCK_SIZE/WARP_SIZE][2];
__shared__ int svIdx[121];
__shared__ float svVals[121];
//stores "start" and "end" of column
__shared__ int bShPtrs[2];
// global thread index
const int thread_id = BLOCK_SIZE * blockIdx.y + threadIdx.y;
// thread index within the warp (0,31)
const int thread_lane = threadIdx.y & (WARP_SIZE-1);
// global warp index
const int warp_id = thread_id / WARP_SIZE;
// warp index within the CTA
const int warp_lane = threadIdx.y / WARP_SIZE;
// total number of active warps
const int num_warps = (BLOCK_SIZE / WARP_SIZE) * gridDim.y;
//index of column in B matrix
const int col = blockDim.x*blockIdx.x+threadIdx.x;
const int col_start = BPtrs[col];
const int col_end = BPtrs[col+1];
for(int th=threadIdx.y; th<(col_end - col_start);th+=blockDim.y)
{
svVals[th]= BVals[col_start+th];
svIdx[th]=BIdx[col_start+th];
}
__syncthreads();
for(int row = warp_id; row < ARows; row += num_warps){
// use two threads to fetch vecPointers[row] and vecPointers[row+1]
// this is considerably faster than the straightforward version
if(thread_lane < 2)
ptrs[warp_lane][thread_lane] = APtrs[row + thread_lane];
const int row_start = ptrs[warp_lane][0]; //same as: row_start = vecPointers[row];
const int row_end = ptrs[warp_lane][1]; //same as: row_end = vecPointers[row+1];
// compute local sum
float sum = 0;
float bVal=0;
for(int jj = row_start + thread_lane; jj < row_end; jj += WARP_SIZE)
{
bVal=FindValForBIdx(svIdx,svVals,AIdx[jj],0,(col_end-col_start));
sum += AVals[jj] * bVal;
}
// reduce local sums to row sum (ASSUME: warpsize 32)
sdata[threadIdx.y] = sum;
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 16];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 8];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 4];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 2];
__syncthreads();
sdata[threadIdx.y] = sum = sum + sdata[threadIdx.y + 1];
__syncthreads();
// first thread writes warp result
if (thread_lane == 0){
//results[row] += sdata[threadIdx.x];
//result[row] =sdata[threadIdx.x];
result[row*BCols+col] = sdata[threadIdx.y];
}
}
}
//computes two sparse matrix product in CRS format, try to align memory access
//in warps use shared memory to cache B column
//AVals - values for first matrix
//AIdx - indexes for first matrix
//APtrs - pointers to next vector
//BVals - values for second matrix
//BIdx - indexes for second matrix
//BPtrs - pointers to next vectors
//result - result matrix
//ARows - number of rows in first matrix
//BCols - number of cols in second matrix
extern "C" __global__ void spmm_csr_warp_shared_Y(const float * AVals,
const int * AIdx,
const int * APtrs,
const float * BVals,
const int * BIdx,
const int * BPtrs,
float * result,
const int ARows,
const int BCols,
const int AElements,
const int BElements)
{
__shared__ float sdata[BLOCK_SIZE + 16]; // padded to avoid reduction ifs
__shared__ int ptrs[BLOCK_SIZE/WARP_SIZE][2];
__shared__ int svIdx[121];
__shared__ float svVals[121];
//stores "start" and "end" of column
__shared__ int bShPtrs[2];
// global thread index
const int thread_id = BLOCK_SIZE * blockIdx.x + threadIdx.x;
// thread index within the warp (0,31)
const int thread_lane = threadIdx.x & (WARP_SIZE-1);
// global warp index
const int warp_id = thread_id / WARP_SIZE;
// warp index within the CTA
const int warp_lane = threadIdx.x / WARP_SIZE;
// total number of active warps
const int num_warps = (BLOCK_SIZE / WARP_SIZE) * gridDim.x;
//index of column in B matrix
const int col = blockDim.y*blockIdx.y+threadIdx.y;
const int col_start = BPtrs[col];
const int col_end = BPtrs[col+1];
for(int th=threadIdx.x; th<(col_end - col_start);th+=blockDim.x)
{
svVals[th]= BVals[col_start+th];
svIdx[th]=BIdx[col_start+th];
}
__syncthreads();
for(int row = warp_id; row < ARows; row += num_warps){
// use two threads to fetch vecPointers[row] and vecPointers[row+1]
// this is considerably faster than the straightforward version
if(thread_lane < 2)
ptrs[warp_lane][thread_lane] = APtrs[row + thread_lane];
const int row_start = ptrs[warp_lane][0]; //same as: row_start = vecPointers[row];
const int row_end = ptrs[warp_lane][1]; //same as: row_end = vecPointers[row+1];
// compute local sum
float sum = 0;
float bVal=0;
for(int jj = row_start + thread_lane; jj < row_end; jj += WARP_SIZE)
{
bVal=FindValForBIdx(svIdx,svVals,AIdx[jj],0,(col_end-col_start));
sum += AVals[jj] * bVal;
}
// reduce local sums to row sum (ASSUME: warpsize 32)
sdata[threadIdx.x] = sum;
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 16];
__syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 8];
__syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 4];
__syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 2];
__syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 1];
__syncthreads();
// first thread writes warp result
if (thread_lane == 0){
//results[row] += sdata[threadIdx.x];
//result[row] =sdata[threadIdx.x];
result[row*BCols+col] = sdata[threadIdx.x];
}
}
}
#define BLOCK_XY 2*64
//computes two sparse matrix product in CRS format, try to align memory access
//in warps use shared memory to cache B column
//AVals - values for first matrix
//AIdx - indexes for first matrix
//APtrs - pointers to next vector
//BVals - values for second matrix
//BIdx - indexes for second matrix
//BPtrs - pointers to next vectors
//result - result matrix
//ARows - number of rows in first matrix
//BCols - number of cols in second matrix
extern "C" __global__ void spmm_csr_warp_shared_doubled(const float * AVals,
const int * AIdx,
const int * APtrs,
const float * BVals,
const int * BIdx,
const int * BPtrs,
float * result,
const int ARows,
const int BCols,
const int AElements,
const int BElements)
{
//!!!!!! not ready many errors !!!!!
// do not use it !
__shared__ float sdata[2][BLOCK_XY + 16]; // padded to avoid reduction ifs
__shared__ int ptrs[BLOCK_XY/WARP_SIZE][2];
__shared__ int svIdx[2][121];
__shared__ float svVals[2][121];
//stores "start" and "end" of column
__shared__ int bShPtrs[2][2];
const int threadPart=2*threadIdx.y+threadIdx.x;
// global thread index
const int thread_id =BLOCK_XY* blockIdx.y + threadPart;
// thread index within the warp (0,31)
const int thread_lane = threadPart & (WARP_SIZE-1);
// global warp index
const int warp_id = thread_id / WARP_SIZE;
// warp index within the block (CTA)
const int warp_lane = threadPart / WARP_SIZE;
// total number of active warps
const int num_warps = (blockDim.y / WARP_SIZE) * gridDim.y;//*gridDim.x;
//index of first column in block in B matrix
//assume that blockDim.x==2
const int col = blockDim.x*blockIdx.x; //+threadIdx.x;
//copy pointers to each column to shared memory
if(threadIdx.y<2)
{
//blockDim.x must equal 2
//bShPtrs[threadIdx.x][threadIdx.y]=BPtrs[col+threadIdx.y];
//if col is computed without adding threadIdx.x then above
//line has an error and pointers should be set this way
bShPtrs[threadIdx.x][threadIdx.y]=BPtrs[col+threadIdx.y+threadIdx.x];
}
__syncthreads();
//copy vals and indexes for two column to shared mem.
for(int th=threadIdx.y; th<(bShPtrs[threadIdx.x][1]-bShPtrs[threadIdx.x][0]);th+=blockDim.y)
{
svVals[threadIdx.x][th]= BVals[bShPtrs[threadIdx.x][0]+th];
svIdx[threadIdx.x][th]=BIdx[bShPtrs[threadIdx.x][0]+th];
}
__syncthreads();
for(int row = warp_id; row < ARows; row += num_warps){
// use two threads to fetch vecPointers[row] and vecPointers[row+1]
// this is considerably faster than the straightforward version
if(thread_lane < 2)
ptrs[warp_lane][thread_lane] = APtrs[row + thread_lane];
const int row_start = ptrs[warp_lane][0]; //same as: row_start = vecPointers[row];
const int row_end = ptrs[warp_lane][1]; //same as: row_end = vecPointers[row+1];
// compute local sum for two row and two column
float sum[2] = {0,0};
float bVal1=0;
float bVal2=0;
for(int jj = row_start + thread_lane; jj < row_end; jj += WARP_SIZE)
{
int aIdx=AIdx[jj];
bVal1=FindValForBIdx(svIdx[0],svVals[0],aIdx,0, bShPtrs[0][1]-bShPtrs[0][0]);
bVal2=FindValForBIdx(svIdx[1],svVals[1],aIdx,0, bShPtrs[1][1]-bShPtrs[1][0]);
float aVals = AVals[jj];
sum[0] += aVals * bVal1;
sum[1] += aVals * bVal2;
}
// reduce local sums to row sum (ASSUME: warpsize 32)
/*
sdata[threadIdx.x][threadPart] = sum[threadIdx.x];
sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 16];
__syncthreads();
sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 8];
__syncthreads();
sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 4];
__syncthreads();
sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 2];
__syncthreads();
sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 1];
__syncthreads();
*/
sdata[0][threadPart] = sum[0];
sdata[1][threadPart] = sum[1];
sdata[0][threadPart] = sum[0] = sum[0] + sdata[0][threadPart + 16];
sdata[1][threadPart] = sum[1] = sum[1] + sdata[1][threadPart + 16];
__syncthreads();
//sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 8];
sdata[0][threadPart] = sum[0] = sum[0] + sdata[0][threadPart + 8];
sdata[1][threadPart] = sum[1] = sum[1] + sdata[1][threadPart + 8];
__syncthreads();
// sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 4];
sdata[0][threadPart] = sum[0] = sum[0] + sdata[0][threadPart + 4];
sdata[1][threadPart] = sum[1] = sum[1] + sdata[1][threadPart + 4];
__syncthreads();
//sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 2];
sdata[0][threadPart] = sum[0] = sum[0] + sdata[0][threadPart + 2];
sdata[1][threadPart] = sum[1] = sum[1] + sdata[1][threadPart + 2];
__syncthreads();
//sdata[threadIdx.x][threadPart] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadPart + 1];
sdata[0][threadPart] = sum[0] = sum[0] + sdata[0][threadPart + 1];
sdata[1][threadPart] = sum[1] = sum[1] + sdata[1][threadPart + 1];
__syncthreads();
/*sdata[threadIdx.x][threadIdx.y] = sum[threadIdx.x];
sdata[threadIdx.x][threadIdx.y] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadIdx.y + 16];
__syncthreads();
sdata[threadIdx.x][threadIdx.y] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadIdx.y + 8];
__syncthreads();
sdata[threadIdx.x][threadIdx.y] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadIdx.y + 4];
__syncthreads();
sdata[threadIdx.x][threadIdx.y] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadIdx.y + 2];
__syncthreads();
sdata[threadIdx.x][threadIdx.y] = sum[threadIdx.x] = sum[threadIdx.x] + sdata[threadIdx.x][threadIdx.y + 1];
__syncthreads();*/
// first thread writes warp result
if (thread_lane <1){
result[row*BCols+col] = sdata[0][threadPart];
result[row*BCols+col+1] = sdata[1][threadPart];
}
}
}
//computes two sparse matrix product, matrix A is in CRS format, matrix B is
//divided into set of block kolumn (each kolumn is in dense format, in texture cache)
//AVals - values for first matrix
//AIdx - indexes for first matrix
//APtrs - pointers to next vector
//result - result matrix
//ARows - number of rows in first matrix
//BCols - number of cols in second matrix
//csr vector kernel for linear dot product
extern "C" __global__ void spmm_csr_dense_vector(const float * AVals,
const int * AIdx,
const int * APtrs,
float * result,
const int ARows,
const int BCols,
const int ColumnIndex)
{
__shared__ float sdata[BLOCK_SIZE + 16]; // padded to avoid reduction ifs
__shared__ int ptrs[BLOCK_SIZE/WARP_SIZE][2];
const int thread_id = BLOCK_SIZE * blockIdx.x + threadIdx.x; // global thread index
const int thread_lane = threadIdx.x & (WARP_SIZE-1); // thread index within the warp
const int warp_id = thread_id / WARP_SIZE; // global warp index
const int warp_lane = threadIdx.x / WARP_SIZE; // warp index within the CTA
const int num_warps = (BLOCK_SIZE / WARP_SIZE) * gridDim.x; // total number of active warps
for(int row = warp_id; row < ARows; row += num_warps){
// use two threads to fetch Ap[row] and Ap[row+1]
// this is considerably faster than the straightforward version
if(thread_lane < 2)
ptrs[warp_lane][thread_lane] = APtrs[row + thread_lane];
const int row_start = ptrs[warp_lane][0]; //same as: row_start = Ap[row];
const int row_end = ptrs[warp_lane][1]; //same as: row_end = Ap[row+1];
// compute local sum
float sum = 0;
for(int jj = row_start + thread_lane; jj < row_end; jj += WARP_SIZE)
sum += AVals[jj] * tex1Dfetch(vectorTexRef,AIdx[jj]);
// reduce local sums to row sum (ASSUME: warpsize 32)
sdata[threadIdx.x] = sum;
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 16]; __syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 8]; __syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 4]; __syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 2]; __syncthreads();
sdata[threadIdx.x] = sum = sum + sdata[threadIdx.x + 1]; __syncthreads();
// first thread writes warp result
if (thread_lane == 0)
{
//row major order
result[row*BCols+ColumnIndex]= sdata[threadIdx.x];
//column major order
//result[ColumnIndex*ARows+row]= sdata[threadIdx.x];
}
}
}