summaryrefslogtreecommitdiff
path: root/src/core/cpu/worker.cpp
blob: a456714aad6b0df2a6dff0e5e4dc532b602e9bf0 (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
242
243
244
245
246
#include "worker.h"
#include "device.h"
#include "buffer.h"
#include "kernel.h"
#include "builtins.h"

#include "../commandqueue.h"
#include "../events.h"
#include "../memobject.h"
#include "../kernel.h"

#include <sys/mman.h>

#include <cstring>
#include <iostream>

using namespace Coal;

void *worker(void *data)
{
    CPUDevice *device = (CPUDevice *)data;
    bool stop = false;
    cl_int errcode;
    Event *event;

    // Initialize TLS
    setWorkItemsData(0, 0);

    while (true)
    {
        event = device->getEvent(stop);

        // Ensure we have a good event and we don't have to stop
        if (stop) break;
        if (!event) continue;

        // Get info about the event and its command queue
        Event::Type t = event->type();
        CommandQueue *queue = 0;
        cl_command_queue_properties queue_props = 0;

        errcode = CL_SUCCESS;

        event->info(CL_EVENT_COMMAND_QUEUE, sizeof(CommandQueue *), &queue, 0);

        if (queue)
            queue->info(CL_QUEUE_PROPERTIES, sizeof(cl_command_queue_properties),
                        &queue_props, 0);

        if (queue_props & CL_QUEUE_PROFILING_ENABLE)
            event->updateTiming(Event::Start);

        // Execute the action
        switch (t)
        {
            case Event::ReadBuffer:
            case Event::WriteBuffer:
            {
                ReadWriteBufferEvent *e = (ReadWriteBufferEvent *)event;
                CPUBuffer *buf = (CPUBuffer *)e->buffer()->deviceBuffer(device);
                char *data = (char *)buf->data();

                data += e->offset();

                if (t == Event::ReadBuffer)
                    std::memcpy(e->ptr(), data, e->cb());
                else
                    std::memcpy(data, e->ptr(), e->cb());

                break;
            }
            case Event::CopyBuffer:
            {
                CopyBufferEvent *e = (CopyBufferEvent *)event;
                CPUBuffer *src = (CPUBuffer *)e->source()->deviceBuffer(device);
                CPUBuffer *dst = (CPUBuffer *)e->destination()->deviceBuffer(device);

                std::memcpy(dst->data(), src->data(), e->cb());

                break;
            }
            case Event::ReadBufferRect:
            case Event::WriteBufferRect:
            case Event::CopyBufferRect:
            case Event::ReadImage:
            case Event::WriteImage:
            case Event::CopyImage:
            case Event::CopyBufferToImage:
            case Event::CopyImageToBuffer:
            {
                // src = buffer and dst = mem if note copy
                ReadWriteCopyBufferRectEvent *e = (ReadWriteCopyBufferRectEvent *)event;
                CPUBuffer *src_buf = (CPUBuffer *)e->source()->deviceBuffer(device);

                unsigned char *src = (unsigned char *)src_buf->data();
                unsigned char *dst;

                switch (t)
                {
                    case Event::CopyBufferRect:
                    case Event::CopyImage:
                    case Event::CopyImageToBuffer:
                    case Event::CopyBufferToImage:
                    {
                        CopyBufferRectEvent *cbre = (CopyBufferRectEvent *)e;
                        CPUBuffer *dst_buf =
                            (CPUBuffer *)cbre->destination()->deviceBuffer(device);

                        dst = (unsigned char *)dst_buf->data();
                        break;
                    }
                    default:
                    {
                        // dst = host memory location
                        ReadWriteBufferRectEvent *rwbre = (ReadWriteBufferRectEvent *)e;

                        dst = (unsigned char *)rwbre->ptr();
                    }
                }

                // Iterate over the lines to copy and use memcpy
                for (size_t z=0; z<e->region(2); ++z)
                {
                    for (size_t y=0; y<e->region(1); ++y)
                    {
                        unsigned char *s;
                        unsigned char *d;

                        d = imageData(dst,
                                      e->dst_origin(0),
                                      y + e->dst_origin(1),
                                      z + e->dst_origin(2),
                                      e->dst_row_pitch(),
                                      e->dst_slice_pitch(),
                                      1);

                        s = imageData(src,
                                      e->src_origin(0),
                                      y + e->src_origin(1),
                                      z + e->src_origin(2),
                                      e->src_row_pitch(),
                                      e->src_slice_pitch(),
                                      1);

                        // Copying and image to a buffer may need to add an offset
                        // to the buffer address (its rectangular origin is
                        // always (0, 0, 0)).
                        if (t == Event::CopyBufferToImage)
                        {
                            CopyBufferToImageEvent *cptie = (CopyBufferToImageEvent *)e;
                            s += cptie->offset();
                        }
                        else if (t == Event::CopyImageToBuffer)
                        {
                            CopyImageToBufferEvent *citbe = (CopyImageToBufferEvent *)e;
                            d += citbe->offset();
                        }

                        if (t == Event::WriteBufferRect || t == Event::WriteImage)
                            std::memcpy(s, d, e->region(0)); // Write dest (memory) in src
                        else
                            std::memcpy(d, s, e->region(0)); // Write src (buffer) in dest (memory), or copy the buffers
                    }
                }

                break;
            }
            case Event::MapBuffer:
            case Event::MapImage:
                // All was already done in CPUBuffer::initEventDeviceData()
                break;

            case Event::NativeKernel:
            {
                NativeKernelEvent *e = (NativeKernelEvent *)event;
                void (*func)(void *) = (void (*)(void *))e->function();
                void *args = e->args();

                func(args);

                break;
            }
            case Event::NDRangeKernel:
            case Event::TaskKernel:
            {
                KernelEvent *e = (KernelEvent *)event;
                CPUKernelEvent *ke = (CPUKernelEvent *)e->deviceData();

                // Take an instance
                CPUKernelWorkGroup *instance = ke->takeInstance();
                ke = 0;     // Unlocked, don't use anymore

                if (!instance->run())
                    errcode = CL_INVALID_PROGRAM_EXECUTABLE;

                delete instance;

                break;
            }
            default:
                break;
        }

        // Cleanups
        if (errcode == CL_SUCCESS)
        {
            bool finished = true;

            if (event->type() == Event::NDRangeKernel ||
                event->type() == Event::TaskKernel)
            {
                CPUKernelEvent *ke = (CPUKernelEvent *)event->deviceData();
                finished = ke->finished();
            }

            if (finished)
            {
                event->setStatus(Event::Complete);

                if (queue_props & CL_QUEUE_PROFILING_ENABLE)
                    event->updateTiming(Event::End);

                // Clean the queue
                if (queue)
                    queue->cleanEvents();
            }
        }
        else
        {
            // The event failed
            event->setStatus((Event::Status)errcode);

            if (queue_props & CL_QUEUE_PROFILING_ENABLE)
                    event->updateTiming(Event::End);
        }
    }

    // Free mmapped() data if needed
    size_t mapped_size;
    void *mapped_data = getWorkItemsData(mapped_size);

    if (mapped_data)
        munmap(mapped_data, mapped_size);

    return 0;
}