summaryrefslogtreecommitdiff
path: root/use_host_ptr.c
blob: 1a24e35dcb6ca4829b1a53a7ec25c54bf9977255 (plain)
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
#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 prestore = 12345;
   unsigned out_size;
   int iterations, i, j;
   unsigned pass = 1;

   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] = prestore;

   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;
         if (new_out[id] != id) {
            fprintf(stderr, "Expected %u, but got %u\n", id, new_out[id]);
            pass = 0;
         }
         fprintf(stderr, "%2d ", new_out[id]);
      }
      fprintf(stderr, "\n");
   }
   if (out[global_work_size * iterations] != prestore) {
      fprintf(stderr, "Prestore expected %d, got %d\n", prestore,
              out[global_work_size * iterations]);
      pass = 0;
   }

   if (pass) {
      fprintf(stderr, "Pass\n");
      return EXIT_SUCCESS;
   } else {
      fprintf(stderr, "Fail\n");
      return EXIT_FAILURE;
   }
}