Halide  12.0.1
Halide compiler and libraries
gpu_object_lifetime_tracker.h
Go to the documentation of this file.
1 #ifndef GPU_OBJECT_LIFETIME_H
2 #define GPU_OBJECT_LIFETIME_H
3 
4 #include <array>
5 #include <stdio.h>
6 #include <string.h>
7 
8 namespace Halide {
9 namespace Internal {
10 
12  struct ObjectType {
13  const char *const created;
14  const char *const destroyed;
15  bool const is_global;
16  int total_created;
17  int live_count;
18 
19  ObjectType(const char *created, const char *destroyed, bool is_global = false)
20  : created(created), destroyed(destroyed),
21  is_global(is_global), total_created(0), live_count(0) {
22  }
23  };
24 
25  std::array<ObjectType, 11> object_types = {{
26  {"Caching compiled kernel:", "Releasing cached compilation:"},
27 
28  // OpenCL objects
29  {"clCreateContext", "clReleaseContext", true},
30  {"clCreateCommandQueue", "clReleaseCommandQueue", true},
31  // This handles both "clCreateProgramWithSource" and
32  // "clCreateProgramWithBinary".
33  {"clCreateBuffer", "clReleaseMemObject"},
34  {"clCreateKernel", "clReleaseKernel"},
35 
36  // CUDA objects
37  {"cuCtxCreate", "cuCtxDestroy", true},
38  {"cuMemAlloc", "cuMemFree"},
39 
40  // Metal objects
41  {"Allocating: MTLCreateSystemDefaultDevice", "Releasing: MTLCreateSystemDefaultDevice", true},
42  {"Allocating: new_command_queue", "Releasing: new_command_queue"},
43 
44  // Hexagon objects
45  {"halide_remote_load_library", "halide_remote_release_library"},
46  {"ion_alloc", "ion_free"},
47  }};
48 
49 public:
50  // Parse a line of output from gpu_debug and update object counts.
51  void record_gpu_debug(const char *str) {
52  for (auto &o : object_types) {
53  if (strstr(str, o.created)) {
54  o.total_created++;
55  o.live_count++;
56  } else if (strstr(str, o.destroyed)) {
57  o.live_count--;
58  }
59  }
60  }
61 
62  // Check that there are no live objects remaining, and we created at least one object.
63  int validate_gpu_object_lifetime(bool allow_globals, bool allow_none, int max_globals) {
64  int total = 0;
65  for (auto &o : object_types) {
66  if (o.live_count != 0 &&
67  !(allow_globals && o.is_global)) {
68  printf("Error! %d objects created by %s still live\n",
69  o.live_count, o.created);
70  return -1;
71  }
72  if (o.is_global && o.total_created > max_globals) {
73  printf("Error! %d global objects created by %s, max is %d\n",
74  o.total_created, o.created, max_globals);
75  return -1;
76  }
77 
78  total += o.total_created;
79  }
80  if (!allow_none && total == 0) {
81  printf("Error! No objects created. Ensure gpu_debug is set, ");
82  printf("and record_gpu_debug is called from halide_print.\n");
83  return -1;
84  }
85  return 0;
86  }
87 };
88 
89 } // namespace Internal
90 } // namespace Halide
91 
92 #endif
int validate_gpu_object_lifetime(bool allow_globals, bool allow_none, int max_globals)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
const char * strstr(const char *, const char *)