| 1 |
// Start of backends/multicore.h |
| 2 |
|
| 3 |
struct futhark_context_config { |
| 4 |
int in_use; |
| 5 |
int debugging; |
| 6 |
int profiling; |
| 7 |
int logging; |
| 8 |
char *cache_fname; |
| 9 |
int num_tuning_params; |
| 10 |
int64_t *tuning_params; |
| 11 |
const char** tuning_param_names; |
| 12 |
const char** tuning_param_vars; |
| 13 |
const char** tuning_param_classes; |
| 14 |
// Uniform fields above. |
| 15 |
|
| 16 |
int num_threads; |
| 17 |
}; |
| 18 |
|
| 19 |
static void backend_context_config_setup(struct futhark_context_config* cfg) { |
| 20 |
cfg->num_threads = 0; |
| 21 |
} |
| 22 |
|
| 23 |
static void backend_context_config_teardown(struct futhark_context_config* cfg) { |
| 24 |
(void)cfg; |
| 25 |
} |
| 26 |
|
| 27 |
void futhark_context_config_set_num_threads(struct futhark_context_config *cfg, int n) { |
| 28 |
cfg->num_threads = n; |
| 29 |
} |
| 30 |
|
| 31 |
int futhark_context_config_set_tuning_param(struct futhark_context_config* cfg, const char *param_name, size_t param_value) { |
| 32 |
(void)cfg; (void)param_name; (void)param_value; |
| 33 |
return 1; |
| 34 |
} |
| 35 |
|
| 36 |
struct futhark_context { |
| 37 |
struct futhark_context_config* cfg; |
| 38 |
int detail_memory; |
| 39 |
int debugging; |
| 40 |
int profiling; |
| 41 |
int profiling_paused; |
| 42 |
int logging; |
| 43 |
lock_t lock; |
| 44 |
char *error; |
| 45 |
lock_t error_lock; |
| 46 |
FILE *log; |
| 47 |
struct constants *constants; |
| 48 |
struct free_list free_list; |
| 49 |
struct event_list event_list; |
| 50 |
int64_t peak_mem_usage_default; |
| 51 |
int64_t cur_mem_usage_default; |
| 52 |
struct program* program; |
| 53 |
bool program_initialised; |
| 54 |
// Uniform fields above. |
| 55 |
|
| 56 |
lock_t event_list_lock; |
| 57 |
struct scheduler scheduler; |
| 58 |
int total_runs; |
| 59 |
long int total_runtime; |
| 60 |
int64_t tuning_timing; |
| 61 |
int64_t tuning_iter; |
| 62 |
}; |
| 63 |
|
| 64 |
int backend_context_setup(struct futhark_context* ctx) { |
| 65 |
// Initialize rand() |
| 66 |
fast_srand(time(0)); |
| 67 |
|
| 68 |
int tune_kappa = 0; |
| 69 |
double kappa = 5.1f * 1000; |
| 70 |
|
| 71 |
if (tune_kappa) { |
| 72 |
if (determine_kappa(&kappa) != 0) { |
| 73 |
ctx->error = strdup("Failed to determine kappa."); |
| 74 |
return 1; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (scheduler_init(&ctx->scheduler, |
| 79 |
ctx->cfg->num_threads > 0 ? |
| 80 |
ctx->cfg->num_threads : num_processors(), |
| 81 |
kappa) != 0) { |
| 82 |
ctx->error = strdup("Failed to initialise scheduler."); |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
create_lock(&ctx->event_list_lock); |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
void backend_context_teardown(struct futhark_context* ctx) { |
| 92 |
(void)scheduler_destroy(&ctx->scheduler); |
| 93 |
free_lock(&ctx->event_list_lock); |
| 94 |
} |
| 95 |
|
| 96 |
int futhark_context_sync(struct futhark_context* ctx) { |
| 97 |
(void)ctx; |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
struct mc_event { |
| 102 |
// Time in microseconds. |
| 103 |
uint64_t bef, aft; |
| 104 |
}; |
| 105 |
|
| 106 |
static struct mc_event* mc_event_new(struct futhark_context* ctx) { |
| 107 |
if (ctx->profiling && !ctx->profiling_paused) { |
| 108 |
struct mc_event* e = malloc(sizeof(struct mc_event)); |
| 109 |
return e; |
| 110 |
} else { |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
static int mc_event_report(struct str_builder* sb, struct mc_event* e) { |
| 116 |
float ms = e->aft - e->bef; |
| 117 |
str_builder(sb, ",\"duration\":%f", ms); |
| 118 |
free(e); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
// End of backends/multicore.h |