summaryrefslogtreecommitdiff
path: root/src/core/context.cpp
blob: 54c3d66b62c8670b83cc16fb96edf9e9821a559b (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
#include "context.h"
#include "deviceinterface.h"
#include "propertylist.h"

#include <cstring>
#include <cstdlib>

#include <llvm/Target/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_props_len(0), p_user_data(user_data), p_platform(0), p_devices(0),
  p_num_devices(0)
{
    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();

    // 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);

        while (true)
        {
            GET_PROP(cl_context_properties, prop)

            if (!prop)
                break;

            switch (prop)
            {
                case CL_CONTEXT_PLATFORM:
                    GET_PROP(cl_platform_id, p_platform);
                    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 != 0)
    {
        *errcode_ret = CL_INVALID_PLATFORM;
        return;
    }

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

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

    for (int i=0; i<num_devices; ++i)
    {
        cl_device_id device = 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] = (DeviceInterface *)device;
    }
}

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

    if (p_devices)
        std::free((void *)p_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(DeviceInterface *), p_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 (int i=0; i<p_num_devices; ++i)
        if (p_devices[i] == device)
            return true;

    return false;
}