summaryrefslogtreecommitdiff
path: root/src/cl_cmrt.cpp
blob: f6538444872028562f89d4eeffc58f17b18977f7 (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
#include "cl_cmrt.h"
#include "cl_device_id.h"
#include "intel/intel_defines.h"
#include "cl_command_queue.h"

#include "cm_rt.h"      //header file of libcmrt.so
typedef INT (*CreateCmDeviceFunc)(CmDevice * &pDevice, UINT & version,
			    CmDriverContext * drivercontext, UINT DevCreateOption);
typedef INT (*DestroyCmDeviceFunc)(CmDevice * &pDevice);

#include <dlfcn.h>

static void* dlhCMRT = NULL;
static CreateCmDeviceFunc pfnCreateCmDevice = NULL;
static DestroyCmDeviceFunc pfnDestroyCmDevice = NULL;

#define XSTR(x) #x
#define STR(x) XSTR(x)

class CmrtCleanup
{
public:
  CmrtCleanup(){}
  ~CmrtCleanup()
  {
    if (dlhCMRT != NULL)
      dlclose(dlhCMRT);
  }
};

enum CMRT_MEM_TYPE
{
    CMRT_BUFFER,
    CMRT_SURFACE2D,
};

static CmrtCleanup cmrtCleanup;

static bool LoadCmrtLibrary()
{
  if (dlhCMRT == NULL) {
    dlhCMRT = dlopen(STR(CMRT_PATH), RTLD_LAZY | RTLD_LOCAL);

    if (dlhCMRT == NULL)
      return false;

    pfnCreateCmDevice = (CreateCmDeviceFunc)dlsym(dlhCMRT, "CreateCmDevice");
    if (pfnCreateCmDevice == NULL)
      return false;

    pfnDestroyCmDevice = (DestroyCmDeviceFunc)dlsym(dlhCMRT, "DestroyCmDevice");
    if (pfnDestroyCmDevice == NULL)
      return false;
  }
  return true;
}

cl_int cmrt_build_program(cl_program p, const char *options)
{
  CmDevice*& cmrt_device = (CmDevice*&)(p->ctx->device->cmrt_device);
  int result;
  if (cmrt_device == NULL)
  {
    if (!LoadCmrtLibrary())
      return CL_DEVICE_NOT_AVAILABLE;   //yes, the error is not accurate, but i do not find a bettere one

    CmDriverContext ctx;
    ctx.shared_bufmgr = 1;
    ctx.bufmgr = (drm_intel_bufmgr*)cl_context_get_bufmgr(p->ctx);
    ctx.userptr_enabled = 0;
    ctx.deviceid = p->ctx->device->device_id;
    ctx.device_rev = -1;
    UINT version = 0;
    result = (*pfnCreateCmDevice)(cmrt_device, version, &ctx, CM_DEVICE_CREATE_OPTION_DEFAULT);
    if (result != CM_SUCCESS)
      return CL_DEVICE_NOT_AVAILABLE;
  }

  CmProgram* cmrt_program = NULL;
  result = cmrt_device->LoadProgram(p->binary, p->binary_sz, cmrt_program, options);
  if (result != CM_SUCCESS)
    return CL_COMPILE_PROGRAM_FAILURE;

  p->cmrt_program = cmrt_program;
  cmrt_program->GetKernelCount(p->ker_n);
  return CL_SUCCESS;
}

cl_int cmrt_destroy_program(cl_program p)
{
  CmDevice* cmrt_device = (CmDevice*)(p->ctx->device->cmrt_device);
  CmProgram*& cmrt_program = (CmProgram*&)(p->cmrt_program);
  if (cmrt_device->DestroyProgram(cmrt_program) != CM_SUCCESS)
    return CL_INVALID_PROGRAM;
  return CL_SUCCESS;
}

cl_int cmrt_destroy_device(cl_device_id device)
{
  CmDevice*& cmrt_device = (CmDevice*&)(device->cmrt_device);
  if ((*pfnDestroyCmDevice)(cmrt_device) != CM_SUCCESS)
    return CL_INVALID_DEVICE;
  return CL_SUCCESS;
}

void* cmrt_create_kernel(cl_program p, const char *name)
{
  CmDevice* cmrt_device = (CmDevice*)(p->ctx->device->cmrt_device);
  CmKernel* cmrt_kernel = NULL;
  int result = cmrt_device->CreateKernel((CmProgram*)(p->cmrt_program), name, cmrt_kernel);
  if (result != CM_SUCCESS)
    return NULL;

  return cmrt_kernel;
}

cl_int cmrt_destroy_kernel(cl_kernel k)
{
  CmDevice* cmrt_device = (CmDevice*)(k->program->ctx->device->cmrt_device);
  CmKernel*& cmrt_kernel = (CmKernel*&)(k->cmrt_kernel);
  if (cmrt_device->DestroyKernel(cmrt_kernel) != CM_SUCCESS)
    return CL_INVALID_KERNEL;
  return CL_SUCCESS;
}

cl_int cmrt_enqueue(cl_command_queue cq, cl_kernel k, const size_t* global_work_size, const size_t* local_work_size)
{
  CmDevice* cmrt_device = (CmDevice*)(k->program->ctx->device->cmrt_device);
  CmKernel* cmrt_kernel = (CmKernel*)(k->cmrt_kernel);

  int result = 0;

  cmrt_kernel->SetThreadCount(global_work_size[0]*global_work_size[1]);

  //no need to destory queue explicitly,
  //and there is only one queue instance within each device,
  //CreateQueue always returns the same instance
  CmQueue* pCmQueue = NULL;
  cmrt_device->CreateQueue(pCmQueue);

  CmTask *pKernelArray = NULL;
  cmrt_device->CreateTask(pKernelArray);

  pKernelArray->AddKernel(cmrt_kernel);

  CmEvent* e = NULL;

  if (local_work_size == NULL) {
    CmThreadSpace* pTS = NULL;
    cmrt_device->CreateThreadSpace(global_work_size[0], global_work_size[1], pTS);
    result = pCmQueue->Enqueue(pKernelArray, e, pTS);
  } else {
    CmThreadGroupSpace* pTGS = NULL;
	cmrt_device->CreateThreadGroupSpace(global_work_size[0], global_work_size[1], local_work_size[0], local_work_size[1], pTGS);
    result = pCmQueue->EnqueueWithGroup(pKernelArray, e, pTGS);
    cmrt_device->DestroyThreadGroupSpace(pTGS);
  }

  if (result != CM_SUCCESS)
    return CL_INVALID_OPERATION;

  cmrt_device->DestroyTask(pKernelArray);

  CmEvent*& olde = (CmEvent*&)cq->cmrt_event;
  if (olde != NULL)
    pCmQueue->DestroyEvent(e);

  cq->cmrt_event = e;

  return CL_SUCCESS;
}

static VA_CM_FORMAT GetCmrtFormat(_cl_mem_image* image)
{
    switch (image->intel_fmt)
    {
    case I965_SURFACEFORMAT_B8G8R8A8_UNORM:
      return VA_CM_FMT_A8R8G8B8;
    case I965_SURFACEFORMAT_B8G8R8X8_UNORM:
      return VA_CM_FMT_X8R8G8B8;
    case I965_SURFACEFORMAT_A8_UNORM:
      return VA_CM_FMT_A8;
    case I965_SURFACEFORMAT_R10G10B10A2_UNORM:
      return VA_CM_FMT_A2B10G10R10;
    case I965_SURFACEFORMAT_R16G16B16A16_UNORM:
      return VA_CM_FMT_A16B16G16R16;
    case I965_SURFACEFORMAT_L8_UNORM:
      return VA_CM_FMT_L8;
    case I965_SURFACEFORMAT_R16_UINT:
      return VA_CM_FMT_R16U;
    case I965_SURFACEFORMAT_R8_UNORM:
      return VA_CM_FMT_R8U;
    case I965_SURFACEFORMAT_L16_UNORM:
      return VA_CM_FMT_L16;
    case I965_SURFACEFORMAT_R32_FLOAT:
      return VA_CM_FMT_R32F;
    default:
      return VA_CM_FMT_UNKNOWN;
    }
}

static bool CreateCmrtMemory(cl_mem mem)
{
  if (mem->cmrt_mem != NULL)
    return true;

  CmDevice* cmrt_device = (CmDevice*)(mem->ctx->device->cmrt_device);
  int result;
  CmOsResource osResource;
  osResource.bo_size = mem->size;
  osResource.bo_flags = DRM_BO_HANDLE;
  osResource.bo = (drm_intel_bo*)mem->bo;
  if (IS_IMAGE(mem)) {
    _cl_mem_image* image = cl_mem_image(mem);
    if (CL_MEM_OBJECT_IMAGE2D != image->image_type)
      return CL_INVALID_ARG_VALUE;
    osResource.format = GetCmrtFormat(image);
    if (osResource.format == VA_CM_FMT_UNKNOWN)
      return false;
    osResource.aligned_width = image->row_pitch;
    osResource.aligned_height = mem->size / image->row_pitch;
    osResource.pitch = image->row_pitch;
    osResource.tile_type = image->tiling;
    osResource.orig_width = image->w;
    osResource.orig_height = image->h;
    CmSurface2D*& cmrt_surface2d = (CmSurface2D*&)(mem->cmrt_mem);
    result = cmrt_device->CreateSurface2D(&osResource, cmrt_surface2d);
    mem->cmrt_mem_type = CMRT_SURFACE2D;
  } else {
    osResource.format = VA_CM_FMT_BUFFER;
    osResource.buf_bytes = mem->size;
    CmBuffer*& cmrt_buffer = (CmBuffer*&)(mem->cmrt_mem);
    result = cmrt_device->CreateBuffer(&osResource, cmrt_buffer);
    mem->cmrt_mem_type = CMRT_BUFFER;
  }

  if (result != CM_SUCCESS)
    return false;

  return true;
}

cl_int cmrt_set_kernel_arg(cl_kernel k, cl_uint index, size_t sz, const void *value)
{
  if(value == NULL)
    return CL_INVALID_ARG_VALUE;

  CmKernel* cmrt_kernel = (CmKernel*)(k->cmrt_kernel);

  WORD argKind = -1;
  if (cmrt_kernel->GetArgKind(index, argKind) != CM_SUCCESS)
    return CL_INVALID_ARG_INDEX;

  int result;
  if (argKind == ARG_KIND_GENERAL)
    result = cmrt_kernel->SetKernelArg(index, sz, value);
  else {
    cl_mem mem = *(cl_mem*)value;
    if (((cl_base_object)mem)->magic == CL_MAGIC_MEM_HEADER) {
      if (!CreateCmrtMemory(mem))
        return CL_INVALID_ARG_VALUE;

      SurfaceIndex * memIndex = NULL;
      if (mem->cmrt_mem_type == CMRT_BUFFER) {
        CmBuffer* cmrt_buffer = (CmBuffer*)(mem->cmrt_mem);
        cmrt_buffer->GetIndex(memIndex);
      } else {
        CmSurface2D* cmrt_surface2d = (CmSurface2D*)(mem->cmrt_mem);
        cmrt_surface2d->GetIndex(memIndex);
      }
      result = cmrt_kernel->SetKernelArg(index, sizeof(SurfaceIndex), memIndex);
    } else
      return CL_INVALID_ARG_VALUE;
  }

  if (result != CM_SUCCESS)
    return CL_INVALID_KERNEL_ARGS;

  return CL_SUCCESS;
}

cl_int cmrt_destroy_memory(cl_mem mem)
{
  CmDevice* cmrt_device = (CmDevice*)(mem->ctx->device->cmrt_device);
  if (mem->cmrt_mem_type == CMRT_BUFFER) {
    CmBuffer*& cmrt_buffer = (CmBuffer*&)(mem->cmrt_mem);
    cmrt_device->DestroySurface(cmrt_buffer);
  } else {
    CmSurface2D*& cmrt_surface2d = (CmSurface2D*&)(mem->cmrt_mem);
    cmrt_device->DestroySurface(cmrt_surface2d);
  }
  return CL_SUCCESS;
}

cl_int cmrt_destroy_event(cl_command_queue cq)
{
  CmEvent*& cmrt_event = (CmEvent*&)(cq->cmrt_event);
  CmDevice* cmrt_device = (CmDevice*)(cq->ctx->device->cmrt_device);
  CmQueue* pCmQueue = NULL;
  cmrt_event->WaitForTaskFinished();
  cmrt_device->CreateQueue(pCmQueue);
  pCmQueue->DestroyEvent(cmrt_event);
  return CL_SUCCESS;
}

cl_int cmrt_wait_for_task_finished(cl_command_queue cq)
{
  CmEvent* cmrt_event = (CmEvent*)(cq->cmrt_event);
  cmrt_event->WaitForTaskFinished();
  return CL_SUCCESS;
}