summaryrefslogtreecommitdiff
path: root/src/cl_api_program.c
blob: d68f29f359e69d525bd69e7be0ec708b4790086c (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include "cl_context.h"
#include "cl_program.h"
#include "cl_device_id.h"
#include <string.h>

cl_int
clGetProgramInfo(cl_program program,
                 cl_program_info param_name,
                 size_t param_value_size,
                 void *param_value,
                 size_t *param_value_size_ret)
{
  const void *src_ptr = NULL;
  size_t src_size = 0;
  const char *ret_str = "";
  cl_int ref;
  cl_uint num_dev, kernels_num;

  if (!CL_OBJECT_IS_PROGRAM(program)) {
    return CL_INVALID_PROGRAM;
  }

  if (param_name == CL_PROGRAM_REFERENCE_COUNT) {
    ref = CL_OBJECT_GET_REF(program);
    src_ptr = &ref;
    src_size = sizeof(cl_int);
  } else if (param_name == CL_PROGRAM_CONTEXT) {
    src_ptr = &program->ctx;
    src_size = sizeof(cl_context);
  } else if (param_name == CL_PROGRAM_NUM_DEVICES) {
    num_dev = program->ctx->device_num; // Just 1 dev now.
    src_ptr = &num_dev;
    src_size = sizeof(cl_uint);
  } else if (param_name == CL_PROGRAM_DEVICES) {
    src_ptr = program->ctx->devices;
    src_size = program->ctx->device_num * sizeof(cl_device_id);
  } else if (param_name == CL_PROGRAM_NUM_KERNELS) {
    kernels_num = program->ker_n;
    src_ptr = &kernels_num;
    src_size = sizeof(cl_uint);
  } else if (param_name == CL_PROGRAM_SOURCE) {
    if (!program->source) {
      src_ptr = ret_str;
      src_size = 1;
    } else {
      src_ptr = program->source;
      src_size = strlen(program->source) + 1;
    }
  } else if (param_name == CL_PROGRAM_KERNEL_NAMES) {
    // TODO: need to refine this.
    cl_program_get_kernel_names(program, param_value_size, (char *)param_value, param_value_size_ret);
    return CL_SUCCESS;
  } else if (param_name == CL_PROGRAM_BINARY_SIZES) {
    if (program->binary == NULL) {
      if (program->binary_type == CL_PROGRAM_BINARY_TYPE_EXECUTABLE) {
        program->binary_sz = compiler_program_serialize_to_binary(program->opaque, &program->binary, 0);
      } else if (program->binary_type == CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT) {
        program->binary_sz = compiler_program_serialize_to_binary(program->opaque, &program->binary, 1);
      } else if (program->binary_type == CL_PROGRAM_BINARY_TYPE_LIBRARY) {
        program->binary_sz = compiler_program_serialize_to_binary(program->opaque, &program->binary, 2);
      } else {
        return CL_INVALID_BINARY;
      }
    }

    if (program->binary == NULL || program->binary_sz == 0) {
      return CL_OUT_OF_RESOURCES;
    }
    src_ptr = &program->binary_sz;
    src_size = sizeof(size_t);
  } else if (param_name == CL_PROGRAM_BINARIES) {
    if (param_value_size_ret)
      *param_value_size_ret = sizeof(void *);
    if (!param_value)
      return CL_SUCCESS;

    /* param_value points to an array of n
       pointers allocated by the caller */
    if (program->binary == NULL) {
      if (program->binary_type == CL_PROGRAM_BINARY_TYPE_EXECUTABLE) {
        program->binary_sz = compiler_program_serialize_to_binary(program->opaque, &program->binary, 0);
      } else if (program->binary_type == CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT) {
        program->binary_sz = compiler_program_serialize_to_binary(program->opaque, &program->binary, 1);
      } else if (program->binary_type == CL_PROGRAM_BINARY_TYPE_LIBRARY) {
        program->binary_sz = compiler_program_serialize_to_binary(program->opaque, &program->binary, 2);
      } else {
        return CL_INVALID_BINARY;
      }
    }

    if (program->binary == NULL || program->binary_sz == 0) {
      return CL_OUT_OF_RESOURCES;
    }

    memcpy(*((void **)param_value), program->binary, program->binary_sz);
    return CL_SUCCESS;
  } else {
    return CL_INVALID_VALUE;
  }

  return cl_get_info_helper(src_ptr, src_size,
                            param_value, param_value_size, param_value_size_ret);
}

cl_int
clGetProgramBuildInfo(cl_program program,
                      cl_device_id device,
                      cl_program_build_info param_name,
                      size_t param_value_size,
                      void *param_value,
                      size_t *param_value_size_ret)
{
  const void *src_ptr = NULL;
  size_t src_size = 0;
  const char *ret_str = "";
  size_t global_size;

  if (!CL_OBJECT_IS_PROGRAM(program)) {
    return CL_INVALID_PROGRAM;
  }

  cl_int err = cl_devices_list_include_check(program->ctx->device_num,
                                             program->ctx->devices, 1, &device);
  if (err != CL_SUCCESS)
    return err;

  if (param_name == CL_PROGRAM_BUILD_STATUS) {
    src_ptr = &program->build_status;
    src_size = sizeof(cl_build_status);
  } else if (param_name == CL_PROGRAM_BUILD_OPTIONS) {
    if (program->is_built && program->build_opts) {
      ret_str = program->build_opts;
    }
    src_ptr = ret_str;
    src_size = strlen(ret_str) + 1;
  } else if (param_name == CL_PROGRAM_BUILD_LOG) {
    src_ptr = program->build_log;
    src_size = program->build_log_sz + 1;
  } else if (param_name == CL_PROGRAM_BINARY_TYPE) {
    src_ptr = &program->binary_type;
    src_size = sizeof(cl_uint);
  } else if (param_name == CL_PROGRAM_BUILD_GLOBAL_VARIABLE_TOTAL_SIZE) {
    global_size = 0;
    if (program->is_built)
      global_size = cl_program_get_global_variable_size(program);
    src_ptr = &global_size;
    src_size = sizeof(global_size);
  } else {
    return CL_INVALID_VALUE;
  }

  return cl_get_info_helper(src_ptr, src_size,
                            param_value, param_value_size, param_value_size_ret);
}