StarPU Internal Handbook
utils.h
Go to the documentation of this file.
1 /* StarPU --- Runtime system for heterogeneous multicore architectures.
2  *
3  * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
4  *
5  * StarPU is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or (at
8  * your option) any later version.
9  *
10  * StarPU is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
15  */
16 
17 #ifndef __COMMON_UTILS_H__
18 #define __COMMON_UTILS_H__
19 
22 #include <common/config.h>
23 #include <starpu.h>
24 #include <sys/stat.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #ifdef STARPU_HAVE_SCHED_YIELD
29 #include <sched.h>
30 #endif
31 
32 #ifdef STARPU_HAVE_HELGRIND_H
33 #include <valgrind/helgrind.h>
34 #endif
35 
36 #ifndef DO_CREQ_v_WW
37 #define DO_CREQ_v_WW(_creqF, _ty1F, _arg1F, _ty2F, _arg2F) ((void)0)
38 #endif
39 #ifndef DO_CREQ_v_W
40 #define DO_CREQ_v_W(_creqF, _ty1F, _arg1F) ((void)0)
41 #endif
42 #ifndef ANNOTATE_HAPPENS_BEFORE
43 #define ANNOTATE_HAPPENS_BEFORE(obj) ((void)0)
44 #endif
45 #ifndef ANNOTATE_HAPPENS_BEFORE_FORGET_ALL
46 #define ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(obj) ((void)0)
47 #endif
48 #ifndef ANNOTATE_HAPPENS_AFTER
49 #define ANNOTATE_HAPPENS_AFTER(obj) ((void)0)
50 #endif
51 #ifndef VALGRIND_HG_DISABLE_CHECKING
52 #define VALGRIND_HG_DISABLE_CHECKING(start, len) ((void)0)
53 #endif
54 #ifndef VALGRIND_HG_ENABLE_CHECKING
55 #define VALGRIND_HG_ENABLE_CHECKING(start, len) ((void)0)
56 #endif
57 #ifndef VALGRIND_STACK_REGISTER
58 #define VALGRIND_STACK_REGISTER(stackbottom, stacktop) 0
59 #endif
60 #ifndef VALGRIND_STACK_DEREGISTER
61 #define VALGRIND_STACK_DEREGISTER(id) ((void)0)
62 #endif
63 #ifndef RUNNING_ON_VALGRIND
64 #define RUNNING_ON_VALGRIND 0
65 #endif
66 #ifdef STARPU_SANITIZE_THREAD
67 #define STARPU_RUNNING_ON_VALGRIND 1
68 #else
69 #define STARPU_RUNNING_ON_VALGRIND RUNNING_ON_VALGRIND
70 #endif
71 #define STARPU_HG_DISABLE_CHECKING(variable) VALGRIND_HG_DISABLE_CHECKING(&(variable), sizeof(variable))
72 #define STARPU_HG_ENABLE_CHECKING(variable) VALGRIND_HG_ENABLE_CHECKING(&(variable), sizeof(variable))
73 
74 #if defined(__KNC__) || defined(__KNF__)
75 #define STARPU_DEBUG_PREFIX "[starpu-mic]"
76 #else
77 #define STARPU_DEBUG_PREFIX "[starpu]"
78 #endif
79 
80 /* This is needed in some places to make valgrind yield to another thread to be
81  * able to progress. */
82 #if defined(__i386__) || defined(__x86_64__)
83 #define _STARPU_UYIELD() __asm__ __volatile("rep; nop")
84 #else
85 #define _STARPU_UYIELD() ((void)0)
86 #endif
87 #if defined(STARPU_HAVE_SCHED_YIELD) && defined(STARPU_HAVE_HELGRIND_H)
88 #define STARPU_UYIELD() do { if (STARPU_RUNNING_ON_VALGRIND) sched_yield(); else _STARPU_UYIELD(); } while (0)
89 #else
90 #define STARPU_UYIELD() _STARPU_UYIELD()
91 #endif
92 
93 #ifdef STARPU_VERBOSE
94 # define _STARPU_DEBUG(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); fflush(stderr); }} while(0)
95 # define _STARPU_DEBUG_NO_HEADER(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, fmt , ## __VA_ARGS__); fflush(stderr); }} while(0)
96 #else
97 # define _STARPU_DEBUG(fmt, ...) do { } while (0)
98 # define _STARPU_DEBUG_NO_HEADER(fmt, ...) do { } while (0)
99 #endif
100 
101 #ifdef STARPU_EXTRA_VERBOSE
102 # define _STARPU_EXTRA_DEBUG(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); fflush(stderr); }} while(0)
103 #else
104 # define _STARPU_EXTRA_DEBUG(fmt, ...) do { } while (0)
105 #endif
106 
107 #ifdef STARPU_EXTRA_VERBOSE
108 # define _STARPU_LOG_IN() do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%ld][%s:%s@%d] -->\n", starpu_pthread_self(), __starpu_func__,__FILE__, __LINE__); }} while(0)
109 # define _STARPU_LOG_OUT() do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%ld][%s:%s@%d] <--\n", starpu_pthread_self(), __starpu_func__, __FILE__, __LINE__); }} while(0)
110 # define _STARPU_LOG_OUT_TAG(outtag) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%ld][%s:%s@%d] <-- (%s)\n", starpu_pthread_self(), __starpu_func__, __FILE__, __LINE__, outtag); }} while(0)
111 #else
112 # define _STARPU_LOG_IN()
113 # define _STARPU_LOG_OUT()
114 # define _STARPU_LOG_OUT_TAG(outtag)
115 #endif
116 
117 /* TODO: cache */
118 #define _STARPU_MSG(fmt, ...) do { fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); } while(0)
119 #define _STARPU_DISP(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); }} while(0)
120 #define _STARPU_ERROR(fmt, ...) \
121  do { \
122  fprintf(stderr, "\n\n[starpu][%s] Error: " fmt ,__starpu_func__ ,## __VA_ARGS__); \
123  fprintf(stderr, "\n\n"); \
124  STARPU_ABORT(); \
125  } while (0)
126 
127 
128 #ifdef _MSC_VER
129 # if defined(__cplusplus)
130 # define _STARPU_DECLTYPE(x) (decltype(x))
131 # else
132 # define _STARPU_DECLTYPE(x)
133 # endif
134 #else
135 # define _STARPU_DECLTYPE(x) (__typeof(x))
136 #endif
137 
138 #define _STARPU_MALLOC(ptr, size) do { ptr = _STARPU_DECLTYPE(ptr) malloc(size); STARPU_ASSERT_MSG(ptr != NULL || size == 0, "Cannot allocate %ld bytes\n", (long) (size)); } while (0)
139 #define _STARPU_CALLOC(ptr, nmemb, size) do { ptr = _STARPU_DECLTYPE(ptr) calloc(nmemb, size); STARPU_ASSERT_MSG(ptr != NULL || size == 0, "Cannot allocate %ld bytes\n", (long) (nmemb*size)); } while (0)
140 #define _STARPU_REALLOC(ptr, size) do { void *_new_ptr = realloc(ptr, size); STARPU_ASSERT_MSG(_new_ptr != NULL || size == 0, "Cannot reallocate %ld bytes\n", (long) (size)); ptr = _STARPU_DECLTYPE(ptr) _new_ptr;} while (0)
141 
142 #ifdef _MSC_VER
143 #define _STARPU_IS_ZERO(a) (a == 0.0)
144 #else
145 #define _STARPU_IS_ZERO(a) (fpclassify(a) == FP_ZERO)
146 #endif
147 
148 char *_starpu_mkdtemp_internal(char *tmpl);
149 char *_starpu_mkdtemp(char *tmpl);
150 int _starpu_mkpath(const char *s, mode_t mode);
151 void _starpu_mkpath_and_check(const char *s, mode_t mode);
152 char *_starpu_mktemp(const char *directory, int flags, int *fd);
153 /* This version creates a hierarchy of n temporary directories, useful when
154  * creating a lot of temporary files to be stored in the same place */
155 char *_starpu_mktemp_many(const char *directory, int depth, int flags, int *fd);
156 void _starpu_rmtemp_many(char *path, int depth);
157 void _starpu_rmdir_many(char *path, int depth);
158 int _starpu_fftruncate(FILE *file, size_t length);
159 int _starpu_ftruncate(int fd, size_t length);
160 int _starpu_frdlock(FILE *file);
161 int _starpu_frdunlock(FILE *file);
162 int _starpu_fwrlock(FILE *file);
163 int _starpu_fwrunlock(FILE *file);
164 char *_starpu_get_home_path(void);
165 void _starpu_gethostname(char *hostname, size_t size);
166 
168 void _starpu_drop_comments(FILE *f);
169 
170 struct _starpu_job;
174 const char *_starpu_job_get_task_name(struct _starpu_job *j);
175 
176 struct starpu_codelet;
178 const char *_starpu_codelet_get_model_name(struct starpu_codelet *cl);
179 
180 int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex);
181 
182 void _starpu_util_init(void);
183 
184 #endif // __COMMON_UTILS_H__
_starpu_drop_comments
void _starpu_drop_comments(FILE *f)
_starpu_job_get_task_name
const char * _starpu_job_get_task_name(struct _starpu_job *j)
_starpu_codelet_get_model_name
const char * _starpu_codelet_get_model_name(struct starpu_codelet *cl)
_starpu_job
Definition: jobs.h:79
_starpu_job_get_model_name
const char * _starpu_job_get_model_name(struct _starpu_job *j)