-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
430 lines (392 loc) · 9.42 KB
/
Copy pathtest.cpp
File metadata and controls
430 lines (392 loc) · 9.42 KB
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
#include <CL/cl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include "gclFile.h"
#include "gclTimer.h"
using namespace std;
#define BUFSIZE 262144
#define NUMERSIZE 9000000
#define NUMERSIZE1 256
#pragma comment (lib,"OpenCL.lib")
//定义结构体
typedef struct coordinationNode
{
float X;
float Y;
float Z;
} coordination;
cl_program CreateProgram(cl_context context, cl_device_id device, const char* fileName)
{
cl_int errNum;
cl_program program;
std::ifstream kernelFile(fileName, std::ios::in);
if (!kernelFile.is_open())
{
std::cerr << "Failed to open file for reading: " << fileName << std::endl;
return NULL;
}
std::ostringstream oss;
oss << kernelFile.rdbuf();
std::string srcStdStr = oss.str();
const char *srcStr = srcStdStr.c_str();
program = clCreateProgramWithSource(context, 1,
(const char**)&srcStr,
NULL, NULL);
if (program == NULL)
{
std::cerr << "Failed to create CL program from source." << std::endl;
return NULL;
}
errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (errNum != CL_SUCCESS)
{
// Determine the reason for the error
char buildLog[16384];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
sizeof(buildLog), buildLog, NULL);
std::cerr << "Error in kernel: " << std::endl;
std::cerr << buildLog;
clReleaseProgram(program);
return NULL;
}
return program;
}
void Cleanup(cl_context context, cl_command_queue commandQueue,
cl_program program, cl_kernel kernel)
{
if (commandQueue != 0)
clReleaseCommandQueue(commandQueue);
if (kernel != 0)
clReleaseKernel(kernel);
if (program != 0)
clReleaseProgram(program);
if (context != 0)
clReleaseContext(context);
}
//等待事件完成
int waitForEventAndRelease(cl_event *event)
{
cl_int status = CL_SUCCESS;
cl_int eventStatus = CL_QUEUED;
while (eventStatus != CL_COMPLETE)
{
status = clGetEventInfo(
*event,
CL_EVENT_COMMAND_EXECUTION_STATUS,
sizeof(cl_int),
&eventStatus,
NULL);
}
status = clReleaseEvent(*event);
return 0;
}
int main(int argc, char* argv[])
//int main(void)
{
//在host内存中创建三个缓冲区
ifstream myfile("test.txt");
ifstream myfile1("test.txt");
ofstream gridout("gridout.txt");
cl_kernel kernel = 0;
cl_program program = 0;
if (!myfile){
cout << "Unable to open myfile";
exit(1); // terminate with error
}
if (!gridout){
cout << "Unable to open gridout";
exit(1); // terminate with error
}
int pointNum = 1;
int gridNum = 1;
float gridx;
float gridy;
char c;
while (myfile1.get(c))
{
if (c == '\n')
{
pointNum++;
}
}
myfile1.close();
//coordination dic;
coordination *dic = new coordination[NUMERSIZE1 * NUMERSIZE1];
for (int i = 0; i < NUMERSIZE1; i++){
dic[i].X = 0;
dic[i].Y = 0;
dic[i].Z = 0;
}
for (int i = 0; i < pointNum; i++){
for (int j = 0; j < 3; j++)
{
if (j == 0)
{
myfile >> dic[i].X;
}
if (j == 1)
{
myfile >> dic[i].Y;
}
if (j == 2)
{
myfile >> dic[i].Z;
}
}
}
myfile.close();
//依据读入的离散点坐标获取插值范围(X及Y的最大值和最小值)
float xmin, xmax, ymin, ymax;
xmin = dic[0].X;
xmax = dic[0].X;
ymin = dic[0].Y;
ymax = dic[0].Y;
for (int i = 1; i < pointNum; i++){
if (xmin > dic[i].X)
{
xmin = dic[i].X;
}
if (xmax < dic[i].X)
{
xmax = dic[i].X;
}
if (ymin > dic[i].Y)
{
ymin = dic[i].Y;
}
if (ymax < dic[i].Y)
{
ymax = dic[i].Y;
}
}
xmin--;
xmax++;
ymin--;
ymax++;
float stepx;
float stepy;
stepx = (xmax - xmin) / NUMERSIZE1;
stepy = (ymax - ymin) / NUMERSIZE1;
coordination **grid1 = new coordination*[NUMERSIZE1];//动态分配数组空间
for (int i = 0; i < NUMERSIZE1; i++)
{
grid1[i] = new coordination[NUMERSIZE1];
}
coordination *grid = new coordination[NUMERSIZE1 * NUMERSIZE1];//动态分配数组空间
coordination **martix = new coordination*[NUMERSIZE1];//动态分配数组空间
for (int i = 0; i < NUMERSIZE1; i++)
{
martix[i] = new coordination[NUMERSIZE1];
}
//coordination martix[NUMERSIZE1][NUMERSIZE1];
coordination *martix1 = new coordination[NUMERSIZE1 * NUMERSIZE1];//动态分配数组空间
for (int i = 0; i < NUMERSIZE1; i++)
{
for (int j = 0; j < NUMERSIZE1; j++)
{
grid1[i][j].X = xmin + stepx * i;
grid1[i][j].Y = ymin + stepy * j;
grid1[i][j].Z = 0;
grid[i * NUMERSIZE1 + j].X = xmin + stepx * i;
grid[i * NUMERSIZE1 + j].Y = ymin + stepy * j;
grid[i * NUMERSIZE1 + j].Z = 0;
}
}
for (int i = 0; i < NUMERSIZE1; i++)
{
for (int j = 0; j < NUMERSIZE1; j++)
{
martix[i][j].X = 0;
martix[i][j].Y = 0;
martix[i][j].Z = 0;
martix1[i * NUMERSIZE1 + j].X = 0;
martix1[i * NUMERSIZE1 + j].Y = 0;
martix1[i * NUMERSIZE1 + j].Z = 0;
}
}
int dicx, dicy;
for (int i = 0; i < pointNum; ++i)
{
dicx = (int)((dic[i].X - xmin) / stepx);
dicy = (int)((dic[i].Y - ymin) / stepy);
martix[dicx][dicy].X = dic[i].X;
martix[dicx][dicy].Y = dic[i].Y;
martix[dicx][dicy].Z = dic[i].Z;
martix1[dicx * NUMERSIZE1 + dicy].X = dic[i].X;
martix1[dicx * NUMERSIZE1 + dicy].Y = dic[i].Y;
martix1[dicx * NUMERSIZE1 + dicy].Z = dic[i].Z;
}
int msn = 5;//最大影响点数
cl_uint status;
cl_platform_id platform;
//创建平台对象
status = clGetPlatformIDs(1, &platform, NULL);
cl_uint numPlatforms;
std::string platformVendor;
status = clGetPlatformIDs(0, NULL, &numPlatforms);
if (status != CL_SUCCESS)
{
return 0;
}
if (0 < numPlatforms)
{
cl_platform_id* platforms = new cl_platform_id[numPlatforms];
status = clGetPlatformIDs(numPlatforms, platforms, NULL);
char platformName[100];
for (unsigned i = 0; i < numPlatforms; ++i)
{
status = clGetPlatformInfo(platforms[i],
CL_PLATFORM_VENDOR,
sizeof(platformName),
platformName,
NULL);
platform = platforms[i];
platformVendor.assign(platformName);
if (!strcmp(platformName, "Advanced Micro Devices, Inc."))
{
break;
}
}
std::cout << "Platform found : " << platformName << "\n";
delete[] platforms;
}
cl_device_id device;
//创建GPU设备
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU,
1,
&device,
NULL);
//创建context
cl_context context = clCreateContext(NULL,
1,
&device,
NULL, NULL, NULL);
//创建命令队列
cl_command_queue queue = clCreateCommandQueue(context,
device,
CL_QUEUE_PROFILING_ENABLE, NULL);
//创建三个OpenCL内存对象,并把buf1的内容通过隐式拷贝的方式
//拷贝到clbuf1,buf2的内容通过显示拷贝的方式拷贝到clbuf2
cl_mem clbuf1 = clCreateBuffer(context,
CL_MEM_COPY_HOST_PTR,
NUMERSIZE1 * NUMERSIZE1 * sizeof(coordination), grid,
NULL);
cl_mem clbuf2 = clCreateBuffer(context,
CL_MEM_COPY_HOST_PTR,
NUMERSIZE1 * NUMERSIZE1 * sizeof(coordination), martix1,
NULL);
unsigned int *outbuffer = new unsigned int[300000];
memset(outbuffer, 0, 300000);
cl_mem buffer = clCreateBuffer(context,
CL_MEM_ALLOC_HOST_PTR,
BUFSIZE * sizeof(cl_float),
NULL, NULL);
// Create OpenCL program from HelloWorld.cl kernel source
program = CreateProgram(context, device, "tt.cl");
if (program == NULL)
{
Cleanup(context, queue, program, kernel);
return 1;
}
//创建Kernel对象
kernel = clCreateKernel(program, "tt", NULL);
//if (kernel == NULL)
//{
// std::cerr << "Failed to create kernel" << std::endl;
// Cleanup(context, queue, program, kernel);
// return 1;
//}
//设置Kernel参数
cl_int clnum = 3000;
clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&clbuf1);
clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&clbuf2);
clSetKernelArg(kernel, 2, sizeof(int), (void*)&msn);
clSetKernelArg(kernel, 3, sizeof(cl_mem), (void*)&buffer);
//执行kernel,Range用1维,work itmes size为BUFSIZE,
cl_event ev;
size_t globalThreads[] = { NUMERSIZE1, NUMERSIZE1 };
size_t localx, localy;
if (NUMERSIZE1 / 8 > 4)
localx = 16;
else if (NUMERSIZE1 < 8)
localx = NUMERSIZE1;
else localx = 8;
if (NUMERSIZE1 / 8 > 4)
localy = 16;
else if (NUMERSIZE1 < 8)
localy = NUMERSIZE1;
else localy = 8;
size_t localThreads[] = { localx, localy }; // localx*localy应该是64的倍数
gclTimer clTimer;
clTimer.Reset();
clTimer.Start();
clEnqueueNDRangeKernel(queue,
kernel,
2,
NULL,
globalThreads,
localThreads, 0, NULL, &ev);
status = clFlush(queue);
waitForEventAndRelease(&ev);
//clWaitForEvents(1, &ev);
clTimer.Stop();
printf("GPU内核总计时间:%.6f ms \n ", clTimer.GetElapsedTime() * 1000);
//数据拷回host内存
cl_float *ptr;
clTimer.Reset();
clTimer.Start();
cl_event mapevt;
//cl_int sta;
//sta = clEnqueueReadBuffer(queue,
// buffer, CL_TRUE, 0,
// 0, outbuffer, 0, NULL, NULL);
ptr = (cl_float *)clEnqueueMapBuffer(queue,
buffer,
CL_TRUE,
CL_MAP_READ,
0,
BUFSIZE * sizeof(cl_float),
0, NULL, &mapevt, NULL);
status = clFlush(queue);
waitForEventAndRelease(&mapevt);
//clWaitForEvents(1, &mapevt);
clTimer.Stop();
printf("从设备到主机拷贝:%.6f ms \n ", clTimer.GetElapsedTime() * 1000);
////输出结果到TXT文件
for (int i = 0; i < NUMERSIZE1 * NUMERSIZE1; i++)
{
gridout << i + 1;
gridout << "值:";
gridout << ptr[i];
gridout << "\n";
// std::cout << "result:" << "第" << i + 1 << "个"<< ptr[i] << "\n";
}
//删除OpenCL资源对象
clReleaseMemObject(clbuf1);
clReleaseMemObject(clbuf2);
clReleaseMemObject(buffer);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(queue);
clReleaseContext(context);
gridout.close();
delete dic;
delete outbuffer;
for (int i = 0; i != NUMERSIZE1; i++)
{
delete[] martix[i];
}
delete[] martix;
delete[] martix1;
delete[] grid;
for (int i = 0; i != NUMERSIZE1; i++)
{
delete[] grid1[i];
}
delete[] grid1;
return 0;
}