program.cpp 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
// Copyright 2012 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17
18
19
20
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
21
22
23
24
//

#include "api/util.hpp"
#include "core/program.hpp"
25
#include "util/u_debug.h"
26

Serge Martin's avatar
Serge Martin committed
27
28
#include <sstream>

29
30
using namespace clover;

31
namespace {
32
33
34
35
36
37
   void
   validate_build_common(const program &prog, cl_uint num_devs,
                         const cl_device_id *d_devs,
                         void (*pfn_notify)(cl_program, void *),
                         void *user_data) {
      if (!pfn_notify && user_data)
38
39
40
41
42
43
         throw error(CL_INVALID_VALUE);

      if (prog.kernel_ref_count())
         throw error(CL_INVALID_OPERATION);

      if (any_of([&](const device &dev) {
44
               return !count(dev, prog.devices());
45
            }, objs<allow_empty_tag>(d_devs, num_devs)))
46
47
48
49
         throw error(CL_INVALID_DEVICE);
   }
}

50
CLOVER_API cl_program
51
clCreateProgramWithSource(cl_context d_ctx, cl_uint count,
52
                          const char **strings, const size_t *lengths,
53
                          cl_int *r_errcode) try {
54
   auto &ctx = obj(d_ctx);
55
56
57
   std::string source;

   if (!count || !strings ||
58
       any_of(is_zero(), range(strings, count)))
59
60
61
62
63
64
65
66
67
      throw error(CL_INVALID_VALUE);

   // Concatenate all the provided fragments together
   for (unsigned i = 0; i < count; ++i)
         source += (lengths && lengths[i] ?
                    std::string(strings[i], strings[i] + lengths[i]) :
                    std::string(strings[i]));

   // ...and create a program object for them.
68
   ret_error(r_errcode, CL_SUCCESS);
69
   return new program(ctx, source);
70
71

} catch (error &e) {
72
   ret_error(r_errcode, e);
73
74
75
   return NULL;
}

76
CLOVER_API cl_program
77
clCreateProgramWithBinary(cl_context d_ctx, cl_uint n,
78
79
80
81
                          const cl_device_id *d_devs,
                          const size_t *lengths,
                          const unsigned char **binaries,
                          cl_int *r_status, cl_int *r_errcode) try {
82
   auto &ctx = obj(d_ctx);
83
84
85
   auto devs = objs(d_devs, n);

   if (!lengths || !binaries)
86
87
      throw error(CL_INVALID_VALUE);

88
   if (any_of([&](const device &dev) {
89
            return !count(dev, ctx.devices());
90
         }, devs))
91
92
93
      throw error(CL_INVALID_DEVICE);

   // Deserialize the provided binaries,
94
   std::vector<std::pair<cl_int, module>> result = map(
95
96
97
98
99
      [](const unsigned char *p, size_t l) -> std::pair<cl_int, module> {
         if (!p || !l)
            return { CL_INVALID_VALUE, {} };

         try {
Serge Martin's avatar
Serge Martin committed
100
101
            std::stringbuf bin( { (char*)p, l } );
            std::istream s(&bin);
102
103
104

            return { CL_SUCCESS, module::deserialize(s) };

Serge Martin's avatar
Serge Martin committed
105
         } catch (std::istream::failure &e) {
106
107
108
            return { CL_INVALID_BINARY, {} };
         }
      },
109
110
      range(binaries, n),
      range(lengths, n));
111
112

   // update the status array,
113
114
   if (r_status)
      copy(map(keys(), result), r_status);
115

116
   if (any_of(key_equals(CL_INVALID_VALUE), result))
117
118
      throw error(CL_INVALID_VALUE);

119
   if (any_of(key_equals(CL_INVALID_BINARY), result))
120
121
122
      throw error(CL_INVALID_BINARY);

   // initialize a program object with them.
123
124
   ret_error(r_errcode, CL_SUCCESS);
   return new program(ctx, devs, map(values(), result));
125
126

} catch (error &e) {
127
   ret_error(r_errcode, e);
128
129
130
   return NULL;
}

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
CLOVER_API cl_program
clCreateProgramWithBuiltInKernels(cl_context d_ctx, cl_uint n,
                                  const cl_device_id *d_devs,
                                  const char *kernel_names,
                                  cl_int *r_errcode) try {
   auto &ctx = obj(d_ctx);
   auto devs = objs(d_devs, n);

   if (any_of([&](const device &dev) {
            return !count(dev, ctx.devices());
         }, devs))
      throw error(CL_INVALID_DEVICE);

   // No currently supported built-in kernels.
   throw error(CL_INVALID_VALUE);

} catch (error &e) {
   ret_error(r_errcode, e);
   return NULL;
}


153
CLOVER_API cl_int
154
155
clRetainProgram(cl_program d_prog) try {
   obj(d_prog).retain();
156
   return CL_SUCCESS;
157
158
159

} catch (error &e) {
   return e.get();
160
161
}

162
CLOVER_API cl_int
163
164
165
clReleaseProgram(cl_program d_prog) try {
   if (obj(d_prog).release())
      delete pobj(d_prog);
166
167

   return CL_SUCCESS;
168
169
170

} catch (error &e) {
   return e.get();
171
172
}

173
CLOVER_API cl_int
174
175
176
clBuildProgram(cl_program d_prog, cl_uint num_devs,
               const cl_device_id *d_devs, const char *p_opts,
               void (*pfn_notify)(cl_program, void *),
177
178
               void *user_data) try {
   auto &prog = obj(d_prog);
179
180
   auto devs =
      (d_devs ? objs(d_devs, num_devs) : ref_vector<device>(prog.devices()));
181
182
   const auto opts = std::string(p_opts ? p_opts : "") + " " +
                     debug_get_option("CLOVER_EXTRA_BUILD_OPTIONS", "");
183

184
   validate_build_common(prog, num_devs, d_devs, pfn_notify, user_data);
185

186
187
188
   if (prog.has_source) {
      prog.compile(devs, opts);
      prog.link(devs, opts, { prog });
189
190
191
192
193
194
195
   } else if (any_of([&](const device &dev){
         return prog.build(dev).binary_type() != CL_PROGRAM_BINARY_TYPE_EXECUTABLE;
         }, devs)) {
      // According to the OpenCL 1.2 specification, “if program is created
      // with clCreateProgramWithBinary, then the program binary must be an
      // executable binary (not a compiled binary or library).”
      throw error(CL_INVALID_BINARY);
196
197
   }

198
   return CL_SUCCESS;
199

200
201
} catch (error &e) {
   return e.get();
202
203
204
205
206
207
208
209
210
}

CLOVER_API cl_int
clCompileProgram(cl_program d_prog, cl_uint num_devs,
                 const cl_device_id *d_devs, const char *p_opts,
                 cl_uint num_headers, const cl_program *d_header_progs,
                 const char **header_names,
                 void (*pfn_notify)(cl_program, void *),
                 void *user_data) try {
211
   auto &prog = obj(d_prog);
212
213
   auto devs =
       (d_devs ? objs(d_devs, num_devs) : ref_vector<device>(prog.devices()));
214
215
   const auto opts = std::string(p_opts ? p_opts : "") + " " +
                     debug_get_option("CLOVER_EXTRA_COMPILE_OPTIONS", "");
216
   header_map headers;
217

218
   validate_build_common(prog, num_devs, d_devs, pfn_notify, user_data);
219

220
221
   if (bool(num_headers) != bool(header_names))
      throw error(CL_INVALID_VALUE);
222

223
   if (!prog.has_source)
224
225
      throw error(CL_INVALID_OPERATION);

226
227
228
229
230
   for_each([&](const char *name, const program &header) {
         if (!header.has_source)
            throw error(CL_INVALID_OPERATION);

         if (!any_of(key_equals(name), headers))
Serge Martin's avatar
Serge Martin committed
231
            headers.push_back(std::pair<std::string, std::string>(
232
233
234
                                 name, header.source()));
      },
      range(header_names, num_headers),
235
      objs<allow_empty_tag>(d_header_progs, num_headers));
236

237
   prog.compile(devs, opts, headers);
238
239
   return CL_SUCCESS;

240
241
242
} catch (invalid_build_options_error &e) {
   return CL_INVALID_COMPILER_OPTIONS;

243
244
245
} catch (build_error &e) {
   return CL_COMPILE_PROGRAM_FAILURE;

246
247
248
249
} catch (error &e) {
   return e.get();
}

250
251
252
namespace {
   ref_vector<device>
   validate_link_devices(const ref_vector<program> &progs,
253
254
                         const ref_vector<device> &all_devs,
                         const std::string &opts) {
255
      std::vector<device *> devs;
256
257
      const bool create_library =
         opts.find("-create-library") != std::string::npos;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
      const bool enable_link_options =
         opts.find("-enable-link-options") != std::string::npos;
      const bool has_link_options =
         opts.find("-cl-denorms-are-zero") != std::string::npos ||
         opts.find("-cl-no-signed-zeroes") != std::string::npos ||
         opts.find("-cl-unsafe-math-optimizations") != std::string::npos ||
         opts.find("-cl-finite-math-only") != std::string::npos ||
         opts.find("-cl-fast-relaxed-math") != std::string::npos ||
         opts.find("-cl-no-subgroup-ifp") != std::string::npos;

      // According to the OpenCL 1.2 specification, "[the
      // -enable-link-options] option must be specified with the
      // create-library option".
      if (enable_link_options && !create_library)
         throw error(CL_INVALID_LINKER_OPTIONS);

      // According to the OpenCL 1.2 specification, "the
      // [program linking options] can be specified when linking a program
      // executable".
      if (has_link_options && create_library)
         throw error(CL_INVALID_LINKER_OPTIONS);
279
280
281

      for (auto &dev : all_devs) {
         const auto has_binary = [&](const program &prog) {
282
283
284
            const auto t = prog.build(dev).binary_type();
            return t == CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT ||
                   t == CL_PROGRAM_BINARY_TYPE_LIBRARY;
285
286
         };

287
288
289
290
291
292
293
294
295
296
297
298
         // According to the OpenCL 1.2 specification, a library is made of
         // “compiled binaries specified in input_programs argument to
         // clLinkProgram“; compiled binaries does not refer to libraries:
         // “input_programs is an array of program objects that are compiled
         // binaries or libraries that are to be linked to create the program
         // executable”.
         if (create_library && any_of([&](const program &prog) {
                  const auto t = prog.build(dev).binary_type();
                  return t != CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT;
               }, progs))
            throw error(CL_INVALID_OPERATION);

299
300
301
         // According to the CL 1.2 spec, when "all programs specified [..]
         // contain a compiled binary or library for the device [..] a link is
         // performed",
302
         else if (all_of(has_binary, progs))
303
304
305
306
307
308
309
            devs.push_back(&dev);

         // otherwise if "none of the programs contain a compiled binary or
         // library for that device [..] no link is performed.  All other
         // cases will return a CL_INVALID_OPERATION error."
         else if (any_of(has_binary, progs))
            throw error(CL_INVALID_OPERATION);
310
311
312
313
314
315
316
317
318
319
320
321
322
323

         // According to the OpenCL 1.2 specification, "[t]he linker may apply
         // [program linking options] to all compiled program objects
         // specified to clLinkProgram. The linker may apply these options
         // only to libraries which were created with the
         // -enable-link-option."
         else if (has_link_options && any_of([&](const program &prog) {
                  const auto t = prog.build(dev).binary_type();
                  return !(t == CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT ||
                          (t == CL_PROGRAM_BINARY_TYPE_LIBRARY &&
                           prog.build(dev).opts.find("-enable-link-options") !=
                              std::string::npos));
               }, progs))
            throw error(CL_INVALID_LINKER_OPTIONS);
324
325
326
327
328
329
      }

      return map(derefs(), devs);
   }
}

330
331
332
CLOVER_API cl_program
clLinkProgram(cl_context d_ctx, cl_uint num_devs, const cl_device_id *d_devs,
              const char *p_opts, cl_uint num_progs, const cl_program *d_progs,
333
334
335
              void (*pfn_notify) (cl_program, void *), void *user_data,
              cl_int *r_errcode) try {
   auto &ctx = obj(d_ctx);
336
337
   const auto opts = std::string(p_opts ? p_opts : "") + " " +
                     debug_get_option("CLOVER_EXTRA_LINK_OPTIONS", "");
338
   auto progs = objs(d_progs, num_progs);
339
340
341
   auto all_devs =
      (d_devs ? objs(d_devs, num_devs) : ref_vector<device>(ctx.devices()));
   auto prog = create<program>(ctx, all_devs);
342
   auto devs = validate_link_devices(progs, all_devs, opts);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

   validate_build_common(prog, num_devs, d_devs, pfn_notify, user_data);

   try {
      prog().link(devs, opts, progs);
      ret_error(r_errcode, CL_SUCCESS);

   } catch (build_error &e) {
      ret_error(r_errcode, CL_LINK_PROGRAM_FAILURE);
   }

   return ret_object(prog);

} catch (invalid_build_options_error &e) {
   ret_error(r_errcode, CL_INVALID_LINKER_OPTIONS);
   return NULL;

} catch (error &e) {
   ret_error(r_errcode, e);
362
363
364
   return NULL;
}

365
CLOVER_API cl_int
366
367
368
369
clUnloadCompiler() {
   return CL_SUCCESS;
}

370
371
372
373
374
CLOVER_API cl_int
clUnloadPlatformCompiler(cl_platform_id d_platform) {
   return CL_SUCCESS;
}

375
CLOVER_API cl_int
376
clGetProgramInfo(cl_program d_prog, cl_program_info param,
377
378
                 size_t size, void *r_buf, size_t *r_size) try {
   property_buffer buf { r_buf, size, r_size };
379
   auto &prog = obj(d_prog);
380
381
382

   switch (param) {
   case CL_PROGRAM_REFERENCE_COUNT:
383
      buf.as_scalar<cl_uint>() = prog.ref_count();
384
      break;
385
386

   case CL_PROGRAM_CONTEXT:
387
      buf.as_scalar<cl_context>() = desc(prog.context());
388
      break;
389
390

   case CL_PROGRAM_NUM_DEVICES:
391
392
      buf.as_scalar<cl_uint>() = (prog.devices().size() ?
                                  prog.devices().size() :
393
                                  prog.context().devices().size());
394
      break;
395
396

   case CL_PROGRAM_DEVICES:
397
398
      buf.as_vector<cl_device_id>() = (prog.devices().size() ?
                                       descs(prog.devices()) :
399
                                       descs(prog.context().devices()));
400
      break;
401
402

   case CL_PROGRAM_SOURCE:
403
      buf.as_string() = prog.source();
404
      break;
405
406

   case CL_PROGRAM_BINARY_SIZES:
407
      buf.as_vector<size_t>() = map([&](const device &dev) {
408
            return prog.build(dev).binary.size();
409
410
         },
         prog.devices());
411
      break;
412
413

   case CL_PROGRAM_BINARIES:
414
      buf.as_matrix<unsigned char>() = map([&](const device &dev) {
Serge Martin's avatar
Serge Martin committed
415
416
            std::stringbuf bin;
            std::ostream s(&bin);
417
            prog.build(dev).binary.serialize(s);
Serge Martin's avatar
Serge Martin committed
418
            return bin.str();
419
420
         },
         prog.devices());
421
      break;
422

423
424
425
426
427
428
   case CL_PROGRAM_NUM_KERNELS:
      buf.as_scalar<cl_uint>() = prog.symbols().size();
      break;

   case CL_PROGRAM_KERNEL_NAMES:
      buf.as_string() = fold([](const std::string &a, const module::symbol &s) {
429
            return ((a.empty() ? "" : a + ";") + s.name);
430
431
432
         }, std::string(), prog.symbols());
      break;

433
   default:
434
      throw error(CL_INVALID_VALUE);
435
   }
436
437
438
439
440

   return CL_SUCCESS;

} catch (error &e) {
   return e.get();
441
442
}

443
CLOVER_API cl_int
444
clGetProgramBuildInfo(cl_program d_prog, cl_device_id d_dev,
445
                      cl_program_build_info param,
446
447
                      size_t size, void *r_buf, size_t *r_size) try {
   property_buffer buf { r_buf, size, r_size };
448
449
   auto &prog = obj(d_prog);
   auto &dev = obj(d_dev);
450

451
   if (!count(dev, prog.context().devices()))
452
453
454
455
      return CL_INVALID_DEVICE;

   switch (param) {
   case CL_PROGRAM_BUILD_STATUS:
456
      buf.as_scalar<cl_build_status>() = prog.build(dev).status();
457
      break;
458
459

   case CL_PROGRAM_BUILD_OPTIONS:
460
      buf.as_string() = prog.build(dev).opts;
461
      break;
462
463

   case CL_PROGRAM_BUILD_LOG:
464
      buf.as_string() = prog.build(dev).log;
465
      break;
466

467
468
469
470
   case CL_PROGRAM_BINARY_TYPE:
      buf.as_scalar<cl_program_binary_type>() = prog.build(dev).binary_type();
      break;

471
   default:
472
      throw error(CL_INVALID_VALUE);
473
   }
474
475
476
477
478

   return CL_SUCCESS;

} catch (error &e) {
   return e.get();
479
}