File: vtree.c

package info (click to toggle)
paraview 5.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,108 kB
  • ctags: 236,092
  • sloc: cpp: 2,416,026; ansic: 190,891; python: 99,856; xml: 81,001; tcl: 46,915; yacc: 5,039; java: 4,413; perl: 3,108; sh: 1,974; lex: 1,926; f90: 748; asm: 471; pascal: 228; makefile: 198; objc: 83; fortran: 31
file content (426 lines) | stat: -rw-r--r-- 16,791 bytes parent folder | download | duplicates (7)
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
/* -*- c -*- *******************************************************/
/*
 * Copyright (C) 2003 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.
 */

#include <IceT.h>

#include <IceTDevImage.h>
#include <IceTDevCommunication.h>
#include <IceTDevState.h>
#include <IceTDevDiagnostics.h>
#include "common.h"

#include <string.h>

#define VTREE_IMAGE_BUFFER              ICET_STRATEGY_BUFFER_0
#define VTREE_IN_SPARSE_IMAGE_BUFFER    ICET_STRATEGY_BUFFER_1
#define VTREE_OUT_SPARSE_IMAGE_BUFFER   ICET_STRATEGY_BUFFER_2
#define VTREE_INFO_BUFFER               ICET_STRATEGY_BUFFER_3
#define VTREE_ALL_CONTAINED_TMASKS_BUFFER ICET_STRATEGY_BUFFER_4

#define VTREE_IMAGE_DATA 40

struct node_info {
    int rank;
    int num_contained;
    int tile_held;
    int tile_sending;
    int tile_receiving;
    int send_dest;
    int recv_src;
};

static void sort_by_contained(struct node_info *info, int size);
static int find_sender(struct node_info *info, int num_proc,
                       int recv_node, int tile,
                       int display_node,
                       int num_tiles, IceTBoolean *all_contained_tmasks);
static int find_receiver(struct node_info *info, int num_proc,
                         int send_node, int tile,
                         int display_node,
                         int num_tiles, IceTBoolean *all_contained_tmasks);
static void do_send_receive(const struct node_info *my_info, int tile_held,
                            IceTInt num_tiles,
                            IceTBoolean *all_contained_tmasks,
                            IceTImage image,
                            IceTVoid *inSparseImageBuffer,
                            IceTSizeType inSparseImageBufferSize,
                            IceTSparseImage outSparseImage);

IceTImage icetVtreeCompose(void)
{
    IceTInt rank, num_proc;
    IceTInt num_tiles;
    IceTInt max_width, max_height;
    const IceTInt *display_nodes;
    IceTInt tile_displayed;
    IceTBoolean *all_contained_tmasks;
    const IceTInt *tile_viewports;
    IceTImage image;
    IceTVoid *inSparseImageBuffer;
    IceTSparseImage outSparseImage;
    IceTSizeType sparseImageSize;
    struct node_info *info;
    struct node_info *my_info;
    int tile, node;
    int tiles_transfered;
    int tile_held = -1;

    icetRaiseDebug("In vtreeCompose");

  /* Get state. */
    icetGetIntegerv(ICET_RANK, &rank);
    icetGetIntegerv(ICET_NUM_PROCESSES, &num_proc);
    icetGetIntegerv(ICET_NUM_TILES, &num_tiles);
    icetGetIntegerv(ICET_TILE_MAX_WIDTH, &max_width);
    icetGetIntegerv(ICET_TILE_MAX_HEIGHT, &max_height);
    display_nodes = icetUnsafeStateGetInteger(ICET_DISPLAY_NODES);
    tile_viewports = icetUnsafeStateGetInteger(ICET_TILE_VIEWPORTS);
    icetGetIntegerv(ICET_TILE_DISPLAYED, &tile_displayed);

  /* Allocate buffers. */
    sparseImageSize = icetSparseImageBufferSize(max_width, max_height);

    image                = icetGetStateBufferImage(VTREE_IMAGE_BUFFER,
                                                   max_width, max_height);
    inSparseImageBuffer  = icetGetStateBuffer(VTREE_IN_SPARSE_IMAGE_BUFFER,
                                              sparseImageSize);
    outSparseImage       = icetGetStateBufferSparseImage(
                                                  VTREE_OUT_SPARSE_IMAGE_BUFFER,
                                                  max_width, max_height);
    info                 = icetGetStateBuffer(VTREE_INFO_BUFFER,
                                             sizeof(struct node_info)*num_proc);
    all_contained_tmasks = icetGetStateBuffer(VTREE_ALL_CONTAINED_TMASKS_BUFFER,
                                        sizeof(IceTBoolean)*num_proc*num_tiles);

    icetGetBooleanv(ICET_ALL_CONTAINED_TILES_MASKS, all_contained_tmasks);
    
  /* Initialize info array. */
    for (node = 0; node < num_proc; node++) {
        info[node].rank = node;
        info[node].tile_held = -1;        /* Id of tile image held in memory. */
        info[node].num_contained = 0;        /* # of images to be rendered. */
        for (tile = 0; tile < num_tiles; tile++) {
            if (all_contained_tmasks[node*num_tiles + tile]) {
                info[node].num_contained++;
            }
        }
    }

#define CONTAINS_TILE(nodei, tile)                                \
    (all_contained_tmasks[info[nodei].rank*num_tiles+(tile)])

    tile_held = -1;
    do {
        int recv_node;

        tiles_transfered = 0;
        sort_by_contained(info, num_proc);
        for (node = 0; node < num_proc; node++) {
            info[node].tile_sending = -1;
            info[node].tile_receiving = -1;
        }

        for (recv_node = 0; recv_node < num_proc; recv_node++) {
            struct node_info *recv_info = info + recv_node;

            if (recv_info->tile_receiving >= 0) continue;

            if (recv_info->tile_held >= 0) {
              /* This node is holding a tile.  It must either send or
                 receive this tile. */
                if (find_sender(info, num_proc, recv_node, recv_info->tile_held,
                                display_nodes[recv_info->tile_held],
                                num_tiles, all_contained_tmasks)) {
                    tiles_transfered = 1;
                    continue;
                }

              /* Could not find a match for a sender, how about someone who
                 can receive it? */
                if (   (recv_info->tile_sending < 0)
                    && (recv_info->rank != display_nodes[recv_info->tile_held])
                    && find_receiver(info, num_proc, recv_node,
                                     recv_info->tile_held,
                                     display_nodes[recv_info->tile_held],
                                     num_tiles, all_contained_tmasks) ) {
                    tiles_transfered = 1;
                } else {
                  /* Could not send or receive.  Give up. */
                    continue;
                }
            }

          /* OK.  Let's try to receive any tile that we still have. */
            for (tile = 0; tile < num_tiles; tile++) {
                if (   (   !CONTAINS_TILE(recv_node, tile)
                        && (display_nodes[tile] != recv_info->rank) )
                    || (recv_info->tile_sending == tile) ) continue;
                if (find_sender(info, num_proc, recv_node, tile,
                                display_nodes[tile], num_tiles,
                                all_contained_tmasks)) {
                    tiles_transfered = 1;
                    break;
                }
            }
        }

      /* Now that we figured out who is sending to who, do the actual
         send and receive. */
        my_info = NULL;
        for (node = 0; node < num_proc; node++) {
            if (info[node].rank == rank) {
                my_info = info + node;
                break;
            }
        }

        do_send_receive(my_info, tile_held, num_tiles,
                        all_contained_tmasks,
                        image, inSparseImageBuffer, sparseImageSize,
                        outSparseImage);

        tile_held = my_info->tile_held;

    } while (tiles_transfered);

  /* It's possible that a composited image ended up on a processor that        */
  /* is not the display node for that image.  Do one last round of        */
  /* transfers to make sure all the tiles ended up in the right place.        */
    for (node = 0; node < num_proc; node++) {
        if (info[node].rank == rank) {
            my_info = info + node;
            break;
        }
    }
    my_info->tile_receiving = -1;
    my_info->tile_sending = -1;
    if ((my_info->tile_held >= 0) && (my_info->tile_held != tile_displayed)) {
      /* I'm holding an image that does not belong to me.  Ship it off. */
        my_info->tile_sending = my_info->tile_held;
        my_info->send_dest = display_nodes[my_info->tile_held];
        my_info->tile_held = -1;
    }
    if ((my_info->tile_held != tile_displayed) && (tile_displayed >= 0)) {
      /* Someone may be holding an image that belongs to me.  Check. */
        for (node = 0; node < num_proc; node++) {
            if (info[node].tile_held == tile_displayed) {
                my_info->tile_receiving = tile_displayed;
                my_info->recv_src = info[node].rank;
                my_info->tile_held = tile_displayed;
                break;
            }
        }
    }
    do_send_receive(my_info, tile_held,
                    num_tiles, all_contained_tmasks,
                    image, inSparseImageBuffer, sparseImageSize,
                    outSparseImage);
    tile_held = my_info->tile_held;

  /* Hacks for when "this" tile was not rendered. */
    if ((tile_displayed >= 0) && (tile_displayed != tile_held)) {
        if (all_contained_tmasks[rank*num_tiles + tile_displayed]) {
          /* Only "this" node draws "this" tile.  Because the image never needed
              to be transferred, it was never rendered above.  Just render it
              now.  We might save some time by rendering with the true
              background color rather than correcting it later. */
            IceTFloat true_background[4];
            IceTInt true_background_word;
            IceTFloat original_background[4];
            IceTInt original_background_word;

            icetGetFloatv(ICET_TRUE_BACKGROUND_COLOR, true_background);
            icetGetIntegerv(ICET_TRUE_BACKGROUND_COLOR_WORD,
                            &true_background_word);

            icetGetFloatv(ICET_BACKGROUND_COLOR, original_background);
            icetGetIntegerv(ICET_BACKGROUND_COLOR_WORD,
                            &original_background_word);

            icetStateSetFloatv(ICET_BACKGROUND_COLOR, 4, true_background);
            icetStateSetInteger(ICET_BACKGROUND_COLOR_WORD,
                                true_background_word);

            icetRaiseDebug("Rendering tile to display.");
          /* This may uncessarily read a buffer if not outputing an input
             buffer */
            icetGetTileImage(tile_displayed, image);

            icetStateSetFloatv(ICET_BACKGROUND_COLOR, 4, original_background);
            icetStateSetInteger(ICET_BACKGROUND_COLOR_WORD,
                                original_background_word);
        } else {
          /* "This" tile is blank. */
            const IceTInt *display_tile_viewport
                = tile_viewports + 4*tile_displayed;
            IceTInt display_tile_width = display_tile_viewport[2];
            IceTInt display_tile_height = display_tile_viewport[3];

            icetRaiseDebug("Returning blank image.");
            icetImageSetDimensions(
                                image, display_tile_width, display_tile_height);
            icetClearImageTrueBackground(image);
        }
    } else if (tile_displayed >= 0) {
        icetImageCorrectBackground(image);
    }

    return image;
}

static int find_sender(struct node_info *info, int num_proc,
                       int recv_node, int tile,
                       int display_node, int num_tiles,
                       IceTBoolean *all_contained_tmasks)
{
    int send_node;
    int sender = -1;

    for (send_node = num_proc-1; send_node >= 0; send_node--) {
        if (   (info[send_node].tile_sending >= 0)
            || !CONTAINS_TILE(send_node, tile)
            || (info[send_node].tile_receiving == tile)
            || (info[send_node].rank == display_node)
            || (send_node == recv_node) ) {
            continue;
        }
        if (info[send_node].tile_held == tile) {
          /* Favor sending held images. */
            sender = send_node;
            break;
        } else if (sender < 0) {
            sender = send_node;
        }
    }
    
    if (sender >= 0) {
        info[recv_node].tile_held = tile;
        info[recv_node].tile_receiving = tile;
        info[recv_node].recv_src = info[sender].rank;
        info[sender].tile_sending = tile;
        info[sender].send_dest = info[recv_node].rank;
        if (info[sender].tile_held == tile) info[sender].tile_held = -1;
        info[sender].num_contained--;
        all_contained_tmasks[info[sender].rank*num_tiles + tile] = 0;
        return 1;
    } else {
        return 0;
    }
}
 
static int find_receiver(struct node_info *info, int num_proc,
                         int send_node, int tile,
                         int display_node, int num_tiles,
                         IceTBoolean *all_contained_tmasks)
{
    int recv_node;

    for (recv_node = send_node+1; recv_node < num_proc; recv_node++) {
        if (   (info[recv_node].tile_receiving < 0)
            && (   (info[recv_node].tile_held < 0)
                || (info[recv_node].tile_held == tile) )
            && (   CONTAINS_TILE(recv_node, tile)
                || (info[recv_node].rank == display_node) ) ) {
            info[recv_node].tile_held = tile;
            info[recv_node].tile_receiving = tile;
            info[recv_node].recv_src = info[send_node].rank;
            info[send_node].tile_sending = tile;
            info[send_node].send_dest = info[recv_node].rank;
            if (info[send_node].tile_held == tile) {
                info[send_node].tile_held = -1;
            }
            info[send_node].num_contained--;
            all_contained_tmasks[info[send_node].rank*num_tiles + tile] = 0;
            return 1;
        }
    }

    return 0;
}

/* Yes, bubblesort is a slow way to sort, but I don't expect the keys to
   change much between iterations and the list is not very long, so we
   should be OK. */
static void sort_by_contained(struct node_info *info, int size)
{
    int swap_happened;
    int node;

    do {
        swap_happened = 0;
        for (node = 0; node < size-1; node++) {
            if (info[node].num_contained > info[node+1].num_contained) {
                struct node_info tmp = info[node];
                info[node] = info[node+1];
                info[node+1] = tmp;
                swap_happened = 1;
            }
        }
    } while (swap_happened);
}

static void do_send_receive(const struct node_info *my_info, int tile_held,
                            IceTInt num_tiles,
                            IceTBoolean *all_contained_tmasks,
                            IceTImage image,
                            IceTVoid *inSparseImageBuffer,
                            IceTSizeType inSparseImageBufferSize,
                            IceTSparseImage outSparseImage)
{
    IceTSparseImage inSparseImage;
    IceTVoid *package_buffer;
    IceTSizeType package_size;

    if (my_info->tile_sending != -1) {
        icetRaiseDebug2("Sending tile %d to node %d.", my_info->tile_sending,
                        my_info->send_dest);
        if (tile_held == my_info->tile_sending) {
            icetCompressImage(image, outSparseImage);
            tile_held = -1;
        } else {
            icetGetCompressedTileImage(my_info->tile_sending,
                                       outSparseImage);
        }
        icetSparseImagePackageForSend(outSparseImage,
                                      &package_buffer, &package_size);
    }

    if (my_info->tile_receiving != -1) {
        icetRaiseDebug2("Receiving tile %d from node %d.",
                        my_info->tile_receiving, my_info->recv_src);
        if (   (tile_held != my_info->tile_receiving)
            && all_contained_tmasks[my_info->rank*num_tiles
                                   +my_info->tile_receiving])
        {
            icetGetTileImage(my_info->tile_receiving, image);
            tile_held = my_info->tile_receiving;
        }

        if (my_info->tile_sending != -1) {
            icetCommSendrecv(package_buffer, package_size, ICET_BYTE,
                             my_info->send_dest, VTREE_IMAGE_DATA,
                             inSparseImageBuffer, inSparseImageBufferSize,
                             ICET_BYTE, my_info->recv_src, VTREE_IMAGE_DATA);
        } else {
            icetCommRecv(inSparseImageBuffer, inSparseImageBufferSize,
                         ICET_BYTE, my_info->recv_src, VTREE_IMAGE_DATA);
        }
        inSparseImage =icetSparseImageUnpackageFromReceive(inSparseImageBuffer);

        if (tile_held == my_info->tile_receiving) {
            icetCompressedComposite(image, inSparseImage, 1);
        } else {
            icetDecompressImage(inSparseImage, image);
        }

    } else if (my_info->tile_sending != -1) {
        icetCommSend(package_buffer, package_size, ICET_BYTE,
                     my_info->send_dest, VTREE_IMAGE_DATA);
    }
}