summaryrefslogtreecommitdiff
path: root/loop.c
blob: 151dfded6e3653120af170ee2b3c5e8ed9f0429d (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
#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)
{
   int i,j;

   struct cl_simple_context context;

   cl_int error;

   cl_mem out_buffer;
   int * out_data;
   size_t global_work_size = 5;
   int iterations;
   unsigned out_data_size;

   if (argc != 2) {
      fprintf(stderr, "Usage: loop iterations\n");
      return EXIT_FAILURE;
   }

   iterations = atoi(argv[1]);
   out_data_size = global_work_size * iterations * sizeof(int);
   out_data = malloc(out_data_size);

   if (!clSimpleSimpleInit(&context, "loop")) {
      return EXIT_FAILURE;
   }

   out_buffer = clCreateBuffer(context.cl_ctx,
                              CL_MEM_WRITE_ONLY,
                              out_data_size,
                              NULL, &error);

   assert(error == CL_SUCCESS);

   if (!clSimpleKernelSetArg(context.kernel, 0, sizeof(cl_mem), &out_buffer)
       || !clSimpleKernelSetArg(context.kernel, 1, sizeof(int), &iterations)) {
      return EXIT_FAILURE;
   }

   error = clEnqueueNDRangeKernel(context.command_queue,
                                  context.kernel,
                                  1,
                                  NULL,
                                  &global_work_size,
                                  &global_work_size,
                                  0, NULL, NULL);

   assert(error == CL_SUCCESS);

   error = clEnqueueReadBuffer(context.command_queue,
                               out_buffer,
                               CL_TRUE,
                               0,
                               out_data_size,
                               out_data,
                               0, NULL, NULL);

   assert(error == CL_SUCCESS);

   for (i = 0; i < global_work_size; i++) {
      for (j = 0; j < iterations; j++) {
         fprintf(stderr, "%2u ", out_data[(i * iterations) + j]);
      }
      fprintf(stderr, "\n");
   }
}