Debian Codegraph @

rts/c/context.h

raw
1 // Start of context.h
2
3 // Internal functions.
4
5 static void set_error(struct futhark_context* ctx, char *error) {
6 lock_lock(&ctx->error_lock);
7 if (ctx->error == NULL) {
8 ctx->error = error;
9 } else {
10 free(error);
11 }
12 lock_unlock(&ctx->error_lock);
13 }
14
15 // XXX: should be static, but used in ispc_util.h
16 void lexical_realloc_error(struct futhark_context* ctx, size_t new_size) {
17 set_error(ctx,
18 msgprintf("Failed to allocate memory.\nAttempted allocation: %12lld bytes\n",
19 (long long) new_size));
20 }
21
22 static int lexical_realloc(struct futhark_context *ctx,
23 unsigned char **ptr,
24 int64_t *old_size,
25 int64_t new_size) {
26 unsigned char *new = realloc(*ptr, (size_t)new_size);
27 if (new == NULL) {
28 lexical_realloc_error(ctx, new_size);
29 return FUTHARK_OUT_OF_MEMORY;
30 } else {
31 *ptr = new;
32 *old_size = new_size;
33 return FUTHARK_SUCCESS;
34 }
35 }
36
37 static void free_all_in_free_list(struct futhark_context* ctx) {
38 fl_mem mem;
39 free_list_pack(&ctx->free_list);
40 while (free_list_first(&ctx->free_list, (fl_mem*)&mem) == 0) {
41 free((void*)mem);
42 }
43 }
44
45 static int is_small_alloc(size_t size) {
46 return size < 1024*1024;
47 }
48
49 static void host_alloc(struct futhark_context* ctx,
50 size_t size, const char* tag, size_t* size_out, void** mem_out) {
51 if (is_small_alloc(size) || free_list_find(&ctx->free_list, size, tag, size_out, (fl_mem*)mem_out) != 0) {
52 *size_out = size;
53 *mem_out = malloc(size);
54 }
55 }
56
57 static void host_free(struct futhark_context* ctx,
58 size_t size, const char* tag, void* mem) {
59 // Small allocations are handled by malloc()s own free list. The
60 // threshold here is kind of arbitrary, but seems to work OK.
61 // Larger allocations are mmap()ed/munmapped() every time, which is
62 // very slow, and Futhark programs tend to use a few very large
63 // allocations.
64 if (is_small_alloc(size)) {
65 free(mem);
66 } else {
67 free_list_insert(&ctx->free_list, size, (fl_mem)mem, tag);
68 }
69 }
70
71 static void add_event(struct futhark_context* ctx,
72 const char* name,
73 const char* provenance,
74 struct kvs *kvs,
75 void* data,
76 event_report_fn f) {
77 if (provenance == NULL) {
78 provenance = "unknown";
79 }
80 if (ctx->logging) {
81 fprintf(ctx->log, "Event: %s\n at: %s\n", name, provenance);
82 if (kvs) {
83 kvs_log(kvs, " ", ctx->log);
84 }
85 }
86 add_event_to_list(&ctx->event_list, name, provenance, kvs, data, f);
87 }
88
89 char *futhark_context_get_error(struct futhark_context *ctx) {
90 char *error = ctx->error;
91 ctx->error = NULL;
92 return error;
93 }
94
95 void futhark_context_config_set_debugging(struct futhark_context_config *cfg, int flag) {
96 cfg->profiling = cfg->logging = cfg->debugging = flag;
97 }
98
99 void futhark_context_config_set_profiling(struct futhark_context_config *cfg, int flag) {
100 cfg->profiling = flag;
101 }
102
103 void futhark_context_config_set_logging(struct futhark_context_config *cfg, int flag) {
104 cfg->logging = flag;
105 }
106
107 void futhark_context_config_set_cache_file(struct futhark_context_config *cfg, const char *f) {
108 cfg->cache_fname = strdup(f);
109 }
110
111 int futhark_get_tuning_param_count(void) {
112 return num_tuning_params;
113 }
114
115 const char *futhark_get_tuning_param_name(int i) {
116 return tuning_param_names[i];
117 }
118
119 const char *futhark_get_tuning_param_class(int i) {
120 return tuning_param_classes[i];
121 }
122
123 void futhark_context_set_logging_file(struct futhark_context *ctx, FILE *f){
124 ctx->log = f;
125 }
126
127 void futhark_context_pause_profiling(struct futhark_context *ctx) {
128 ctx->profiling_paused = 1;
129 }
130
131 void futhark_context_unpause_profiling(struct futhark_context *ctx) {
132 ctx->profiling_paused = 0;
133 }
134
135 struct futhark_context_config* futhark_context_config_new(void) {
136 struct futhark_context_config* cfg = malloc(sizeof(struct futhark_context_config));
137 if (cfg == NULL) {
138 return NULL;
139 }
140 cfg->in_use = 0;
141 cfg->debugging = 0;
142 cfg->profiling = 0;
143 cfg->logging = 0;
144 cfg->cache_fname = NULL;
145 cfg->num_tuning_params = num_tuning_params;
146 cfg->tuning_params = malloc(cfg->num_tuning_params * sizeof(int64_t));
147 memcpy(cfg->tuning_params, tuning_param_defaults,
148 cfg->num_tuning_params * sizeof(int64_t));
149 cfg->tuning_param_names = tuning_param_names;
150 cfg->tuning_param_vars = tuning_param_vars;
151 cfg->tuning_param_classes = tuning_param_classes;
152 backend_context_config_setup(cfg);
153 return cfg;
154 }
155
156 void futhark_context_config_free(struct futhark_context_config* cfg) {
157 assert(!cfg->in_use);
158 backend_context_config_teardown(cfg);
159 free(cfg->cache_fname);
160 free(cfg->tuning_params);
161 free(cfg);
162 }
163
164 struct futhark_context* futhark_context_new(struct futhark_context_config* cfg) {
165 struct futhark_context* ctx = malloc(sizeof(struct futhark_context));
166 if (ctx == NULL) {
167 return NULL;
168 }
169 assert(!cfg->in_use);
170 ctx->cfg = cfg;
171 ctx->cfg->in_use = 1;
172 ctx->program_initialised = false;
173 create_lock(&ctx->error_lock);
174 create_lock(&ctx->lock);
175 free_list_init(&ctx->free_list);
176 event_list_init(&ctx->event_list);
177 ctx->peak_mem_usage_default = 0;
178 ctx->cur_mem_usage_default = 0;
179 ctx->constants = malloc(sizeof(struct constants));
180 ctx->debugging = cfg->debugging;
181 ctx->logging = cfg->logging;
182 ctx->detail_memory = cfg->logging;
183 ctx->profiling = cfg->profiling;
184 ctx->profiling_paused = 0;
185 ctx->error = NULL;
186 ctx->log = stderr;
187 set_tuning_params(ctx);
188 if (backend_context_setup(ctx) == 0) {
189 setup_program(ctx);
190 init_constants(ctx);
191 ctx->program_initialised = true;
192 (void)futhark_context_clear_caches(ctx);
193 (void)futhark_context_sync(ctx);
194 }
195 return ctx;
196 }
197
198 void futhark_context_free(struct futhark_context* ctx) {
199 if (ctx->program_initialised) {
200 free_constants(ctx);
201 teardown_program(ctx);
202 }
203 backend_context_teardown(ctx);
204 free_all_in_free_list(ctx);
205 free_list_destroy(&ctx->free_list);
206 event_list_free(&ctx->event_list);
207 free(ctx->constants);
208 free(ctx->error);
209 free_lock(&ctx->lock);
210 free_lock(&ctx->error_lock);
211 ctx->cfg->in_use = 0;
212 free(ctx);
213 }
214
215 // End of context.h