-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewbit_acc.c
139 lines (117 loc) · 2.9 KB
/
newbit_acc.c
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ASCENDING 1
#define DESCENDING 0
struct timespec startwtime, endwtime;
double seq_time;
int *genArray;
int arraySize;
int power = 0;
void generator(int size);
void printArray(int size);
char verify(int size);
void getPower(int size);
int main(int argc, char *argv[]){
clock_gettime(CLOCK_REALTIME,&startwtime);
srand((unsigned int)startwtime.tv_nsec);
printf("The array size you want (integer): \n");
scanf("%d", &arraySize);
if(arraySize == 0){
printf("Really, type in an integer larger 0...\n");
scanf("%d", &arraySize);
}
genArray = malloc(arraySize*sizeof(int));
generator(arraySize);
//printArray(arraySize);
clock_gettime(CLOCK_REALTIME,&startwtime);
getPower(arraySize);
if(power != 0){
#pragma acc data copy(genArray[0:arraySize])
for (int k = 2; k <= arraySize; k *= 2) //Parallel bitonic sort
{
for (int j = k / 2; j>0; j /= 2) //Bitonic merge
{
int cnt = arraySize;
// because http://techenablement.com/pragma-puzzler-ambiguous-loop-trip-count-in-openmp-and-openacc/
#pragma omp parallel for
#pragma acc kernels loop independent present(genArray[0:arraySize])
for (int threadid = 0; threadid < cnt; ++threadid)
{
//#pragma acc atomic capture
{
int index = threadid ^ j; //XOR
if (index > threadid) // segment of the array; is threadid < j?
{
if ((threadid & k) == 0) // ascending – descending
{
//#pragma acc atomic update
if (genArray[threadid] > genArray[index])
{
int t;
t = genArray[threadid];
genArray[threadid] = genArray[index];
genArray[index] = t;
}
}
else
{
//#pragma acc atomic update
if (genArray[threadid] < genArray[index])
{
int t;
t = genArray[threadid];
genArray[threadid] = genArray[index];
genArray[index] = t;
}
}
}
}
}
}
}
}
clock_gettime(CLOCK_REALTIME,&endwtime);
seq_time = (double)((endwtime.tv_nsec - startwtime.tv_nsec)/1.0e9
+ endwtime.tv_sec - startwtime.tv_sec);
//printArray(arraySize);
if(verify(arraySize) == 1){
printf("The array is verified to be sorted!\n"
"It sorted in %f s\n", seq_time);
} else{
printf("Something went wrong, because the array isn't sorted!\n"
"It still took %f s\n to complete", seq_time);
}
free(genArray);
return 0;
}
// HELPER FUNCTIONS
void getPower(int size){
int i;
for(i=1; i<arraySize; i*=2){
power += 1;
}
}
void generator(int size){
int i;
for(i = 0; i < size; i++){
genArray[i] = (int) rand() % 3571;
}
}
/* Function to print an array */
void printArray(int size){
int i;
for (i=0; i < size; i++){
if(i % 10 == 0)
printf("\n");
printf("%d\t", genArray[i]);
}
}
char verify(int size){
int i;
for(i = 1; i < size; i++){
if(genArray[i - 1] <= genArray[i]){
} else {return 0;}
}
return 1;
}