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
|
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <CL/cl.h>
#include "cl_simple.h"
#include "cl_util.h"
int main (int argc, char ** argv)
{
struct cl_simple_context context;
cl_mem src_buffer;
cl_int error = CL_SUCCESS;
unsigned num_items;
int * src_data;
int * out;
unsigned src_size;
unsigned i;
int copy_host_ptr;
cl_mem_flags mem_flags;
size_t global_work_size = 1;
if (argc != 3) {
fprintf(stderr, "Usage: copy-host-ptr num_items copy_host_ptr(T/F)\n");
return EXIT_FAILURE;
}
num_items = atoi(argv[1]);
copy_host_ptr = atoi(argv[2]);
src_size = num_items * sizeof(int);
src_data = malloc(src_size * sizeof(int));
out = malloc(src_size * sizeof(int));
for (i = 0; i < num_items; i++) {
src_data[i] = i;
}
if (!clSimpleSimpleInit(&context, "memcpy")) {
return EXIT_FAILURE;
}
if (!clSimpleSetOutputBuffer(&context, src_size)) {
return EXIT_FAILURE;
}
if (copy_host_ptr) {
src_buffer = clCreateBuffer(context.cl_ctx,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
src_size, src_data, &error);
if (error != CL_SUCCESS) {
fprintf(stderr, "clCreateBuffer() failed.\n");
return EXIT_FAILURE;
}
} else {
if (!clSimpleCreateBuffer(&src_buffer, context.cl_ctx,
CL_MEM_READ_ONLY, src_size)) {
return EXIT_FAILURE;
}
if (!clSimpleEnqueueWriteBuffer(context.command_queue, src_buffer,
src_size, src_data)) {
return EXIT_FAILURE;
}
}
if (!clSimpleKernelSetArg(context.kernel, 1, sizeof(cl_mem), &src_buffer)
|| !clSimpleKernelSetArg(context.kernel, 2, sizeof(int), &num_items)) {
return EXIT_FAILURE;
}
if (!clSimpleEnqueueNDRangeKernel(context.command_queue,
context.kernel, 1, &global_work_size,
&global_work_size)) {
return EXIT_FAILURE;
}
if (!clSimpleReadOutput(&context, out, src_size)) {
return EXIT_FAILURE;
}
for (i = 0; i < num_items; i++) {
fprintf(stderr, "%4d ", out[i]);
if (num_items % 10 == 9)
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
return EXIT_SUCCESS;
}
|