[go: up one dir, main page]

File: dag_node.c

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (458 lines) | stat: -rw-r--r-- 12,387 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
Copyright (C) 2014- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "dag.h"
#include "dag_node_footprint.h"
#include "dag_node.h"

#include "debug.h"
#include "batch_task.h"
#include "rmsummary.h"
#include "list.h"
#include "stringtools.h"
#include "xxmalloc.h"
#include "jx.h"
#include "jx_print.h"

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

extern char **environ; 

struct dag_node *dag_node_create(struct dag *d, int linenum)
{
	struct dag_node *n = calloc(1, sizeof(*n));

	n->d = d;
	n->linenum = linenum;
	n->state = DAG_NODE_STATE_WAITING;
	n->nodeid = d->nodeid_counter++;
	n->variables = hash_table_create(0, 0);

	n->type = DAG_NODE_TYPE_COMMAND;
	n->source_files = list_create();
	n->target_files = list_create();

	n->remote_names = itable_create(0);
	n->remote_names_inv = hash_table_create(0, 0);

	n->descendants = set_create(0);
	n->ancestors = set_create(0);

	n->ancestor_depth = -1;

	// resources explicitely requested for only this node in the dag file.
	// PROBABLY not what you want. Most likely you want dag_node_dynamic_label(n)
	n->resources_requested = rmsummary_create(-1);

	// the value of dag_node_dynamic_label(n) when this node was submitted.
	n->resources_allocated  = rmsummary_create(-1);

	// resources used by the node, as measured by the resource_monitor (if
	// using monitoring).
	n->resources_measured  = NULL;

	n->resource_request = CATEGORY_ALLOCATION_FIRST;

	// arguments for subworkflow nodes
	n->workflow_args = NULL;

	return n;
}

void dag_node_delete(struct dag_node *n)
{
	hash_table_delete(n->variables);

	itable_delete(n->remote_names);
	hash_table_delete(n->remote_names_inv);

	set_delete(n->descendants);
	set_delete(n->ancestors);

	list_delete(n->source_files);
	list_delete(n->target_files);

	if(n->footprint)
		dag_node_footprint_delete(n->footprint);

	rmsummary_delete(n->resources_requested);
	if(n->resources_measured)
		rmsummary_delete(n->resources_measured);


	jx_delete(n->workflow_args);
	free(n);
}

void dag_node_set_command(struct dag_node *n, const char *cmd) {
	assert(n);
	assert(cmd);
	assert(!n->command);
	assert(!n->workflow_file);

	n->type = DAG_NODE_TYPE_COMMAND;
	n->command = xxstrdup(cmd);
}

const char *dag_node_nested_workflow_filename(struct dag_node *n, const char *which_file) {
	static char filename[PATH_MAX];
	snprintf(filename, PATH_MAX, "%s.%d.%s", n->workflow_file, n->nodeid, which_file);
	return filename;
}

void dag_node_set_workflow(struct dag_node *n, const char *dag, struct jx * args, int is_jx )
{
	assert(n);
	assert(dag);
	assert(!n->workflow_file);

	n->type = DAG_NODE_TYPE_WORKFLOW;
	n->workflow_file = xxstrdup(dag);
	n->workflow_is_jx = is_jx;

	n->workflow_args = jx_copy(args);

	if(n->workflow_args) { 
		const char *args_file = dag_node_nested_workflow_filename(n, "args");
		FILE *file = fopen(args_file,"w");
		jx_print_stream(n->workflow_args,file);
		fclose(file);
	}

	/* Record a placeholder in the command field */
	/* A usable command will be created at submit time. */

	n->command = xxstrdup(n->workflow_file);
}

void dag_node_insert(struct dag_node *n) {
	assert(n);
	assert(n->d);

	n->next = n->d->nodes;
	n->d->nodes = n;
	itable_insert(n->d->node_table, n->nodeid, n);
}

const char *dag_node_state_name(dag_node_state_t state)
{
	switch (state) {
	case DAG_NODE_STATE_WAITING:
		return "waiting";
	case DAG_NODE_STATE_RUNNING:
		return "running";
	case DAG_NODE_STATE_COMPLETE:
		return "complete";
	case DAG_NODE_STATE_FAILED:
		return "failed";
	case DAG_NODE_STATE_ABORTED:
		return "aborted";
	default:
		return "unknown";
	}
}

/* Returns the remotename used in rule n for local name filename */
const char *dag_node_get_remote_name(struct dag_node *n, const char *filename)
{
	struct dag_file *f;
	char *name;

	f = dag_file_from_name(n->d, filename);
	name = (char *) itable_lookup(n->remote_names, (uintptr_t) f);

	return name;
}

/* Returns the local name of filename */
const char *dag_node_get_local_name(struct dag_node *n, const char *filename)
{
	struct dag_file *f;
	const char *name;

	f = hash_table_lookup(n->remote_names_inv, filename);

	if(!f)
	{
		name =  NULL;
	}
	else
	{
		name = f->filename;
	}

	return name;
}

void dag_node_set_umbrella_spec(struct dag_node *n, const char *umbrella_spec)
{
	struct stat st;

	if(!n) return;

	if(lstat(umbrella_spec, &st) == -1) {
		fatal("lstat(`%s`) failed: %s\n", umbrella_spec, strerror(errno));
	}
	if((st.st_mode & S_IFMT) != S_IFREG) {
		fatal("the umbrella spec (`%s`) should specify a regular file\n", umbrella_spec);
	}

	n->umbrella_spec = umbrella_spec;
}

/* Translate an absolute filename into a unique slash-less name to allow for the
   sending of any file to remote systems. The function allows for upto a million name collisions. */
static char *dag_node_translate_filename(struct dag_node *n, const char *filename)
{
	int len;
	char *newname_ptr;

	len = strlen(filename);

	/* If there are no slashes in path, then we don't need to translate. */
	if(!strchr(filename, '/')) {
		newname_ptr = xxstrdup(filename);
		return newname_ptr;
	}

	/* If the filename is in the current directory and doesn't contain any
	 * additional slashes, then we can also skip translation.
	 *
	 * Note: this doesn't handle redundant ./'s such as ./././././foo/bar */
	if(!strncmp(filename, "./", 2) && !strchr(filename + 2, '/')) {
		newname_ptr = xxstrdup(filename);
		return newname_ptr;
	}

	/* Make space for the new filename + a hyphen + a number to
	 * handle upto a million name collisions */
	newname_ptr = calloc(len + 8, sizeof(char));
	strcpy(newname_ptr, filename);

	char *c;
	for(c = newname_ptr; *c; ++c) {
		switch (*c) {
		case '/':
		case '.':
			*c = '_';
			break;
		default:
			break;
		}
	}

	if(!n)
		return newname_ptr;

	int i = 0;
	char *newname_org = xxstrdup(newname_ptr);
	while(hash_table_lookup(n->remote_names_inv, newname_ptr)) {
		sprintf(newname_ptr, "%06d-%s", i, newname_org);
		i++;
	}

	free(newname_org);

	return newname_ptr;
}

/* Adds remotename to the local name filename in the namespace of
 * the given node. If remotename is NULL, then a new name is
 * found using dag_node_translate_filename. If the remotename
 * given is different from a previosly specified, a warning is
 * written to the debug output, but otherwise this is ignored. */
static const char *dag_node_add_remote_name(struct dag_node *n, const char *filename, const char *remotename)
{
	char *oldname;
	struct dag_file *f = dag_file_from_name(n->d, filename);

	if(!f)
		fatal("trying to add remote name %s to unknown file %s.\n", remotename, filename);

	if(!remotename)
		remotename = dag_node_translate_filename(n, filename);
	else
		remotename = xxstrdup(remotename);

	oldname = hash_table_lookup(n->remote_names_inv, remotename);

	if(oldname && strcmp(oldname, filename) == 0)
		debug(D_MAKEFLOW_RUN, "Remote name %s for %s already in use for %s\n", remotename, filename, oldname);

	itable_insert(n->remote_names, (uintptr_t) f, remotename);
	hash_table_insert(n->remote_names_inv, remotename, (void *) f);

	return remotename;
}

/* Adds the local name to the list of source files of the node,
 * and adds the node as a dependant of the file. If remotename is
 * not NULL, it is added to the namespace of the node. */
void dag_node_add_source_file(struct dag_node *n, const char *filename, const char *remotename)
{
	struct dag_file *source = dag_file_lookup_or_create(n->d, filename);

	if(remotename)
		dag_node_add_remote_name(n, filename, remotename);

	/* register this file as a source of the node */
	list_push_head(n->source_files, source);

	/* register this file as a requirement of the node */
	list_push_head(source->needed_by, n);

	source->reference_count++;
}

/* Adds the local name as a target of the node, and register the
 * node as the producer of the file. If remotename is not NULL,
 * it is added to the namespace of the node. */
void dag_node_add_target_file(struct dag_node *n, const char *filename, const char *remotename)
{
	struct dag_file *target = dag_file_lookup_or_create(n->d, filename);

	if(target->created_by && target->created_by != n)
		fatal("%s is defined multiple times at %s:%d and %s:%d\n", filename, filename, target->created_by->linenum, filename, n->linenum);

	if(remotename)
		dag_node_add_remote_name(n, filename, remotename);

	/* register this file as a target of the node */
	list_push_head(n->target_files, target);

	/* register this node as the creator of the file */
	target->created_by = n;
}

void dag_node_print_debug_resources(struct dag_node *n)
{
	const struct rmsummary *r = dag_node_dynamic_label(n);

	if(!r)
		return;

	if( r->cores > -1 )
		debug(D_MAKEFLOW_RUN, "cores:  %s\n", rmsummary_resource_to_str("cores", r->cores, 0));
	if( r->memory > -1 )
		debug(D_MAKEFLOW_RUN, "memory: %s\n", rmsummary_resource_to_str("memory", r->memory, 1));
	if( r->disk > -1 )
		debug(D_MAKEFLOW_RUN, "disk:   %s\n", rmsummary_resource_to_str("disk", r->disk, 0));
	if( r->gpus > -1 )
		debug(D_MAKEFLOW_RUN, "gpus:   %s\n", rmsummary_resource_to_str("gpus", r->gpus, 1));
}

void dag_node_add_local_environment(struct jx *j) {

		char **var;
		for(var = environ; *var; var++) {
			char *name   = xxstrdup(*var);
			char *value  = strchr(name, '=');

			if(value) {
				*value = '\0'; 
				value++;
			} else {
				value = "";
			}

			jx_insert(j, jx_string(name), jx_string(value));

			free(name);
		}
}

/*
Creates a jx object containing the explicit environment
strings for this given node.
*/

struct jx * dag_node_env_create( struct dag *d, struct dag_node *n, int should_send_all_local_environment )
{
	struct dag_variable_lookup_set s = { d, n->category, n, NULL };
	char *key;

	struct jx *object = jx_object(0);

	if(should_send_all_local_environment) {
		dag_node_add_local_environment(object);
	}

	char *num_cores = dag_variable_lookup_string(RESOURCES_CORES, &s);
	char *num_omp_threads = dag_variable_lookup_string("OMP_NUM_THREADS", &s);

	if (num_cores && !num_omp_threads) {
		// if number of cores is set, number of omp threads is not set,
		// then we set number of omp threads to number of cores
		jx_insert(object, jx_string("OMP_NUM_THREADS"), jx_string(num_cores));
	} else if (num_omp_threads) {
		// if number of omp threads is set, then we set number of cores
		// to the number of omp threads
		jx_insert(object, jx_string(RESOURCES_CORES), jx_string(num_omp_threads));
	} else {
		// if both number of cores and omp threads are not set, we
		// set them to 1
		jx_insert(object, jx_string("OMP_NUM_THREADS"), jx_string("1"));
		jx_insert(object, jx_string(RESOURCES_CORES), jx_string("1"));
	}

	string_set_first_element(d->export_vars);
	while(string_set_next_element(d->export_vars, &key)) {
		char *value = dag_variable_lookup_string(key, &s);
		if(value) {
			jx_insert(object,jx_string(key),jx_string(value));
			debug(D_MAKEFLOW_RUN, "export %s=%s", key, value);
			free(value);
		}
	}

	free(num_cores);
	free(num_omp_threads);

	return object;
}

/* Return resources according to request. */

const struct rmsummary *dag_node_dynamic_label(const struct dag_node *n) {
	return category_dynamic_task_max_resources(n->category, n->resources_requested, n->resource_request);
}

/* Return JX object containing cmd, inputs, outputs, env, and resources. */

struct jx * dag_node_to_jx( struct dag *d, struct dag_node *n, int send_all_local_env)
{
	struct jx *task = jx_object(0);

	jx_insert(task, jx_string("resources"), rmsummary_to_json(dag_node_dynamic_label(n), 1));
	jx_insert(task, jx_string("category"), jx_string(n->category->name));
	jx_insert(task, jx_string("environment"), dag_node_env_create(d, n, send_all_local_env));

	struct dag_file *f = NULL;

	struct jx *outputs = jx_array(0);
	list_first_item(n->target_files);
	while((f = list_next_item(n->target_files))) {
		jx_array_insert(outputs, dag_file_to_jx(f, n));
	}
	jx_insert(task, jx_string("outputs"), outputs);

	struct jx *inputs = jx_array(0);
	list_first_item(n->source_files);
	while((f = list_next_item(n->source_files))) {
		jx_array_insert(inputs, dag_file_to_jx(f, n));
	}
	jx_insert(task, jx_string("inputs"), inputs);

	jx_insert(task, jx_string("command"), jx_string(n->command));

	return task;
}

/* vim: set noexpandtab tabstop=4: */