-
Notifications
You must be signed in to change notification settings - Fork 0
/
cudaPrintf.cu
120 lines (94 loc) · 2.63 KB
/
cudaPrintf.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
/*
// How to use from device
// after intialization from host
strBuffer[0] = 0;
it=0;
lenBuffer = strlen("Worker ID = ");
//for(it=0; it<lenBuffer;it++);// ( (byte*)&("Worker ID = ") );
strBuffer[it++] = '0' + ((lBlockNum/1000));
strBuffer[it++] = '0' + ((lBlockNum/100) %10);
strBuffer[it++] = '0' + ((lBlockNum/10) %10);
strBuffer[it++] = '0' + ((lBlockNum/1) %10);
strBuffer[it++] = '\n';
strBuffer[it++] = '\0';
cudaPrintf( (byte*)&strBuffer );
*/
/*-----------------------------------------------.
| Debugging and Printing Facilities |
`-----------------------------------------------*/
__device__ int printingLocked = 0;
__device__ int queue = 0;
__device__ void cudaPrintf ( byte *buffer )
{
int i = 0;
while( atomicCAS(&printingLocked, 0, 1) != 0 );
if( debuggingDevice.index > debuggingDevice.upperLimit )
{
while( atomicCAS(&printingLocked, 1, 0) != 1 );
return;
}
while((buffer[i] != 0) && (debuggingDevice.index < debuggingDevice.upperLimit))
{
debuggingDevice.buffer[debuggingDevice.index++] = buffer[i++];
}
while( atomicCAS(&printingLocked, 1, 0) != 1 );
}
__device__ int cudaStrLen ( byte *buffer )
{
int i;
for(i = 0; buffer[i] != 0; i++);
return i;
}
__device__ int getStrArrayItem ( int index , byte* array , byte delimiter)
{
int count;
int i;
i = 0;
count = -1;
while(count < index)
{
if(array[i++] == delimiter)
count++;
}
return i;
}
__device__ int getStrArrayItem ( int index , byte* array)
{
return getStrArrayItem ( index , array , '\0');
}
__device__ void cudaPrintf ( byte character )
{
while( atomicCAS(&printingLocked, 0, 1) != 0 );
if( debuggingDevice.index > debuggingDevice.upperLimit - (character == 0 ? 0 : 1) )
{
while( atomicCAS(&printingLocked, 1, 0) != 1 );
return;
}
debuggingDevice.buffer[debuggingDevice.index++] = character;
while( atomicCAS(&printingLocked, 1, 0) != 1 );
}
__device__ void cudaPrintf ( const char *string )
{
cudaPrintf( ((byte*)&string) );
}
__global__ void initializeDeviceDebugging ( byte *sharedBuffer , unsigned int lInputSize )
{
debuggingDevice.buffer = sharedBuffer;
debuggingDevice.index = 0;
debuggingDevice.upperLimit = lInputSize;
}
void initializeDebugging( int lInputSize )
{
cudaMalloc( (void**)&printingBuffer , lInputSize );
initializeDeviceDebugging<<<1,1>>>( printingBuffer , lInputSize);
cudaThreadSynchronize();
}
__global__ void closeDeviceDebugging()
{
cudaPrintf( (byte)'\0' );
}
void closeDebugging()
{
closeDeviceDebugging<<<1,1>>>();
cudaThreadSynchronize();
}