File: numpy_dlpack.c

package info (click to toggle)
dlpack 0.0~git20200217.3ec0443-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 244 kB
  • sloc: ansic: 117; python: 88; cpp: 57; sh: 38; makefile: 30
file content (52 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <dlpack/dlpack.h>

DLManagedTensor *given = NULL;

void display(DLManagedTensor a) {
  puts("On C side:");
  int i;
  int ndim = a.dl_tensor.ndim;
  printf("data = %p\n", a.dl_tensor.data);
  printf("ctx = (device_type = %d, device_id = %d)\n",
          (int) a.dl_tensor.ctx.device_type,
          (int) a.dl_tensor.ctx.device_id);
  printf("dtype = (code = %d, bits = %d, lanes = %d)\n",
          (int) a.dl_tensor.dtype.code,
          (int) a.dl_tensor.dtype.bits,
          (int) a.dl_tensor.dtype.lanes);
  printf("ndim = %d\n",
          (int) a.dl_tensor.ndim);
  printf("shape = (");
  for (i = 0; i < ndim; ++i) {
    if (i != 0) {
      printf(", ");
    }
    printf("%d", (int) a.dl_tensor.shape[i]);
  }
  printf(")\n");
  printf("strides = (");
  for (i = 0; i < ndim; ++i) {
    if (i != 0) {
      printf(", ");
    }
    printf("%d", (int) a.dl_tensor.strides[i]);
  }
  printf(")\n");
}

void Give(DLManagedTensor dl_managed_tensor) {
  display(dl_managed_tensor);
  given = (DLManagedTensor *) malloc(sizeof(DLManagedTensor));
  *given = dl_managed_tensor;
}

void Finalize() {
  given->deleter(given);
}

void FreeHandle() {
  free(given);
  given = NULL;
}