-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier_example.cu
More file actions
39 lines (31 loc) · 804 Bytes
/
barrier_example.cu
File metadata and controls
39 lines (31 loc) · 804 Bytes
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
#include <stdio.h>
__global__ void shift_forward(int * value)
{
int index = threadIdx.x;
__shared__ int array[64];
array[index] = threadIdx.x;
__syncthreads(); // Garantir que todos os valores foram armazenados antes de começar o shift
if(index < 63)
{
int tmp = array[index + 1];
__syncthreads(); // Salvar cada valor antes que o mesmo seja trocado por outra thread
value[index] = tmp;
__syncthreads();
}
}
int main(int argc,char ** argv)
{
const int ARRAY_SIZE = 64;
const int SIZE = ARRAY_SIZE * sizeof(int);
int * d_out;
cudaMalloc((void **) &d_out,SIZE);
shift_forward<<<1,64>>>(d_out);
int h_out[ARRAY_SIZE];
cudaMemcpy(h_out,d_out,SIZE,cudaMemcpyDeviceToHost);
for(int i = 0 ; i < ARRAY_SIZE;i++)
{
printf("%d ",h_out[i]);
}
printf("\n");
return 0;
}