1 module grain.cl.allocator;
2 
3 import grain.tensor : Opt;
4 import grain.cl.testing;
5 import grain.cl.device : ClDevice;
6 
7 import grain.dpp.cl;
8 
9 
10 struct ClMem
11 {
12     @nogc nothrow:
13 
14     cl_mem ptr;
15     size_t length;
16 
17     alias ptr this;
18 }
19 
20 struct ClMallocator
21 {
22     Opt opt;
23     alias opt this;
24 
25     /// device indicator
26     enum deviceof = "cl";
27     enum pinned = false;
28 
29     ///
30     @trusted @nogc nothrow
31     ClMem allocate()(size_t bytes)
32     {
33         // import grain.dpp.cuda_driver : cuMemAlloc_v2, CUdeviceptr;
34         if (!bytes) return ClMem(null, 0);
35 
36         auto id = ClDevice.get(opt.deviceId, opt.platformId);
37         auto context = checkClFun!clCreateContext(null, 1, &id, null, null);
38         auto da = checkClFun!clCreateBuffer(context, CL_MEM_READ_WRITE, bytes, null);
39         return ClMem(da, bytes);
40     }
41 
42     ///
43     @system @nogc nothrow
44     bool deallocate()(ClMem b)
45     {
46         checkCl(clReleaseMemObject(b.ptr));
47         return true;
48     }
49 
50     enum instance = ClMallocator();
51 }