1/*******************************************************************************
2* Copyright 2016-2018 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#include <assert.h>
18
19#include "c_types_map.hpp"
20#include "engine.hpp"
21#include "primitive_desc.hpp"
22#include "primitive.hpp"
23#include "type_helpers.hpp"
24#include "stream.hpp"
25#include "utils.hpp"
26
27using namespace mkldnn::impl;
28using namespace mkldnn::impl::status;
29using namespace mkldnn::impl::primitive_kind;
30
31namespace {
32// XXX: this is a huge hammer. This disables all and any msan checks on
33// primitives outputs.
34//
35// A proper approach would be an implementation-specific unpoisoning.
36void unpoison_outputs(const exec_args_t &args) {
37 for(const auto &arg: args) {
38 if (arg.second.is_const) continue;
39 auto *mem = arg.second.mem;
40 void *p;
41 mem->get_data_handle(&p);
42 size_t s = memory_desc_wrapper(*mem->md()).size();
43 msan_unpoison(p, s);
44 }
45}
46}
47
48status_t mkldnn_primitive_desc_destroy(primitive_desc_t *primitive_desc) {
49 if (primitive_desc) delete primitive_desc;
50 return success;
51}
52
53status_t mkldnn_primitive_create(primitive_t **primitive,
54 const primitive_desc_t *primitive_desc) {
55 if (utils::any_null(primitive, primitive_desc))
56 return invalid_arguments;
57 return primitive_desc->create_primitive(primitive);
58}
59
60status_t mkldnn_primitive_execute(const primitive_t *primitive,
61 stream_t *stream, int nargs, const mkldnn_exec_arg_t *c_args) {
62 bool ok = true
63 && !utils::any_null(primitive, stream)
64 && primitive->engine() == stream->engine()
65 && IMPLICATION(nargs > 0, c_args != nullptr);
66 if (!ok) return invalid_arguments;
67
68 exec_args_t args;
69 status_t status = cvt_primtive_args(primitive->pd(), nargs, c_args, args);
70 if (status != status::success) return status;
71
72 exec_ctx_t ctx(stream, std::move(args));
73
74 if (mkldnn_verbose()->level) {
75 double ms = get_msec();
76 status = primitive->execute(ctx);
77 ms = get_msec() - ms;
78 printf("mkldnn_verbose,exec,%s,%g\n", primitive->pd()->info(), ms);
79 fflush(0);
80 } else {
81 status = primitive->execute(ctx);
82 }
83
84 if (msan_enabled) unpoison_outputs(ctx.args());
85
86 return status;
87}
88
89status_t mkldnn_primitive_get_primitive_desc(const primitive_t *primitive,
90 const primitive_desc_t **primitive_desc) {
91 if (utils::any_null(primitive, primitive_desc))
92 return invalid_arguments;
93 return safe_ptr_assign<const primitive_desc_t>(*primitive_desc,
94 primitive->pd());
95}
96
97status_t mkldnn_primitive_destroy(primitive_t *primitive) {
98 if (primitive != nullptr)
99 delete primitive;
100 return success;
101}
102
103// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
104