summaryrefslogtreecommitdiff
path: root/loop.c
blob: b3d960c9a813a9aca840ef0fc94cbef68e0df828 (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;

   unsigned pass = 1;
   int * out;
   char * kernel_name;
   size_t global_work_size = 10;
   int iterations;
   unsigned out_size;

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

   kernel_name = argv[1];
   iterations = atoi(argv[2]);
   out_size = global_work_size * iterations * sizeof(int);
   out = malloc(out_size);

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

   if (!clSimpleSetOutputBuffer(&context, out_size)) {
      return EXIT_FAILURE;
   }

   if (!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;
   }

   if (!clSimpleReadOutput(&context, out, out_size)) {
      return EXIT_FAILURE;
   }

   for (i = 0; i < global_work_size; i++) {
      for (j = 0; j < iterations; j++) {
            unsigned id = (i * iterations) + j;
            if (out[id] != id) {
               fprintf(stderr, "\n\nExpected %d, but got %d\n\n", id, out[id]);
               pass = 0;
            }
         fprintf(stderr, "%2u ", out[id]);
      }
      fprintf(stderr, "\n");
   }
   if (pass) {
      fprintf(stderr, "Pass\n");
      return EXIT_SUCCESS;
   } else {
      fprintf(stderr, "Fail\n");
      return EXIT_FAILURE;
   }
}