-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
139 lines (130 loc) · 3.05 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "core.cu"
// CUDA通用函数声明
void (*func)(int, int, int, float *, float *, int **);
// 生成随机数
float getRand()
{
return rand() / double(RAND_MAX);
}
/**
* @brief Get the Sample object
*
* @param k int 样例维度
* @param m int 测试点数量
* @param n int 参考点数量
* @param s_points float** 测试点数据指针
* @param r_points float** 参考点数据指针
*/
void getSample(int k, int m, int n, float **s_points, float **r_points)
{
float *tmp;
tmp = (float *)malloc(sizeof(float) * k * m);
for (int i = 0; i < k * m; i++)
tmp[i] = getRand();
*s_points = tmp;
tmp = (float *)malloc(sizeof(float) * k * n);
for (int i = 0; i < k * n; i++)
tmp[i] = getRand();
*r_points = tmp;
}
// 样例设置
int samples[] = {
3, 1, 1024,
16, 1, 1024,
3, 1, 65536,
16, 1, 65536,
3, 1024, 1024,
16, 1024, 1024,
3, 1024, 65536,
16, 1024, 65536,
3, 1024, 1048576,
16, 1024, 1048576
};
int total = 0; // 样例个数
int seed = 1000; // 随机种子
long st, et; // 开始和结束的时间
/**
* @brief 调用对应的核函数
*
* @param v 指定 version 版本
*/
void test(int v)
{
srand(seed);
for (int i = 0; i < total; ++i)
{
int k = samples[3 * i];
int m = samples[3 * i + 1];
int n = samples[3 * i + 2];
float *s_points, *r_points;
getSample(k, m, n, &s_points, &r_points);
int *results;
st = getTime();
(*func)(k, m, n, s_points, r_points, &results);
et = getTime();
printf("CudaCall %d, %2d, %4d, %10d, %10.3fms\n", v, k, m, n, (et - st) / 1e6);
free(s_points);
free(r_points);
}
}
// 主函数
int main()
{
total = sizeof(samples) / (3 * sizeof(*samples)); // 样例个数
// 运行全部的优化版本
for (int v = 0; v < 14; ++v)
{
switch (v)
{
case 0:
func = &v0::cudaCall;
break;
case 1:
func = &v1::cudaCall;
break;
case 2:
func = &v2::cudaCall;
break;
case 3:
func = &v3::cudaCall;
break;
case 4:
func = &v4::cudaCall;
break;
case 5:
func = &v5::cudaCall;
break;
case 6:
func = &v6::cudaCall;
break;
case 7:
func = &v7::cudaCall;
break;
case 8:
func = &v8::cudaCall;
break;
case 9:
func = &v9::cudaCall;
break;
case 10:
func = &v10::cudaCall;
break;
case 11:
func = &v11::cudaCall;
break;
case 12:
func = &v12::cudaCall;
break;
case 13:
func = &v13::cudaCall;
break;
default:
break;
}
printf("\nRunning CUDACALL %d...\n", v);
test(v);
}
}