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
|
#include <CL/cl.h>
#include <CL/cl_platform.h>
#include "cl_simple.h"
#include "cl_util.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
cl_int error;
struct cl_simple_context context;
int * out;
int * new_out;
size_t global_work_size = 10;
unsigned out_size;
int iterations, i, j;
if (argc !=2 ) {
fprintf(stderr, "Usage: use-host-ptr iterations\n");
return EXIT_FAILURE;
}
iterations = atoi(argv[1]);
out_size = (global_work_size * iterations + 1) * sizeof(int);
out = malloc(out_size);
out[global_work_size * iterations] = 12345;
if (!clSimpleSimpleInit(&context, "loop_lt")) {
return EXIT_FAILURE;
}
context.out_buffer = clCreateBuffer(context.cl_ctx,
CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
out_size,
out,
&error);
if (error != CL_SUCCESS) {
fprintf(stderr, "clCreateBuffer() failed: %s\n",
clUtilErrorString(error));
return EXIT_FAILURE;
}
if ( !clSimpleKernelSetArg(context.kernel, 0, sizeof(cl_mem),
&context.out_buffer)
|| !clSimpleKernelSetArg(context.kernel, 1, sizeof(int),
&iterations)) {
return EXIT_FAILURE;
}
if (!clSimpleEnqueueNDRangeKernel(context.command_queue,
context.kernel,
1, &global_work_size, &global_work_size)) {
return EXIT_FAILURE;
}
new_out = clEnqueueMapBuffer(context.command_queue,
context.out_buffer,
CL_TRUE, /* Blocking */
CL_MAP_READ,
0, /* Offset */
out_size, /* cb */
0, NULL, NULL, &error);
if (error != CL_SUCCESS) {
fprintf(stderr, "clEnqueueMapBuffer() failed: %s\n",
clUtilErrorString(error));
return EXIT_FAILURE;
}
error = clEnqueueUnmapMemObject(context.command_queue, context.out_buffer,
new_out, 0, NULL, NULL);
if (error != CL_SUCCESS) {
fprintf(stderr, "clEnqueueUnmapMemObject() failed: %s\n",
clUtilErrorString(error));
}
for (i = 0; i < global_work_size; i++) {
for (j = 0; j < iterations; j++) {
unsigned id = (i * iterations) + j;
fprintf(stderr, "%2d ", new_out[id]);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "Prestore = %d\n", out[global_work_size * iterations]);
return EXIT_SUCCESS;
}
|