-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathqueueHelpers.cuh
1369 lines (1176 loc) · 41.1 KB
/
queueHelpers.cuh
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
// Project Whippletree
// http://www.icg.tugraz.at/project/parallel
//
// Copyright (C) 2014 Institute for Computer Graphics and Vision,
// Graz University of Technology
//
// Author(s): Markus Steinberger - steinberger ( at ) icg.tugraz.at
// Michael Kenzel - kenzel ( at ) icg.tugraz.at
// Pedro Boechat - boechat ( at ) icg.tugraz.at
// Bernhard Kerbl - kerbl ( at ) icg.tugraz.at
// Mark Dokter - dokter ( at ) icg.tugraz.at
// Dieter Schmalstieg - schmalstieg ( at ) icg.tugraz.at
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#pragma once
#include "random.cuh"
#if (defined(_MSC_VER) && defined(_WIN64)) || defined(__LP64__)
#define __RET_PTR "l"
#else
#define __RET_PTR "r"
#endif
template<class TAdditionalData>
struct AdditionalDataInfo
{
static const int size = sizeof(TAdditionalData);
};
template<>
struct AdditionalDataInfo<void>
{
static const int size = 0;
};
template<int Mod, int MaxWarps>
__device__ __inline__ int warpBroadcast(int val, int who)
{
#if __CUDA_ARCH__ < 300
__shared__ volatile int comm[MaxWarps];
for(int offset = 0; offset < 32; offset += Mod)
{
if(Tools::laneid() - offset == who)
comm[threadIdx.x/32] = val;
if(Tools::laneid() < offset + Mod)
return comm[threadIdx.x/32];
}
return val;
#else
return __shfl(val, who, Mod);
#endif
}
template<int Mod>
__device__ __inline__ int warpBroadcast(int val, int who)
{
return warpBroadcast<Mod,32>(val, who);
}
template<int Mod, int MaxWarps>
__device__ __inline__ int warpShfl(int val, int who)
{
#if __CUDA_ARCH__ < 300
__shared__ volatile int comm[MaxWarps];
int runid = 0;
int res = val;
for(int offset = 0; offset < 32; offset += Mod)
{
for(int within = 0; within < Mod; ++within)
{
if(Tools::laneid() == runid)
comm[threadIdx.x/32] = val;
if( Tools::laneid() >= offset
&& Tools::laneid() < offset + Mod
&& (runid % Mod) == ((who + 32) % Mod) )
res = comm[threadIdx.x/32];
++runid;
}
}
return res;
#else
return __shfl(val, who, Mod);
#endif
}
template<int Mod>
__device__ __inline__ int warpShfl(int val, int who)
{
return warpShfl<Mod,32>(val, who);
}
template<int Maxrand>
__device__ __inline__ void backoff(int num)
{
volatile int local = threadIdx.x;
for(int i = 0; i < (whippletree::random::rand() % Maxrand); ++i)
{
local += num*threadIdx.x/(i+1234);
__threadfence();
}
}
__inline__ __device__ uint4& load(uint4& dest, const volatile uint4& src)
{
asm("ld.volatile.global.v4.u32 {%0, %1, %2, %3}, [%4];" : "=r"(dest.x), "=r"(dest.y), "=r"(dest.z), "=r"(dest.w) : __RET_PTR(&src));
return dest;
}
__inline__ __device__ uint2& load(uint2& dest, const volatile uint2& src)
{
asm("ld.volatile.global.v2.u32 {%0, %1}, [%2];" : "=r"(dest.x), "=r"(dest.y) : __RET_PTR(&src));
return dest;
}
__inline__ __device__ uint& load(uint& dest, const volatile uint& src)
{
dest = src;
return dest;
}
__inline__ __device__ uint1& load(uint1& dest, const volatile uint1& src)
{
dest.x = src.x;
return dest;
}
__inline__ __device__ uchar3& load(uchar3& dest, const volatile uchar3& src)
{
dest.x = src.x;
dest.y = src.y;
dest.z = src.z;
return dest;
}
__inline__ __device__ uchar2& load(uchar2& dest, const volatile uchar2& src)
{
dest.x = src.x;
dest.y = src.y;
return dest;
}
__inline__ __device__ uchar1& load(uchar1& dest, const volatile uchar1& src)
{
dest.x = src.x;
return dest;
}
__inline__ __device__ volatile uint4& store(volatile uint4& dest, const uint4& src)
{
//printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
// too much register usage, added by ZHENG Zhen
//dest.x = src.x;
//dest.y = src.y;
//dest.z = src.z;
//dest.w = src.w;
asm("st.volatile.global.v4.u32 [%0], {%1, %2, %3, %4};" : : __RET_PTR(&dest), "r"(src.x), "r"(src.y), "r"(src.z), "r"(src.w));
return dest;
}
__inline__ __device__ volatile uint2& store(volatile uint2& dest, const uint2& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
asm("st.volatile.global.v2.u32 [%0], {%1, %2};" : : __RET_PTR(&dest), "r"(src.x), "r"(src.y));
return dest;
}
__inline__ __device__ volatile uint& store(volatile uint& dest, const uint& src)
{
dest = src;
return dest;
}
__inline__ __device__ volatile uint1& store(volatile uint1& dest, const uint1& src)
{
dest.x = src.x;
return dest;
}
__inline__ __device__ volatile uchar3& store(volatile uchar3& dest, const uchar3& src)
{
dest.x = src.x;
dest.y = src.y;
dest.z = src.z;
return dest;
}
__inline__ __device__ volatile uchar2& store(volatile uchar2& dest, const uchar2& src)
{
dest.x = src.x;
dest.y = src.y;
return dest;
}
__inline__ __device__ volatile uchar1& store(volatile uchar1& dest, const uchar1& src)
{
dest.x = src.x;
return dest;
}
// 存储单元,最小单位是16个字节
template<uint TElementSize>
struct StorageElement16
{
static const int num_storage_owords = (TElementSize + 15) / 16;
uint4 storage[num_storage_owords];
__device__ volatile StorageElement16& operator=(const StorageElement16& ele) volatile
{
//printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
#pragma unroll 1
for(int i=0; i<num_storage_owords; i++)
{
store(storage[i], ele.storage[i]);
/*
const uint4 &src = ele.storage[i];
volatile uint4 &dest = storage[i];
dest.x = src.x;
dest.y = src.y;
dest.z = src.z;
dest.w = src.w;
*/
}
return *this;
}
};
// 存储单元的封装函数
template <int i>
struct StorageDude16
{
template<uint ElementSize>
__inline__ __device__ static StorageElement16<ElementSize>& assign(StorageElement16<ElementSize>& dest, const StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
StorageDude16<i - 1>::assign(dest, src);
dest.storage[i] = src.storage[i];
return dest;
}
template<uint ElementSize>
__inline__ __device__ static StorageElement16<ElementSize>& load(StorageElement16<ElementSize>& dest, const volatile StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
StorageDude16<i - 1>::load(dest, src);
::load(dest.storage[i], src.storage[i]);
return dest;
}
template<uint ElementSize>
__inline__ __device__ static volatile StorageElement16<ElementSize>& store(volatile StorageElement16<ElementSize>& dest, const StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
StorageDude16<i - 1>::store(dest, src);
::store(dest.storage[i], src.storage[i]);
return dest;
}
};
template <>
struct StorageDude16<0>
{
template<uint ElementSize>
__inline__ __device__ static StorageElement16<ElementSize>& assign(StorageElement16<ElementSize>& dest, const StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
dest.storage[0] = src.storage[0];
return dest;
}
template<uint ElementSize>
__inline__ __device__ static StorageElement16<ElementSize>& load(StorageElement16<ElementSize>& dest, const volatile StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
::load(dest.storage[0], src.storage[0]);
return dest;
}
template<uint ElementSize>
__inline__ __device__ static volatile StorageElement16<ElementSize>& store(volatile StorageElement16<ElementSize>& dest, const StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
::store(dest.storage[0], src.storage[0]);
return dest;
}
};
template<uint ElementSize>
__inline__ __device__ StorageElement16<ElementSize>& assign(StorageElement16<ElementSize>& dest, const StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
return StorageDude16<StorageElement16<ElementSize>::num_storage_owords - 1>::assign(dest, src);
}
template<uint ElementSize>
__inline__ __device__ StorageElement16<ElementSize>& load(StorageElement16<ElementSize>& dest, const volatile StorageElement16<ElementSize>& src)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
return StorageDude16<StorageElement16<ElementSize>::num_storage_owords - 1>::load(dest, src);
}
template<uint ElementSize>
__inline__ __device__ volatile StorageElement16<ElementSize>& store(volatile StorageElement16<ElementSize>& dest, const StorageElement16<ElementSize>& src)
{
// printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
return StorageDude16<StorageElement16<ElementSize>::num_storage_owords - 1>::store(dest, src);
}
// 最小单位是8字节
struct StorageElement8
{
uint2 storage;
__host__ __device__ volatile StorageElement8& operator=(const StorageElement8& ele) volatile
{
// Reach here for some cases
//printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
storage.x = ele.storage.x;
storage.y = ele.storage.y;
return *this;
}
};
__inline__ __device__ StorageElement8& assign(StorageElement8& dest, const StorageElement8& src)
{
// printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
dest.storage = src.storage;
return dest;
}
__inline__ __device__ StorageElement8& load(StorageElement8& dest, const volatile StorageElement8& src)
{
// printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
load(dest.storage, src.storage);
return dest;
}
__inline__ __device__ volatile StorageElement8& store(volatile StorageElement8& dest, const StorageElement8& src)
{
// printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
store(dest.storage, src.storage);
return dest;
}
// 选择storateelement的类型,8或者16,true就是8
// <个数,类型>
template<uint TElementSize, bool take_eight>
struct StorageElementSelector
{
typedef StorageElement16<TElementSize> type;
};
template<uint TElementSize>
struct StorageElementSelector<TElementSize, true>
{
typedef StorageElement8 type;
};
template<uint TElementSize>
struct StorageElementTyping
{
typedef typename StorageElementSelector<TElementSize, TElementSize <= 8>::type Type;
};
template<>
struct StorageElementTyping<0>; // life finds a way...
template<>
struct StorageElementTyping<1>
{
typedef unsigned char Type;
};
template<>
struct StorageElementTyping<2>
{
typedef uchar2 Type;
};
template<>
struct StorageElementTyping<3>
{
typedef uchar3 Type;
};
template<>
struct StorageElementTyping<4>
{
typedef uint Type;
};
template <unsigned int width>
struct selectVectorCopyType;
template <>
struct selectVectorCopyType<16U>
{
typedef uint4 type;
};
template <>
struct selectVectorCopyType<8U>
{
typedef uint2 type;
};
template <>
struct selectVectorCopyType<4U>
{
typedef uint1 type;
};
template <>
struct selectVectorCopyType<3U>
{
typedef uchar3 type;
};
template <>
struct selectVectorCopyType<2U>
{
typedef uchar2 type;
};
template <>
struct selectVectorCopyType<1U>
{
typedef uchar1 type;
};
// 拷贝bytes大小的数据,封装的结构体
template <unsigned int bytes, int threads = 1>
struct vectorCopy
{
static const unsigned int byte_width = bytes >= 16 ? 16 : bytes >= 8 ? 8 : bytes >= 4 ? 4 : 1;
static const unsigned int iterations = bytes / byte_width;
static const unsigned int max_threads = iterations < threads ? iterations : threads;
static const unsigned int iterations_threaded = iterations / max_threads;
static const unsigned int vectors_copied = max_threads * iterations_threaded;
typedef typename selectVectorCopyType<byte_width>::type vector_type;
__device__ __inline__ static void storeThreaded(volatile void* dest, const void* src, int i);
__device__ __inline__ static void loadThreaded(void* dest, const volatile void* src, int i);
};
template <int threads>
struct vectorCopy<0, threads>
{
__device__ __inline__ static void storeThreaded(volatile void* dest, const void* src, int i) {}
__device__ __inline__ static void loadThreaded(void* dest, const volatile void* src, int i) {}
};
// 拷贝bytes大小的数据,从src拷贝到dest,TODO: threads和参数i的作用未知
// threads表示用来拷贝的线程数量,参数i表示线程的id
template <unsigned int bytes, int threads>
__device__ __inline__ void vectorCopy<bytes, threads>::storeThreaded(volatile void* dest, const void* src, int i)
{
volatile vector_type* const destv = static_cast<volatile vector_type*>(dest);
const vector_type* const srcv = static_cast<const vector_type*>(src);
if (i < max_threads)
{
volatile vector_type* d = destv + i;
const vector_type* s = srcv + i;
#pragma unroll
for (int j = 0; j < iterations_threaded; ++j)
{
store(*d, *s);
d += max_threads;
s += max_threads;
}
}
vectorCopy<bytes - byte_width * vectors_copied, threads>::storeThreaded(destv + vectors_copied, srcv + vectors_copied, i);
}
template <unsigned int bytes, int threads>
__device__ __inline__ void vectorCopy<bytes, threads>::loadThreaded(void* dest, const volatile void* src, int i)
{
vector_type* const destv = static_cast<volatile vector_type*>(dest);
const volatile vector_type* const srcv = static_cast<const volatile vector_type*>(src);
if (i < max_threads)
{
vector_type* d = destv + i;
const volatile vector_type* s = srcv + i;
#pragma unroll
for (int j = 0; j < iterations_threaded; ++j)
{
load(*d, *s);
d += max_threads;
s += max_threads;
}
}
vectorCopy<bytes - byte_width * vectors_copied, threads>::loadThreaded(destv + vectors_copied, srcv + vectors_copied, i);
}
// 使用Threads个线程拷贝sizeof(T)大小的数据,从data拷贝到data_out
template<int Threads, class T>
__device__ __inline__ void multiWrite(volatile T* data_out, T* data)
{
vectorCopy<sizeof(T), Threads>::storeThreaded(data_out, data, Tools::laneid() % Threads);
//if (Tools::laneid() % Threads == 0)
//{
// for (int i = 0; i < sizeof(T); ++i)
// reinterpret_cast<volatile char*>(data_out)[i] = reinterpret_cast<char*>(data)[i];
//}
}
// 使用Threads个线程拷贝sizeof(T)大小的数据,从data_in拷贝到data
template<int Threads, class T>
__device__ __inline__ void multiRead(T* data, volatile T* data_in)
{
vectorCopy<sizeof(T), Threads>::loadThreaded(data, data_in, Tools::laneid() % Threads);
//if (Tools::laneid() % Threads == 0)
//{
// for (int i = 0; i < sizeof(T); ++i)
// reinterpret_cast<volatile char*>(data_in)[i] = reinterpret_cast<char*>(data)[i];
//}
}
//__inline__ __device__ void readStorageElement(void* data, const volatile void* stored, uint size)
//{
//uint* pData = reinterpret_cast<uint*>(data);
//const volatile uint* pReadData = reinterpret_cast<const volatile uint*>(stored);
//while(size >= 32)
//{
// *reinterpret_cast<StorageElementTyping<32>::Type*>(pData) =
// *reinterpret_cast<const volatile typename StorageElementTyping<32>::Type*>(pReadData);
// size -= 32;
// pData += 8;
// pReadData += 8;
//}
//if(size >= 16)
//{
// *reinterpret_cast<StorageElementTyping<16>::Type*>(pData) =
// *reinterpret_cast<const volatile typename StorageElementTyping<16>::Type*>(pReadData);
// size -= 16;
// pData += 4;
// pReadData += 4;
//}
//if(size >= 8)
//{
// *reinterpret_cast<StorageElementTyping<8>::Type*>(pData) =
// *reinterpret_cast<const volatile typename StorageElementTyping<8>::Type*>(pReadData);
// size -= 8;
// pData += 2;
// pReadData += 2;
//}
//if(size > 0)
//{
// *reinterpret_cast<StorageElementTyping<4>::Type*>(pData) =
// *reinterpret_cast<const volatile typename StorageElementTyping<4>::Type*>(pReadData);
//}
//}
template<uint TElementSize, class TAdditionalData, uint TQueueSize>
class QueueStorage
{
protected:
typedef typename StorageElementTyping<TElementSize>::Type QueueData_T;
typedef typename StorageElementTyping<sizeof(TAdditionalData)>::Type QueueAddtionalData_T;
public:
QueueData_T volatile storage[TQueueSize];
QueueAddtionalData_T volatile additionalStorage[TQueueSize];
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
static std::string name()
{
return "";
}
__inline__ __device__ void init()
{
}
template<class T>
__inline__ __device__ uint prepareData(T data, TAdditionalData additionalData)
{
return 0;
}
template<int TThreadsPerElenent, class T>
__inline__ __device__ uint prepareDataParallel(T* data, TAdditionalData additionalData)
{
return 0;
}
// 将data和additionalData分别写到storage和addidionalStorage的pos.x位置
// TODO: pos会对TQueueSize进行取模操作,所以仿佛可能覆盖之前queue中的值!
template<class T>
__inline__ __device__ void writeData(T data, TAdditionalData additionalData, uint2 pos)
{
pos.x = pos.x%TQueueSize;
storage[pos.x] = *reinterpret_cast<QueueData_T*>(&data);
additionalStorage[pos.x] = *reinterpret_cast<QueueAddtionalData_T*>(&additionalData);
}
// 将data和additionalData分别写到storage和addidionalStorage的pos位置,并行地写,使用TThreadsPerElenent个线程
// TODO: pos会对TQueueSize进行取模操作,所以仿佛可能覆盖之前queue中的值!
template<int TThreadsPerElenent, class T>
__inline__ __device__ void writeDataParallel(T* data, TAdditionalData additionalData, uint2 pos)
{
pos.x = pos.x%TQueueSize;
multiWrite<TThreadsPerElenent, T>(reinterpret_cast<volatile T*>(storage + pos.x), data);
multiWrite<TThreadsPerElenent, TAdditionalData>(reinterpret_cast<volatile TAdditionalData*>(additionalStorage + pos.x), &additionalData);
////TODO this could be unrolled in some cases...
//for(int i = Tools::laneid()%TThreadsPerElenent; i < TElementSize/sizeof(uint); i+=TThreadsPerElenent)
// reinterpret_cast<volatile uint*>(storage + pos.x)[i] = reinterpret_cast<uint*>(data)[i];
//for(int i = Tools::laneid()%TThreadsPerElenent; i < sizeof(TAdditionalData)/sizeof(uint); i+=TThreadsPerElenent)
// reinterpret_cast<volatile uint*>(additionalStorage + pos.x)[i] = reinterpret_cast<uint*>(&additionalData)[i];
}
// 读取pos位置的data和additionalData
// TODO pos仍然是取模,还是可能读到不想要的。
__inline__ __device__ void readData(void* data, TAdditionalData* additionalData, uint pos)
{
pos = pos%TQueueSize;
*reinterpret_cast<QueueData_T*>(data) = storage[pos];
*reinterpret_cast<QueueAddtionalData_T*>(additionalData) = additionalStorage[pos];
}
// 返回storage的pos位置的指针针。更新additionalData的值为addidionalStorage的第pos个位置的值
// TODO pos仍然是取模,还是可能读到不想要的。
__inline__ __device__ void* readDataPointers(TAdditionalData* additionalData, uint pos)
{
pos = pos%TQueueSize;
*reinterpret_cast<QueueAddtionalData_T*>(additionalData) = additionalStorage[pos];
return (void*)(storage + pos);
}
__inline__ __device__ void storageFinishRead(uint2 pos)
{
}
};
template<uint TElementSize, uint TQueueSize>
class QueueStorage<TElementSize, void, TQueueSize>
{
protected:
typedef typename StorageElementTyping<TElementSize>::Type QueueData_T;
QueueData_T volatile storage[TQueueSize];
public:
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
static std::string name()
{
return "";
}
__inline__ __device__ void init()
{
}
template<class T>
__inline__ __device__ uint prepareData(T data)
{
return 0;
}
template<int TThreadsPerElenent, class T>
__inline__ __device__ uint prepareDataParallel(T* data)
{
return 0;
}
// 将data写到storage的pos.x位置
// TODO: pos会对TQueueSize进行取模操作,所以仿佛可能覆盖之前queue中的值!
template<class T>
__inline__ __device__ void writeData(T data, uint2 pos)
{
// ZHENG Zhen added. Called here from enqueue.
// printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
pos.x = pos.x%TQueueSize;
storage[pos.x] = *reinterpret_cast<QueueData_T*>(&data);
// Too many register usages. Zhen added
//store(storage[pos.x], *reinterpret_cast<QueueData_T*>(&data));
//printf("TQueueSize: %d, Elementsize %d, offset0: %llx, offset1 %llx\n", TQueueSize,TElementSize, &storage[0], &storage[1]);
}
// 将data写到storage的pos.x位置,并行地写,使用TThreadsPerElenent个线程
// TODO: pos会对TQueueSize进行取模操作,所以仿佛可能覆盖之前queue中的值!
template<int TThreadsPerElenent, class T>
__inline__ __device__ void writeDataParallel(T* data, uint2 pos)
{
pos.x = pos.x%TQueueSize;
multiWrite<TThreadsPerElenent, T>(reinterpret_cast<volatile T*>(storage + pos.x), data);
////TODO this could be unrolled in some cases...
//for(int i = Tools::laneid()%TThreadsPerElenent; i < TElementSize/sizeof(uint); i+=TThreadsPerElenent)
// reinterpret_cast<volatile uint*>(storage + pos.x)[i] = reinterpret_cast<uint*>(data)[i];
}
// 读取pos位置的data
// TODO pos仍然是取模,还是可能读到不想要的。
__inline__ __device__ void readData(void* data, uint pos)
{
pos = pos%TQueueSize;
load(*reinterpret_cast<QueueData_T*>(data), storage[pos]);
}
// 返回storage的pos位置的指针。
// TODO pos仍然是取模,还是可能读到不想要的。
__inline__ __device__ void* readDataPointers(uint pos)
{
pos = pos%TQueueSize;
return (void*)(storage + pos);
}
__inline__ __device__ void storageFinishRead(uint2 pos)
{
}
};
template<uint TElementSize, uint TQueueSize, class TAdditionalData, class QueueStub, class TQueueStorage >
class QueueBuilder : public ::BasicQueue<TAdditionalData>, protected TQueueStorage, public QueueStub
{
static const uint ElementSize = (TElementSize + sizeof(uint) - 1)/sizeof(uint);
public:
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
__inline__ __device__ void init()
{
QueueStub::init();
TQueueStorage::init();
}
static std::string name()
{
return QueueStub::name() + TQueueStorage::name();
}
// 强data和additionalData实际写入到队列中,并更新相关的index值
// 返回值表示是否写入成功
template<class Data>
__inline__ __device__ bool enqueueInitial(Data data, TAdditionalData additionalData)
{
return enqueue<Data>(data, additionalData);
}
// 强data和additionalData实际写入到队列中,并更新相关的index值
// 返回值表示是否写入成功
template<class Data>
__device__ bool enqueue(Data data, TAdditionalData additionalData)
{
int2 pos = make_int2(-1,0);
uint addinfo = prepareData (data, additionalData);
do
{
pos = QueueStub:: template enqueuePrep<1>(pos);
if(pos.x >= 0)
{
writeData(data, additionalData, make_uint2(pos.x, addinfo) );
__threadfence();
QueueStub:: template enqueueEnd<1>(pos);
}
} while(pos.x == -2);
return pos.x >= 0;
}
// 将data和additionalData实际写入到队列中,并更新相关的index值。并行地写,使用TThreadssPerElment个线程
// 返回值表示是否写入成功
template<int TThreadssPerElment, class Data>
__device__ bool enqueue(Data* data, TAdditionalData additionalData)
{
int2 pos = make_int2(-1,0);
uint addinfo = TQueueStorage :: template prepareDataParallel<TThreadssPerElment> (data, additionalData);
do
{
pos = QueueStub:: template enqueuePrep<TThreadssPerElment>(pos);
if(pos.x >= 0)
{
TQueueStorage :: template writeDataParallel<TThreadssPerElment> (data, additionalData, make_uint2(pos.x, addinfo) );
__threadfence();
QueueStub:: template enqueueEnd<TThreadssPerElment>(pos);
}
} while(pos.x == -2);
return pos.x >= 0;
}
// TODO: 没有看懂,dequeuePrep的具体实现不知道是怎样的,目前看到的是返回(0,0),应该不对
// dequeuePrep:来自QueueDistLockStub,准备dequeue num个数据,更新count和front
__inline__ __device__ int dequeue(void* data, TAdditionalData* addtionalData, int num)
{
uint2 offset_take = QueueStub::dequeuePrep(num);
if(threadIdx.x < offset_take.y)
{
readData(reinterpret_cast<uint*>(data) + threadIdx.x * ElementSize, addtionalData + threadIdx.x, offset_take.x + threadIdx.x);
__threadfence();
}
__syncthreads();
QueueStub::dequeueEnd(offset_take);
TQueueStorage::storageFinishRead(offset_take);
return offset_take.y;
}
};
template<uint TElementSize, uint TQueueSize, class QueueStub, class TQueueStorage >
class QueueBuilder<TElementSize, TQueueSize, void, QueueStub, TQueueStorage>
: public ::BasicQueue<void>, protected TQueueStorage, public QueueStub
{
static const uint ElementSize = (TElementSize + sizeof(uint) - 1)/sizeof(uint);
public:
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
__inline__ __device__ void init()
{
QueueStub::init();
TQueueStorage::init();
}
static std::string name()
{
return QueueStub::name() + TQueueStorage::name();
}
// is the queue fill rate > portion ?
// added by zhengzhen
__inline__ __device__ bool isFill(float portion)
{
float fsize = QueueStub::size();
float fcapa = QueueStub::capacity();
//printf("size: %d\n", QueueStub::size());
//printf("queue, size: %.0f, capa: %.0f, ratio: %.2f\n", fsize, fcapa, fsize/fcapa);
if(fcapa <= 0)
{
return true;
}
return (fsize / fcapa) > portion;
}
// 将data实际写入到队列中,并更新相关的index值
// 返回值表示是否写入成功
template<class Data>
__inline__ __device__ bool enqueueInitial(Data data)
{
//printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
return enqueue<Data>(data);
}
// 将data实际写入到队列中,并更新相关的index值
// 返回值表示是否写入成功
template<class Data>
__device__ bool enqueue(Data data)
{
// succeed here, ZHENG Zhen
//printf("%f, %s in %s, at line %d\n", data, __FUNCTION__, __FILE__, __LINE__);
int2 pos = make_int2(-1,0);
uint addinfo = prepareData(data);
//do
{
pos = QueueStub::template enqueuePrep<1>(pos);
if(pos.x >= 0)
{
writeData(data, make_uint2(pos.x, addinfo));
__threadfence();
QueueStub:: template enqueueEnd<1>(pos);
}
}// while(pos.x == -2);
return pos.x >= 0;
}
// 将data实际写入到队列中,并更新相关的index值。并行地写,使用TThreadssPerElment个线程
// 返回值表示是否写入成功
template<int TThreadssPerElment, class Data>
__device__ bool enqueue(Data* data)
{
int2 pos = make_int2(-1,0);
uint addinfo = TQueueStorage :: template prepareDataParallel<TThreadssPerElment> (data);
do
{
pos = QueueStub:: template enqueuePrep<TThreadssPerElment>(pos);
if(pos.x >= 0)
{
TQueueStorage :: template writeDataParallel<TThreadssPerElment> (data, make_uint2(pos.x, addinfo) );
__threadfence();
QueueStub:: template enqueueEnd<TThreadssPerElment>(pos);
}
} while(pos.x == -2);
return pos.x >= 0;
}
// TODO: 没有看懂,dequeuePrep的具体实现不知道是怎样的,目前看到的是返回(0,0),应该不对
__inline__ __device__ int dequeue(void* data, int num)
{
uint2 offset_take = QueueStub::dequeuePrep(num);
if(threadIdx.x < offset_take.y)
{
TQueueStorage::readData(reinterpret_cast<uint*>(data) + threadIdx.x * ElementSize, offset_take.x + threadIdx.x);
__threadfence();
}
__syncthreads();
QueueStub::dequeueEnd(offset_take);
TQueueStorage::storageFinishRead(offset_take);
return offset_take.y;
}
};
// TODO: 没有看懂,这个类是什么意思?
//FIXME: class is not overflowsave / has no free!!!!
template<uint MemSize>
class MemoryAllocFastest
{
static const uint AllocElements = MemSize/sizeof(uint);
uint allocPointer;
public:
uint4 volatile dataAllocation[AllocElements/4];
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
__inline__ __device__ void init()
{
uint lid = threadIdx.x + blockIdx.x*blockDim.x;
if(lid == 0)
allocPointer = 0;
}
__inline__ __device__ uint allocOffset(uint size)
{
size = size/sizeof(uint);
uint p = atomicAdd(&allocPointer,size)%AllocElements;
while(p + size > AllocElements)
p = atomicAdd(&allocPointer,size)%AllocElements;
return p;
}
__inline__ __device__ volatile uint* offsetToPointer(uint offset)
{
return reinterpret_cast<volatile uint*>(dataAllocation) + offset;
}
__inline__ __device__ volatile uint* alloc(uint size)
{
return offsetToPointer(allocOffset(size));
}