forked from open-mpi/mpi-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tst_output.c
168 lines (142 loc) · 4.03 KB
/
tst_output.c
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
/**
* \todo Multiple outputs at the same time
* \todo Rank specific output
* \todo Macros for different output types
*/
#define _TST_OUTPUT_C_SRC
#include "tst_output.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <mpi.h>
#include "mpi_test_suite.h"
/****************************************************************************/
/** **/
/** GLOBAL VARIABLES **/
/** **/
/****************************************************************************/
static int tst_output_global_rank;
/* Corresponding strings to values in enum tst_report_types. */
const char * tst_reports[] = {
"Summary",
"Run",
"Full",
"Max"
};
#ifdef HAVE_MPI2_THREADS
extern int tst_thread_running (void);
#endif
/****************************************************************************/
/** **/
/** EXPORTED FUNCTIONS **/
/** **/
/****************************************************************************/
int tst_output_set_level(tst_output_stream * output, tst_report_types level) {
#ifdef HAVE_MPI2_THREADS
if (tst_thread_running() && tst_thread_get_num() != 0) {
return 0;
}
#endif
output->level = level;
return level;
}
tst_output_types tst_output_init(tst_output_stream * output, int rank,
tst_report_types level, tst_output_types type, ...) {
va_list arglist;
char * fname;
int freelen;
MPI_Comm_rank (MPI_COMM_WORLD, &tst_output_global_rank);
#ifdef HAVE_MPI2_THREADS
{
if (tst_thread_running()) {
if (tst_thread_get_num() != 0) {
return output->type;
}
}
}
#endif
/* Now do the initialisation for the different stream types */
switch (type) {
case TST_OUTPUT_TYPE_STDERR:
output->streamptr = stderr;
break;
case TST_OUTPUT_TYPE_STDOUT:
output->streamptr = stdout;
break;
case TST_OUTPUT_TYPE_LOGFILE:
va_start(arglist, type);
fname = va_arg (arglist, char *);
snprintf(output->filename, TST_OUTPUT_FILENAME_MAX, "R%d_%s",
tst_output_global_rank, fname);
va_end (arglist);
output->streamptr = fopen(output->filename, "w+");
if (output->streamptr == NULL) {
fprintf (stderr, "Error opening stream: Could not open output file.");
return 0;
}
break;
default:
fprintf (stderr, "Error opening stream: Unknown stream type (%i).", type);
return TST_OUTPUT_TYPE_NONE;
break;
}
/* set the rest of the info of the stream */
output->type = type;
output->level = level;
output->rank = rank;
output->isopen = 1;
return output->type;
}
int tst_output_close(tst_output_stream * output) {
#ifdef HAVE_MPI2_THREADS
{
if (tst_thread_running()) {
if (tst_thread_get_num() != 0) {
return 0;
}
}
}
#endif
if (output->isopen) {
switch (output->type) {
case TST_OUTPUT_TYPE_LOGFILE:
strcpy (output->filename,"");
fclose (output->streamptr);
break;
default:
break;
}
output->streamptr = NULL;
output->type = TST_OUTPUT_TYPE_NONE;
output->level = 0;
output->isopen = 0;
}
else {
return 0;
}
return 1;
}
int tst_output_printf(tst_output_stream * output,
tst_report_types error_level, char * format, ...) {
int count;
va_list arglist;
#ifdef HAVE_MPI2_THREADS
{
if (tst_thread_running()) {
if (tst_thread_get_num() != 0) {
return 0;
}
}
}
#endif
if ((output->isopen == 1) && (output->rank == tst_output_global_rank) && (error_level <= output->level)) {
va_start(arglist, format);
count = vfprintf (output->streamptr, format, arglist);
fflush (output->streamptr);
va_end(arglist);
}
else {
count = 0;
}
return count;
}