test_common.c 8.55 KB
Newer Older
Andy Cedilnik's avatar
Andy Cedilnik committed
1 2
/* -*- c -*- *******************************************************/
/*
3
 * Copyright (C) 2015 Sandia Corporation
4 5 6 7
 * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
 * the U.S. Government retains certain rights in this software.
 *
 * This source code is released under the New BSD License.
Andy Cedilnik's avatar
Andy Cedilnik committed
8 9
 */

10
#include "test_util.h"
11
#include "test_codes.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
12

13 14
#include <IceTDevCommunication.h>

15
#ifndef __USE_POSIX
16
#define __USE_POSIX
17
#endif
18

Andy Cedilnik's avatar
Andy Cedilnik committed
19 20 21 22 23 24 25 26 27 28 29 30 31
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifndef WIN32
#include <unistd.h>
#else
#include <io.h>
32 33
#define dup(fildes)             _dup(fildes)
#define dup2(fildes, fildes2)   _dup2(fildes, fildes2)
Andy Cedilnik's avatar
Andy Cedilnik committed
34 35
#endif

36
IceTEnum strategy_list[5];
Andy Cedilnik's avatar
Andy Cedilnik committed
37 38 39
int STRATEGY_LIST_SIZE = 5;
/* int STRATEGY_LIST_SIZE = 1; */

40
IceTEnum single_image_strategy_list[5];
41
int SINGLE_IMAGE_STRATEGY_LIST_SIZE = 5;
42 43
/* int SINGLE_IMAGE_STRATEGY_LIST_SIZE = 1; */

44 45
IceTSizeType SCREEN_WIDTH;
IceTSizeType SCREEN_HEIGHT;
Andy Cedilnik's avatar
Andy Cedilnik committed
46 47 48

static void checkIceTError(void)
{
49
    IceTEnum error = icetGetError();
Kenneth Moreland's avatar
Kenneth Moreland committed
50 51

#define CASE_ERROR(ename)                                               \
52
    case ename: printrank("## Current IceT error = " #ename "\n"); break;
Kenneth Moreland's avatar
Kenneth Moreland committed
53 54

    switch (error) {
55
      case ICET_NO_ERROR: break;
Kenneth Moreland's avatar
Kenneth Moreland committed
56 57 58 59 60 61 62
      CASE_ERROR(ICET_SANITY_CHECK_FAIL);
      CASE_ERROR(ICET_INVALID_ENUM);
      CASE_ERROR(ICET_BAD_CAST);
      CASE_ERROR(ICET_OUT_OF_MEMORY);
      CASE_ERROR(ICET_INVALID_OPERATION);
      CASE_ERROR(ICET_INVALID_VALUE);
      default:
63
          printrank("## UNKNOWN ICET ERROR CODE!!!!!\n");
Kenneth Moreland's avatar
Kenneth Moreland committed
64
          break;
Andy Cedilnik's avatar
Andy Cedilnik committed
65
    }
Kenneth Moreland's avatar
Kenneth Moreland committed
66 67

#undef CASE_ERROR
Andy Cedilnik's avatar
Andy Cedilnik committed
68 69 70 71 72
}

/* Just in case I need to actually print stuff out to the screen in the
   future. */
static FILE *realstdout;
73
#if 0
Andy Cedilnik's avatar
Andy Cedilnik committed
74 75 76 77 78
static void realprintf(const char *fmt, ...)
{
    va_list ap;

    if (realstdout != NULL) {
79 80 81 82
        va_start(ap, fmt);
        vfprintf(realstdout, fmt, ap);
        va_end(ap);
        fflush(realstdout);
Andy Cedilnik's avatar
Andy Cedilnik committed
83 84
    }
}
85
#endif
Andy Cedilnik's avatar
Andy Cedilnik committed
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
void printstat(const char *fmt, ...)
{
    va_list ap;
    IceTInt rank;

    icetGetIntegerv(ICET_RANK, &rank);

    if ((rank == 0) || (realstdout == NULL)) {
        va_start(ap, fmt);
        vprintf(fmt, ap);
        va_end(ap);
        fflush(stdout);
    }
}

void printrank(const char *fmt, ...)
{
    va_list ap;
    IceTInt rank;

    icetGetIntegerv(ICET_RANK, &rank);

    printf("%d> ", rank);
    va_start(ap, fmt);
    vprintf(fmt, ap);
    va_end(ap);
    fflush(stdout);
}

Andy Cedilnik's avatar
Andy Cedilnik committed
116 117 118 119 120 121 122 123 124 125
static IceTContext context;

static void usage(char **argv)
{
    printf("\nUSAGE: %s [options] [-R] testname [testargs]\n", argv[0]);
    printf("\nWhere options are:\n");
    printf("  -width <n>  Width of window (default n=1024)\n");
    printf("  -height <n> Height of window (default n=768).\n");
    printf("  -display <display>\n");
    printf("              X server each node contacts.  Default display=localhost:0\n");
126 127
    printf("  -logdebug   Add debugging statements.  Provides more information, but\n");
    printf("              makes identifying errors, warnings, and statuses harder\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
128 129
    printf("  -redirect   Redirect standard output to log.????, where ???? is the rank\n");
    printf("  --          Parse no more arguments.\n");
130
    printf("  -h, -help   This help message.\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
131 132
}

133
extern void initialize_render_window(int width, int height);
Andy Cedilnik's avatar
Andy Cedilnik committed
134 135 136 137 138 139 140
void initialize_test(int *argcp, char ***argvp, IceTCommunicator comm)
{
    int arg;
    int argc = *argcp;
    char **argv = *argvp;
    int width = 1024;
    int height = 768;
141
    IceTBitField diag_level = ICET_DIAG_ALL_NODES | ICET_DIAG_WARNINGS;
Andy Cedilnik's avatar
Andy Cedilnik committed
142 143 144 145 146 147
    int redirect = 0;
    int rank, num_proc;

    rank = (*comm->Comm_rank)(comm);
    num_proc = (*comm->Comm_size)(comm);

148 149
  /* This is convenience code to attach a debugger to a particular process at
     the start of a test. */
150 151 152 153 154 155 156
#if 0
    if (rank == 0) {
        int i = 0;
        printf("Waiting in process %d\n", getpid());
        while (i == 0) sleep(1);
    }
#endif
157

Andy Cedilnik's avatar
Andy Cedilnik committed
158 159
  /* Parse my arguments. */
    for (arg = 1; arg < argc; arg++) {
160 161 162 163
        if (strcmp(argv[arg], "-width") == 0) {
            width = atoi(argv[++arg]);
        } else if (strcmp(argv[arg], "-height") == 0) {
            height = atoi(argv[++arg]);
164
        } else if (strcmp(argv[arg], "-logdebug") == 0) {
165
            diag_level = ICET_DIAG_FULL;
166 167
        } else if (strcmp(argv[arg], "-redirect") == 0) {
            redirect = 1;
168 169
        } else if (   (strcmp(argv[arg], "-h") == 0)
                   || (strcmp(argv[arg], "-help") == 0) ) {
170 171 172 173 174 175 176 177 178 179 180 181
            usage(argv);
            exit(0);
        } else if (   (strcmp(argv[arg], "-R") == 0)
                   || (strncmp(argv[arg], "-", 1) != 0) ) {
            break;
        } else if (strcmp(argv[arg], "--") == 0) {
            arg++;
            break;
        } else {
            printf("Unknown options `%s'.  Try -h for help.\n", argv[arg]);
            exit(1);
        }
Andy Cedilnik's avatar
Andy Cedilnik committed
182 183 184 185 186
    }

  /* Fix arguments for next bout of parsing. */
    *argcp = 1;
    for ( ; arg < argc; arg++, (*argcp)++) {
187
        argv[*argcp] = argv[arg];
Andy Cedilnik's avatar
Andy Cedilnik committed
188 189 190 191 192
    }
    argc = *argcp;

  /* Make sure selected options are consistent. */
    if ((num_proc > 1) && (argc < 2)) {
193 194 195
        printf("You must select a test on the command line when using more than one process.\n");
        printf("Try -h for help.\n");
        exit(1);
Andy Cedilnik's avatar
Andy Cedilnik committed
196 197
    }
    if (redirect && (argc < 2)) {
198 199 200
        printf("You must select a test on the command line when redirecting the output.\n");
        printf("Try -h for help.\n");
        exit(1);
Andy Cedilnik's avatar
Andy Cedilnik committed
201 202 203 204 205
    }

    SCREEN_WIDTH = width;
    SCREEN_HEIGHT = height;

Kenneth Moreland's avatar
Kenneth Moreland committed
206
  /* Create an IceT context. */
Andy Cedilnik's avatar
Andy Cedilnik committed
207 208
    context = icetCreateContext(comm);
    icetDiagnostics(diag_level);
209 210

    initialize_render_window(width, height);
Andy Cedilnik's avatar
Andy Cedilnik committed
211 212 213

  /* Redirect standard output on demand. */
    if (redirect) {
214 215 216 217 218 219 220 221 222 223 224 225 226 227
        char filename[64];
        int outfd;
        if (rank == 0) {
            realstdout = fdopen(dup(1), "wt");
        } else {
            realstdout = NULL;
        }
        sprintf(filename, "log.%04d", rank);
        outfd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
        if (outfd < 0) {
            printf("Could not open %s for writing.\n", filename);
            exit(1);
        }
        dup2(outfd, 1);
Andy Cedilnik's avatar
Andy Cedilnik committed
228
    } else {
229
        realstdout = stdout;
Andy Cedilnik's avatar
Andy Cedilnik committed
230 231 232
    }

    strategy_list[0] = ICET_STRATEGY_DIRECT;
233
    strategy_list[1] = ICET_STRATEGY_SEQUENTIAL;
Andy Cedilnik's avatar
Andy Cedilnik committed
234 235 236
    strategy_list[2] = ICET_STRATEGY_SPLIT;
    strategy_list[3] = ICET_STRATEGY_REDUCE;
    strategy_list[4] = ICET_STRATEGY_VTREE;
237 238 239

    single_image_strategy_list[0] = ICET_SINGLE_IMAGE_STRATEGY_AUTOMATIC;
    single_image_strategy_list[1] = ICET_SINGLE_IMAGE_STRATEGY_BSWAP;
240
    single_image_strategy_list[2] = ICET_SINGLE_IMAGE_STRATEGY_RADIXK;
241
    single_image_strategy_list[3] = ICET_SINGLE_IMAGE_STRATEGY_RADIXKR;
242
    single_image_strategy_list[4] = ICET_SINGLE_IMAGE_STRATEGY_TREE;
243 244
}

245
IceTBoolean strategy_uses_single_image_strategy(IceTEnum strategy)
246
{
247 248 249 250 251 252 253
    switch (strategy) {
      case ICET_STRATEGY_DIRECT:        return ICET_FALSE;
      case ICET_STRATEGY_SEQUENTIAL:    return ICET_TRUE;
      case ICET_STRATEGY_SPLIT:         return ICET_FALSE;
      case ICET_STRATEGY_REDUCE:        return ICET_TRUE;
      case ICET_STRATEGY_VTREE:         return ICET_FALSE;
      default:
254
          printrank("ERROR: unknown strategy type.");
255
          return ICET_TRUE;
256
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
257 258
}

259
int run_test_base(int (*test_function)(void))
260
{
261 262 263
    int result;

    result = test_function();
264 265 266

    finalize_test(result);

267 268 269
    return result;
}

270
#define TEST_RESULT_TAG 3492
271
extern void finalize_rendering(void);
Andy Cedilnik's avatar
Andy Cedilnik committed
272
extern void finalize_communication(void);
273
void finalize_test(IceTInt result)
Andy Cedilnik's avatar
Andy Cedilnik committed
274
{
275
    IceTInt rank;
276
    IceTInt num_proc;
277

278 279
    finalize_rendering();

Andy Cedilnik's avatar
Andy Cedilnik committed
280 281
    checkIceTError();

282
    icetGetIntegerv(ICET_RANK, &rank);
283 284
    icetGetIntegerv(ICET_NUM_PROCESSES, &num_proc);

285
    if (rank == 0) {
286 287 288 289 290 291 292 293 294
        IceTInt p_id;
        for (p_id = 1; p_id < num_proc; p_id++) {
            IceTInt remote_result;
            icetCommRecv(&remote_result, 1, ICET_INT, p_id, TEST_RESULT_TAG);
            if (remote_result != TEST_PASSED) {
                result = remote_result;
            }
        }

295 296 297 298 299 300 301 302 303 304 305
        switch (result) {
          case TEST_PASSED:
              printf("***Test Passed***\n");
              break;
          case TEST_NOT_RUN:
              printf("***TEST NOT RUN***\n");
              break;
          case TEST_NOT_PASSED:
              printf("***TEST NOT PASSED***\n");
              break;
          case TEST_FAILED:
306
          default:
307 308 309
              printf("***TEST FAILED***\n");
              break;
        }
310 311
    } else {
        icetCommSend(&result, 1, ICET_INT, 0, TEST_RESULT_TAG);
312 313
    }

Andy Cedilnik's avatar
Andy Cedilnik committed
314 315 316
    icetDestroyContext(context);
    finalize_communication();
}