aboutsummaryrefslogtreecommitdiff
path: root/src/core/context.cpp
blob: 4d51a6070c4c33531a4c4ff48516b7e0efc9c7e6 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (c) 2011, Denis Steckelmacher <steckdenis@yahoo.fr>
 * Copyright (c) 2012-2014, Texas Instruments Incorporated - http://www.ti.com/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * \file context.cpp
 * \brief Context
 */

#include "context.h"
#include "deviceinterface.h"
#include "propertylist.h"
#include "platform.h"

#include <cstring>
#include <cstdlib>

#include <llvm/Support/TargetSelect.h>

using namespace Coal;

static void default_pfn_notify(const char *, const void *, size_t, void *)
{
    return;
}

Context::Context(const cl_context_properties *properties,
                 cl_uint num_devices,
                 const cl_device_id *devices,
                 void (CL_CALLBACK *pfn_notify)(const char *, const void *,
                                                size_t, void *),
                 void *user_data,
                 cl_int *errcode_ret)
: Object(Object::T_Context, 0), p_properties(0), p_pfn_notify(pfn_notify),
  p_user_data(user_data), p_devices(0), p_d_devices(0), p_num_devices(0), p_props_len(0),
  p_platform((cl_platform_id) &the_platform::Instance())
{
    if (!p_pfn_notify)
        p_pfn_notify = &default_pfn_notify;

    // Intialize LLVM, this can be done more than one time per program
    llvm::InitializeNativeTarget();
    llvm::InitializeNativeTargetAsmPrinter();
    llvm::InitializeNativeTargetAsmParser();

    // Explore the properties
    if (properties)
    {
        const unsigned char *props = (const unsigned char *)properties;
        cl_context_properties prop;
        size_t props_len = 0;

#define GET_PROP(type, var)     \
    var = *(const type *)props;       \
    props += sizeof(type);      \
    props_len += sizeof(type);

		int propset = 0;
        while (true)
        {
            GET_PROP(cl_context_properties, prop)

            if (!prop)
                break;

            switch (prop)
            {
                case CL_CONTEXT_PLATFORM:
                    if (!propset)
                    {
                        GET_PROP(cl_platform_id, p_platform);
                        propset = 1;
                    }
                    else
                    {
                        *errcode_ret = CL_INVALID_PROPERTY;
                        return;
                    }
                    break;

                default:
                    *errcode_ret = CL_INVALID_PROPERTY;
                    return;
            }
        }

        // properties may be allocated on the stack of the client application
        // copy it into a real buffer
        p_properties = (cl_context_properties *)std::malloc(props_len);
        p_props_len = props_len;

        if (!p_properties)
        {
            *errcode_ret = CL_OUT_OF_HOST_MEMORY;
            return;
        }

        std::memcpy((void *)p_properties, (const void *)properties, props_len);
    }

    // Verify that the platform is good
    if (p_platform != (cl_platform_id) &the_platform::Instance())
    {
        *errcode_ret = CL_INVALID_PLATFORM;
        return;
    }

    // Explore the devices
    p_devices = (DeviceInterface **)std::malloc(num_devices * sizeof(DeviceInterface *));
    p_d_devices = (cl_device_id *)std::malloc(num_devices * sizeof(cl_device_id));
    p_num_devices = num_devices;

    if (!p_devices || !p_d_devices)
    {
        *errcode_ret = CL_OUT_OF_HOST_MEMORY;
        return;
    }

    for (cl_uint i=0; i<num_devices; ++i)
    {
        auto device = pobj(devices[i]);

        if (device == 0)
        {
            *errcode_ret = CL_INVALID_DEVICE;
            return;
        }

        // Verify that the device is available
        cl_bool device_available;

        *errcode_ret = device->info(CL_DEVICE_AVAILABLE,
                                    sizeof(device_available),
                                    &device_available,
                                    0);

        if (*errcode_ret != CL_SUCCESS)
            return;

        if (!device_available)
        {
            *errcode_ret = CL_DEVICE_NOT_AVAILABLE;
            return;
        }

        // Add the device to the list
        p_devices[i] = device;
        p_d_devices[i] = devices[i];
    }
}

Context::~Context()
{
    if (p_properties)
        std::free((void *)p_properties);

    if (p_devices)
        std::free((void *)p_devices);

    if (p_d_devices)
        std::free((void *)p_d_devices);
}

cl_int Context::info(cl_context_info param_name,
                     size_t param_value_size,
                     void *param_value,
                     size_t *param_value_size_ret) const
{
    void *value = 0;
    size_t value_length = 0;

    union {
        cl_uint cl_uint_var;
    };

    switch (param_name)
    {
        case CL_CONTEXT_REFERENCE_COUNT:
            SIMPLE_ASSIGN(cl_uint, references());
            break;

        case CL_CONTEXT_NUM_DEVICES:
            SIMPLE_ASSIGN(cl_uint, p_num_devices);
            break;

        case CL_CONTEXT_DEVICES:
            MEM_ASSIGN(p_num_devices * sizeof(cl_device_id), p_d_devices);
            break;

        case CL_CONTEXT_PROPERTIES:
            MEM_ASSIGN(p_props_len, p_properties);
            break;

        default:
            return CL_INVALID_VALUE;
    }

    if (param_value && param_value_size < value_length)
        return CL_INVALID_VALUE;

    if (param_value_size_ret)
        *param_value_size_ret = value_length;

    if (param_value && value_length /* CONTEXT_PROPERTIES can be of length 0 */)
        std::memcpy(param_value, value, value_length);

    return CL_SUCCESS;
}

bool Context::hasDevice(DeviceInterface *device) const
{
    for (unsigned int i=0; i<p_num_devices; ++i)
        if (p_devices[i] == device)
            return true;

    return false;
}