summaryrefslogtreecommitdiff
path: root/src/core/cpu/kernel.cpp
blob: a898522441a474e57595e02058a4ee351e28e6fe (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "kernel.h"
#include "device.h"
#include "buffer.h"
#include "program.h"

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

#include <llvm/Function.h>
#include <llvm/Constants.h>
#include <llvm/ADT/APInt.h>
#include <llvm/ADT/APFloat.h>
#include <llvm/Support/Casting.h>
#include <llvm/Instructions.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>

#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace Coal;

template<typename T>
bool incVec(cl_ulong dims, T *vec, T *maxs)
{
    bool overflow = false;

    for (cl_ulong i=0; i<dims; ++i)
    {
        vec[i] += 1;

        if (vec[i] > maxs[i])
        {
            vec[i] = 0;
            overflow = true;
        }
        else
        {
            overflow = false;
            break;
        }
    }

    return overflow;
}

static llvm::Constant *getPointerConstant(llvm::LLVMContext &C,
                                          llvm::Type *type,
                                          void *const *value)
{
    llvm::Constant *rs = 0;

    if (sizeof(void *) == 4)
        rs = llvm::ConstantInt::get(llvm::Type::getInt32Ty(C), *(uint32_t *)value);
    else
        rs = llvm::ConstantInt::get(llvm::Type::getInt64Ty(C), *(uint64_t *)value);

    // Cast to kernel's pointer type
    rs = llvm::ConstantExpr::getIntToPtr(rs, type);

    return rs;
}

CPUKernel::CPUKernel(CPUDevice *device, Kernel *kernel, llvm::Function *function)
: DeviceKernel(), p_device(device), p_kernel(kernel), p_function(function),
  p_call_function(0)
{
    pthread_mutex_init(&p_call_function_mutex, 0);
}

CPUKernel::~CPUKernel()
{
    if (p_call_function)
        p_call_function->eraseFromParent();

    pthread_mutex_destroy(&p_call_function_mutex);
}

size_t CPUKernel::workGroupSize() const
{
    return 0; // TODO
}

cl_ulong CPUKernel::localMemSize() const
{
    return 0; // TODO
}

cl_ulong CPUKernel::privateMemSize() const
{
    return 0; // TODO
}

size_t CPUKernel::preferredWorkGroupSizeMultiple() const
{
    return 0; // TODO
}

template<typename T>
T k_exp(T base, unsigned int e)
{
    T rs = base;

    for (unsigned int i=1; i<e; ++i)
        rs *= base;

    return rs;
}

// Try to find the size a work group has to have to be executed the fastest on
// the CPU.
size_t CPUKernel::guessWorkGroupSize(cl_uint num_dims, cl_uint dim,
                          size_t global_work_size) const
{
    unsigned int cpus = p_device->numCPUs();

    // Don't break in too small parts
    if (k_exp(global_work_size, num_dims) > 64)
        return global_work_size;

    // Find the divisor of global_work_size the closest to cpus but >= than it
    unsigned int divisor = cpus;

    while (true)
    {
        if ((global_work_size % divisor) == 0)
            break;

        // Don't let the loop go up to global_work_size, the overhead would be
        // too huge
        if (divisor > global_work_size || divisor > cpus * 32)
        {
            divisor = 1;  // Not parallel but has no CommandQueue overhead
            break;
        }
    }

    // Return the size
    return global_work_size / divisor;
}

llvm::Function *CPUKernel::function() const
{
    return p_function;
}

Kernel *CPUKernel::kernel() const
{
    return p_kernel;
}

CPUDevice *CPUKernel::device() const
{
    return p_device;
}

llvm::Function *CPUKernel::callFunction(std::vector<void *> &freeLocal)
{
    pthread_mutex_lock(&p_call_function_mutex);

    // If we can reuse the same function between work groups, do it
    if (!p_kernel->needsLocalAllocation() && p_call_function)
    {
        llvm::Function *rs = p_call_function;
        pthread_mutex_unlock(&p_call_function_mutex);

        return rs;
    }

    // Create a LLVM function that calls the kernels with its arguments
    // Code inspired from llvm/lib/ExecutionEngine/JIT/JIT.cpp
    // Copyright The LLVM Compiler Infrastructure
    llvm::FunctionType *k_func_type = p_function->getFunctionType();
    llvm::FunctionType *f_type =
        llvm::FunctionType::get(p_function->getReturnType(), false);
    llvm::Function *stub = llvm::Function::Create(f_type,
                                                  llvm::Function::InternalLinkage,
                                                "", p_function->getParent());

    // Insert a basic block
    llvm::BasicBlock *block = llvm::BasicBlock::Create(p_function->getContext(),
                                                       "", stub);

    llvm::SmallVector<llvm::Value *, 8> args;

    // Add each kernel arg to args
    for (int i=0; i<p_kernel->numArgs(); ++i)
    {
        const Kernel::Arg &a = p_kernel->arg(i);
        llvm::Constant *arg_constant = 0;

        // To handle vectors (float4, etc)
        llvm::SmallVector<llvm::Constant *, 4> vec_elements;

        // Explore the vector elements
        for (unsigned short k=0; k<a.vecDim(); ++k)
        {
            const void *value = a.value(k);
            llvm::Constant *C = 0;

            switch (a.kind())
            {
                case Kernel::Arg::Int8:
                    C = llvm::ConstantInt::get(stub->getContext(),
                                               llvm::APInt(8, *(uint8_t *)value));
                    break;

                case Kernel::Arg::Int16:
                    C = llvm::ConstantInt::get(stub->getContext(),
                                               llvm::APInt(16, *(uint16_t *)value));
                    break;

                case Kernel::Arg::Int32:
                    C = llvm::ConstantInt::get(stub->getContext(),
                                               llvm::APInt(32, *(uint32_t *)value));
                    break;

                case Kernel::Arg::Int64:
                    C = llvm::ConstantInt::get(stub->getContext(),
                                               llvm::APInt(64, *(uint64_t *)value));
                    break;

                case Kernel::Arg::Float:
                    C = llvm::ConstantFP::get(stub->getContext(),
                                              llvm::APFloat(*(float *)value));
                    break;

                case Kernel::Arg::Double:
                    C = llvm::ConstantFP::get(stub->getContext(),
                                              llvm::APFloat(*(double *)value));
                    break;

                case Kernel::Arg::Buffer:
                {
                    MemObject *buffer = *(MemObject **)value;

                    if (a.file() == Kernel::Arg::Local)
                    {
                        // Alloc a buffer and pass it to the kernel
                        void *local_buffer = std::malloc(a.allocAtKernelRuntime());
                        C = getPointerConstant(stub->getContext(),
                                               k_func_type->getParamType(i),
                                               &local_buffer);

                        freeLocal.push_back(local_buffer);
                    }
                    else
                    {
                        if (!buffer)
                        {
                            // We can do that, just send NULL
                            C = llvm::ConstantPointerNull::get(
                                    llvm::cast<llvm::PointerType>(
                                        k_func_type->getParamType(i)));
                        }
                        else
                        {
                            // Get the CPU buffer, allocate it and get its pointer
                            CPUBuffer *cpubuf =
                                (CPUBuffer *)buffer->deviceBuffer(p_device);
                            void *buf_ptr = 0;

                            if (!cpubuf->allocated())
                                cpubuf->allocate();

                            buf_ptr = cpubuf->data();

                            C = getPointerConstant(stub->getContext(),
                                                   k_func_type->getParamType(i),
                                                   &buf_ptr);
                        }
                    }

                    break;
                }

                case Kernel::Arg::Image2D:
                case Kernel::Arg::Image3D:
                    // Assign a pointer to the image object, the intrinsic functions
                    // will handle them
                    C = getPointerConstant(stub->getContext(),
                                           k_func_type->getParamType(i),
                                           (void **)value);
                    break;

                default:
                    break;
            }

            // Add the vector element
            vec_elements.push_back(C);
        }

        // If the arg was a vector, handle it
        if (a.vecDim() == 1)
        {
            arg_constant = vec_elements.front();
        }
        else
        {
            arg_constant = llvm::ConstantVector::get(vec_elements);
        }

        // Append the arg
        args.push_back(arg_constant);
    }

    // Create the call instruction
    llvm::CallInst *call_inst = llvm::CallInst::Create(p_function, args, "", block);
    call_inst->setCallingConv(p_function->getCallingConv());
    call_inst->setTailCall();

    // Create a return instruction to end the stub
    llvm::ReturnInst::Create(stub->getContext(), block);

    // Retain the function if it can be reused
    if (!p_kernel->needsLocalAllocation())
        p_call_function = stub;

    pthread_mutex_unlock(&p_call_function_mutex);

    return stub;
}

/*
 * CPUKernelEvent
 */
CPUKernelEvent::CPUKernelEvent(CPUDevice *device, KernelEvent *event)
: p_device(device), p_event(event), p_current_wg(0), p_finished_wg(0)
{
    // Mutex
    pthread_mutex_init(&p_mutex, 0);

    // Set current work group to (0, 0, ..., 0)
    std::memset(p_current_work_group, 0, event->work_dim() * sizeof(size_t));

    // Populate p_max_work_groups
    p_num_wg = 1;

    for (cl_uint i=0; i<event->work_dim(); ++i)
    {
        p_max_work_groups[i] =
            (event->global_work_size(i) / event->local_work_size(i)) - 1; // 0..n-1, not 1..n

        p_num_wg *= p_max_work_groups[i] + 1;
    }
}

CPUKernelEvent::~CPUKernelEvent()
{
    pthread_mutex_destroy(&p_mutex);
}

bool CPUKernelEvent::reserve()
{
    int rs;

    // Lock, this will be unlocked in takeInstance()
    pthread_mutex_lock(&p_mutex);

    // Last work group if current == max - 1
    return (p_current_wg == p_num_wg - 1);
}

bool CPUKernelEvent::finished()
{
    bool rs;

    pthread_mutex_lock(&p_mutex);

    rs = (p_finished_wg == p_num_wg);

    pthread_mutex_unlock(&p_mutex);

    return rs;
}

void CPUKernelEvent::workGroupFinished()
{
    pthread_mutex_lock(&p_mutex);

    p_finished_wg++;

    pthread_mutex_unlock(&p_mutex);
}

CPUKernelWorkGroup *CPUKernelEvent::takeInstance()
{
    CPUKernelWorkGroup *wg = new CPUKernelWorkGroup((CPUKernel *)p_event->deviceKernel(),
                                                    p_event,
                                                    this,
                                                    p_current_work_group);

    // Increment current work group
    incVec(p_event->work_dim(), p_current_work_group, p_max_work_groups);
    p_current_wg += 1;

    // Release event
    pthread_mutex_unlock(&p_mutex);

    return wg;
}

/*
 * CPUKernelWorkGroup
 */
CPUKernelWorkGroup::CPUKernelWorkGroup(CPUKernel *kernel, KernelEvent *event,
                                       CPUKernelEvent *cpu_event,
                                       const size_t *work_group_index)
: p_kernel(kernel), p_event(event), p_cpu_event(cpu_event),
  p_work_dim(event->work_dim())
{

    // Set index
    std::memcpy(p_index, work_group_index, p_work_dim * sizeof(size_t));

    // Set maxs and global id
    for (unsigned int i=0; i<p_work_dim; ++i)
    {
        p_maxs[i] = event->local_work_size(i) - 1; // 0..n-1, not 1..n

        // Set global id
        p_global_id[i] = (p_index[i] * event->local_work_size(i))
                         + event->global_work_offset(i);
    }
}

CPUKernelWorkGroup::~CPUKernelWorkGroup()
{
    p_cpu_event->workGroupFinished();
}

bool CPUKernelWorkGroup::run()
{
    // Set current pos to 0
    std::memset(p_current, 0, p_work_dim * sizeof(size_t));

    // Get the kernel function to call
    bool free_after = p_kernel->kernel()->needsLocalAllocation();
    std::vector<void *> local_to_free;
    llvm::Function *kernel_func = p_kernel->callFunction(local_to_free);

    if (!kernel_func)
        return false;

    CPUProgram *prog =
        (CPUProgram *)(p_kernel->kernel()->program()
            ->deviceDependentProgram(p_kernel->device()));

    void (*kernel_func_addr)() = (void(*)())prog->jit()->getPointerToFunction(kernel_func);

    // Tell the builtins this thread will run a kernel
    setThreadLocalWorkGroup(this);

    do
    {
        // Simply call the "call function", it and the builtins will do the rest
        kernel_func_addr();
    } while (!incVec(p_work_dim, p_current, p_maxs));

    // We may have some cleanup to do
    if (free_after)
    {
        for (int i=0; i<local_to_free.size(); ++i)
        {
            std::free(local_to_free[i]);
        }

        // Bye function
        kernel_func->eraseFromParent();
    }

    return true;
}

cl_uint CPUKernelWorkGroup::getWorkDim() const
{
    return p_work_dim;
}

size_t CPUKernelWorkGroup::getGlobalId(cl_uint dimindx) const
{
    if (dimindx > p_work_dim)
        return 0;

    return p_global_id[dimindx] + p_current[dimindx];
}

size_t CPUKernelWorkGroup::getGlobalSize(cl_uint dimindx) const
{
    if (dimindx >p_work_dim)
        return 1;

    return p_event->global_work_size(dimindx);
}

size_t CPUKernelWorkGroup::getLocalSize(cl_uint dimindx) const
{
    if (dimindx > p_work_dim)
        return 1;

    return p_event->local_work_size(dimindx);
}

size_t CPUKernelWorkGroup::getLocalID(cl_uint dimindx) const
{
    if (dimindx > p_work_dim)
        return 0;

    return p_current[dimindx];
}

size_t CPUKernelWorkGroup::getNumGroups(cl_uint dimindx) const
{
    if (dimindx > p_work_dim)
        return 1;

    return (p_event->global_work_size(dimindx) /
            p_event->local_work_size(dimindx));
}

size_t CPUKernelWorkGroup::getGroupID(cl_uint dimindx) const
{
    if (dimindx > p_work_dim)
        return 0;

    return p_index[dimindx];
}

size_t CPUKernelWorkGroup::getGlobalOffset(cl_uint dimindx) const
{
    if (dimindx > p_work_dim)
        return 0;

    return p_event->global_work_offset(dimindx);
}

void CPUKernelWorkGroup::builtinNotFound(const std::string &name) const
{
    std::cout << "OpenCL: Non-existant builtin function " << name
              << " found in kernel " << p_kernel->function()->getNameStr()
              << '.' << std::endl;
}