SimpleTiming.c 64.8 KB
Newer Older
1 2 3 4 5 6 7
/* -*- c -*- *****************************************************************
** Copyright (C) 2010 Sandia Corporation
** 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.
**
8 9
** This test provides a simple means of timing the IceT compositing.  It can be
** used for quick measurements and simple scaling studies.
10 11
*****************************************************************************/

12
#include <IceTDevCommunication.h>
13
#include <IceTDevContext.h>
14
#include <IceTDevImage.h>
15
#include <IceTDevMatrix.h>
Kenneth Moreland's avatar
Kenneth Moreland committed
16
#include <IceTDevPorting.h>
17
#include "test_util.h"
18 19 20 21
#include "test_codes.h"

#include <stdlib.h>
#include <stdio.h>
22
#include <math.h>
23
#include <string.h>
24
#include <time.h>
25

26 27 28 29
#ifndef M_E
#define M_E         2.71828182845904523536028747135266250   /* e */
#endif

30 31 32 33 34 35 36 37 38 39 40
/* Structure used to capture the recursive division of space. */
struct region_divide_struct {
    int axis;           /* x = 0, y = 1, z = 2: the index to a vector array. */
    float cut;          /* Coordinate where cut occurs. */
    int my_side;        /* -1 on the negative side, 1 on the positive side. */
    int num_other_side; /* Number of partitions on other side. */
    struct region_divide_struct *next;
};

typedef struct region_divide_struct *region_divide;

41
#define NAME_SIZE 32
42
typedef struct {
43 44 45 46 47 48 49
    IceTInt num_proc;
    char strategy_name[NAME_SIZE];
    char si_strategy_name[NAME_SIZE];
    IceTInt num_tiles_x;
    IceTInt num_tiles_y;
    IceTInt screen_width;
    IceTInt screen_height;
50
    IceTFloat zoom;
51 52 53
    IceTBoolean transparent;
    IceTBoolean no_interlace;
    IceTBoolean no_collect;
54
    IceTBoolean dense_images;
55 56
    IceTInt max_image_split;
    IceTInt frame_number;
57 58 59 60
    IceTDouble render_time;
    IceTDouble buffer_read_time;
    IceTDouble buffer_write_time;
    IceTDouble compress_time;
61
    IceTDouble interlace_time;
62 63 64
    IceTDouble blend_time;
    IceTDouble draw_time;
    IceTDouble composite_time;
65
    IceTDouble collect_time;
66
    IceTInt64 bytes_sent;
67 68 69
    IceTDouble frame_time;
} timings_type;

70 71 72
static timings_type *g_timing_log;
static IceTSizeType g_timing_log_size;

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* Array for quick opacity lookups. */
#define OPACITY_LOOKUP_SIZE 4096
#define OPACITY_MAX_DT 4
#define OPACITY_COMPUTE_VALUE(dt) (1.0 - pow(M_E, -(dt)))
#define OPACITY_DT_2_INDEX(dt) \
    (  ((dt) < OPACITY_MAX_DT) \
     ? (int)((dt)*(OPACITY_LOOKUP_SIZE/OPACITY_MAX_DT)) \
     : OPACITY_LOOKUP_SIZE )
#define OPACITY_INDEX_2_DT(index) \
    ((index)*((double)OPACITY_MAX_DT/OPACITY_LOOKUP_SIZE))
static IceTDouble g_opacity_lookup[OPACITY_LOOKUP_SIZE+1];
#define QUICK_OPACITY(dt) (g_opacity_lookup[OPACITY_DT_2_INDEX(dt)])

static void init_opacity_lookup(void)
{
88
    IceTSizeType idx;
89

90 91 92
    for (idx = 0; idx < OPACITY_LOOKUP_SIZE+1; idx++) {
        IceTDouble distance_times_tau = OPACITY_INDEX_2_DT(idx);
        g_opacity_lookup[idx] = OPACITY_COMPUTE_VALUE(distance_times_tau);
93 94 95
    }
}

96 97 98
/* Used to signal the first render of a frame. */
static IceTBoolean g_first_render;

99
/* Program arguments. */
100 101 102 103
static IceTInt g_num_tiles_x;
static IceTInt g_num_tiles_y;
static IceTInt g_num_frames;
static IceTInt g_seed;
104
static IceTFloat g_zoom;
105 106
static IceTBoolean g_transparent;
static IceTBoolean g_colored_background;
107
static IceTBoolean g_no_interlace;
108
static IceTBoolean g_no_collect;
109
static IceTBoolean g_use_callback;
110
static IceTBoolean g_dense_images;
111 112
static IceTBoolean g_sync_render;
static IceTBoolean g_write_image;
113 114
static IceTEnum g_strategy;
static IceTEnum g_single_image_strategy;
115 116
static IceTBoolean g_do_magic_k_study;
static IceTInt g_max_magic_k;
117 118
static IceTBoolean g_do_image_split_study;
static IceTInt g_min_image_split;
119
static IceTBoolean g_do_scaling_study_factor_2;
120
static IceTBoolean g_do_scaling_study_factor_2_3;
121
static IceTInt g_num_scaling_study_random;
122

123 124
static float g_color[4];

125 126
static void usage(char *argv[])
{
127 128 129 130
    printstat("\nUSAGE: %s [testargs]\n", argv[0]);
    printstat("\nWhere  testargs are:\n");
    printstat("  -tilesx <num> Sets the number of tiles horizontal (default 1).\n");
    printstat("  -tilesy <num> Sets the number of tiles vertical (default 1).\n");
131
    printstat("  -frames <num> Sets the number of frames to render (default 2).\n");
132
    printstat("  -seed <num>   Use the given number as the random seed.\n");
133
    printstat("  -zoom <num>   Set the zoom factor for the camera (larger = more zoom).\n");
134 135 136 137
    printstat("  -transparent  Render transparent images.  (Uses 4 floats for colors.)\n");
    printstat("  -colored-background Use a color for the background and correct as necessary.\n");
    printstat("  -no-interlace Turn off the image interlacing optimization.\n");
    printstat("  -no-collect   Turn off image collection.\n");
138
    printstat("  -use-callback Do the drawing in an IceT callback.\n");
139
    printstat("  -sync-render  Synchronize rendering by adding a barrier to the draw callback.\n");
140
    printstat("  -dense-images Composite dense images by classifying no pixels as background.\n");
141 142 143 144 145
    printstat("  -write-image  Write an image on the first frame.\n");
    printstat("  -reduce       Use the reduce strategy (default).\n");
    printstat("  -vtree        Use the virtual trees strategy.\n");
    printstat("  -sequential   Use the sequential strategy.\n");
    printstat("  -bswap        Use the binary-swap single-image strategy.\n");
146
    printstat("  -bswapfold    Use the binary-swap with folding single-image strategy.\n");
147
    printstat("  -radixk       Use the radix-k single-image strategy.\n");
148
    printstat("  -radixkr      Use the radix-kr single-image strategy.\n");
149 150
    printstat("  -tree         Use the tree single-image strategy.\n");
    printstat("  -magic-k-study <num> Use the radix-k single-image strategy and repeat for\n"
151
           "                   multiple values of k, up to <num>, doubling each time.\n");
152
    printstat("  -max-image-split-study <num> Repeat the test for multiple maximum image\n"
153 154 155
           "                   splits starting at <num> and doubling each time.\n");
    printstat("  -scaling-study-factor-2 Perform a scaling study for all process counts\n"
              "                that are a factor of 2.\n");
156 157 158 159
    printstat("  -scaling-study-factor-2-3 Perform a scaling study that includes all\n"
              "                process counts that are a factor of 2 plus all process\n"
              "                counts that are a factor of 3 plus most process counts\n"
              "                that have factors of 2 and 3.\n");
160 161 162 163
    printstat("  -scaling-study-random <num> Picks a random number to bifurcate the\n"
              "                processes and runs the compositing on each of them. This\n"
              "                experiment is run <num> times. Run enough times this test\n"
              "                should give performance over scales at odd process counts.\n");
164
    printstat("  -h, -help     Print this help message.\n");
165
    printstat("\nFor general testing options, try -h or -help before test name.\n");
166 167
}

168 169 170 171 172 173
static void parse_arguments(int argc, char *argv[])
{
    int arg;

    g_num_tiles_x = 1;
    g_num_tiles_y = 1;
174
    g_num_frames = 2;
175
    g_seed = (IceTInt)time(NULL);
176
    g_zoom = (IceTFloat)1.0;
177 178
    g_transparent = ICET_FALSE;
    g_colored_background = ICET_FALSE;
179
    g_no_interlace = ICET_FALSE;
180
    g_no_collect = ICET_FALSE;
181
    g_use_callback = ICET_FALSE;
182
    g_dense_images = ICET_FALSE;
183
    g_sync_render = ICET_FALSE;
184
    g_write_image = ICET_FALSE;
185 186
    g_strategy = ICET_STRATEGY_REDUCE;
    g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_AUTOMATIC;
187 188
    g_do_magic_k_study = ICET_FALSE;
    g_max_magic_k = 0;
189 190
    g_do_image_split_study = ICET_FALSE;
    g_min_image_split = 0;
191
    g_do_scaling_study_factor_2 = ICET_FALSE;
192
    g_do_scaling_study_factor_2_3 = ICET_FALSE;
193
    g_num_scaling_study_random = 0;
194 195 196 197 198 199 200 201

    for (arg = 1; arg < argc; arg++) {
        if (strcmp(argv[arg], "-tilesx") == 0) {
            arg++;
            g_num_tiles_x = atoi(argv[arg]);
        } else if (strcmp(argv[arg], "-tilesy") == 0) {
            arg++;
            g_num_tiles_y = atoi(argv[arg]);
202 203 204 205 206 207
        } else if (strcmp(argv[arg], "-frames") == 0) {
            arg++;
            g_num_frames = atoi(argv[arg]);
        } else if (strcmp(argv[arg], "-seed") == 0) {
            arg++;
            g_seed = atoi(argv[arg]);
208 209 210
        } else if (strcmp(argv[arg], "-zoom") == 0) {
            arg++;
            g_zoom = (IceTFloat)atof(argv[arg]);
211
        } else if (strcmp(argv[arg], "-transparent") == 0) {
212
            g_transparent = ICET_TRUE;
213
        } else if (strcmp(argv[arg], "-colored-background") == 0) {
214
            g_colored_background = ICET_TRUE;
215 216
        } else if (strcmp(argv[arg], "-no-interlace") == 0) {
            g_no_interlace = ICET_TRUE;
217 218
        } else if (strcmp(argv[arg], "-no-collect") == 0) {
            g_no_collect = ICET_TRUE;
219 220
        } else if (strcmp(argv[arg], "-use-callback") == 0) {
            g_use_callback = ICET_TRUE;
221 222
        } else if (strcmp(argv[arg], "-dense-images") == 0) {
            g_dense_images = ICET_TRUE;
223 224
            /* Turn of interlacing. It won't help here. */
            g_no_interlace = ICET_TRUE;
225 226
        } else if (strcmp(argv[arg], "-sync-render") == 0) {
            g_sync_render = ICET_TRUE;
227
        } else if (strcmp(argv[arg], "-write-image") == 0) {
228
            g_write_image = ICET_TRUE;
229 230 231 232 233 234 235 236
        } else if (strcmp(argv[arg], "-reduce") == 0) {
            g_strategy = ICET_STRATEGY_REDUCE;
        } else if (strcmp(argv[arg], "-vtree") == 0) {
            g_strategy = ICET_STRATEGY_VTREE;
        } else if (strcmp(argv[arg], "-sequential") == 0) {
            g_strategy = ICET_STRATEGY_SEQUENTIAL;
        } else if (strcmp(argv[arg], "-bswap") == 0) {
            g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_BSWAP;
237 238
        } else if (strcmp(argv[arg], "-bswapfold") == 0) {
            g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_BSWAP_FOLDING;
239 240
        } else if (strcmp(argv[arg], "-radixk") == 0) {
            g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_RADIXK;
241 242
        } else if (strcmp(argv[arg], "-radixkr") == 0) {
            g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_RADIXKR;
243 244
        } else if (strcmp(argv[arg], "-tree") == 0) {
            g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_TREE;
245 246
        } else if (strcmp(argv[arg], "-magic-k-study") == 0) {
            g_do_magic_k_study = ICET_TRUE;
247
            g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_RADIXKR;
248 249
            arg++;
            g_max_magic_k = atoi(argv[arg]);
250 251
        } else if (strcmp(argv[arg], "-max-image-split-study") == 0) {
            g_do_image_split_study = ICET_TRUE;
252
            g_single_image_strategy = ICET_SINGLE_IMAGE_STRATEGY_RADIXKR;
253 254
            arg++;
            g_min_image_split = atoi(argv[arg]);
255 256
        } else if (strcmp(argv[arg], "-scaling-study-factor-2") == 0) {
            g_do_scaling_study_factor_2 = ICET_TRUE;
257 258
        } else if (strcmp(argv[arg], "-scaling-study-factor-2-3") == 0) {
            g_do_scaling_study_factor_2_3 = ICET_TRUE;
259 260 261
        } else if (strcmp(argv[arg], "-scaling-study-random") == 0) {
            arg++;
            g_num_scaling_study_random = atoi(argv[arg]);
262 263 264 265
        } else if (   (strcmp(argv[arg], "-h") == 0)
                   || (strcmp(argv[arg], "-help")) ) {
            usage(argv);
            exit(0);
266
        } else {
267
            printstat("Unknown option `%s'.\n", argv[arg]);
268
            usage(argv);
269 270 271 272
            exit(1);
        }
    }
}
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
#define NUM_HEX_PLANES 6
struct hexahedron {
    IceTDouble planes[NUM_HEX_PLANES][4];
};

static void intersect_ray_plane(const IceTDouble *ray_origin,
                                const IceTDouble *ray_direction,
                                const IceTDouble *plane,
                                IceTDouble *distance,
                                IceTBoolean *front_facing,
                                IceTBoolean *parallel)
{
    IceTDouble distance_numerator = icetDot3(plane, ray_origin) + plane[3];
    IceTDouble distance_denominator = icetDot3(plane, ray_direction);

    if (distance_denominator == 0.0) {
        *parallel = ICET_TRUE;
        *front_facing = (distance_numerator > 0);
    } else {
        *parallel = ICET_FALSE;
        *distance = -distance_numerator/distance_denominator;
        *front_facing = (distance_denominator < 0);
    }
}

/* This algorithm (and associated intersect_ray_plane) come from Graphics Gems
 * II, Fast Ray-Convex Polyhedron Intersection by Eric Haines. */
static void intersect_ray_hexahedron(const IceTDouble *ray_origin,
                                     const IceTDouble *ray_direction,
                                     const struct hexahedron hexahedron,
                                     IceTDouble *near_distance,
                                     IceTDouble *far_distance,
                                     IceTInt *near_plane_index,
                                     IceTBoolean *intersection_happened)
{
    int planeIdx;

    *near_distance = 0.0;
    *far_distance = 2.0;
    *near_plane_index = -1;

    for (planeIdx = 0; planeIdx < NUM_HEX_PLANES; planeIdx++) {
        IceTDouble distance;
        IceTBoolean front_facing;
        IceTBoolean parallel;

        intersect_ray_plane(ray_origin,
                            ray_direction,
                            hexahedron.planes[planeIdx],
                            &distance,
                            &front_facing,
                            &parallel);

        if (!parallel) {
            if (front_facing) {
                if (*near_distance < distance) {
                    *near_distance = distance;
                    *near_plane_index = planeIdx;
                }
            } else {
                if (distance < *far_distance) {
                    *far_distance = distance;
                }
            }
        } else { /*parallel*/
            if (front_facing) {
                /* Ray missed parallel plane.  No intersection. */
                *intersection_happened = ICET_FALSE;
                return;
            }
        }
    }

    *intersection_happened = (*near_distance < *far_distance);
}

/* Plane equations for unit box on origin. */
struct hexahedron unit_box = {
    {
        { -1.0, 0.0, 0.0, -0.5 },
        { 1.0, 0.0, 0.0, -0.5 },
        { 0.0, -1.0, 0.0, -0.5 },
        { 0.0, 1.0, 0.0, -0.5 },
        { 0.0, 0.0, -1.0, -0.5 },
        { 0.0, 0.0, 1.0, -0.5 }
    }
};

362 363 364 365 366
static void draw(const IceTDouble *projection_matrix,
                 const IceTDouble *modelview_matrix,
                 const IceTFloat *background_color,
                 const IceTInt *readback_viewport,
                 IceTImage result)
367
{
368 369 370 371 372
    IceTDouble transform[16];
    IceTDouble inverse_transpose_transform[16];
    IceTBoolean success;
    int planeIdx;
    struct hexahedron transformed_box;
373 374
    IceTInt width;
    IceTInt height;
375 376 377 378 379 380 381
    IceTFloat *colors_float = NULL;
    IceTUByte *colors_byte = NULL;
    IceTFloat *depths = NULL;
    IceTInt pixel_x;
    IceTInt pixel_y;
    IceTDouble ray_origin[3];
    IceTDouble ray_direction[3];
382 383
    IceTFloat background_depth;
    IceTFloat background_alpha;
384 385 386 387 388

    icetMatrixMultiply(transform, projection_matrix, modelview_matrix);

    success = icetMatrixInverseTranspose((const IceTDouble *)transform,
                                         inverse_transpose_transform);
389

390
    if (!success) {
391
        printrank("ERROR: Inverse failed.\n");
392
    }
393

394 395 396
    for (planeIdx = 0; planeIdx < NUM_HEX_PLANES; planeIdx++) {
        const IceTDouble *original_plane = unit_box.planes[planeIdx];
        IceTDouble *transformed_plane = transformed_box.planes[planeIdx];
397

398 399 400 401
        icetMatrixVectorMultiply(transformed_plane,
                                 inverse_transpose_transform,
                                 original_plane);
    }
402

403 404
    width = icetImageGetWidth(result);
    height = icetImageGetHeight(result);
405

406 407 408 409 410 411
    if (g_transparent) {
        colors_float = icetImageGetColorf(result);
    } else {
        colors_byte = icetImageGetColorub(result);
        depths = icetImageGetDepthf(result);
    }
412

413 414 415 416 417 418 419 420 421 422
    if (!g_dense_images) {
        background_depth = 1.0f;
        background_alpha = background_color[3];
    } else {
        IceTSizeType pixel_index;

        /* To fake dense images, use a depth and alpha for the background that
         * IceT will not recognize as background. */
        background_depth = 0.999f;
        background_alpha
Kenneth Moreland's avatar
Kenneth Moreland committed
423
                = (background_color[3] == 0) ? 0.001f : background_color[3];
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

        /* Clear out the the images to background so that pixels outside of
         * the contained viewport have valid values. */
        for (pixel_index = 0; pixel_index < width*height; pixel_index++) {
            if (g_transparent) {
                IceTFloat *color_dest = colors_float + 4*pixel_index;
                color_dest[0] = background_color[0];
                color_dest[1] = background_color[1];
                color_dest[2] = background_color[2];
                color_dest[3] = background_alpha;
            } else {
                IceTUByte *color_dest = colors_byte + 4*pixel_index;
                IceTFloat *depth_dest = depths + pixel_index;
                color_dest[0] = (IceTUByte)(background_color[0]*255);
                color_dest[1] = (IceTUByte)(background_color[1]*255);
                color_dest[2] = (IceTUByte)(background_color[2]*255);
                color_dest[3] = (IceTUByte)(background_alpha*255);
                depth_dest[0] = background_depth;
            }
        }
    }

446 447 448 449 450 451
    ray_direction[0] = ray_direction[1] = 0.0;
    ray_direction[2] = 1.0;
    ray_origin[2] = -1.0;
    for (pixel_y = readback_viewport[1];
         pixel_y < readback_viewport[1] + readback_viewport[3];
         pixel_y++) {
452
        ray_origin[1] = (2.0*pixel_y)/height - 1.0;
453 454 455 456 457 458 459 460 461 462
        for (pixel_x = readback_viewport[0];
             pixel_x < readback_viewport[0] + readback_viewport[2];
             pixel_x++) {
            IceTDouble near_distance;
            IceTDouble far_distance;
            IceTInt near_plane_index;
            IceTBoolean intersection_happened;
            IceTFloat color[4];
            IceTFloat depth;

463
            ray_origin[0] = (2.0*pixel_x)/width - 1.0;
464 465 466 467 468 469 470 471 472 473 474 475 476 477

            intersect_ray_hexahedron(ray_origin,
                                     ray_direction,
                                     transformed_box,
                                     &near_distance,
                                     &far_distance,
                                     &near_plane_index,
                                     &intersection_happened);

            if (intersection_happened) {
                const IceTDouble *near_plane;
                IceTDouble shading;

                near_plane = transformed_box.planes[near_plane_index];
478
                shading = -near_plane[2]/sqrt(icetDot3(near_plane, near_plane));
479

480 481 482
                color[0] = g_color[0] * (IceTFloat)shading;
                color[1] = g_color[1] * (IceTFloat)shading;
                color[2] = g_color[2] * (IceTFloat)shading;
483
                color[3] = g_color[3];
484
                depth = (IceTFloat)(0.5*near_distance);
485 486 487
                if (g_transparent) {
                    /* Modify color by an opacity determined by thickness. */
                    IceTDouble thickness = far_distance - near_distance;
488
                    IceTDouble opacity = QUICK_OPACITY(4.0*thickness);
489
                    if (opacity < 0.001) { opacity = 0.001; }
490 491 492 493
                    color[0] *= (IceTFloat)opacity;
                    color[1] *= (IceTFloat)opacity;
                    color[2] *= (IceTFloat)opacity;
                    color[3] *= (IceTFloat)opacity;
494
                }
495 496 497 498
            } else {
                color[0] = background_color[0];
                color[1] = background_color[1];
                color[2] = background_color[2];
499 500
                color[3] = background_alpha;
                depth = background_depth;
501
            }
502

503 504
            if (g_transparent) {
                IceTFloat *color_dest
505
                    = colors_float + 4*(pixel_y*width + pixel_x);
506 507 508 509 510 511
                color_dest[0] = color[0];
                color_dest[1] = color[1];
                color_dest[2] = color[2];
                color_dest[3] = color[3];
            } else {
                IceTUByte *color_dest
512
                    = colors_byte + 4*(pixel_y*width + pixel_x);
513
                IceTFloat *depth_dest
514
                    = depths + pixel_y*width + pixel_x;
515 516 517 518 519 520
                color_dest[0] = (IceTUByte)(color[0]*255);
                color_dest[1] = (IceTUByte)(color[1]*255);
                color_dest[2] = (IceTUByte)(color[2]*255);
                color_dest[3] = (IceTUByte)(color[3]*255);
                depth_dest[0] = depth;
            }
521 522
        }
    }
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

    if (g_first_render) {
        if (g_sync_render) {
            /* The rendering we are using here is pretty crummy.  It is not
               meant to be practical but to create reasonable images to
               composite.  One problem with it is that the render times are not
               well balanced even though everyone renders roughly the same sized
               object.  If you want to time the composite performance, this can
               interfere with the measurements.  To get around this problem, do
               a barrier that makes it look as if all rendering finishes at the
               same time.  Note that there is a remote possibility that not
               every process will render something, in which case this will
               deadlock.  Note that we make sure only to sync once to get around
               the less remote possibility that some, but not all, processes
               render more than once. */
            icetCommBarrier();
        }
        g_first_render = ICET_FALSE;
    }
542 543 544
}

/* Given the rank of this process in all of them, divides the unit box
545
 * centered on the origin evenly (w.r.t. volume) amongst all processes.  The
546 547 548 549 550
 * region for this process, characterized by the min and max corners, is
 * returned in the bounds_min and bounds_max parameters. */
static void find_region(int rank,
                        int num_proc,
                        float *bounds_min,
551 552
                        float *bounds_max,
                        region_divide *divisions)
553 554 555 556
{
    int axis = 0;
    int start_rank = 0;         /* The first rank. */
    int end_rank = num_proc;    /* One after the last rank. */
557
    region_divide current_division = NULL;
558

559 560
    bounds_min[0] = bounds_min[1] = bounds_min[2] = -0.5f;
    bounds_max[0] = bounds_max[1] = bounds_max[2] = 0.5f;
561

562 563
    *divisions = NULL;

564 565 566 567 568 569
    /* Recursively split each axis, dividing the number of processes in my group
       in half each time. */
    while (1 < (end_rank - start_rank)) {
        float length = bounds_max[axis] - bounds_min[axis];
        int middle_rank = (start_rank + end_rank)/2;
        float region_cut;
570
        region_divide new_divide = malloc(sizeof(struct region_divide_struct));
571 572 573 574 575 576 577

        /* Skew the place where we cut the region based on the relative size
         * of the group size on each side, which may be different if the
         * group cannot be divided evenly. */
        region_cut = (  bounds_min[axis]
                      + length*(middle_rank-start_rank)/(end_rank-start_rank) );

578 579 580 581
        new_divide->axis = axis;
        new_divide->cut = region_cut;
        new_divide->next = NULL;

582 583
        if (rank < middle_rank) {
            /* My rank is in the lower region. */
584 585
            new_divide->my_side = -1;
            new_divide->num_other_side = end_rank - middle_rank;
586 587 588 589
            bounds_max[axis] = region_cut;
            end_rank = middle_rank;
        } else {
            /* My rank is in the upper region. */
590 591
            new_divide->my_side = 1;
            new_divide->num_other_side = middle_rank - start_rank;
592 593 594 595
            bounds_min[axis] = region_cut;
            start_rank = middle_rank;
        }

596 597 598 599 600 601 602
        if (current_division != NULL) {
            current_division->next = new_divide;
        } else {
            *divisions = new_divide;
        }
        current_division = new_divide;

603 604 605 606
        axis = (axis + 1)%3;
    }
}

607 608 609 610 611 612 613 614 615 616 617
/* Free a region divide structure. */
static void free_region_divide(region_divide divisions)
{
    region_divide current_division = divisions;
    while (current_division != NULL) {
        region_divide next_division = current_division->next;
        free(current_division);
        current_division = next_division;
    }
}

618 619 620 621 622 623 624 625 626
/* Given the transformation matricies (representing camera position), determine
 * which side of each axis-aligned plane faces the camera.  The results are
 * stored in plane_orientations, which is expected to be an array of size 3.
 * Entry 0 in plane_orientations will be positive if the vector (1, 0, 0) points
 * towards the camera, negative otherwise.  Entries 1 and 2 are likewise for the
 * y and z vectors. */
static void get_axis_plane_orientations(const IceTDouble *projection,
                                        const IceTDouble *modelview,
                                        int *plane_orientations)
627
{
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    IceTDouble full_transform[16];
    IceTDouble inverse_transpose_transform[16];
    IceTBoolean success;
    int planeIdx;

    icetMatrixMultiply(full_transform, projection, modelview);
    success = icetMatrixInverseTranspose((const IceTDouble *)full_transform,
                                         inverse_transpose_transform);

    for (planeIdx = 0; planeIdx < 3; planeIdx++) {
        IceTDouble plane_equation[4];
        IceTDouble transformed_plane[4];

        plane_equation[0] = plane_equation[1]
            = plane_equation[2] = plane_equation[3] = 0.0;
        plane_equation[planeIdx] = 1.0;

        /* To transform a plane, multiply the vector representing the plane
         * equation (ax + by + cz + d = 0) by the inverse transpose of the
         * transform. */
        icetMatrixVectorMultiply(transformed_plane,
                                 (const IceTDouble*)inverse_transpose_transform,
                                 (const IceTDouble*)plane_equation);

        /* If the normal of the plane is facing in the -z direction, then the
         * front of the plane is facing the camera. */
        if (transformed_plane[3] < 0) {
            plane_orientations[planeIdx] = 1;
656
        } else {
657
            plane_orientations[planeIdx] = -1;
658 659 660 661 662 663 664
        }
    }
}

/* Use the current OpenGL transformation matricies (representing camera
 * position) and the given region divisions to determine the composite
 * ordering. */
665 666 667
static void find_composite_order(const IceTDouble *projection,
                                 const IceTDouble *modelview,
                                 region_divide region_divisions)
668 669
{
    int num_proc = icetCommSize();
670
    IceTInt *process_ranks;
671 672 673 674
    IceTInt my_position;
    int plane_orientations[3];
    region_divide current_divide;

675
    get_axis_plane_orientations(projection, modelview, plane_orientations);
676 677 678 679 680 681 682 683 684 685

    my_position = 0;
    for (current_divide = region_divisions;
         current_divide != NULL;
         current_divide = current_divide->next) {
        int axis = current_divide->axis;
        int my_side = current_divide->my_side;
        int plane_side = plane_orientations[axis];
        /* If my_side is the side of the plane away from the camera, add
           everything on the other side as before me. */
686 687
        if (   ((my_side < 0) && (plane_side < 0))
            || ((0 < my_side) && (0 < plane_side)) ) {
688 689 690 691 692 693 694 695 696 697 698 699 700
            my_position += current_divide->num_other_side;
        }
    }

    process_ranks = malloc(num_proc * sizeof(IceTInt));
    icetCommAllgather(&my_position, 1, ICET_INT, process_ranks);

    icetEnable(ICET_ORDERED_COMPOSITE);
    icetCompositeOrder(process_ranks);

    free(process_ranks);
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
/* Finds the viewport of the bounds of the locally rendered geometry. */
/* This code is stolen from drawFindContainedViewport in draw.c. */
static void find_contained_viewport(IceTInt contained_viewport[4],
                                    const IceTDouble projection_matrix[16],
                                    const IceTDouble modelview_matrix[16])
{
    IceTDouble total_transform[16];
    IceTDouble left, right, bottom, top;
    IceTDouble *transformed_verts;
    IceTInt global_viewport[4];
    IceTInt num_bounding_verts;
    int i;

    icetGetIntegerv(ICET_GLOBAL_VIEWPORT, global_viewport);

    {
        IceTDouble viewport_matrix[16];
        IceTDouble tmp_matrix[16];

        /* Strange projection matrix that transforms the x and y of normalized
           screen coordinates into viewport coordinates that may be cast to
           integers. */
        viewport_matrix[ 0] = global_viewport[2];
        viewport_matrix[ 1] = 0.0;
        viewport_matrix[ 2] = 0.0;
        viewport_matrix[ 3] = 0.0;

        viewport_matrix[ 4] = 0.0;
        viewport_matrix[ 5] = global_viewport[3];
        viewport_matrix[ 6] = 0.0;
        viewport_matrix[ 7] = 0.0;

        viewport_matrix[ 8] = 0.0;
        viewport_matrix[ 9] = 0.0;
        viewport_matrix[10] = 2.0;
        viewport_matrix[11] = 0.0;

        viewport_matrix[12] = global_viewport[2] + global_viewport[0]*2.0;
        viewport_matrix[13] = global_viewport[3] + global_viewport[1]*2.0;
        viewport_matrix[14] = 0.0;
        viewport_matrix[15] = 2.0;

        icetMatrixMultiply(tmp_matrix,
                           (const IceTDouble *)projection_matrix,
                           (const IceTDouble *)modelview_matrix);
        icetMatrixMultiply(total_transform,
                           (const IceTDouble *)viewport_matrix,
                           (const IceTDouble *)tmp_matrix);
    }

    icetGetIntegerv(ICET_NUM_BOUNDING_VERTS, &num_bounding_verts);
    transformed_verts = icetGetStateBuffer(
                                       ICET_TRANSFORMED_BOUNDS,
                                       sizeof(IceTDouble)*num_bounding_verts*4);

    /* Transform each vertex to find where it lies in the global viewport and
       normalized z.  Leave the results in homogeneous coordinates for now. */
    {
        const IceTDouble *bound_vert
            = icetUnsafeStateGetDouble(ICET_GEOMETRY_BOUNDS);
        for (i = 0; i < num_bounding_verts; i++) {
            IceTDouble bound_vert_4vec[4];
            bound_vert_4vec[0] = bound_vert[3*i+0];
            bound_vert_4vec[1] = bound_vert[3*i+1];
            bound_vert_4vec[2] = bound_vert[3*i+2];
            bound_vert_4vec[3] = 1.0;
            icetMatrixVectorMultiply(transformed_verts + 4*i,
                                     (const IceTDouble *)total_transform,
                                     (const IceTDouble *)bound_vert_4vec);
        }
    }

    /* Set absolute mins and maxes. */
    left   = global_viewport[0] + global_viewport[2];
    right  = global_viewport[0];
    bottom = global_viewport[1] + global_viewport[3];
    top    = global_viewport[1];

    /* Now iterate over all the transformed verts and adjust the absolute mins
       and maxs to include them all. */
    for (i = 0; i < num_bounding_verts; i++)
    {
        IceTDouble *vert = transformed_verts + 4*i;

        /* Check to see if the vertex is in front of the near cut plane.  This
           is true when z/w >= -1 or z + w >= 0.  The second form is better just
           in case w is 0. */
        if (vert[2] + vert[3] >= 0.0) {
          /* Normalize homogeneous coordinates. */
            IceTDouble invw = 1.0/vert[3];
            IceTDouble x = vert[0]*invw;
            IceTDouble y = vert[1]*invw;

          /* Update contained region. */
            if (left   > x) left   = x;
            if (right  < x) right  = x;
            if (bottom > y) bottom = y;
            if (top    < y) top    = y;
        } else {
          /* The vertex is being clipped by the near plane.  In perspective
             mode, vertices behind the near clipping plane can sometimes give
             misleading projections.  Instead, find all the other vertices on
             the other side of the near plane, compute the intersection of the
             segment between the two points and the near plane (in homogeneous
             coordinates) and use that as the projection. */
            int j;
            for (j = 0; j < num_bounding_verts; j++) {
                IceTDouble *vert2 = transformed_verts + 4*j;
                double t;
                IceTDouble x, y, invw;
                if (vert2[2] + vert2[3] < 0.0) {
                  /* Ignore other points behind near plane. */
                    continue;
                }
              /* Let the two points in question be v_i and v_j.  Define the
                 segment between them with the parametric equation
                 p(t) = (vert - vert2)t + vert2.  First, find t where the z and
                 w coordinates of p(t) sum to zero. */
                t = (vert2[2]+vert2[3])/(vert2[2]-vert[2] + vert2[3]-vert[3]);
              /* Use t to find the intersection point.  While we are at it,
                 normalize the resulting coordinates.  We don't need z because
                 we know it is going to be -1. */
                invw = 1.0/((vert[3] - vert2[3])*t + vert2[3] );
                x = ((vert[0] - vert2[0])*t + vert2[0] ) * invw;
                y = ((vert[1] - vert2[1])*t + vert2[1] ) * invw;

              /* Update contained region. */
                if (left   > x) left   = x;
                if (right  < x) right  = x;
                if (bottom > y) bottom = y;
                if (top    < y) top    = y;
            }
        }
    }

    left = floor(left);
    right = ceil(right);
    bottom = floor(bottom);
    top = ceil(top);

  /* Clip bounds to global viewport. */
    if (left   < global_viewport[0]) left = global_viewport[0];
    if (right  > global_viewport[0] + global_viewport[2])
        right  = global_viewport[0] + global_viewport[2];
    if (bottom < global_viewport[1]) bottom = global_viewport[1];
    if (top    > global_viewport[1] + global_viewport[3])
        top    = global_viewport[1] + global_viewport[3];

  /* Use this information to build a containing viewport. */
    contained_viewport[0] = (IceTInt)left;
    contained_viewport[1] = (IceTInt)bottom;
    contained_viewport[2] = (IceTInt)(right - left);
    contained_viewport[3] = (IceTInt)(top - bottom);
}

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
static void SimpleTimingCollectAndPrintLog()
{
    IceTInt rank;
    IceTInt num_proc;
    IceTInt *log_sizes;

    icetGetIntegerv(ICET_RANK, &rank);
    icetGetIntegerv(ICET_NUM_PROCESSES, &num_proc);

    /* Collect the number of log entries each process has. */
    log_sizes = malloc(num_proc*sizeof(IceTInt));
    icetCommGather(&g_timing_log_size, 1, ICET_SIZE_TYPE, log_sizes, 0);

    if (rank == 0) {
        timings_type *all_logs;
        IceTSizeType *data_sizes;
        IceTSizeType *offsets;
        IceTInt total_logs;
        IceTInt proc_index;
        IceTInt log_index;

        data_sizes = malloc(num_proc*sizeof(IceTSizeType));
        offsets = malloc(num_proc*sizeof(IceTSizeType));

        total_logs = 0;
        for (proc_index = 0; proc_index < num_proc; proc_index++) {
            data_sizes[proc_index] = log_sizes[proc_index]*sizeof(timings_type);
            offsets[proc_index] = total_logs*sizeof(timings_type);
            total_logs += log_sizes[proc_index];
        }

        all_logs = malloc(total_logs*sizeof(timings_type));
        icetCommGatherv(g_timing_log,
                        g_timing_log_size*sizeof(timings_type),
                        ICET_BYTE,
                        all_logs,
                        data_sizes,
                        offsets,
                        0);

        for (log_index = 0; log_index < total_logs; log_index++) {
            timings_type *timing = all_logs + log_index;
898
            printf("LOG,%d,%s,%s,%d,%d,%d,%d,%0.1f,%s,%s,%s,%s,%d,%d,%lg,%lg,%lg,%lg,%lg,%lg,%lg,%lg,%lg,%ld,%lg\n",
899 900 901 902 903 904 905
                   timing->num_proc,
                   timing->strategy_name,
                   timing->si_strategy_name,
                   timing->num_tiles_x,
                   timing->num_tiles_y,
                   timing->screen_width,
                   timing->screen_height,
906
                   timing->zoom,
907 908 909
                   timing->transparent ? "yes" : "no",
                   timing->no_interlace ? "no" : "yes",
                   timing->no_collect ? "no" : "yes",
910
                   timing->dense_images ? "yes" : "no",
911 912 913 914 915 916
                   timing->max_image_split,
                   timing->frame_number,
                   timing->render_time,
                   timing->buffer_read_time,
                   timing->buffer_write_time,
                   timing->compress_time,
917
                   timing->interlace_time,
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
                   timing->blend_time,
                   timing->draw_time,
                   timing->composite_time,
                   timing->collect_time,
                   (long int)timing->bytes_sent,
                   timing->frame_time);
        }

        free(data_sizes);
        free(offsets);
        free(all_logs);
    } else /* rank != 0 */ {
        icetCommGatherv(g_timing_log,
                        g_timing_log_size*sizeof(timings_type),
                        ICET_BYTE,
                        NULL,
                        NULL,
                        NULL,
                        0);
    }

    free(log_sizes);

    if (g_timing_log_size > 0) {
        free(g_timing_log);
        g_timing_log = NULL;
        g_timing_log_size = 0;
    }

    /* This is to prevent a non-root from printing while the root is writing
       the log. */
    icetCommBarrier();
}

952
static int SimpleTimingDoRender()
953 954 955
{
    IceTInt rank;
    IceTInt num_proc;
956 957 958
    const char *strategy_name;
    const char *si_strategy_name;
    IceTInt max_image_split;
959

960 961
    float aspect = (  (float)(g_num_tiles_x*SCREEN_WIDTH)
                    / (float)(g_num_tiles_y*SCREEN_HEIGHT) );
962
    int frame;
963 964
    float bounds_min[3];
    float bounds_max[3];
965
    region_divide region_divisions;
966

967 968 969
    IceTDouble projection_matrix[16];
    IceTFloat background_color[4];

970 971 972
    IceTImage pre_rendered_image = icetImageNull();
    void *pre_rendered_image_buffer = NULL;

973 974
    timings_type *timing_array;

975 976
    /* Normally, the first thing that you do is set up your communication and
     * then create at least one IceT context.  This has already been done in the
977 978
     * calling function (i.e. icetTests_mpi.c).  See the init_mpi in
     * test_mpi.h for an example.
979 980
     */

981 982
    init_opacity_lookup();

983 984 985 986 987 988
    /* If we had set up the communication layer ourselves, we could have gotten
     * these parameters directly from it.  Since we did not, this provides an
     * alternate way. */
    icetGetIntegerv(ICET_RANK, &rank);
    icetGetIntegerv(ICET_NUM_PROCESSES, &num_proc);

989 990 991 992 993 994 995 996 997 998 999
    if (g_colored_background) {
        background_color[0] = 0.2f;
        background_color[1] = 0.5f;
        background_color[2] = 0.7f;
        background_color[3] = 1.0f;
    } else {
        background_color[0] = 0.0f;
        background_color[1] = 0.0f;
        background_color[2] = 0.0f;
        background_color[3] = 0.0f;
    }
1000

1001 1002
    /* Give IceT a function that will issue the drawing commands. */
    icetDrawCallback(draw);
1003

1004 1005 1006 1007 1008
    /* Other IceT state. */
    if (g_transparent) {
        icetCompositeMode(ICET_COMPOSITE_MODE_BLEND);
        icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_FLOAT);
        icetSetDepthFormat(ICET_IMAGE_DEPTH_NONE);
1009
        icetEnable(ICET_CORRECT_COLORED_BACKGROUND);
1010 1011 1012 1013 1014 1015
    } else {
        icetCompositeMode(ICET_COMPOSITE_MODE_Z_BUFFER);
        icetSetColorFormat(ICET_IMAGE_COLOR_RGBA_UBYTE);
        icetSetDepthFormat(ICET_IMAGE_DEPTH_FLOAT);
    }

1016 1017 1018 1019 1020 1021
    if (g_no_interlace) {
        icetDisable(ICET_INTERLACE_IMAGES);
    } else {
        icetEnable(ICET_INTERLACE_IMAGES);
    }

1022 1023 1024 1025 1026 1027
    if (g_no_collect) {
        icetDisable(ICET_COLLECT_IMAGES);
    } else {
        icetEnable(ICET_COLLECT_IMAGES);
    }

1028 1029 1030
    /* Give IceT the bounds of the polygons that will be drawn.  Note that
         * IceT will take care of any transformation that gets passed to
         * icetDrawFrame. */
1031
    icetBoundingBoxd(-0.5f, 0.5f, -0.5, 0.5, -0.5, 0.5);
1032 1033 1034

    /* Determine the region we want the local geometry to be in.  This will be
     * used for the modelview transformation later. */
1035
    find_region(rank, num_proc, bounds_min, bounds_max, &region_divisions);
1036

1037
    /* Set up the tiled display.  The asignment of displays to processes is
1038
     * arbitrary because, as this is a timing test, I am not too concerned
1039 1040 1041
     * about who shows what. */
    if (g_num_tiles_x*g_num_tiles_y <= num_proc) {
        int x, y, display_rank;
1042
        icetResetTiles();
1043 1044 1045
        display_rank = 0;
        for (y = 0; y < g_num_tiles_y; y++) {
            for (x = 0; x < g_num_tiles_x; x++) {
1046 1047
                icetAddTile(x*(IceTInt)SCREEN_WIDTH,
                            y*(IceTInt)SCREEN_HEIGHT,
1048 1049 1050 1051 1052 1053
                            SCREEN_WIDTH,
                            SCREEN_HEIGHT,
                            display_rank);
                display_rank++;
            }
        }
1054
    } else {
1055
        printstat("Not enough processes to %dx%d tiles.\n",
1056 1057
               g_num_tiles_x, g_num_tiles_y);
        return TEST_FAILED;
1058 1059
    }

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    if (!g_use_callback) {
        IceTInt global_viewport[4];
        IceTInt width, height;
        IceTInt buffer_size;

        icetGetIntegerv(ICET_GLOBAL_VIEWPORT, global_viewport);
        width = global_viewport[2]; height = global_viewport[3];

        buffer_size = icetImageBufferSize(width, height);
        pre_rendered_image_buffer = malloc(buffer_size);
        pre_rendered_image =
                icetImageAssignBuffer(pre_rendered_image_buffer, width, height);
    }

1074 1075
    icetStrategy(g_strategy);
    icetSingleImageStrategy(g_single_image_strategy);
1076

1077
    /* Set up the projection matrix. */
1078 1079 1080
    icetMatrixFrustum(-0.65*aspect/g_zoom, 0.65*aspect/g_zoom,
                      -0.65/g_zoom, 0.65/g_zoom,
                      3.0, 5.0,
1081
                      projection_matrix);
1082

1083 1084 1085 1086 1087
    if (rank%10 < 7) {
        IceTInt color_bits = rank%10 + 1;
        g_color[0] = (float)(color_bits%2);
        g_color[1] = (float)((color_bits/2)%2);
        g_color[2] = (float)((color_bits/4)%2);
1088
        g_color[3] = 1.0f;
1089
    } else {
1090
        g_color[0] = g_color[1] = g_color[2] = 0.5f;
1091
        g_color[rank%10 - 7] = 0.0f;
1092
        g_color[3] = 1.0f;
1093 1094
    }

1095 1096 1097
    /* Initialize randomness. */
    if (rank == 0) {
        int i;
1098
        printstat("Seed = %d\n", g_seed);
1099 1100 1101 1102 1103 1104 1105 1106 1107
        for (i = 1; i < num_proc; i++) {
            icetCommSend(&g_seed, 1, ICET_INT, i, 33);
        }
    } else {
        icetCommRecv(&g_seed, 1, ICET_INT, 0, 33);
    }

    srand(g_seed);

1108
    timing_array = malloc(g_num_frames * sizeof(timings_type));
1109

1110 1111 1112 1113 1114 1115
    strategy_name = icetGetStrategyName();
    if (g_single_image_strategy == ICET_SINGLE_IMAGE_STRATEGY_RADIXK) {
        static char name_buffer[256];
        IceTInt magic_k;

        icetGetIntegerv(ICET_MAGIC_K, &magic_k);
1116
        icetSnprintf(name_buffer, 256, "radix-k %d", (int)magic_k);
1117
        si_strategy_name = name_buffer;
1118 1119 1120 1121 1122
    } else if (g_single_image_strategy == ICET_SINGLE_IMAGE_STRATEGY_RADIXKR) {
            static char name_buffer[256];
            IceTInt magic_k;

            icetGetIntegerv(ICET_MAGIC_K, &magic_k);
1123
            icetSnprintf(name_buffer, 256, "radix-kr %d", (int)magic_k);
1124
            si_strategy_name = name_buffer;
1125 1126 1127 1128 1129 1130
    } else {
        si_strategy_name = icetGetSingleImageStrategyName();
    }

    icetGetIntegerv(ICET_MAX_IMAGE_SPLIT, &max_image_split);

1131
    for (frame = 0; frame < g_num_frames; frame++) {
1132
        IceTDouble elapsed_time;
1133
        IceTDouble modelview_matrix[16];
1134
        IceTImage image;
1135 1136 1137

        /* We can set up a modelview matrix here and IceT will factor this in
         * determining the screen projection of the geometry. */
1138
        icetMatrixIdentity(modelview_matrix);
1139 1140

        /* Move geometry back so that it can be seen by the camera. */
1141
        icetMatrixMultiplyTranslate(modelview_matrix, 0.0, 0.0, -4.0);
1142

1143
        /* Rotate to some random view. */
1144 1145 1146 1147 1148 1149
        icetMatrixMultiplyRotate(modelview_matrix,
                                 (360.0*rand())/RAND_MAX, 1.0, 0.0, 0.0);
        icetMatrixMultiplyRotate(modelview_matrix,
                                 (360.0*rand())/RAND_MAX, 0.0, 1.0, 0.0);
        icetMatrixMultiplyRotate(modelview_matrix,
                                 (360.0*rand())/RAND_MAX, 0.0, 0.0, 1.0);
1150

1151 1152 1153
        /* Determine view ordering of geometry based on camera position
           (represented by the current projection and modelview matrices). */
        if (g_transparent) {
1154 1155 1156
            find_composite_order(projection_matrix,
                                 modelview_matrix,
                                 region_divisions);
1157 1158
        }

1159 1160
        /* Translate the unit box centered on the origin to the region specified
         * by bounds_min and bounds_max. */
1161 1162 1163 1164 1165 1166 1167 1168 1169
        icetMatrixMultiplyTranslate(modelview_matrix,
                                    bounds_min[0],
                                    bounds_min[1],
                                    bounds_min[2]);
        icetMatrixMultiplyScale(modelview_matrix,
                                bounds_max[0] - bounds_min[0],
                                bounds_max[1] - bounds_min[1],
                                bounds_max[2] - bounds_min[2]);
        icetMatrixMultiplyTranslate(modelview_matrix, 0.5, 0.5, 0.5);
1170

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
        if (!g_use_callback) {
            /* Draw the image for the frame. */
            IceTInt contained_viewport[4];
            find_contained_viewport(contained_viewport,
                                    projection_matrix,
                                    modelview_matrix);
            if (g_transparent) {
                IceTFloat black[4] = { 0.0, 0.0, 0.0, 0.0 };
                draw(projection_matrix,
                     modelview_matrix,
                     black,
                     contained_viewport,
                     pre_rendered_image);
            } else {
                draw(projection_matrix,
                     modelview_matrix,
                     background_color,
                     contained_viewport,
                     pre_rendered_image);
            }
        }

1193 1194 1195 1196 1197 1198
        if (g_dense_images) {
            /* With dense images, we want IceT to load in all pixels, so clear
             * out the bounding box/vertices. */
            icetBoundingVertices(0, ICET_VOID, 0, 0, NULL);
        }

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
        /* Get everyone to start at the same time. */
        icetCommBarrier();

        elapsed_time = icetWallTime();

        if (g_use_callback) {
            /* Instead of calling draw() directly, call it indirectly through
             * icetDrawFrame().  IceT will automatically handle image
             * compositing. */
            g_first_render = ICET_TRUE;
            image = icetDrawFrame(projection_matrix,
                                  modelview_matrix,
                                  background_color);
        } else {
            image = icetCompositeImage(
                        icetImageGetColorConstVoid(pre_rendered_image,NULL),
                        g_transparent ? NULL : icetImageGetDepthConstVoid(pre_rendered_image,NULL),
                        NULL,
                        projection_matrix,
                        modelview_matrix,
                        background_color);
        }
1221

1222 1223 1224
        /* Let everyone catch up before finishing the frame. */
        icetCommBarrier();

1225 1226
        elapsed_time = icetWallTime() - elapsed_time;

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
        /* Record timings to logging. */
        timing_array[frame].num_proc = num_proc;
        strncpy(timing_array[frame].strategy_name, strategy_name, NAME_SIZE);
        timing_array[frame].strategy_name[NAME_SIZE-1] = '\0';
        strncpy(timing_array[frame].si_strategy_name, si_strategy_name, NAME_SIZE);
        timing_array[frame].si_strategy_name[NAME_SIZE-1] = '\0';
        timing_array[frame].num_tiles_x = g_num_tiles_x;
        timing_array[frame].num_tiles_y = g_num_tiles_y;
        timing_array[frame].screen_width = SCREEN_WIDTH;
        timing_array[frame].screen_height = SCREEN_HEIGHT;
1237
        timing_array[frame].zoom = g_zoom;
1238 1239 1240
        timing_array[frame].transparent = g_transparent;
        timing_array[frame].no_interlace = g_no_interlace;
        timing_array[frame].no_collect = g_no_collect;
1241
        timing_array[frame].dense_images = g_dense_images;
1242 1243
        timing_array[frame].max_image_split = max_image_split;
        timing_array[frame].frame_number = frame;
1244 1245 1246 1247 1248 1249 1250 1251
        icetGetDoublev(ICET_RENDER_TIME,
                       &timing_array[frame].render_time);
        icetGetDoublev(ICET_BUFFER_READ_TIME,
                       &timing_array[frame].buffer_read_time);
        icetGetDoublev(ICET_BUFFER_WRITE_TIME,
                       &timing_array[frame].buffer_write_time);
        icetGetDoublev(ICET_COMPRESS_TIME,
                       &timing_array[frame].compress_time);
1252 1253
        icetGetDoublev(ICET_INTERLACE_TIME,
                       &timing_array[frame].interlace_time);
1254 1255 1256 1257 1258 1259
        icetGetDoublev(ICET_BLEND_TIME,
                       &timing_array[frame].blend_time);
        icetGetDoublev(ICET_TOTAL_DRAW_TIME,
                       &timing_array[frame].draw_time);
        icetGetDoublev(ICET_COMPOSITE_TIME,
                       &timing_array[frame].composite_time);
1260 1261
        icetGetDoublev(ICET_COLLECT_TIME,
                       &timing_array[frame].collect_time);
1262 1263
        timing_array[frame].bytes_sent
                = icetUnsafeStateGetInteger(ICET_BYTES_SENT)[0];
1264
        timing_array[frame].frame_time = elapsed_time;
1265 1266 1267 1268 1269 1270 1271 1272 1273

        /* Write out image to verify rendering occurred correctly. */
        if (   g_write_image
            && (rank < (g_num_tiles_x*g_num_tiles_y))
            && (frame == 0)
               ) {
            IceTUByte *buffer = malloc(SCREEN_WIDTH*SCREEN_HEIGHT*4);
            char filename[256];
            icetImageCopyColorub(image, buffer, ICET_IMAGE_COLOR_RGBA_UBYTE);
1274
            icetSnprintf(filename, 256, "SimpleTiming%02d.ppm", rank);
1275
            write_ppm(filename, buffer, (int)SCREEN_WIDTH, (int)SCREEN_HEIGHT);
1276 1277
            free(buffer);
        }
1278 1279
    }

1280
    /* Collect max times and log. */
1281 1282 1283
    {
        timings_type *timing_collection = malloc(num_proc*sizeof(timings_type));

1284 1285 1286 1287 1288 1289 1290 1291 1292
        if (rank == 0) {
            if (g_timing_log_size == 0) {
                g_timing_log = malloc(g_num_frames*sizeof(timings_type));
            } else {
                g_timing_log = realloc(g_timing_log,
                                       (g_timing_log_size+g_num_frames)
                                        *sizeof(timings_type));
            }
        }
1293

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
        for (frame = 0; frame < g_num_frames; frame++) {
            timings_type *timing = &timing_array[frame];

            icetCommGather(timing,
                           sizeof(timings_type),
                           ICET_BYTE,
                           timing_collection,
                           0);

            if (rank == 0) {
                int p;
                IceTInt64 total_bytes_sent = 0;

                for (p = 0; p < num_proc; p++) {
#define UPDATE_MAX(field) if (timing->field < timing_collection[p].field) timing->field = timing_collection[p].field;
                    UPDATE_MAX(render_time);
                    UPDATE_MAX(buffer_read_time);
                    UPDATE_MAX(buffer_write_time);
                    UPDATE_MAX(compress_time);
1313
                    UPDATE_MAX(interlace_time);
1314 1315 1316
                    UPDATE_MAX(blend_time);
                    UPDATE_MAX(draw_time);
                    UPDATE_MAX(composite_time);
1317
                    UPDATE_MAX(collect_time);
1318 1319 1320
                    UPDATE_MAX(frame_time);
                    total_bytes_sent += timing_collection[p].bytes_sent;
                }
1321
                timing->bytes_sent = total_bytes_sent;
1322

1323 1324
                g_timing_log[g_timing_log_size] = *timing;
                g_timing_log_size++;
1325 1326 1327 1328 1329 1330
            }
        }

        free(timing_collection);
    }

1331
    free_region_divide(region_divisions);
1332 1333
    free(timing_array);

1334 1335 1336 1337 1338
    pre_rendered_image = icetImageNull();
    if (pre_rendered_image_buffer != NULL) {
        free(pre_rendered_image_buffer);
    }

1339 1340 1341
    return TEST_PASSED;
}

1342
static int SimpleTimingDoParameterStudies()
1343 1344 1345 1346 1347
{
    if (g_do_magic_k_study) {
        IceTContext original_context = icetGetContext();
        IceTInt magic_k;
        for (magic_k = 2; magic_k <= g_max_magic_k; magic_k *= 2) {
1348
            char k_string[64];
1349
            int retval;
1350

1351
            icetSnprintf(k_string, 64, "%d", magic_k);
Kenneth Moreland's avatar
Kenneth Moreland committed
1352
            icetPutEnv("ICET_MAGIC_K", k_string);
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

            /* This is a bit hackish.  The magic k value is set when the IceT
               context is initialized.  Thus, for the environment to take
               effect, we need to make a new context.  (Another benefit:
               resetting buffers.)  To make a new context, we need to get the
               communiator. */
            {
                IceTCommunicator comm = icetGetCommunicator();
                icetCreateContext(comm);
            }

            retval = SimpleTimingDoRender();

            /* We no longer need the context we just created. */
            icetDestroyContext(icetGetContext());
            icetSetContext(original_context);

            if (retval != TEST_PASSED) { return retval; }
        }
        return TEST_PASSED;
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
    } else if (g_do_image_split_study) {
        IceTContext original_context = icetGetContext();
        IceTInt num_proc;
        IceTInt magic_k;
        IceTInt image_split;

        icetGetIntegerv(ICET_NUM_PROCESSES, &num_proc);
        icetGetIntegerv(ICET_MAGIC_K, &magic_k);

        for (image_split = g_min_image_split;
1383 1384
             image_split <= num_proc;
             image_split *= 2) {
1385
            char image_split_string[64];
1386
            int retval;
1387

1388
            icetSnprintf(image_split_string, 64, "%d", image_split);
Kenneth Moreland's avatar
Kenneth Moreland committed
1389
            icetPutEnv("ICET_MAX_IMAGE_SPLIT", image_split_string);
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

            /* This is a bit hackish.  The max image split value is set when the
               IceT context is initialized.  Thus, for the environment to take
               effect, we need to make a new context.  (Another benefit:
               resetting buffers.)  To make a new context, we need to get the
               communiator. */
            {
                IceTCommunicator comm = icetGetCommunicator();
                icetCreateContext(comm);
            }

            retval = SimpleTimingDoRender();

            /* We no longer need the context we just created. */
            icetDestroyContext(icetGetContext());
            icetSetContext(original_context);

            if (retval != TEST_PASSED) { return retval; }
        }
        return TEST_PASSED;
1410 1411 1412 1413 1414
    } else {
        return SimpleTimingDoRender();
    }
}

Kenneth Moreland's avatar