~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to src/mesh/cs_join_set.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*============================================================================
 
2
 * Manipulation of global indexed list
 
3
 *===========================================================================*/
 
4
 
 
5
/*
 
6
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
7
 
 
8
  Copyright (C) 1998-2011 EDF S.A.
 
9
 
 
10
  This program is free software; you can redistribute it and/or modify it under
 
11
  the terms of the GNU General Public License as published by the Free Software
 
12
  Foundation; either version 2 of the License, or (at your option) any later
 
13
  version.
 
14
 
 
15
  This program is distributed in the hope that it will be useful, but WITHOUT
 
16
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
18
  details.
 
19
 
 
20
  You should have received a copy of the GNU General Public License along with
 
21
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
22
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
23
*/
 
24
 
 
25
/*----------------------------------------------------------------------------*/
 
26
 
 
27
#if defined(HAVE_CONFIG_H)
 
28
#include "cs_config.h"
 
29
#endif
 
30
 
 
31
/*----------------------------------------------------------------------------
 
32
 * Standard C library headers
 
33
 *---------------------------------------------------------------------------*/
 
34
 
 
35
#include <assert.h>
 
36
#include <stdio.h>
 
37
#include <string.h>
 
38
 
 
39
/*----------------------------------------------------------------------------
 
40
 * BFT library headers
 
41
 *---------------------------------------------------------------------------*/
 
42
 
 
43
#include <bft_mem.h>
 
44
 
 
45
/*----------------------------------------------------------------------------
 
46
 * FVM library headers
 
47
 *---------------------------------------------------------------------------*/
 
48
 
 
49
#include <fvm_io_num.h>
 
50
#include <fvm_order.h>
 
51
#include <fvm_parall.h>
 
52
 
 
53
/*----------------------------------------------------------------------------
 
54
 *  Local headers
 
55
 *---------------------------------------------------------------------------*/
 
56
 
 
57
#include "cs_join_util.h"
 
58
#include "cs_search.h"
 
59
#include "cs_sort.h"
 
60
 
 
61
/*----------------------------------------------------------------------------
 
62
 *  Header for the current file
 
63
 *---------------------------------------------------------------------------*/
 
64
 
 
65
#include "cs_join_set.h"
 
66
 
 
67
/*---------------------------------------------------------------------------*/
 
68
 
 
69
BEGIN_C_DECLS
 
70
 
 
71
/*============================================================================
 
72
 * Macro and type definitions
 
73
 *===========================================================================*/
 
74
 
 
75
/*============================================================================
 
76
 * Private function definitions
 
77
 *===========================================================================*/
 
78
 
 
79
/*----------------------------------------------------------------------------
 
80
 * Sort an array "a" and apply the sort to its associated array "b"
 
81
 * (global numbering).
 
82
 *
 
83
 * Sort is realized using a shell sort (Knuth algorithm).
 
84
 *
 
85
 * parameters:
 
86
 *   l <-- left bound
 
87
 *   r <-- right bound
 
88
 *   a <-> array to sort
 
89
 *   b <-> associated array
 
90
 *---------------------------------------------------------------------------*/
 
91
 
 
92
static void
 
93
_coupled_adapted_gnum_shellsort(int         l,
 
94
                                int         r,
 
95
                                fvm_gnum_t  a[],
 
96
                                fvm_gnum_t  b[])
 
97
{
 
98
  int  i, start;
 
99
  fvm_gnum_t  ref;
 
100
 
 
101
  int  size = r - l;
 
102
 
 
103
  if (size == 0)
 
104
    return;
 
105
 
 
106
  cs_sort_coupled_gnum_shell(l, r, a, b);
 
107
 
 
108
  /* Order b array for each sub-list where a[] is constant */
 
109
 
 
110
  start = l;
 
111
  while (start < r) {
 
112
 
 
113
    ref = a[start];
 
114
    for (i = start; i < r && ref == a[i]; i++);
 
115
 
 
116
    cs_sort_gnum_shell(start, i, b);
 
117
    start = i;
 
118
 
 
119
  }
 
120
 
 
121
}
 
122
 
 
123
/*----------------------------------------------------------------------------
 
124
 * Test if an array of local numbers with stride 2 is lexicographically
 
125
 * ordered.
 
126
 *
 
127
 * parameters:
 
128
 *   number   <-- array of all entity numbers (number of entity i
 
129
 *                given by number[i] or number[list[i] - 1])
 
130
 *   nb_ent   <-- number of entities considered
 
131
 *
 
132
 * returns:
 
133
 *   1 if ordered, 0 otherwise.
 
134
 *----------------------------------------------------------------------------*/
 
135
 
 
136
static int
 
137
_order_local_test_s2(const fvm_lnum_t  number[],
 
138
                     size_t            n_elts)
 
139
{
 
140
  size_t i = 0;
 
141
 
 
142
  assert (number != NULL || n_elts == 0);
 
143
 
 
144
  for (i = 1 ; i < n_elts ; i++) {
 
145
    size_t i_prev, k;
 
146
    cs_bool_t unordered = false;
 
147
    i_prev = i-1;
 
148
    for (k = 0; k < 2; k++) {
 
149
      if (number[i_prev*2 + k] < number[i*2 + k])
 
150
        break;
 
151
      else if (number[i_prev*2 + k] > number[i*2 + k])
 
152
        unordered = true;
 
153
    }
 
154
    if (unordered == true)
 
155
      break;
 
156
  }
 
157
 
 
158
  if (i == n_elts || n_elts == 0)
 
159
    return 1;
 
160
  else
 
161
    return 0;
 
162
}
 
163
 
 
164
/*----------------------------------------------------------------------------
 
165
 * Descend binary tree for the lexicographical ordering of an local numbering
 
166
 * array of stride 2.
 
167
 *
 
168
 * parameters:
 
169
 *   number    <-- pointer to numbers of entities that should be ordered.
 
170
 *   level     <-- level of the binary tree to descend
 
171
 *   n_elts    <-- number of entities in the binary tree to descend
 
172
 *   order     <-> ordering array
 
173
 *----------------------------------------------------------------------------*/
 
174
 
 
175
inline static void
 
176
_order_descend_tree_s2(const fvm_lnum_t  number[],
 
177
                       size_t            level,
 
178
                       const size_t      n_elts,
 
179
                       fvm_lnum_t        order[])
 
180
{
 
181
  size_t i_save, i1, i2, j, lv_cur;
 
182
 
 
183
  i_save = (size_t)(order[level]);
 
184
 
 
185
  while (level <= (n_elts/2)) {
 
186
 
 
187
    lv_cur = (2*level) + 1;
 
188
 
 
189
    if (lv_cur < n_elts - 1) {
 
190
 
 
191
      i1 = (size_t)(order[lv_cur+1]);
 
192
      i2 = (size_t)(order[lv_cur]);
 
193
 
 
194
      for (j = 0; j < 2; j++) {
 
195
        if (number[i1*2 + j] != number[i2*2 + j])
 
196
          break;
 
197
      }
 
198
 
 
199
      if (j < 2) {
 
200
        if (number[i1*2 + j] > number[i2*2 + j])
 
201
          lv_cur++;
 
202
      }
 
203
 
 
204
    }
 
205
 
 
206
    if (lv_cur >= n_elts) break;
 
207
 
 
208
    i1 = i_save;
 
209
    i2 = (size_t)(order[lv_cur]);
 
210
 
 
211
    for (j = 0; j < 2; j++) {
 
212
      if (number[i1*2 + j] != number[i2*2 + j])
 
213
        break;
 
214
    }
 
215
 
 
216
    if (j == 2) break;
 
217
    if (number[i1*2 + j] >= number[i2*2 + j]) break;
 
218
 
 
219
    order[level] = order[lv_cur];
 
220
    level = lv_cur;
 
221
 
 
222
  }
 
223
 
 
224
  order[level] = i_save;
 
225
}
 
226
 
 
227
/*----------------------------------------------------------------------------
 
228
 * Order array of local numbers with stride 2 lexicographically.
 
229
 *
 
230
 * parameters:
 
231
 *   number   <-- array of entity numbers
 
232
 *   order    --> pre-allocated ordering table
 
233
 *   n_elts   <-- number of entities considered
 
234
 *----------------------------------------------------------------------------*/
 
235
 
 
236
static void
 
237
_order_local_s2(const fvm_lnum_t  number[],
 
238
                fvm_lnum_t        order[],
 
239
                const size_t      n_elts)
 
240
{
 
241
  size_t i;
 
242
  fvm_lnum_t o_save;
 
243
 
 
244
  assert (number != NULL || n_elts == 0);
 
245
 
 
246
  /* Initialize ordering array */
 
247
 
 
248
  for (i = 0 ; i < n_elts ; i++)
 
249
    order[i] = i;
 
250
 
 
251
  if (n_elts < 2)
 
252
    return;
 
253
 
 
254
  /* Create binary tree */
 
255
 
 
256
  i = (n_elts / 2) ;
 
257
  do {
 
258
    i--;
 
259
    _order_descend_tree_s2(number, i, n_elts, order);
 
260
  } while (i > 0);
 
261
 
 
262
  /* Sort binary tree */
 
263
 
 
264
  for (i = n_elts - 1 ; i > 0 ; i--) {
 
265
    o_save   = order[0];
 
266
    order[0] = order[i];
 
267
    order[i] = o_save;
 
268
    _order_descend_tree_s2(number, 0, i, order);
 
269
  }
 
270
}
 
271
 
 
272
/*============================================================================
 
273
 * Public function definitions
 
274
 *===========================================================================*/
 
275
 
 
276
/*----------------------------------------------------------------------------
 
277
 * Allocate a resizable array.
 
278
 *
 
279
 * parameters:
 
280
 *   max_size <-- initial number of elements to allocate
 
281
 *
 
282
 * returns:
 
283
 *   pointer to a new alloacted resizable array
 
284
 *---------------------------------------------------------------------------*/
 
285
 
 
286
cs_join_rset_t *
 
287
cs_join_rset_create(cs_int_t  max_size)
 
288
{
 
289
  cs_join_rset_t  *new_set = NULL;
 
290
 
 
291
  if (max_size > 0) {
 
292
 
 
293
    BFT_MALLOC(new_set, 1, cs_join_rset_t);
 
294
 
 
295
    new_set->n_max_elts = max_size;
 
296
    new_set->n_elts = 0;
 
297
 
 
298
    BFT_MALLOC(new_set->array, max_size, cs_int_t);
 
299
 
 
300
  }
 
301
 
 
302
  return new_set;
 
303
}
 
304
 
 
305
/*----------------------------------------------------------------------------
 
306
 * Destroy a cs_join_rset_t structure.
 
307
 *
 
308
 * parameters:
 
309
 *   set <-- pointer to pointer to the cs_join_rset_t structure to destroy
 
310
 *---------------------------------------------------------------------------*/
 
311
 
 
312
void
 
313
cs_join_rset_destroy(cs_join_rset_t  **set)
 
314
{
 
315
  if (*set != NULL) {
 
316
    BFT_FREE((*set)->array);
 
317
    BFT_FREE(*set);
 
318
  }
 
319
}
 
320
 
 
321
/*----------------------------------------------------------------------------
 
322
 * Check if we need to resize the current cs_join_rset_t structure and do
 
323
 * it if necessary.
 
324
 *
 
325
 * parameters:
 
326
 *   set       <-- pointer to pointer to the cs_join_rset_t structure to test
 
327
 *   test_size <-- target size
 
328
 *---------------------------------------------------------------------------*/
 
329
 
 
330
void
 
331
cs_join_rset_resize(cs_join_rset_t  **set,
 
332
                    cs_int_t          test_size)
 
333
{
 
334
  if (*set != NULL) {
 
335
 
 
336
    if (test_size > 0) {
 
337
 
 
338
      cs_join_rset_t  *_set = *set;
 
339
 
 
340
      if (_set->n_max_elts == 0)
 
341
        _set->n_max_elts = test_size;
 
342
      else if (test_size >= _set->n_max_elts) {
 
343
        while (test_size >= _set->n_max_elts)
 
344
          _set->n_max_elts *= 2; /* Double the list size */
 
345
      }
 
346
 
 
347
      BFT_REALLOC(_set->array, _set->n_max_elts, cs_int_t);
 
348
      assert(test_size <= _set->n_max_elts);
 
349
 
 
350
    }
 
351
 
 
352
  }
 
353
  else
 
354
    *set = cs_join_rset_create(test_size);
 
355
}
 
356
 
 
357
/*----------------------------------------------------------------------------
 
358
 * Create a new cs_join_eset_t structure.
 
359
 *
 
360
 * parameters:
 
361
 *   init_size <-- number of initial equivalences to allocate
 
362
 *
 
363
 * returns:
 
364
 *   a pointer to a new cs_join_eset_t structure
 
365
 *---------------------------------------------------------------------------*/
 
366
 
 
367
cs_join_eset_t *
 
368
cs_join_eset_create(cs_int_t  init_size)
 
369
{
 
370
  cs_join_eset_t  *new_set = NULL;
 
371
 
 
372
  BFT_MALLOC(new_set, 1, cs_join_eset_t);
 
373
 
 
374
  new_set->n_max_equiv = init_size; /* default value */
 
375
  new_set->n_equiv = 0;
 
376
 
 
377
  BFT_MALLOC(new_set->equiv_couple, 2*new_set->n_max_equiv, cs_int_t);
 
378
 
 
379
  return new_set;
 
380
}
 
381
 
 
382
/*----------------------------------------------------------------------------
 
383
 * Check if the requested size if allocated in the structure.
 
384
 *
 
385
 * Reallocate cs_join_eset_t structure if necessary.
 
386
 *
 
387
 * parameters:
 
388
 *   request_size <-- necessary size
 
389
 *   equiv_set    <-> pointer to pointer to the cs_join_eset_t struct.
 
390
 *---------------------------------------------------------------------------*/
 
391
 
 
392
void
 
393
cs_join_eset_check_size(cs_int_t          request_size,
 
394
                        cs_join_eset_t  **equiv_set)
 
395
{
 
396
  cs_join_eset_t  *eset = *equiv_set;
 
397
 
 
398
  if (eset == NULL)
 
399
    eset = cs_join_eset_create(request_size);
 
400
 
 
401
  if (request_size + 1 > eset->n_max_equiv) {
 
402
 
 
403
    if (eset->n_max_equiv == 0)
 
404
      eset->n_max_equiv = 2;
 
405
 
 
406
    eset->n_max_equiv *= 2;
 
407
 
 
408
    BFT_REALLOC(eset->equiv_couple, 2*eset->n_max_equiv, cs_int_t);
 
409
 
 
410
  }
 
411
 
 
412
  /* Ensure return value is set */
 
413
 
 
414
  *equiv_set = eset;
 
415
}
 
416
 
 
417
/*----------------------------------------------------------------------------
 
418
 * Destroy a cs_join_eset_t structure.
 
419
 *
 
420
 * parameters:
 
421
 *   equiv_set <-- pointer to pointer to the structure to destroy
 
422
 *---------------------------------------------------------------------------*/
 
423
 
 
424
void
 
425
cs_join_eset_destroy(cs_join_eset_t  **equiv_set)
 
426
{
 
427
  if (*equiv_set != NULL) {
 
428
    BFT_FREE((*equiv_set)->equiv_couple);
 
429
    BFT_FREE(*equiv_set);
 
430
  }
 
431
}
 
432
 
 
433
/*----------------------------------------------------------------------------
 
434
 * Clean a cs_join_eset_t structure.
 
435
 *
 
436
 * If necessary, create a new cs_join_eset_t structure with no redundancy.
 
437
 *
 
438
 * parameters:
 
439
 *   eset <-- pointer to pointer to the cs_join_eset_t structure to clean
 
440
 *---------------------------------------------------------------------------*/
 
441
 
 
442
void
 
443
cs_join_eset_clean(cs_join_eset_t  **eset)
 
444
{
 
445
  int  i;
 
446
  cs_int_t  prev, current;
 
447
 
 
448
  cs_int_t  count = 0;
 
449
  fvm_lnum_t  *order = NULL;
 
450
  cs_join_eset_t  *new_eset = NULL;
 
451
  cs_join_eset_t  *_eset = *eset;
 
452
 
 
453
  if (_eset == NULL)
 
454
    return;
 
455
 
 
456
  if (_eset->n_equiv == 1)
 
457
    return;
 
458
 
 
459
  BFT_MALLOC(order, _eset->n_equiv, fvm_lnum_t);
 
460
 
 
461
  if (_order_local_test_s2(_eset->equiv_couple,
 
462
                           _eset->n_equiv) == false) {
 
463
 
 
464
    /* Order equiv_lst */
 
465
 
 
466
    _order_local_s2(_eset->equiv_couple,
 
467
                    order,
 
468
                    _eset->n_equiv);
 
469
 
 
470
  }
 
471
  else
 
472
    for (i = 0; i < _eset->n_equiv; i++)
 
473
      order[i] = i;
 
474
 
 
475
  /* Count the number of redundancies */
 
476
 
 
477
  count = 0;
 
478
 
 
479
  for (i = 1; i < _eset->n_equiv; i++) {
 
480
 
 
481
    prev = order[i-1];
 
482
    current = order[i];
 
483
 
 
484
    if (_eset->equiv_couple[2*prev] == _eset->equiv_couple[2*current])
 
485
      if (_eset->equiv_couple[2*prev+1] == _eset->equiv_couple[2*current+1])
 
486
        count++;
 
487
 
 
488
  } /* End of loop on equivalences */
 
489
 
 
490
  new_eset = cs_join_eset_create(_eset->n_equiv - count);
 
491
 
 
492
  new_eset->n_equiv =  _eset->n_equiv - count;
 
493
 
 
494
  if (new_eset->n_equiv > new_eset->n_max_equiv) {
 
495
    new_eset->n_max_equiv = new_eset->n_equiv;
 
496
    BFT_REALLOC(new_eset->equiv_couple, 2*new_eset->n_max_equiv, cs_int_t);
 
497
  }
 
498
 
 
499
  if (new_eset->n_equiv > 0) {
 
500
 
 
501
    new_eset->equiv_couple[0] = _eset->equiv_couple[2*order[0]];
 
502
    new_eset->equiv_couple[1] = _eset->equiv_couple[2*order[0]+1];
 
503
    count = 1;
 
504
 
 
505
    for (i = 1; i < _eset->n_equiv; i++) {
 
506
 
 
507
      prev = order[i-1];
 
508
      current = order[i];
 
509
 
 
510
      if (_eset->equiv_couple[2*prev] != _eset->equiv_couple[2*current]) {
 
511
        new_eset->equiv_couple[2*count] = _eset->equiv_couple[2*current];
 
512
        new_eset->equiv_couple[2*count+1] = _eset->equiv_couple[2*current+1];
 
513
        count++;
 
514
      }
 
515
      else {
 
516
 
 
517
        if (_eset->equiv_couple[2*prev+1] != _eset->equiv_couple[2*current+1]) {
 
518
          new_eset->equiv_couple[2*count] = _eset->equiv_couple[2*current];
 
519
          new_eset->equiv_couple[2*count+1] = _eset->equiv_couple[2*current+1];
 
520
          count++;
 
521
        }
 
522
 
 
523
      }
 
524
 
 
525
    } /* End of loop on equivalences */
 
526
 
 
527
    assert(count == new_eset->n_equiv);
 
528
 
 
529
  }
 
530
 
 
531
  *eset = new_eset;
 
532
 
 
533
  /* Free memory */
 
534
 
 
535
  cs_join_eset_destroy(&_eset);
 
536
 
 
537
  BFT_FREE(order);
 
538
}
 
539
 
 
540
/*----------------------------------------------------------------------------
 
541
 * Create a cs_join_gset_t structure (indexed list on global numbering)
 
542
 *
 
543
 * parameters:
 
544
 *   n_elts <-- number of elements composing the list
 
545
 *
 
546
 * returns:
 
547
 *   a new allocated pointer to a cs_join_gset_t structure.
 
548
 *---------------------------------------------------------------------------*/
 
549
 
 
550
cs_join_gset_t *
 
551
cs_join_gset_create(cs_int_t  n_elts)
 
552
{
 
553
  cs_int_t  i;
 
554
 
 
555
  cs_join_gset_t  *new_set = NULL;
 
556
 
 
557
  BFT_MALLOC(new_set, 1, cs_join_gset_t);
 
558
  BFT_MALLOC(new_set->g_elts, n_elts, fvm_gnum_t);
 
559
 
 
560
  new_set->n_elts = n_elts;
 
561
  new_set->index = NULL;
 
562
 
 
563
  BFT_MALLOC(new_set->index, n_elts + 1, cs_int_t);
 
564
 
 
565
  for (i = 0; i < n_elts + 1; i++)
 
566
    new_set->index[i] = 0;
 
567
 
 
568
  new_set->g_list = NULL;
 
569
 
 
570
  return new_set;
 
571
}
 
572
 
 
573
/*----------------------------------------------------------------------------
 
574
 * Build a cs_join_gset_t structure to store all the potential groups
 
575
 * between elements.
 
576
 *
 
577
 * Values in g_elts are the tag values and values in g_list
 
578
 * are position in tag array.
 
579
 *
 
580
 * parameters:
 
581
 *   n_elts <-- number of elements in tag array
 
582
 *   tag    <-- tag array used to define a new cs_join_gset_t
 
583
 *
 
584
 * returns:
 
585
 *   a new allocated cs_join_gset_t structure
 
586
 *---------------------------------------------------------------------------*/
 
587
 
 
588
cs_join_gset_t *
 
589
cs_join_gset_create_from_tag(cs_int_t          n_elts,
 
590
                             const fvm_gnum_t  tag[])
 
591
{
 
592
  cs_int_t  i, n_list_elts;
 
593
  fvm_gnum_t  prev;
 
594
 
 
595
  fvm_lnum_t  *order = NULL;
 
596
  cs_join_gset_t  *set = NULL;
 
597
 
 
598
  if (n_elts == 0)
 
599
    return  NULL;
 
600
 
 
601
  /* Order tag */
 
602
 
 
603
  assert(tag != NULL);
 
604
 
 
605
  BFT_MALLOC(order, n_elts, fvm_lnum_t);
 
606
 
 
607
  fvm_order_local_allocated(NULL, tag, order, n_elts);
 
608
 
 
609
  /* Create a cs_join_gset_t structure to store the initial position of equiv.
 
610
     element in tag */
 
611
 
 
612
  prev = tag[order[0]];
 
613
  n_list_elts = 1;
 
614
 
 
615
  /* Count the number of elements which will compose the set->g_elts */
 
616
 
 
617
  for (i = 1; i < n_elts; i++) {
 
618
 
 
619
    fvm_gnum_t  cur = tag[order[i]];
 
620
 
 
621
    if (prev != cur) {
 
622
      n_list_elts++;
 
623
      prev = cur;
 
624
    }
 
625
 
 
626
  }
 
627
 
 
628
  set = cs_join_gset_create(n_list_elts);
 
629
 
 
630
  if (n_list_elts > 0) {
 
631
 
 
632
    cs_int_t  shift;
 
633
    cs_int_t  count = 0;
 
634
 
 
635
    /* Define the list of elements in set->g_elts and count the number of
 
636
       associated entities */
 
637
 
 
638
    prev = tag[order[0]];
 
639
    set->g_elts[0] = prev;
 
640
    set->index[1] += 1;
 
641
    n_list_elts = 1;
 
642
 
 
643
    for (i = 1; i < n_elts; i++) {
 
644
 
 
645
      fvm_gnum_t  cur = tag[order[i]];
 
646
 
 
647
      if (prev != cur) {
 
648
        prev = cur;
 
649
        set->g_elts[n_list_elts] = cur;
 
650
        n_list_elts++;
 
651
        set->index[n_list_elts] += 1;
 
652
      }
 
653
      else
 
654
        set->index[n_list_elts] += 1;
 
655
 
 
656
    }
 
657
 
 
658
    /* Build index for the set */
 
659
 
 
660
    for (i = 0; i < set->n_elts; i++)
 
661
      set->index[i+1] += set->index[i];
 
662
 
 
663
    /* Fill list */
 
664
 
 
665
    BFT_MALLOC(set->g_list, set->index[set->n_elts], fvm_gnum_t);
 
666
 
 
667
    n_list_elts = 0;
 
668
    prev = tag[order[0]];
 
669
    set->g_list[0] = order[0];
 
670
 
 
671
    for (i = 1; i < n_elts; i++) {
 
672
 
 
673
      fvm_lnum_t  o_id = order[i];
 
674
      fvm_gnum_t  cur = tag[o_id];
 
675
 
 
676
      if (prev != cur) {
 
677
        prev = cur;
 
678
        count = 0;
 
679
        n_list_elts++;
 
680
        shift = set->index[n_list_elts];
 
681
        set->g_list[shift] = o_id;
 
682
      }
 
683
      else {
 
684
        count++;
 
685
        shift = count + set->index[n_list_elts];
 
686
        set->g_list[shift] = o_id;
 
687
      }
 
688
 
 
689
    }
 
690
 
 
691
  } /* End if n_elts > 0 */
 
692
 
 
693
  /* Free memory */
 
694
 
 
695
  BFT_FREE(order);
 
696
 
 
697
  /* Returns pointers */
 
698
 
 
699
  return  set;
 
700
}
 
701
 
 
702
/*----------------------------------------------------------------------------
 
703
 * Create a new cs_join_gset_t which holds equivalences between elements of
 
704
 * g_list in cs_join_gset_t.
 
705
 *
 
706
 * For a subset of equivalences, we store their initial value in the return
 
707
 * cs_join_gset_t structure. A subset is defined if at least two elements
 
708
 * are equivalent.
 
709
 *
 
710
 * The behavior of this function is near from cs_join_gset_create_from_tag
 
711
 * but we don't store the position in init_array but its value in init_array.
 
712
 *
 
713
 * parameters:
 
714
 *   set        <-- pointer to a cs_join_gset_t structure
 
715
 *   init_array <-- initial values of set->g_list
 
716
 *
 
717
 * returns:
 
718
 *   a new allocated cs_join_gset_t structure
 
719
 *---------------------------------------------------------------------------*/
 
720
 
 
721
cs_join_gset_t *
 
722
cs_join_gset_create_by_equiv(const cs_join_gset_t  *set,
 
723
                             const fvm_gnum_t       init_array[])
 
724
{
 
725
  cs_int_t  i, list_size, n_equiv_grp, count, shift, o_id;
 
726
  fvm_gnum_t  prev, cur;
 
727
 
 
728
  cs_int_t  save_i = -1;
 
729
  fvm_lnum_t  *order = NULL;
 
730
  fvm_gnum_t  *couple_list = NULL;
 
731
  cs_join_gset_t  *equiv = NULL;
 
732
 
 
733
  if (init_array == NULL)
 
734
    return NULL;
 
735
 
 
736
  assert(set != NULL);
 
737
 
 
738
  list_size = set->index[set->n_elts];
 
739
 
 
740
  /* Order set->g_list */
 
741
 
 
742
  BFT_MALLOC(order, list_size, fvm_lnum_t);
 
743
  BFT_MALLOC(couple_list, 2*list_size, fvm_gnum_t);
 
744
 
 
745
  for (i = 0; i < list_size; i++) {
 
746
    couple_list[2*i] = set->g_list[i];
 
747
    couple_list[2*i+1] = init_array[i];
 
748
  }
 
749
 
 
750
  fvm_order_local_allocated_s(NULL, couple_list, 2, order, list_size);
 
751
 
 
752
  /* Create a cs_join_gset_t structure to store the initial value of equiv.
 
753
     element in set->g_list */
 
754
 
 
755
  /* Count the number of elements which will compose the equiv->g_elts */
 
756
 
 
757
  n_equiv_grp = 0;
 
758
  prev = set->g_list[order[0]];
 
759
  count = 0;
 
760
 
 
761
  for (i = 1; i < list_size; i++) {
 
762
 
 
763
    cur = set->g_list[order[i]];
 
764
 
 
765
    if (prev != cur) {
 
766
      count = 0;
 
767
      prev = cur;
 
768
    }
 
769
    else {
 
770
      count++;
 
771
      if (count == 1)
 
772
        n_equiv_grp++;
 
773
    }
 
774
 
 
775
  }
 
776
 
 
777
  equiv = cs_join_gset_create(n_equiv_grp);
 
778
 
 
779
  if (n_equiv_grp > 0) {
 
780
 
 
781
    /* Define the list of elements in equiv->g_list and count the number of
 
782
       associated elements */
 
783
 
 
784
    n_equiv_grp = 0;
 
785
    prev = set->g_list[order[0]];
 
786
    count = 0;
 
787
 
 
788
    for (i = 1; i < list_size; i++) {
 
789
 
 
790
      cur = set->g_list[order[i]];
 
791
 
 
792
      if (prev != cur) {
 
793
        count = 0;
 
794
        prev = cur;
 
795
      }
 
796
      else {
 
797
        count++;
 
798
        if (count == 1) { /* Add this group */
 
799
          equiv->g_elts[n_equiv_grp] = cur;
 
800
          n_equiv_grp++;
 
801
          equiv->index[n_equiv_grp] = 1;
 
802
        }
 
803
        else {
 
804
          assert(count > 1);
 
805
          equiv->index[n_equiv_grp] += 1;
 
806
        }
 
807
 
 
808
      } /* cur == prev */
 
809
 
 
810
    } /* End of loop on list_size */
 
811
 
 
812
    /* Build index for the set */
 
813
 
 
814
    for (i = 0; i < equiv->n_elts; i++)
 
815
      equiv->index[i+1] += equiv->index[i];
 
816
 
 
817
    /* Fill list */
 
818
 
 
819
    BFT_MALLOC(equiv->g_list, equiv->index[equiv->n_elts], fvm_gnum_t);
 
820
 
 
821
    n_equiv_grp = 0;
 
822
    prev = set->g_list[order[0]] + 1;
 
823
 
 
824
    for (i = 0; i < list_size; i++) {
 
825
 
 
826
      o_id = order[i];
 
827
      cur = set->g_list[o_id];
 
828
 
 
829
      if (prev != cur) {
 
830
        count = 0;
 
831
        prev = cur;
 
832
        save_i = o_id;
 
833
      }
 
834
      else {
 
835
 
 
836
        if (count == 0)
 
837
          n_equiv_grp++;
 
838
 
 
839
        shift = count + equiv->index[n_equiv_grp-1];
 
840
        if (cur != init_array[o_id])
 
841
          equiv->g_list[shift] = init_array[o_id];
 
842
        else
 
843
          equiv->g_list[shift] = init_array[save_i];
 
844
        count++;
 
845
 
 
846
      } /* cur == prev */
 
847
 
 
848
    } /* End of loop on list_size */
 
849
 
 
850
  } /* End if n_elts > 0 */
 
851
 
 
852
  /* Free memory */
 
853
 
 
854
  BFT_FREE(couple_list);
 
855
  BFT_FREE(order);
 
856
 
 
857
  /* Returns pointer */
 
858
 
 
859
  return  equiv;
 
860
}
 
861
 
 
862
/*----------------------------------------------------------------------------
 
863
 * Copy a cs_join_gset_t structure.
 
864
 *
 
865
 * parameters:
 
866
 *   src <-- pointer to the cs_join_gset_t structure to copy
 
867
 *
 
868
 * returns:
 
869
 *   a new allocated cs_join_gset_t structure.
 
870
 *---------------------------------------------------------------------------*/
 
871
 
 
872
cs_join_gset_t *
 
873
cs_join_gset_copy(const cs_join_gset_t  *src)
 
874
{
 
875
  cs_int_t  i;
 
876
 
 
877
  cs_join_gset_t  *copy = NULL;
 
878
 
 
879
  if (src == NULL)
 
880
    return copy;
 
881
 
 
882
  copy = cs_join_gset_create(src->n_elts);
 
883
 
 
884
  for (i = 0; i < src->n_elts; i++)
 
885
    copy->g_elts[i] = src->g_elts[i];
 
886
 
 
887
  for (i = 0; i < src->n_elts + 1; i++)
 
888
    copy->index[i] = src->index[i];
 
889
 
 
890
  BFT_MALLOC(copy->g_list, copy->index[copy->n_elts], fvm_gnum_t);
 
891
 
 
892
  for (i = 0; i < src->index[src->n_elts]; i++)
 
893
    copy->g_list[i] = src->g_list[i];
 
894
 
 
895
  return copy;
 
896
}
 
897
 
 
898
/*----------------------------------------------------------------------------
 
899
 * Destroy a cs_join_gset_t structure.
 
900
 *
 
901
 * parameters:
 
902
 *   set <-- pointer to pointer to the cs_join_gset_t structure to destroy
 
903
 *---------------------------------------------------------------------------*/
 
904
 
 
905
void
 
906
cs_join_gset_destroy(cs_join_gset_t  **set)
 
907
{
 
908
  if (*set != NULL) {
 
909
    BFT_FREE((*set)->index);
 
910
    BFT_FREE((*set)->g_elts);
 
911
    BFT_FREE((*set)->g_list);
 
912
    BFT_FREE(*set);
 
913
  }
 
914
}
 
915
 
 
916
/*----------------------------------------------------------------------------
 
917
 * Sort a cs_join_gset_t structure according to the global numbering of
 
918
 * the g_elts in cs_join_gset_t structure.
 
919
 *
 
920
 * parameters:
 
921
 *   set <-> pointer to the structure to order
 
922
 *---------------------------------------------------------------------------*/
 
923
 
 
924
void
 
925
cs_join_gset_sort_elts(cs_join_gset_t  *set)
 
926
{
 
927
  int  i, j, k, o_id, shift;
 
928
  cs_int_t  n_elts;
 
929
 
 
930
  cs_int_t  *new_index = NULL;
 
931
  fvm_lnum_t  *order = NULL;
 
932
  fvm_gnum_t  *tmp = NULL, *g_elts = NULL, *g_list = NULL;
 
933
 
 
934
  if (set == NULL)
 
935
    return;
 
936
 
 
937
  g_elts = set->g_elts;
 
938
  g_list = set->g_list;
 
939
  n_elts = set->n_elts;
 
940
 
 
941
  BFT_MALLOC(order, n_elts, fvm_lnum_t);
 
942
  BFT_MALLOC(tmp, n_elts, fvm_gnum_t);
 
943
  BFT_MALLOC(new_index, n_elts + 1, cs_int_t);
 
944
 
 
945
  for (i = 0; i < n_elts; i++)
 
946
    tmp[i] = g_elts[i];
 
947
 
 
948
  /* Sort g_elts */
 
949
 
 
950
  fvm_order_local_allocated(NULL, g_elts, order, n_elts);
 
951
 
 
952
  /* Reshape cs_join_gset_t according to the new ordering */
 
953
 
 
954
  new_index[0] = 0;
 
955
 
 
956
  for (i = 0; i < n_elts; i++) {
 
957
 
 
958
    o_id = order[i];
 
959
    g_elts[i] = tmp[o_id];
 
960
    new_index[i+1] =  new_index[i] + set->index[o_id+1] - set->index[o_id];
 
961
 
 
962
  } /* End of loop on elements */
 
963
 
 
964
  assert(new_index[n_elts] == set->index[n_elts]);
 
965
 
 
966
  /* Define new g_list */
 
967
 
 
968
  BFT_REALLOC(tmp, set->index[n_elts], fvm_gnum_t);
 
969
 
 
970
  for (i = 0; i < set->index[n_elts]; i++)
 
971
    tmp[i] = g_list[i];
 
972
 
 
973
  for (i = 0; i < n_elts; i++) {
 
974
 
 
975
    o_id = order[i];
 
976
    shift = new_index[i];
 
977
 
 
978
    for (k = 0, j = set->index[o_id]; j < set->index[o_id+1]; j++, k++)
 
979
      g_list[shift + k] = tmp[j];
 
980
 
 
981
  } /* End of loop on elements */
 
982
 
 
983
  /* Free memory */
 
984
 
 
985
  BFT_FREE(set->index);
 
986
  BFT_FREE(order);
 
987
  BFT_FREE(tmp);
 
988
 
 
989
  /* Return structure */
 
990
 
 
991
  set->index = new_index;
 
992
  set->g_elts = g_elts;
 
993
  set->g_list = g_list;
 
994
}
 
995
 
 
996
/*----------------------------------------------------------------------------
 
997
 * Sort each sub-list of the g_list array in a cs_join_gset_t structure.
 
998
 *
 
999
 * parameters:
 
1000
 *   p_set <-> pointer to the structure to sort
 
1001
 *---------------------------------------------------------------------------*/
 
1002
 
 
1003
void
 
1004
cs_join_gset_sort_sublist(cs_join_gset_t  *set)
 
1005
{
 
1006
  int  i;
 
1007
 
 
1008
  if (set == NULL)
 
1009
    return;
 
1010
 
 
1011
  /* Sort each sub-list */
 
1012
 
 
1013
  for (i = 0; i < set->n_elts; i++)
 
1014
    cs_sort_gnum_shell(set->index[i], set->index[i+1], set->g_list);
 
1015
}
 
1016
 
 
1017
/*----------------------------------------------------------------------------
 
1018
 * Invert a cs_join_gset_t structure.
 
1019
 *
 
1020
 * parameters:
 
1021
 *   set <-- pointer to the cs_join_gset_t structure to work with
 
1022
 *
 
1023
 * returns:
 
1024
 *   the new allocated and inverted set structure
 
1025
 *---------------------------------------------------------------------------*/
 
1026
 
 
1027
cs_join_gset_t *
 
1028
cs_join_gset_invert(const cs_join_gset_t  *set)
 
1029
{
 
1030
  int  i, j, o_id, shift, elt_id;
 
1031
  fvm_gnum_t  prev, cur;
 
1032
 
 
1033
  cs_int_t  list_size = 0, n_elts = 0;
 
1034
  cs_int_t  *count = NULL;
 
1035
  fvm_lnum_t  *order = NULL;
 
1036
  cs_join_gset_t  *invert_set = NULL;
 
1037
 
 
1038
  if (set == NULL)
 
1039
    return invert_set;
 
1040
 
 
1041
  /* Order g_list to count the number of different entities */
 
1042
 
 
1043
  list_size = set->index[set->n_elts];
 
1044
 
 
1045
  if (list_size == 0)
 
1046
    return cs_join_gset_create(list_size);
 
1047
 
 
1048
  BFT_MALLOC(order, list_size, fvm_lnum_t);
 
1049
 
 
1050
  fvm_order_local_allocated(NULL, set->g_list, order, list_size);
 
1051
 
 
1052
  /* Count the number of elements */
 
1053
 
 
1054
  prev = set->g_list[order[0]] + 1;
 
1055
 
 
1056
  for (i = 0; i < list_size; i++) {
 
1057
 
 
1058
    o_id = order[i];
 
1059
    cur = set->g_list[o_id];
 
1060
 
 
1061
    if (prev != cur) {
 
1062
      prev = cur;
 
1063
      n_elts++;
 
1064
    }
 
1065
 
 
1066
  }
 
1067
 
 
1068
  invert_set = cs_join_gset_create(n_elts);
 
1069
 
 
1070
  /* Fill g_elts for the inverted set */
 
1071
 
 
1072
  n_elts = 0;
 
1073
  prev = set->g_list[order[0]] + 1;
 
1074
 
 
1075
  for (i = 0; i < list_size; i++) {
 
1076
 
 
1077
    o_id = order[i];
 
1078
    cur = set->g_list[o_id];
 
1079
 
 
1080
    if (prev != cur) {
 
1081
      prev = cur;
 
1082
      invert_set->g_elts[n_elts] = cur;
 
1083
      n_elts++;
 
1084
    }
 
1085
 
 
1086
  }
 
1087
 
 
1088
  BFT_FREE(order);
 
1089
 
 
1090
  /* Define an index for the inverted set */
 
1091
 
 
1092
  for (i = 0; i < set->n_elts; i++) {
 
1093
    for (j = set->index[i]; j < set->index[i+1]; j++) {
 
1094
 
 
1095
      elt_id = cs_search_g_binary(invert_set->n_elts,
 
1096
                                  set->g_list[j],
 
1097
                                  invert_set->g_elts);
 
1098
 
 
1099
      if (elt_id == -1)
 
1100
        bft_error(__FILE__, __LINE__, 0,
 
1101
                  _("  Fail to build an inverted cs_join_gset_t structure.\n"
 
1102
                    "  Cannot find %u in element list.\n"), set->g_list[j]);
 
1103
 
 
1104
      invert_set->index[elt_id+1] += 1;
 
1105
 
 
1106
    }
 
1107
  } /* End of loop on elements of list */
 
1108
 
 
1109
  for (i = 0; i < invert_set->n_elts; i++)
 
1110
    invert_set->index[i+1] += invert_set->index[i];
 
1111
 
 
1112
  BFT_MALLOC(invert_set->g_list,
 
1113
             invert_set->index[invert_set->n_elts],
 
1114
             fvm_gnum_t);
 
1115
 
 
1116
  /* Define invert_set->g_list */
 
1117
 
 
1118
  BFT_MALLOC(count, invert_set->n_elts, cs_int_t);
 
1119
 
 
1120
  for (i = 0; i < invert_set->n_elts; i++)
 
1121
    count[i] = 0;
 
1122
 
 
1123
  for (i = 0; i < set->n_elts; i++) {
 
1124
    for (j = set->index[i]; j < set->index[i+1]; j++) {
 
1125
 
 
1126
      elt_id = cs_search_g_binary(invert_set->n_elts,
 
1127
                                  set->g_list[j],
 
1128
                                  invert_set->g_elts);
 
1129
 
 
1130
      shift = count[elt_id] + invert_set->index[elt_id];
 
1131
      invert_set->g_list[shift] = set->g_elts[i];
 
1132
      count[elt_id] += 1;
 
1133
 
 
1134
    }
 
1135
 
 
1136
  } /* End of loop on elements of list */
 
1137
 
 
1138
  BFT_FREE(count);
 
1139
 
 
1140
  return invert_set;
 
1141
}
 
1142
 
 
1143
/*----------------------------------------------------------------------------
 
1144
 * Delete redudancies in a cs_join_gset_t structure.
 
1145
 *
 
1146
 * Output set has an ordered sub-list for each element in set.
 
1147
 *
 
1148
 * parameters:
 
1149
 *   set <-> pointer to the structure to clean
 
1150
 *---------------------------------------------------------------------------*/
 
1151
 
 
1152
void
 
1153
cs_join_gset_clean(cs_join_gset_t  *set)
 
1154
{
 
1155
  int  i, j, l, r, save, n_elts;
 
1156
 
 
1157
  int  shift = 0;
 
1158
  fvm_gnum_t  *g_list = NULL;
 
1159
 
 
1160
  if (set == NULL)
 
1161
    return;
 
1162
 
 
1163
  g_list = set->g_list;
 
1164
  n_elts = set->n_elts;
 
1165
 
 
1166
  /* Sort g_list for each element in index */
 
1167
 
 
1168
  cs_join_gset_sort_sublist(set);
 
1169
 
 
1170
  /* Define a new index without redundant elements */
 
1171
 
 
1172
  save = set->index[0];
 
1173
 
 
1174
  for (i = 0; i < n_elts; i++) {
 
1175
 
 
1176
    l = save;
 
1177
    r = set->index[i+1];
 
1178
 
 
1179
    if (r - l > 0) {
 
1180
 
 
1181
      g_list[shift++] = g_list[l];
 
1182
 
 
1183
      for (j = l + 1; j < r; j++)
 
1184
        if (g_list[j] != g_list[j-1])
 
1185
          g_list[shift++] = g_list[j];
 
1186
 
 
1187
    }
 
1188
 
 
1189
    save = r;
 
1190
    set->index[i+1] = shift;
 
1191
 
 
1192
  } /* End of loop on elements */
 
1193
}
 
1194
 
 
1195
/*----------------------------------------------------------------------------
 
1196
 * Delete redudancies in g_list array of a cs_join_gset_t structure.
 
1197
 *
 
1198
 * parameters:
 
1199
 *   set          <-> pointer to the structure to clean
 
1200
 *   linked_array <-> array for which redundancies are scanned
 
1201
 *---------------------------------------------------------------------------*/
 
1202
 
 
1203
void
 
1204
cs_join_gset_clean_from_array(cs_join_gset_t  *set,
 
1205
                              fvm_gnum_t       linked_array[])
 
1206
{
 
1207
  int  i, j, l, r;
 
1208
  cs_int_t  n_elts;
 
1209
 
 
1210
  int  shift = 0;
 
1211
  cs_int_t  *new_index = NULL;
 
1212
  fvm_gnum_t  *g_list = NULL;
 
1213
 
 
1214
  if (set == NULL)
 
1215
    return;
 
1216
 
 
1217
  if (linked_array == NULL)
 
1218
    return;
 
1219
 
 
1220
  g_list = set->g_list;
 
1221
  n_elts = set->n_elts;
 
1222
 
 
1223
  /* Sort linked_array and apply change to g_list for each element in index */
 
1224
 
 
1225
  for (i = 0; i < n_elts; i++)
 
1226
    _coupled_adapted_gnum_shellsort(set->index[i],
 
1227
                                    set->index[i+1],
 
1228
                                    linked_array,
 
1229
                                    g_list);
 
1230
 
 
1231
  /* Define a new index without redundant elements */
 
1232
 
 
1233
  BFT_MALLOC(new_index, n_elts + 1, cs_int_t);
 
1234
  new_index[0] = 0;
 
1235
 
 
1236
  for (i = 0; i < n_elts; i++) {
 
1237
 
 
1238
    l = set->index[i];
 
1239
    r = set->index[i+1];
 
1240
 
 
1241
    if (r - l > 0) {
 
1242
 
 
1243
      g_list[shift] = g_list[l];
 
1244
      shift++;
 
1245
 
 
1246
      for (j = l + 1; j < r; j++) {
 
1247
 
 
1248
        if (linked_array[j] != linked_array[j-1]) {
 
1249
          g_list[shift] = g_list[j];
 
1250
          shift++;
 
1251
        }
 
1252
        assert(g_list[shift-1] <= g_list[j]);
 
1253
 
 
1254
      }
 
1255
      new_index[i+1] = shift;
 
1256
 
 
1257
    }
 
1258
    else { /* No match for this element */
 
1259
 
 
1260
      new_index[i+1] = new_index[i];
 
1261
 
 
1262
    }
 
1263
 
 
1264
  } /* End of loop on elements */
 
1265
 
 
1266
  /* Reshape cs_join_gset_t structure */
 
1267
 
 
1268
  BFT_REALLOC(g_list, new_index[n_elts], fvm_gnum_t);
 
1269
  BFT_FREE(set->index);
 
1270
 
 
1271
  set->index = new_index;
 
1272
  set->g_list = g_list;
 
1273
}
 
1274
 
 
1275
/*----------------------------------------------------------------------------
 
1276
 * Concatenate the two g_elts and g_list arrays.
 
1277
 *
 
1278
 * Order the new concatenated array and delete redundant elements.
 
1279
 * We get a single ordered array.
 
1280
 *
 
1281
 * parameters:
 
1282
 *   set       <-- pointer to the structure to work with
 
1283
 *   n_elts    --> number of elements in the new set
 
1284
 *   new_array --> pointer to the new created array
 
1285
 *---------------------------------------------------------------------------*/
 
1286
 
 
1287
void
 
1288
cs_join_gset_single_order(const cs_join_gset_t  *set,
 
1289
                          cs_int_t              *n_elts,
 
1290
                          fvm_gnum_t            *new_array[])
 
1291
{
 
1292
  cs_int_t  _n_elts = 0;
 
1293
  fvm_gnum_t  *_new_array = NULL;
 
1294
 
 
1295
  *n_elts = _n_elts;
 
1296
  *new_array = _new_array;
 
1297
 
 
1298
  if (set == NULL) /* Nothing to do */
 
1299
    return;
 
1300
 
 
1301
  _n_elts = set->n_elts;
 
1302
 
 
1303
  if (_n_elts > 0) {
 
1304
 
 
1305
    cs_int_t  i, shift;
 
1306
    fvm_gnum_t  prev;
 
1307
 
 
1308
    fvm_lnum_t  *order = NULL;
 
1309
    fvm_gnum_t  *elt_list = NULL;
 
1310
 
 
1311
    _n_elts += set->index[_n_elts]; /* Add number of elements in g_list */
 
1312
 
 
1313
    BFT_MALLOC(elt_list, _n_elts, fvm_gnum_t);
 
1314
 
 
1315
    for (i = 0; i < set->n_elts; i++)
 
1316
      elt_list[i] = set->g_elts[i];
 
1317
 
 
1318
    shift = set->n_elts;
 
1319
    for (i = 0; i < set->index[set->n_elts]; i++)
 
1320
      elt_list[shift + i] = set->g_list[i];
 
1321
 
 
1322
    /* Define an ordered list of elements */
 
1323
 
 
1324
    BFT_MALLOC(_new_array, _n_elts, fvm_gnum_t);
 
1325
    BFT_MALLOC(order, _n_elts, fvm_lnum_t);
 
1326
 
 
1327
    fvm_order_local_allocated(NULL, elt_list, order, _n_elts);
 
1328
 
 
1329
    for (i = 0; i < _n_elts; i++)
 
1330
      _new_array[i] = elt_list[order[i]];
 
1331
 
 
1332
    /* Delete redundant elements */
 
1333
 
 
1334
    shift = 0;
 
1335
    prev = _new_array[0] + 1;
 
1336
 
 
1337
    for (i = 0; i < _n_elts; i++) {
 
1338
 
 
1339
      if (prev != _new_array[i]) {
 
1340
 
 
1341
        _new_array[shift] = _new_array[i];
 
1342
        prev = _new_array[i];
 
1343
        shift++;
 
1344
 
 
1345
      }
 
1346
 
 
1347
    }
 
1348
    _n_elts = shift; /* Real number of elements without redundancy */
 
1349
 
 
1350
    /* Memory management */
 
1351
 
 
1352
    BFT_FREE(order);
 
1353
    BFT_FREE(elt_list);
 
1354
    BFT_REALLOC(_new_array, _n_elts, fvm_gnum_t);
 
1355
 
 
1356
  } /* End if n_elts > 0 */
 
1357
 
 
1358
  /* Set return pointers */
 
1359
 
 
1360
  *n_elts = _n_elts;
 
1361
  *new_array = _new_array;
 
1362
}
 
1363
 
 
1364
/*----------------------------------------------------------------------------
 
1365
 * Compress a g_list such as for each element "e" in g_elts:
 
1366
 *  - there is no redundancy for the linked elements of set->g_list
 
1367
 *  - there is no element in set->g_list < e except if this element is not
 
1368
 *    present in g_elts
 
1369
 *
 
1370
 * g_list and g_elts must be ordered before calling this function
 
1371
 *
 
1372
 * parameters:
 
1373
 *   set <-> pointer to the structure to work with
 
1374
 *---------------------------------------------------------------------------*/
 
1375
 
 
1376
void
 
1377
cs_join_gset_compress(cs_join_gset_t  *set)
 
1378
{
 
1379
  cs_int_t  i, j, start, end, save, shift;
 
1380
  fvm_gnum_t  cur;
 
1381
 
 
1382
  if (set == NULL)
 
1383
    return;
 
1384
 
 
1385
  if (set->n_elts == 0)
 
1386
    return;
 
1387
 
 
1388
  shift = 0;
 
1389
  save = set->index[0];
 
1390
 
 
1391
  for (i = 0; i < set->n_elts; i++) {
 
1392
 
 
1393
    cur = set->g_elts[i];
 
1394
    start = save;
 
1395
    end = set->index[i+1];
 
1396
 
 
1397
    if (end - start > 0) {
 
1398
 
 
1399
      /* Sub-lists must be ordered */
 
1400
 
 
1401
      if (cur < set->g_list[start])
 
1402
        set->g_list[shift++] = set->g_list[start];
 
1403
      else if (cur > set->g_list[start]) {
 
1404
 
 
1405
        int  id = cs_search_g_binary(i+1,
 
1406
                                     set->g_list[start],
 
1407
                                     set->g_elts);
 
1408
 
 
1409
        if (id == -1) /* Not found. Keep it. */
 
1410
          set->g_list[shift++] = set->g_list[start];
 
1411
 
 
1412
      }
 
1413
 
 
1414
      for (j = start + 1; j < end; j++) {
 
1415
 
 
1416
        if (cur < set->g_list[j]) {
 
1417
          if (set->g_list[j-1] != set->g_list[j])
 
1418
            set->g_list[shift++] = set->g_list[j];
 
1419
        }
 
1420
        else if (cur > set->g_list[j]) {
 
1421
 
 
1422
          int  id = cs_search_g_binary(i+1,
 
1423
                                       set->g_list[j],
 
1424
                                       set->g_elts);
 
1425
 
 
1426
          if (id == -1) /* Not found. Keep it. */
 
1427
            set->g_list[shift++] = set->g_list[j];
 
1428
 
 
1429
        }
 
1430
 
 
1431
      } /* End of loop on sub-elements */
 
1432
 
 
1433
    } /* end - start > 0 */
 
1434
 
 
1435
    save = end;
 
1436
    set->index[i+1] = shift;
 
1437
 
 
1438
  } /* End of loop on elements in g_elts */
 
1439
 
 
1440
  /* Reshape cs_join_gset_t structure if necessary */
 
1441
 
 
1442
  if (save != set->index[set->n_elts]) {
 
1443
    assert(save > set->index[set->n_elts]);
 
1444
    BFT_REALLOC(set->g_list, set->index[set->n_elts], fvm_gnum_t);
 
1445
  }
 
1446
 
 
1447
#if 0 && defined(DEBUG) && !defined(NDEBUG)
 
1448
  cs_join_gset_dump(NULL, set);
 
1449
#endif
 
1450
}
 
1451
 
 
1452
/*----------------------------------------------------------------------------
 
1453
 * Delete redundancies in set->g_elts.
 
1454
 *
 
1455
 * Merge sub-arrays associated to a common set->g_elts[i].
 
1456
 *
 
1457
 * parameters:
 
1458
 *   set       <-- pointer to the structure to work with
 
1459
 *   order_tag <-- 0: set->g_elts is not ordered, 1: ordered
 
1460
 *---------------------------------------------------------------------------*/
 
1461
 
 
1462
void
 
1463
cs_join_gset_merge_elts(cs_join_gset_t  *set,
 
1464
                        int              order_tag)
 
1465
{
 
1466
  cs_int_t  i, save, start, end, n_init_elts, n_sub_elts;
 
1467
  fvm_gnum_t  prev, cur;
 
1468
 
 
1469
  if (set == NULL)
 
1470
    return;
 
1471
 
 
1472
  n_init_elts = set->n_elts;
 
1473
 
 
1474
  if (n_init_elts < 2)
 
1475
    return;
 
1476
 
 
1477
  assert(order_tag == 0 || order_tag == 1);
 
1478
 
 
1479
  /* Delete redundancies in g_elts. Merge sub-lists associated to common
 
1480
     g_elts */
 
1481
 
 
1482
  if (order_tag == 0)
 
1483
    cs_join_gset_sort_elts(set);
 
1484
 
 
1485
  set->n_elts = 0;            /* Reset and will be redefinied */
 
1486
  prev = set->g_elts[0] + 1;  /* Force prev to be different from g_elts[0] */
 
1487
  save = set->index[0];
 
1488
 
 
1489
  for (i = 0; i < n_init_elts; i++) {
 
1490
 
 
1491
    cur = set->g_elts[i];
 
1492
    start = save;
 
1493
    end = set->index[i+1];
 
1494
    save = end;
 
1495
    n_sub_elts = end - start;
 
1496
 
 
1497
    if (prev != cur) {
 
1498
 
 
1499
      set->g_elts[set->n_elts] = cur;
 
1500
      set->n_elts += 1;
 
1501
      set->index[set->n_elts] = n_sub_elts;
 
1502
      prev = cur;
 
1503
 
 
1504
    }
 
1505
    else {
 
1506
 
 
1507
      set->index[set->n_elts] += n_sub_elts;
 
1508
 
 
1509
    } /* prev != next */
 
1510
 
 
1511
  } /* Loop on elements of set->g_elts */
 
1512
 
 
1513
  /* Get the new index */
 
1514
 
 
1515
  for (i = 0; i < set->n_elts; i++)
 
1516
    set->index[i+1] += set->index[i];
 
1517
 
 
1518
  /* Reshape cs_join_gset_t structure if necessary */
 
1519
 
 
1520
  if (n_init_elts != set->n_elts) {
 
1521
 
 
1522
    assert(n_init_elts > set->n_elts);
 
1523
 
 
1524
    BFT_REALLOC(set->g_elts, set->n_elts, fvm_gnum_t);
 
1525
    BFT_REALLOC(set->index, set->n_elts + 1, cs_int_t);
 
1526
    BFT_REALLOC(set->g_list, set->index[set->n_elts], fvm_gnum_t);
 
1527
 
 
1528
  }
 
1529
 
 
1530
#if 0 && defined(DEBUG) && !defined(NDEBUG)
 
1531
  cs_join_gset_dump(NULL, set);
 
1532
#endif
 
1533
}
 
1534
 
 
1535
#if defined(HAVE_MPI)
 
1536
 
 
1537
/*----------------------------------------------------------------------------
 
1538
 * Synchronize a cs_join_gset_t structure and distribute the resulting set
 
1539
 * over the rank using a round-robin distribution. Elements in sync_set
 
1540
 * are ordered and there is no redundancy but list may have redundancies.
 
1541
 * Use cs_join_gset_clean() to remove redundancies in g_list.
 
1542
 *
 
1543
 * parameters:
 
1544
 *   loc_set  <-> pointer to the local structure to work with
 
1545
 *   comm     <-- mpi_comm on which synchro. and distribution take place
 
1546
 *
 
1547
 * returns:
 
1548
 *   a synchronized and distributed cs_join_gset_t structure.
 
1549
 *---------------------------------------------------------------------------*/
 
1550
 
 
1551
cs_join_gset_t *
 
1552
cs_join_gset_robin_sync(cs_join_gset_t  *loc_set,
 
1553
                        MPI_Comm         comm)
 
1554
{
 
1555
  int  i, j, shift, elt_id;
 
1556
  int  rank, local_rank, n_ranks, n_recv_elts, n_sub_elts;
 
1557
  fvm_gnum_t  gnum;
 
1558
 
 
1559
  cs_int_t  *send_count = NULL, *recv_count = NULL;
 
1560
  cs_int_t  *send_shift = NULL, *recv_shift = NULL;
 
1561
  fvm_gnum_t  *send_buffer = NULL, *recv_buffer = NULL;
 
1562
  cs_join_gset_t  *sync_set = NULL;
 
1563
 
 
1564
  MPI_Comm_rank(comm, &local_rank);
 
1565
  MPI_Comm_size(comm, &n_ranks);
 
1566
 
 
1567
  /* Allocate parameters for MPI functions */
 
1568
 
 
1569
  BFT_MALLOC(send_count, n_ranks, cs_int_t);
 
1570
  BFT_MALLOC(recv_count, n_ranks, cs_int_t);
 
1571
  BFT_MALLOC(send_shift, n_ranks + 1, cs_int_t);
 
1572
  BFT_MALLOC(recv_shift, n_ranks + 1, cs_int_t);
 
1573
 
 
1574
  /* Initialization */
 
1575
 
 
1576
  for (i = 0; i < n_ranks; i++)
 
1577
    send_count[i] = 0;
 
1578
 
 
1579
  /* Synchronize list definition for each global element */
 
1580
 
 
1581
  for (i = 0; i < loc_set->n_elts; i++) {
 
1582
    rank = (loc_set->g_elts[i] - 1) % n_ranks;
 
1583
    send_count[rank] += 1;
 
1584
  }
 
1585
 
 
1586
  MPI_Alltoall(send_count, 1, MPI_INT, recv_count, 1, MPI_INT, comm);
 
1587
 
 
1588
  send_shift[0] = 0;
 
1589
  recv_shift[0] = 0;
 
1590
 
 
1591
  for (rank = 0; rank < n_ranks; rank++) {
 
1592
    send_shift[rank + 1] = send_shift[rank] + send_count[rank];
 
1593
    recv_shift[rank + 1] = recv_shift[rank] + recv_count[rank];
 
1594
  }
 
1595
 
 
1596
  n_recv_elts = recv_shift[n_ranks];
 
1597
 
 
1598
  /* Define sync_set: a distributed cs_join_gset_t structure which
 
1599
     synchronize data over the ranks */
 
1600
 
 
1601
  sync_set = cs_join_gset_create(n_recv_elts);
 
1602
 
 
1603
  /* Synchronize list definition for each global element */
 
1604
 
 
1605
  for (i = 0; i < n_ranks; i++)
 
1606
    send_count[i] = 0;
 
1607
 
 
1608
  for (i = 0; i < loc_set->n_elts; i++) {
 
1609
 
 
1610
    rank = (loc_set->g_elts[i] - 1) % n_ranks;
 
1611
    n_sub_elts = loc_set->index[i+1] - loc_set->index[i];
 
1612
    send_count[rank] += 2 + n_sub_elts;
 
1613
 
 
1614
  }
 
1615
 
 
1616
  MPI_Alltoall(send_count, 1, MPI_INT, recv_count, 1, MPI_INT, comm);
 
1617
 
 
1618
  send_shift[0] = 0;
 
1619
  recv_shift[0] = 0;
 
1620
 
 
1621
  for (rank = 0; rank < n_ranks; rank++) {
 
1622
    send_shift[rank + 1] = send_shift[rank] + send_count[rank];
 
1623
    recv_shift[rank + 1] = recv_shift[rank] + recv_count[rank];
 
1624
  }
 
1625
 
 
1626
  /* Fill send_buffer: global number and number of elements in index */
 
1627
 
 
1628
  BFT_MALLOC(send_buffer, send_shift[n_ranks], fvm_gnum_t);
 
1629
  BFT_MALLOC(recv_buffer, recv_shift[n_ranks], fvm_gnum_t);
 
1630
 
 
1631
  for (i = 0; i < n_ranks; i++)
 
1632
    send_count[i] = 0;
 
1633
 
 
1634
  for (i = 0; i < loc_set->n_elts; i++) {
 
1635
 
 
1636
    gnum = loc_set->g_elts[i];
 
1637
    rank = (gnum - 1) % n_ranks;
 
1638
    shift = send_shift[rank] + send_count[rank];
 
1639
    n_sub_elts = loc_set->index[i+1] - loc_set->index[i];
 
1640
 
 
1641
    send_buffer[shift++] = gnum;
 
1642
    send_buffer[shift++] = n_sub_elts;
 
1643
 
 
1644
    for (j = 0; j < n_sub_elts; j++)
 
1645
      send_buffer[shift + j] = loc_set->g_list[loc_set->index[i] + j];
 
1646
 
 
1647
    send_count[rank] += 2 + n_sub_elts;
 
1648
 
 
1649
  }
 
1650
 
 
1651
  MPI_Alltoallv(send_buffer, send_count, send_shift, FVM_MPI_GNUM,
 
1652
                recv_buffer, recv_count, recv_shift, FVM_MPI_GNUM,
 
1653
                comm);
 
1654
 
 
1655
  n_recv_elts = recv_shift[n_ranks];
 
1656
 
 
1657
  /* Partial free memory */
 
1658
 
 
1659
  BFT_FREE(send_buffer);
 
1660
  BFT_FREE(send_count);
 
1661
  BFT_FREE(send_shift);
 
1662
  BFT_FREE(recv_count);
 
1663
  BFT_FREE(recv_shift);
 
1664
 
 
1665
  /* Fill sync_set->index and define a new recv_count for the next comm. */
 
1666
 
 
1667
  i = 0; /* position in recv_buffer */
 
1668
  elt_id = 0; /* position of the element in sync_set */
 
1669
 
 
1670
  while (i < n_recv_elts) {
 
1671
 
 
1672
    sync_set->g_elts[elt_id] = recv_buffer[i++];
 
1673
    n_sub_elts = recv_buffer[i++];
 
1674
    sync_set->index[elt_id+1] = n_sub_elts;
 
1675
    i += n_sub_elts;
 
1676
    elt_id++;
 
1677
 
 
1678
  } /* End of loop on ranks */
 
1679
 
 
1680
  /* Build index on elements of sync_set */
 
1681
 
 
1682
  for (i = 0; i < sync_set->n_elts; i++)
 
1683
    sync_set->index[i+1] += sync_set->index[i];
 
1684
 
 
1685
  BFT_MALLOC(sync_set->g_list,
 
1686
             sync_set->index[sync_set->n_elts],
 
1687
             fvm_gnum_t);
 
1688
 
 
1689
  /* Fill g_list of sync_set */
 
1690
 
 
1691
  i = 0; /* position in recv_buffer */
 
1692
  elt_id = 0; /* position of the element in sync_set */
 
1693
 
 
1694
  while (i < n_recv_elts) {
 
1695
 
 
1696
    i++; /* element numbering */
 
1697
    shift = sync_set->index[elt_id];
 
1698
    n_sub_elts = recv_buffer[i++];
 
1699
 
 
1700
    for (j = 0; j < n_sub_elts; j++)
 
1701
      sync_set->g_list[j + shift] = recv_buffer[i++];
 
1702
 
 
1703
    elt_id++;
 
1704
 
 
1705
  } /* End of loop on ranks */
 
1706
 
 
1707
  BFT_FREE(recv_buffer);
 
1708
 
 
1709
  /* Return pointer */
 
1710
 
 
1711
  cs_join_gset_merge_elts(sync_set, 0); /* sync_set elts are not ordered */
 
1712
 
 
1713
  return  sync_set;
 
1714
}
 
1715
 
 
1716
/*----------------------------------------------------------------------------
 
1717
 * Update a local cs_join_gset_t structure from a distributed and
 
1718
 * synchronized cs_join_gset_t structure. Round-robin distribution is used
 
1719
 * to store synchronized elements.
 
1720
 *
 
1721
 * loc_set should not have redundant elements.
 
1722
 *
 
1723
 * parameters:
 
1724
 *   sync_set <-- pointer to the structure which holds a synchronized block
 
1725
 *   loc_set  <-> pointer to a local structure holding elements to update
 
1726
 *   comm     <-- comm on which synchronization and distribution take place
 
1727
 *---------------------------------------------------------------------------*/
 
1728
 
 
1729
void
 
1730
cs_join_gset_robin_update(const cs_join_gset_t  *sync_set,
 
1731
                          cs_join_gset_t        *loc_set,
 
1732
                          MPI_Comm               comm)
 
1733
{
 
1734
  int  i, j, k, shift, elt_id, start, end;
 
1735
  int  rank, local_rank, n_ranks, n_sub_elts, n_recv_elts;
 
1736
  fvm_gnum_t  gnum;
 
1737
 
 
1738
  cs_int_t  *send_count = NULL, *recv_count = NULL;
 
1739
  cs_int_t  *send_shift = NULL, *recv_shift = NULL, *wanted_rank_index = NULL;
 
1740
  fvm_gnum_t  *send_buffer = NULL, *recv_buffer = NULL, *wanted_elts = NULL;
 
1741
 
 
1742
  /* Sanity checks */
 
1743
 
 
1744
  assert(sync_set != NULL);
 
1745
 
 
1746
  /* Build a cs_join_block_info_t structure */
 
1747
 
 
1748
  MPI_Comm_rank(comm, &local_rank);
 
1749
  MPI_Comm_size(comm, &n_ranks);
 
1750
 
 
1751
  /* Allocate parameters for MPI functions */
 
1752
 
 
1753
  BFT_MALLOC(send_count, n_ranks, cs_int_t);
 
1754
  BFT_MALLOC(recv_count, n_ranks, cs_int_t);
 
1755
  BFT_MALLOC(send_shift, n_ranks + 1, cs_int_t);
 
1756
  BFT_MALLOC(recv_shift, n_ranks + 1, cs_int_t);
 
1757
  BFT_MALLOC(wanted_rank_index, n_ranks + 1, cs_int_t);
 
1758
 
 
1759
  /* Initialization */
 
1760
 
 
1761
  for (i = 0; i < n_ranks; i++)
 
1762
    send_count[i] = 0;
 
1763
 
 
1764
  /* Get a synchronized list definition for each global element */
 
1765
 
 
1766
  for (i = 0; i < loc_set->n_elts; i++) {
 
1767
    rank = (loc_set->g_elts[i] - 1) % n_ranks;
 
1768
    send_count[rank] += 1;
 
1769
  }
 
1770
 
 
1771
  MPI_Alltoall(send_count, 1, MPI_INT, recv_count, 1, MPI_INT, comm);
 
1772
 
 
1773
  send_shift[0] = 0;
 
1774
  wanted_rank_index[0] = 0;
 
1775
 
 
1776
  for (rank = 0; rank < n_ranks; rank++) {
 
1777
    send_shift[rank + 1] = send_shift[rank] + send_count[rank];
 
1778
    wanted_rank_index[rank + 1] = wanted_rank_index[rank] + recv_count[rank];
 
1779
  }
 
1780
 
 
1781
  /* Fill send_buffer: global number */
 
1782
 
 
1783
  BFT_MALLOC(send_buffer, send_shift[n_ranks], fvm_gnum_t);
 
1784
  BFT_MALLOC(wanted_elts, wanted_rank_index[n_ranks], fvm_gnum_t);
 
1785
 
 
1786
  for (i = 0; i < n_ranks; i++)
 
1787
    send_count[i] = 0;
 
1788
 
 
1789
  for (i = 0; i < loc_set->n_elts; i++) {
 
1790
 
 
1791
    gnum = loc_set->g_elts[i];
 
1792
    rank = (gnum - 1) % n_ranks;
 
1793
    shift = send_shift[rank] + send_count[rank];
 
1794
 
 
1795
    send_buffer[shift] = gnum;
 
1796
    send_count[rank] += 1;
 
1797
 
 
1798
  }
 
1799
 
 
1800
  MPI_Alltoallv(send_buffer, send_count, send_shift, FVM_MPI_GNUM,
 
1801
                wanted_elts, recv_count, wanted_rank_index, FVM_MPI_GNUM,
 
1802
                comm);
 
1803
 
 
1804
  /* Send new list definition holding by sync_set to ranks which have
 
1805
     request it */
 
1806
 
 
1807
  for (i = 0; i < n_ranks; i++)
 
1808
    send_count[i] = 0;
 
1809
 
 
1810
  for (rank = 0; rank < n_ranks; rank++) {
 
1811
 
 
1812
    for (i = wanted_rank_index[rank]; i < wanted_rank_index[rank+1]; i++) {
 
1813
 
 
1814
      elt_id = cs_search_g_binary(sync_set->n_elts,
 
1815
                                  wanted_elts[i],
 
1816
                                  sync_set->g_elts);
 
1817
 
 
1818
      assert(elt_id != -1);
 
1819
 
 
1820
      wanted_elts[i] = elt_id;
 
1821
      n_sub_elts = sync_set->index[elt_id+1] - sync_set->index[elt_id];
 
1822
      send_count[rank] +=  2 + n_sub_elts; /* glob. num,
 
1823
                                              n_sub_elts,
 
1824
                                              g_list */
 
1825
 
 
1826
    }
 
1827
 
 
1828
  } /* End of loop on ranks */
 
1829
 
 
1830
  MPI_Alltoall(send_count, 1, MPI_INT, recv_count, 1, MPI_INT, comm);
 
1831
 
 
1832
  send_shift[0] = 0;
 
1833
  recv_shift[0] = 0;
 
1834
 
 
1835
  for (rank = 0; rank < n_ranks; rank++) {
 
1836
    send_shift[rank + 1] = send_shift[rank] + send_count[rank];
 
1837
    recv_shift[rank + 1] = recv_shift[rank] + recv_count[rank];
 
1838
  }
 
1839
 
 
1840
  BFT_REALLOC(send_buffer, send_shift[n_ranks], fvm_gnum_t);
 
1841
  BFT_MALLOC(recv_buffer, recv_shift[n_ranks], fvm_gnum_t);
 
1842
 
 
1843
  for (i = 0; i < n_ranks; i++)
 
1844
    send_count[i] = 0;
 
1845
 
 
1846
  for (rank = 0; rank < n_ranks; rank++) {
 
1847
 
 
1848
    for (i = wanted_rank_index[rank]; i < wanted_rank_index[rank+1]; i++) {
 
1849
 
 
1850
      shift = send_shift[rank] + send_count[rank];
 
1851
      elt_id = wanted_elts[i];
 
1852
 
 
1853
      start = sync_set->index[elt_id];
 
1854
      end = sync_set->index[elt_id+1];
 
1855
      n_sub_elts = end - start;
 
1856
 
 
1857
      send_buffer[shift++] = sync_set->g_elts[elt_id];
 
1858
      send_buffer[shift++] = n_sub_elts;
 
1859
 
 
1860
      for (j = start, k = 0; j < end; j++, k++)
 
1861
        send_buffer[shift+k] = sync_set->g_list[j];
 
1862
 
 
1863
      send_count[rank] +=  2 + n_sub_elts; /* glob. num, n_sub_elts, g_list */
 
1864
 
 
1865
    }
 
1866
 
 
1867
  } /* End of loop on ranks */
 
1868
 
 
1869
  MPI_Alltoallv(send_buffer, send_count, send_shift, FVM_MPI_GNUM,
 
1870
                recv_buffer, recv_count, recv_shift, FVM_MPI_GNUM,
 
1871
                comm);
 
1872
 
 
1873
  n_recv_elts = recv_shift[n_ranks];
 
1874
 
 
1875
  /* Partial memory free */
 
1876
 
 
1877
  BFT_FREE(send_buffer);
 
1878
  BFT_FREE(send_count);
 
1879
  BFT_FREE(send_shift);
 
1880
  BFT_FREE(recv_count);
 
1881
  BFT_FREE(recv_shift);
 
1882
 
 
1883
  /* Re-initialize loc_set
 
1884
     As loc_set->g_elts and sync_set->g_elts are ordered, it's easier.
 
1885
     We can take values as they come */
 
1886
 
 
1887
  /* First redefine index */
 
1888
 
 
1889
  assert(loc_set->index[0] == 0);
 
1890
 
 
1891
  for (i = 0; i < loc_set->n_elts; i++)
 
1892
    loc_set->index[i+1] = 0;
 
1893
 
 
1894
  i = 0; /* id in recv_buffer */
 
1895
  j = 0; /* id in g_elts */
 
1896
 
 
1897
  while (i < n_recv_elts) {
 
1898
 
 
1899
    gnum = recv_buffer[i++];
 
1900
 
 
1901
    assert(loc_set->g_elts[j] = gnum);
 
1902
 
 
1903
    n_sub_elts = recv_buffer[i++];
 
1904
    loc_set->index[j+1] = n_sub_elts;
 
1905
 
 
1906
    i += n_sub_elts;
 
1907
    j++; /* go to the next elements */
 
1908
 
 
1909
  } /* End of loop on elements of recv_buffer */
 
1910
 
 
1911
  /* Define the new index */
 
1912
 
 
1913
  for (i = 0; i < loc_set->n_elts; i++)
 
1914
    loc_set->index[i+1] += loc_set->index[i];
 
1915
 
 
1916
  BFT_REALLOC(loc_set->g_list, loc_set->index[loc_set->n_elts], fvm_gnum_t);
 
1917
 
 
1918
  i = 0; /* id in recv_buffer */
 
1919
  j = 0; /* id in g_elts */
 
1920
 
 
1921
  while (i < n_recv_elts) {
 
1922
 
 
1923
    gnum = recv_buffer[i++];
 
1924
    n_sub_elts = recv_buffer[i++];
 
1925
 
 
1926
    assert(loc_set->g_elts[j] = gnum);
 
1927
 
 
1928
    for (k = 0; k < n_sub_elts; k++)
 
1929
      loc_set->g_list[loc_set->index[j] + k] = recv_buffer[i + k];
 
1930
 
 
1931
    i += n_sub_elts;
 
1932
    j++; /* go to the next elements */
 
1933
 
 
1934
  } /* End of loop on elements of recv_buffer */
 
1935
 
 
1936
  BFT_FREE(recv_buffer);
 
1937
  BFT_FREE(wanted_elts);
 
1938
  BFT_FREE(wanted_rank_index);
 
1939
 
 
1940
}
 
1941
 
 
1942
/*----------------------------------------------------------------------------
 
1943
 * Synchronize a cs_join_gset_t structure and distribute the resulting set
 
1944
 * over the rank by block
 
1945
 *
 
1946
 * parameters:
 
1947
 *   max_gnum <-- max global number in global element numbering
 
1948
 *   loc_set  <-> pointer to the local structure to work with
 
1949
 *   comm     <-- mpi_comm on which synchro. and distribution take place
 
1950
 *
 
1951
 * returns:
 
1952
 *   a synchronized and distributed cs_join_gset_t structure.
 
1953
 *---------------------------------------------------------------------------*/
 
1954
 
 
1955
cs_join_gset_t *
 
1956
cs_join_gset_block_sync(fvm_gnum_t       max_gnum,
 
1957
                        cs_join_gset_t  *loc_set,
 
1958
                        MPI_Comm         comm)
 
1959
{
 
1960
  int  i, j, shift, block_id;
 
1961
  int  rank, local_rank, n_ranks, n_recv_elts, n_sub_elts;
 
1962
  fvm_gnum_t  gnum;
 
1963
  cs_join_block_info_t  block_info;
 
1964
 
 
1965
  cs_int_t  *send_count = NULL, *recv_count = NULL, *counter = NULL;
 
1966
  cs_int_t  *send_shift = NULL, *recv_shift = NULL;
 
1967
  fvm_gnum_t  *send_buffer = NULL, *recv_buffer = NULL;
 
1968
  cs_join_gset_t  *sync_set = NULL;
 
1969
 
 
1970
  if (max_gnum == 0)
 
1971
    return  sync_set;
 
1972
 
 
1973
  MPI_Comm_rank(comm, &local_rank);
 
1974
  MPI_Comm_size(comm, &n_ranks);
 
1975
 
 
1976
  block_info = cs_join_get_block_info(max_gnum, n_ranks, local_rank);
 
1977
 
 
1978
  /* Allocate parameters for MPI functions */
 
1979
 
 
1980
  BFT_MALLOC(send_count, n_ranks, cs_int_t);
 
1981
  BFT_MALLOC(recv_count, n_ranks, cs_int_t);
 
1982
  BFT_MALLOC(send_shift, n_ranks + 1, cs_int_t);
 
1983
  BFT_MALLOC(recv_shift, n_ranks + 1, cs_int_t);
 
1984
 
 
1985
  /* Initialization */
 
1986
 
 
1987
  for (i = 0; i < n_ranks; i++)
 
1988
    send_count[i] = 0;
 
1989
 
 
1990
  /* Synchronize list definition for each global element */
 
1991
 
 
1992
  for (i = 0; i < loc_set->n_elts; i++) {
 
1993
 
 
1994
    rank = (loc_set->g_elts[i] - 1)/block_info.size;
 
1995
    n_sub_elts = loc_set->index[i+1] - loc_set->index[i];
 
1996
    send_count[rank] += 2 + n_sub_elts;
 
1997
 
 
1998
  }
 
1999
 
 
2000
  MPI_Alltoall(send_count, 1, MPI_INT, recv_count, 1, MPI_INT, comm);
 
2001
 
 
2002
  send_shift[0] = 0;
 
2003
  recv_shift[0] = 0;
 
2004
 
 
2005
  for (rank = 0; rank < n_ranks; rank++) {
 
2006
    send_shift[rank + 1] = send_shift[rank] + send_count[rank];
 
2007
    recv_shift[rank + 1] = recv_shift[rank] + recv_count[rank];
 
2008
  }
 
2009
 
 
2010
  /* Fill send_buffer: global number and number of elements in index */
 
2011
 
 
2012
  BFT_MALLOC(send_buffer, send_shift[n_ranks], fvm_gnum_t);
 
2013
  BFT_MALLOC(recv_buffer, recv_shift[n_ranks], fvm_gnum_t);
 
2014
 
 
2015
  for (i = 0; i < n_ranks; i++)
 
2016
    send_count[i] = 0;
 
2017
 
 
2018
  for (i = 0; i < loc_set->n_elts; i++) {
 
2019
 
 
2020
    gnum = loc_set->g_elts[i];
 
2021
    rank = (gnum - 1)/block_info.size;
 
2022
    shift = send_shift[rank] + send_count[rank];
 
2023
    n_sub_elts = loc_set->index[i+1] - loc_set->index[i];
 
2024
 
 
2025
    send_buffer[shift++] = gnum;
 
2026
    send_buffer[shift++] = n_sub_elts;
 
2027
 
 
2028
    for (j = 0; j < n_sub_elts; j++)
 
2029
      send_buffer[shift + j] = loc_set->g_list[loc_set->index[i] + j];
 
2030
 
 
2031
    send_count[rank] += 2 + n_sub_elts;
 
2032
 
 
2033
  }
 
2034
 
 
2035
  MPI_Alltoallv(send_buffer, send_count, send_shift, FVM_MPI_GNUM,
 
2036
                recv_buffer, recv_count, recv_shift, FVM_MPI_GNUM,
 
2037
                comm);
 
2038
 
 
2039
  n_recv_elts = recv_shift[n_ranks];
 
2040
 
 
2041
  /* Partial free memory */
 
2042
 
 
2043
  BFT_FREE(send_buffer);
 
2044
  BFT_FREE(send_count);
 
2045
  BFT_FREE(send_shift);
 
2046
  BFT_FREE(recv_count);
 
2047
  BFT_FREE(recv_shift);
 
2048
 
 
2049
  /* Define sync_set: a distributed cs_join_gset_t structure which
 
2050
     synchronize data over the ranks */
 
2051
 
 
2052
  sync_set = cs_join_gset_create(block_info.local_size);
 
2053
 
 
2054
  for (i = 0; i < sync_set->n_elts; i++)
 
2055
    sync_set->g_elts[i] = block_info.first_gnum + i;
 
2056
 
 
2057
  /* Fill sync_set->index and define a new recv_count for the next comm. */
 
2058
 
 
2059
  i = 0; /* position in recv_buffer */
 
2060
 
 
2061
  while (i < n_recv_elts) {
 
2062
 
 
2063
    block_id = recv_buffer[i++] - block_info.first_gnum;
 
2064
 
 
2065
    assert(sync_set->g_elts[block_id] == recv_buffer[i-1]);
 
2066
 
 
2067
    n_sub_elts = recv_buffer[i++];
 
2068
    sync_set->index[block_id+1] += n_sub_elts;
 
2069
    i += n_sub_elts;
 
2070
 
 
2071
  } /* End of loop on ranks */
 
2072
 
 
2073
  /* Build index on elements of sync_set */
 
2074
 
 
2075
  for (i = 0; i < sync_set->n_elts; i++)
 
2076
    sync_set->index[i+1] += sync_set->index[i];
 
2077
 
 
2078
  BFT_MALLOC(sync_set->g_list,
 
2079
             sync_set->index[sync_set->n_elts],
 
2080
             fvm_gnum_t);
 
2081
 
 
2082
  /* Fill g_list of sync_set */
 
2083
 
 
2084
  BFT_MALLOC(counter, sync_set->n_elts, cs_int_t);
 
2085
 
 
2086
  for (i = 0; i < sync_set->n_elts; i++)
 
2087
    counter[i] = 0;
 
2088
 
 
2089
  i = 0; /* position in recv_buffer */
 
2090
 
 
2091
  while (i < n_recv_elts) {
 
2092
 
 
2093
    block_id = recv_buffer[i++] - block_info.first_gnum;
 
2094
    shift = sync_set->index[block_id] + counter[block_id];
 
2095
 
 
2096
    n_sub_elts = recv_buffer[i++];
 
2097
 
 
2098
    for (j = 0; j < n_sub_elts; j++)
 
2099
      sync_set->g_list[j + shift] = recv_buffer[i++];
 
2100
 
 
2101
    counter[block_id] += n_sub_elts;
 
2102
 
 
2103
  } /* End of loop on ranks */
 
2104
 
 
2105
  BFT_FREE(recv_buffer);
 
2106
  BFT_FREE(counter);
 
2107
 
 
2108
  /* Return pointer */
 
2109
 
 
2110
  cs_join_gset_clean(sync_set);
 
2111
 
 
2112
  return  sync_set;
 
2113
}
 
2114
 
 
2115
/*----------------------------------------------------------------------------
 
2116
 * Update a local cs_join_gset_t structure from a distributed and
 
2117
 * synchronized cs_join_gset_t structure.
 
2118
 *
 
2119
 * Loc_set should not have redundant elements.
 
2120
 *
 
2121
 * parameters:
 
2122
 *   max_gnum <-- max global number in global element numbering
 
2123
 *   sync_set <-- pointer to the structure which holds a synchronized block
 
2124
 *   loc_set  <-> pointer to a local structure holding elements to update
 
2125
 *   comm     <-- comm on which synchronization and distribution take place
 
2126
 *---------------------------------------------------------------------------*/
 
2127
 
 
2128
void
 
2129
cs_join_gset_block_update(fvm_gnum_t             max_gnum,
 
2130
                          const cs_join_gset_t  *sync_set,
 
2131
                          cs_join_gset_t        *loc_set,
 
2132
                          MPI_Comm               comm)
 
2133
{
 
2134
  int  i, j, k, shift, block_id, start, end;
 
2135
  int  rank, local_rank, n_ranks, n_sub_elts, n_recv_elts;
 
2136
  fvm_gnum_t  gnum;
 
2137
  cs_join_block_info_t  block_info;
 
2138
 
 
2139
  cs_int_t  *send_count = NULL, *recv_count = NULL;
 
2140
  cs_int_t  *send_shift = NULL, *recv_shift = NULL, *wanted_rank_index = NULL;
 
2141
  fvm_gnum_t  *send_buffer = NULL, *recv_buffer = NULL, *wanted_elts = NULL;
 
2142
 
 
2143
  if (max_gnum == 0)
 
2144
    return;
 
2145
 
 
2146
  /* Sanity checks */
 
2147
 
 
2148
  assert(sync_set != NULL);
 
2149
 
 
2150
  /* Build a cs_join_block_info_t structure */
 
2151
 
 
2152
  MPI_Comm_rank(comm, &local_rank);
 
2153
  MPI_Comm_size(comm, &n_ranks);
 
2154
 
 
2155
  block_info = cs_join_get_block_info(max_gnum, n_ranks, local_rank);
 
2156
 
 
2157
  /* Allocate parameters for MPI functions */
 
2158
 
 
2159
  BFT_MALLOC(send_count, n_ranks, cs_int_t);
 
2160
  BFT_MALLOC(recv_count, n_ranks, cs_int_t);
 
2161
  BFT_MALLOC(send_shift, n_ranks + 1, cs_int_t);
 
2162
  BFT_MALLOC(recv_shift, n_ranks + 1, cs_int_t);
 
2163
  BFT_MALLOC(wanted_rank_index, n_ranks + 1, cs_int_t);
 
2164
 
 
2165
  /* Initialization */
 
2166
 
 
2167
  for (i = 0; i < n_ranks; i++)
 
2168
    send_count[i] = 0;
 
2169
 
 
2170
  /* Get a synchronized list definition for each global element */
 
2171
 
 
2172
  for (i = 0; i < loc_set->n_elts; i++) {
 
2173
    rank = (loc_set->g_elts[i] - 1)/block_info.size;
 
2174
    send_count[rank] += 1;
 
2175
  }
 
2176
 
 
2177
  MPI_Alltoall(send_count, 1, MPI_INT, recv_count, 1, MPI_INT, comm);
 
2178
 
 
2179
  send_shift[0] = 0;
 
2180
  wanted_rank_index[0] = 0;
 
2181
 
 
2182
  for (rank = 0; rank < n_ranks; rank++) {
 
2183
    send_shift[rank + 1] = send_shift[rank] + send_count[rank];
 
2184
    wanted_rank_index[rank + 1] = wanted_rank_index[rank] + recv_count[rank];
 
2185
  }
 
2186
 
 
2187
  /* Fill send_buffer: global number */
 
2188
 
 
2189
  BFT_MALLOC(send_buffer, send_shift[n_ranks], fvm_gnum_t);
 
2190
  BFT_MALLOC(wanted_elts, wanted_rank_index[n_ranks], fvm_gnum_t);
 
2191
 
 
2192
  for (i = 0; i < n_ranks; i++)
 
2193
    send_count[i] = 0;
 
2194
 
 
2195
  for (i = 0; i < loc_set->n_elts; i++) {
 
2196
 
 
2197
    gnum = loc_set->g_elts[i];
 
2198
    rank = (gnum - 1)/block_info.size;
 
2199
    shift = send_shift[rank] + send_count[rank];
 
2200
 
 
2201
    send_buffer[shift] = gnum;
 
2202
    send_count[rank] += 1;
 
2203
 
 
2204
  }
 
2205
 
 
2206
  MPI_Alltoallv(send_buffer, send_count, send_shift, FVM_MPI_GNUM,
 
2207
                wanted_elts, recv_count, wanted_rank_index, FVM_MPI_GNUM,
 
2208
                comm);
 
2209
 
 
2210
  /* Send new list definition holding by sync_set to ranks which have
 
2211
     request it */
 
2212
 
 
2213
  for (i = 0; i < n_ranks; i++)
 
2214
    send_count[i] = 0;
 
2215
 
 
2216
  for (rank = 0; rank < n_ranks; rank++) {
 
2217
 
 
2218
    for (i = wanted_rank_index[rank]; i < wanted_rank_index[rank+1]; i++) {
 
2219
 
 
2220
      block_id = wanted_elts[i] - block_info.first_gnum;
 
2221
      n_sub_elts = sync_set->index[block_id+1] - sync_set->index[block_id];
 
2222
 
 
2223
      send_count[rank] +=  2 + n_sub_elts; /* glob. num,
 
2224
                                              n_sub_elts,
 
2225
                                              g_list */
 
2226
 
 
2227
    }
 
2228
 
 
2229
  } /* End of loop on ranks */
 
2230
 
 
2231
  MPI_Alltoall(send_count, 1, MPI_INT, recv_count, 1, MPI_INT, comm);
 
2232
 
 
2233
  send_shift[0] = 0;
 
2234
  recv_shift[0] = 0;
 
2235
 
 
2236
  for (rank = 0; rank < n_ranks; rank++) {
 
2237
    send_shift[rank + 1] = send_shift[rank] + send_count[rank];
 
2238
    recv_shift[rank + 1] = recv_shift[rank] + recv_count[rank];
 
2239
  }
 
2240
 
 
2241
  BFT_REALLOC(send_buffer, send_shift[n_ranks], fvm_gnum_t);
 
2242
  BFT_MALLOC(recv_buffer, recv_shift[n_ranks], fvm_gnum_t);
 
2243
 
 
2244
  for (i = 0; i < n_ranks; i++)
 
2245
    send_count[i] = 0;
 
2246
 
 
2247
  for (rank = 0; rank < n_ranks; rank++) {
 
2248
 
 
2249
    for (i = wanted_rank_index[rank]; i < wanted_rank_index[rank+1]; i++) {
 
2250
 
 
2251
      shift = send_shift[rank] + send_count[rank];
 
2252
      block_id = wanted_elts[i] - block_info.first_gnum;
 
2253
 
 
2254
      start = sync_set->index[block_id];
 
2255
      end = sync_set->index[block_id+1];
 
2256
      n_sub_elts = end - start;
 
2257
 
 
2258
      send_buffer[shift++] = wanted_elts[i];
 
2259
      send_buffer[shift++] = n_sub_elts;
 
2260
 
 
2261
      for (j = start, k = 0; j < end; j++, k++)
 
2262
        send_buffer[shift+k] = sync_set->g_list[j];
 
2263
 
 
2264
      send_count[rank] +=  2 + n_sub_elts; /* glob. num, n_sub_elts, g_list */
 
2265
 
 
2266
    }
 
2267
 
 
2268
  } /* End of loop on ranks */
 
2269
 
 
2270
  MPI_Alltoallv(send_buffer, send_count, send_shift, FVM_MPI_GNUM,
 
2271
                recv_buffer, recv_count, recv_shift, FVM_MPI_GNUM,
 
2272
                comm);
 
2273
 
 
2274
  n_recv_elts = recv_shift[n_ranks];
 
2275
 
 
2276
  /* Partial free memory */
 
2277
 
 
2278
  BFT_FREE(send_buffer);
 
2279
  BFT_FREE(send_count);
 
2280
  BFT_FREE(send_shift);
 
2281
  BFT_FREE(recv_count);
 
2282
  BFT_FREE(recv_shift);
 
2283
 
 
2284
  /* Re-initialize loc_set
 
2285
     As loc_set->g_elts and sync_set->g_elts are ordered, it's easier.
 
2286
     We can take values as they come */
 
2287
 
 
2288
  /* First redefine index */
 
2289
 
 
2290
  assert(loc_set->index[0] == 0);
 
2291
 
 
2292
  for (i = 0; i < loc_set->n_elts; i++)
 
2293
    loc_set->index[i+1] = 0;
 
2294
 
 
2295
  i = 0; /* id in recv_buffer */
 
2296
  j = 0; /* id in g_elts */
 
2297
 
 
2298
  while (i < n_recv_elts) {
 
2299
 
 
2300
    gnum = recv_buffer[i++];
 
2301
 
 
2302
    assert(loc_set->g_elts[j] = gnum);
 
2303
 
 
2304
    n_sub_elts = recv_buffer[i++];
 
2305
    loc_set->index[j+1] = n_sub_elts;
 
2306
 
 
2307
    i += n_sub_elts;
 
2308
    j++; /* go to the next elements */
 
2309
 
 
2310
  } /* End of loop on elements of recv_buffer */
 
2311
 
 
2312
  /* Define the new index */
 
2313
 
 
2314
  for (i = 0; i < loc_set->n_elts; i++)
 
2315
    loc_set->index[i+1] += loc_set->index[i];
 
2316
 
 
2317
  BFT_REALLOC(loc_set->g_list, loc_set->index[loc_set->n_elts], fvm_gnum_t);
 
2318
 
 
2319
  i = 0; /* id in recv_buffer */
 
2320
  j = 0; /* id in g_elts */
 
2321
 
 
2322
  while (i < n_recv_elts) {
 
2323
 
 
2324
    gnum = recv_buffer[i++];
 
2325
    n_sub_elts = recv_buffer[i++];
 
2326
 
 
2327
    assert(loc_set->g_elts[j] = gnum);
 
2328
 
 
2329
    for (k = 0; k < n_sub_elts; k++)
 
2330
      loc_set->g_list[loc_set->index[j] + k] = recv_buffer[i + k];
 
2331
 
 
2332
    i += n_sub_elts;
 
2333
    j++; /* go to the next elements */
 
2334
 
 
2335
  } /* End of loop on elements of recv_buffer */
 
2336
 
 
2337
  BFT_FREE(recv_buffer);
 
2338
  BFT_FREE(wanted_elts);
 
2339
  BFT_FREE(wanted_rank_index);
 
2340
}
 
2341
 
 
2342
#endif /* HAVE_MPI */
 
2343
 
 
2344
/*----------------------------------------------------------------------------
 
2345
 * Dump an array (int or double).
 
2346
 *
 
2347
 * This function is called according to the verbosity.
 
2348
 *
 
2349
 * parameters:
 
2350
 *   f       <-- handle to output file
 
2351
 *   type    <-- type of the array to display
 
2352
 *   header  <-- header to display in front of the array
 
2353
 *   n_elts  <-- number of elements to display
 
2354
 *   array   <-- array to display
 
2355
 *---------------------------------------------------------------------------*/
 
2356
 
 
2357
void
 
2358
cs_join_dump_array(FILE        *f,
 
2359
                   const char  *type,
 
2360
                   const char  *header,
 
2361
                   int          n_elts,
 
2362
                   const void  *array)
 
2363
{
 
2364
  int  i;
 
2365
 
 
2366
  fprintf(f, "  %s: ", header);
 
2367
 
 
2368
  if (!strncmp(type, "int", strlen("int"))) { /* "int" array  */
 
2369
 
 
2370
    const int *i_array = array;
 
2371
 
 
2372
    for (i = 0; i < n_elts; i++)
 
2373
      fprintf(f, " %8d", i_array[i]);
 
2374
 
 
2375
  }
 
2376
  else if (!strncmp(type, "bool", strlen("bool"))) { /* "boolean" array  */
 
2377
 
 
2378
    const cs_bool_t *b_array = array;
 
2379
 
 
2380
    for (i = 0; i < n_elts; i++)
 
2381
      if (b_array[i] == true)
 
2382
        fprintf(f, " T");
 
2383
      else {
 
2384
        assert(b_array[i] == false);
 
2385
        fprintf(f, " F");
 
2386
      }
 
2387
  }
 
2388
  else if (!strncmp(type, "double", strlen("double"))) { /* "double" array */
 
2389
 
 
2390
    const double  *d_array = array;
 
2391
 
 
2392
    for (i = 0; i < n_elts; i++)
 
2393
      fprintf(f, " %10.8e", d_array[i]);
 
2394
 
 
2395
  }
 
2396
  else if (!strncmp(type, "gnum", strlen("gnum"))) { /* "gnum" array */
 
2397
 
 
2398
    const fvm_gnum_t  *u_array = array;
 
2399
 
 
2400
    for (i = 0; i < n_elts; i++)
 
2401
      fprintf(f, " %9llu", (unsigned long long)u_array[i]);
 
2402
 
 
2403
  }
 
2404
  else
 
2405
    bft_error(__FILE__, __LINE__, 0,
 
2406
              _(" Unexpected type (%s) to display in the current dump.\n"),
 
2407
              type);
 
2408
 
 
2409
  fprintf(f, "\n");
 
2410
}
 
2411
 
 
2412
/*----------------------------------------------------------------------------
 
2413
 * Dump a cs_join_gset_t structure.
 
2414
 *
 
2415
 * parameters:
 
2416
 *   f    <-- handle to output file
 
2417
 *   set  <-- pointer to the cs_join_gset_t structure to dump
 
2418
 *---------------------------------------------------------------------------*/
 
2419
 
 
2420
void
 
2421
cs_join_gset_dump(FILE                  *f,
 
2422
                  const cs_join_gset_t  *set)
 
2423
{
 
2424
  int  i, j;
 
2425
 
 
2426
  if (set == NULL)
 
2427
    return;
 
2428
 
 
2429
  if (f == NULL)
 
2430
    f = stdout;
 
2431
 
 
2432
  fprintf(f, "\nDump cs_join_gset_t structure: %p\n", (const void *)set);
 
2433
  fprintf(f, "number of elements: %10d\n", set->n_elts);
 
2434
  fprintf(f, "size of the list  : %10d\n\n", set->index[set->n_elts]);
 
2435
 
 
2436
  for (i = 0; i < set->n_elts; i++) {
 
2437
 
 
2438
    int  s = set->index[i], e = set->index[i+1];
 
2439
    int  n_matches = e-s;
 
2440
    int  n_loops = n_matches/10;
 
2441
 
 
2442
    fprintf(f, "Global num: %8llu | subsize: %3d |",
 
2443
            (unsigned long long)set->g_elts[i], n_matches);
 
2444
 
 
2445
    for (j = 0; j < n_loops; j++) {
 
2446
      if (j == 0)
 
2447
        fprintf(f,
 
2448
                "%8llu %8llu %8llu %8llu %8llu "
 
2449
                "%8llu %8llu %8llu %8llu %8llu\n",
 
2450
                (unsigned long long)set->g_list[s+ 10*j],
 
2451
                (unsigned long long)set->g_list[s+ 10*j + 1],
 
2452
                (unsigned long long)set->g_list[s+ 10*j + 2],
 
2453
                (unsigned long long)set->g_list[s+ 10*j + 3],
 
2454
                (unsigned long long)set->g_list[s+ 10*j + 4],
 
2455
                (unsigned long long)set->g_list[s+ 10*j + 5],
 
2456
                (unsigned long long)set->g_list[s+ 10*j + 6],
 
2457
                (unsigned long long)set->g_list[s+ 10*j + 7],
 
2458
                (unsigned long long)set->g_list[s+ 10*j + 8],
 
2459
                (unsigned long long)set->g_list[s+ 10*j + 9]);
 
2460
      else
 
2461
        fprintf(f, "                                     "
 
2462
                "%8llu %8llu %8llu %8llu %8llu "
 
2463
                "%8llu %8llu %8llu %8llu %8llu\n",
 
2464
                (unsigned long long)set->g_list[s+ 10*j],
 
2465
                (unsigned long long)set->g_list[s+ 10*j + 1],
 
2466
                (unsigned long long)set->g_list[s+ 10*j + 2],
 
2467
                (unsigned long long)set->g_list[s+ 10*j + 3],
 
2468
                (unsigned long long)set->g_list[s+ 10*j + 4],
 
2469
                (unsigned long long)set->g_list[s+ 10*j + 5],
 
2470
                (unsigned long long)set->g_list[s+ 10*j + 6],
 
2471
                (unsigned long long)set->g_list[s+ 10*j + 7],
 
2472
                (unsigned long long)set->g_list[s+ 10*j + 8],
 
2473
                (unsigned long long)set->g_list[s+ 10*j + 9]);
 
2474
    }
 
2475
 
 
2476
    if (e - s+10*n_loops > 0) {
 
2477
      for (j = s + 10*n_loops; j < e; j++) {
 
2478
        if (j == s + 10*n_loops && n_loops > 0)
 
2479
          fprintf(f, "                                     ");
 
2480
        fprintf(f, "%8llu ", (unsigned long long)set->g_list[j]);
 
2481
      }
 
2482
      fprintf(f, "\n");
 
2483
    }
 
2484
 
 
2485
    if (n_matches == 0)
 
2486
      fprintf(f, "\n");
 
2487
 
 
2488
  } /* End of loop on boxes */
 
2489
 
 
2490
  fflush(f);
 
2491
}
 
2492
 
 
2493
/*---------------------------------------------------------------------------*/
 
2494
 
 
2495
END_C_DECLS